<commit_before><%@ page import="java.util.Collection" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<%@ include file="resources.jsp" %>
<%= displayTitle("Clinical data form") %>
</head>

<body>
<%= displayContentTitle("Clinical data form demo") %>

<form id="clinical-info-form" action="clinical-info-validation.jsp" method="post">
<fieldset class="twothird-width clear patient-info">
  <legend>Patient Information</legend>

  <fieldset>
  <label class="section" for="last_name">Patient name:</label>
  <div class="half-width last_name">
    <label for="last_name" class="hint">Last name</label>
    <input type="text" name="last_name" id="last_name"/>
  </div>
  <div class="half-width first_name">
    <label for="first_name" class="hint">First name</label>
    <input type="text" name="first_name" id="first_name"/>
  </div>
  </fieldset>
  <fieldset>
  <div class="half-width clear date_of_birth">
    <label class="section" for="date_of_birth">Date of birth: <span class="hint">(mm/dd/yyyy)</span></label>
    <input type="text" name="date_of_birth" id="date_of_birth"/>
  </div>
  </fieldset>
  <fieldset>
  <div class="half-width clear gender">
    <label>Gender: </label>
    <label for="gender_male"><input type="radio" name="gender" id="gender_male" value="male"/>Male</label> 
    <label for="gender_female"><input type="radio" name="gender" id="gender_female" value="female"/>Female</label>
  </div>
  </fieldset>
</fieldset>

<fieldset class="clinical-info">
  <legend>Clinical Information</legend>

  <div id="quick-search-box">
  <fieldset class="group-other quick-search-box emphasized-box">
    <h2 class="section">Quick phenotype search</h2>
    <label for="quick-phenotype-search">Enter a free text and choose among suggested ontology terms</label>
    <input type='text' name='phenotype' class='suggested multi suggest-hpo quickSearch' value='' size='16' id='quick-phenotype-search'/>
  </fieldset>
  </div>

  <div class="twothird-width">
  
<%!
String OTHER_FIELD_MARKER = "_other";
String DEFAULT_NAME = "phenotype";

public boolean isHPId(String id)
{
  return id.matches("[0-9]{7}");
}
public boolean isHPOther(String id)
{
  return id.equals("OTHER_FIELD_MARKER");
}
public boolean isNonHPCheckBox(String id)
{
  return !isHPOther(id) && id.startsWith("_c_");
}
public boolean isNonHPInput(String id)
{
  return !isHPOther(id) && id.startsWith("_i_");
}
public boolean isSubsection(String id)
{
  return !isHPId(id) && !id.startsWith("_");
}

public String handleSection(LinkedHashMap<String, Object> sectionData)
{
  
  String result = "";
  boolean hasOtherInput = false;
  if (sectionData.containsKey(OTHER_FIELD_MARKER)) {
    hasOtherInput = true;
    sectionData.remove(OTHER_FIELD_MARKER);
  }
  Object[] fieldIds = sectionData.keySet().toArray();
  for (int i = 0; i < fieldIds.length; ++i) {
    result += handleField((String)fieldIds[i], sectionData);
  }
  if (hasOtherInput) {
    result  = "<div class='phenotypes-main'>" + result + "</div>";
    result += "<div class='phenotypes-other'>" + generateInput(DEFAULT_NAME, OTHER_FIELD_MARKER, true) + "</div>";
    result += "<div class='clear'></div>";
  }
  return result;
}
public String handleField(String id, LinkedHashMap<String, Object> data)
{
  Object label = (data.get(id) == null ? id : data.get(id));
  if (isHPId(id)) {
    return generateCheckBox(DEFAULT_NAME, "HP:" + id, (String)label);
  } else if (isHPOther(id)) {
    return generateInput(DEFAULT_NAME, (String)label, true);
  } else if (isNonHPCheckBox(id)) {
    return generateCheckBox(DEFAULT_NAME, id.substring(3), (String)label);
  } else if (isNonHPInput(id)) {
    return generateInput(id.substring(3), (String)label, false);
  } else if (isSubsection(id)) {
    return "<label class='section'>" + id + "</label><div class='subsection'>" + handleSection((LinkedHashMap<String, Object>)data.get(id)) + "</div>";
  }
  return "";
}

public String generateCheckBox(String name, String value, String label)
{
  String id = name + (value.startsWith("_") ? "" : "_") + value;
  return "<label for='" + id + "'><input type='checkbox' name='" + name + "' id='" + id + "' value='" + value + "'/>" + label + "</label><br/>";
}
public String generateInput(String name, String label, boolean suggested)
{
  String result = "";
  String id = name + "_" + Math.random();
  String displayedLabel = (suggested ? "Other" : label);
  if (displayedLabel.matches("^\\(.*\\)$")) {
    displayedLabel = "<span class='hint'>" + displayedLabel + "</span>";
  } else {
    displayedLabel += ":";
  }
  result = "<label for='" + id + "'" + (suggested ? " class='label-other-" + name + "'" : "") + ">" + displayedLabel + "</label>";
  if (suggested) {
    result +="<p class='hint'>(enter a free text and choose among suggested ontology terms)</p>";
  }
  result +="<input type='text' name='" + name + "'" +(suggested ? "' class='suggested multi suggest-hpo generateCheckboxes'" : "") + " value='' size='16' id='" + id + "'/>";
  return result;
}
%>

<%
  LinkedHashMap<String, LinkedHashMap<String, Object>> sections = new LinkedHashMap<String, LinkedHashMap<String, Object>>() {
    {
      put ("Prenatal and perinatal history", new LinkedHashMap<String, Object>(){{
        put("0010880", "Increased nuchal translucency");
        put("0010942", "Echogenic intracardiac focus");
        put("0010945", "Fetal phyelectasis");
        put("0010943", "Echogenic fetal bowel");
        //put("_c_thickened_nuchal_fold", "Thickened nuchal fold"); // 0000474, 0000477 ?
        put("0000474", "Thickened nuchal fold");
        put("0010952", "Fetal ventriculomegaly");
        put("0002190", "Choroid plexus cyst");
        //put("_c_short_humerus_femur", "Short humerus and/or femur");
        put("0003014", "Short humerus");
        put("0003097", "Short femur");
        put("0001562", "Oligohydramnios");
        put("0001561", "Polyhydramnios");
        put("0001511", "IUGR");
        put("0001622", "Premature birth");
        put(OTHER_FIELD_MARKER, "");
      }});

      put ("Family history", new LinkedHashMap<String, Object>(){{
        put("_c_miscarriages", "Parents with at least 3 miscarriages");
        put("_c_relatives", "Other relatives with similar clinical history");
        put("_i_relative_details", "(please give details here)");
      }});
      put ("Growth parameters", new LinkedHashMap<String, Object>(){{
        put("Weight for age", new LinkedHashMap<String, Object>(){{
          put("0004325", "<3rd");
          put("0001513", ">97th");
        }});<commit_msg>[form] reorganized some terms, according to sebastien's suggestions<commit_after><%@ page import="java.util.Collection" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<%@ include file="resources.jsp" %>
<%= displayTitle("Clinical data form") %>
</head>

<body>
<%= displayContentTitle("Clinical data form demo") %>

<form id="clinical-info-form" action="clinical-info-validation.jsp" method="post">
<fieldset class="twothird-width clear patient-info">
  <legend>Patient Information</legend>

  <fieldset>
  <label class="section" for="last_name">Patient name:</label>
  <div class="half-width last_name">
    <label for="last_name" class="hint">Last name</label>
    <input type="text" name="last_name" id="last_name"/>
  </div>
  <div class="half-width first_name">
    <label for="first_name" class="hint">First name</label>
    <input type="text" name="first_name" id="first_name"/>
  </div>
  </fieldset>
  <fieldset>
  <div class="half-width clear date_of_birth">
    <label class="section" for="date_of_birth">Date of birth: <span class="hint">(mm/dd/yyyy)</span></label>
    <input type="text" name="date_of_birth" id="date_of_birth"/>
  </div>
  </fieldset>
  <fieldset>
  <div class="half-width clear gender">
    <label>Gender: </label>
    <label for="gender_male"><input type="radio" name="gender" id="gender_male" value="male"/>Male</label> 
    <label for="gender_female"><input type="radio" name="gender" id="gender_female" value="female"/>Female</label>
  </div>
  </fieldset>
</fieldset>

<fieldset class="clinical-info">
  <legend>Clinical Information</legend>

  <div id="quick-search-box">
  <fieldset class="group-other quick-search-box emphasized-box">
    <h2 class="section">Quick phenotype search</h2>
    <label for="quick-phenotype-search">Enter a free text and choose among suggested ontology terms</label>
    <input type='text' name='phenotype' class='suggested multi suggest-hpo quickSearch' value='' size='16' id='quick-phenotype-search'/>
  </fieldset>
  </div>

  <div class="twothird-width">
  
<%!
String OTHER_FIELD_MARKER = "_other";
String DEFAULT_NAME = "phenotype";

public boolean isHPId(String id)
{
  return id.matches("[0-9]{7}");
}
public boolean isHPOther(String id)
{
  return id.equals("OTHER_FIELD_MARKER");
}
public boolean isNonHPCheckBox(String id)
{
  return !isHPOther(id) && id.startsWith("_c_");
}
public boolean isNonHPInput(String id)
{
  return !isHPOther(id) && id.startsWith("_i_");
}
public boolean isSubsection(String id)
{
  return !isHPId(id) && !id.startsWith("_");
}

public String handleSection(LinkedHashMap<String, Object> sectionData)
{
  
  String result = "";
  boolean hasOtherInput = false;
  if (sectionData.containsKey(OTHER_FIELD_MARKER)) {
    hasOtherInput = true;
    sectionData.remove(OTHER_FIELD_MARKER);
  }
  Object[] fieldIds = sectionData.keySet().toArray();
  for (int i = 0; i < fieldIds.length; ++i) {
    result += handleField((String)fieldIds[i], sectionData);
  }
  if (hasOtherInput) {
    result  = "<div class='phenotypes-main'>" + result + "</div>";
    result += "<div class='phenotypes-other'>" + generateInput(DEFAULT_NAME, OTHER_FIELD_MARKER, true) + "</div>";
    result += "<div class='clear'></div>";
  }
  return result;
}
public String handleField(String id, LinkedHashMap<String, Object> data)
{
  Object label = (data.get(id) == null ? id : data.get(id));
  if (isHPId(id)) {
    return generateCheckBox(DEFAULT_NAME, "HP:" + id, (String)label);
  } else if (isHPOther(id)) {
    return generateInput(DEFAULT_NAME, (String)label, true);
  } else if (isNonHPCheckBox(id)) {
    return generateCheckBox(DEFAULT_NAME, id.substring(3), (String)label);
  } else if (isNonHPInput(id)) {
    return generateInput(id.substring(3), (String)label, false);
  } else if (isSubsection(id)) {
    return "<label class='section'>" + id + "</label><div class='subsection'>" + handleSection((LinkedHashMap<String, Object>)data.get(id)) + "</div>";
  }
  return "";
}

public String generateCheckBox(String name, String value, String label)
{
  String id = name + (value.startsWith("_") ? "" : "_") + value;
  return "<label for='" + id + "'><input type='checkbox' name='" + name + "' id='" + id + "' value='" + value + "'/>" + label + "</label><br/>";
}
public String generateInput(String name, String label, boolean suggested)
{
  String result = "";
  String id = name + "_" + Math.random();
  String displayedLabel = (suggested ? "Other" : label);
  if (displayedLabel.matches("^\\(.*\\)$")) {
    displayedLabel = "<span class='hint'>" + displayedLabel + "</span>";
  } else {
    displayedLabel += ":";
  }
  result = "<label for='" + id + "'" + (suggested ? " class='label-other-" + name + "'" : "") + ">" + displayedLabel + "</label>";
  if (suggested) {
    result +="<p class='hint'>(enter a free text and choose among suggested ontology terms)</p>";
  }
  result +="<input type='text' name='" + name + "'" +(suggested ? "' class='suggested multi suggest-hpo generateCheckboxes'" : "") + " value='' size='16' id='" + id + "'/>";
  return result;
}
%>

<%
  LinkedHashMap<String, LinkedHashMap<String, Object>> sections = new LinkedHashMap<String, LinkedHashMap<String, Object>>() {
    {
      put ("Prenatal and perinatal history", new LinkedHashMap<String, Object>(){{
        put("0010880", "Increased nuchal translucency");
        put("0010942", "Echogenic intracardiac focus");
        put("0010945", "Fetal phyelectasis");
        put("0010943", "Echogenic fetal bowel");
        put("_c_thickened_nuchal_fold", "Thickened nuchal fold"); // 0000474, 0000477 ?
        //put("0000474", "Thickened nuchal fold");
        put("0010952", "Fetal ventriculomegaly");
        put("0002190", "Choroid plexus cyst");
        //put("_c_short_humerus_femur", "Short humerus and/or femur");
        //put("0003014", "Short humerus");
        //put("0003097", "Short femur");
        put("0001562", "Oligohydramnios");
        put("0001561", "Polyhydramnios");
        put("0001511", "IUGR");
        put("0001622", "Premature birth");
        put(OTHER_FIELD_MARKER, "");
      }});

      put ("Family history", new LinkedHashMap<String, Object>(){{
        put("_c_miscarriages", "Parents with at least 3 miscarriages");
        put("_c_relatives", "Other relatives with similar clinical history");
        put("_i_relative_details", "(please give details here)");
      }});
      put ("Growth parameters", new LinkedHashMap<String, Object>(){{
        put("Weight for age", new LinkedHashMap<String, Object>(){{
          put("0004325", "<3rd");
          put("0001513", ">97th");
        }});
{'fraction_non_alphanumeric': 0.1843160552502599, 'fraction_numerical': 0.020941630773800683, 'mean_word_length': 4.421497584541063, 'pattern_counts': {'":': 2, '<': 203, '<?xml version=': 0, '>': 199, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3390655', 'n_tokens_mistral': 4749, 'n_tokens_neox': 4415, 'n_words': 989}