extensions/net.sf.basedb.meludi/trunk/src/net/sf/basedb/meludi/servlet/CaseSummaryServlet.java

Code
Comments
Other
Rev Date Author Line
2933 14 Nov 14 olle 1 package net.sf.basedb.meludi.servlet;
2933 14 Nov 14 olle 2
2933 14 Nov 14 olle 3 import java.io.IOException;
2933 14 Nov 14 olle 4 import java.util.Date;
2933 14 Nov 14 olle 5 import java.util.List;
2933 14 Nov 14 olle 6 import java.util.regex.Matcher;
2933 14 Nov 14 olle 7 import java.util.regex.Pattern;
2933 14 Nov 14 olle 8
2933 14 Nov 14 olle 9 import javax.servlet.ServletException;
2933 14 Nov 14 olle 10 import javax.servlet.http.HttpServlet;
2933 14 Nov 14 olle 11 import javax.servlet.http.HttpServletRequest;
2933 14 Nov 14 olle 12 import javax.servlet.http.HttpServletResponse;
2933 14 Nov 14 olle 13
2933 14 Nov 14 olle 14 import org.json.simple.JSONArray;
2933 14 Nov 14 olle 15 import org.json.simple.JSONObject;
2933 14 Nov 14 olle 16
2933 14 Nov 14 olle 17 import net.sf.basedb.core.Application;
3660 09 Dec 15 olle 18 import net.sf.basedb.core.BioMaterial;
2933 14 Nov 14 olle 19 import net.sf.basedb.core.BioMaterialEvent;
2933 14 Nov 14 olle 20 import net.sf.basedb.core.BioPlate;
2933 14 Nov 14 olle 21 import net.sf.basedb.core.BioSource;
2933 14 Nov 14 olle 22 import net.sf.basedb.core.BioWell;
2933 14 Nov 14 olle 23 import net.sf.basedb.core.DbControl;
2933 14 Nov 14 olle 24 import net.sf.basedb.core.Extract;
3434 29 Jun 15 olle 25 import net.sf.basedb.core.ItemList;
2933 14 Nov 14 olle 26 import net.sf.basedb.core.ItemNotFoundException;
5093 14 Nov 18 olle 27 import net.sf.basedb.core.ItemQuery;
3660 09 Dec 15 olle 28 import net.sf.basedb.core.Kit;
2933 14 Nov 14 olle 29 import net.sf.basedb.core.MeasuredBioMaterial;
2933 14 Nov 14 olle 30 import net.sf.basedb.core.Permission;
2933 14 Nov 14 olle 31 import net.sf.basedb.core.PermissionDeniedException;
4241 16 Nov 16 olle 32 import net.sf.basedb.core.Project;
3660 09 Dec 15 olle 33 import net.sf.basedb.core.Protocol;
2933 14 Nov 14 olle 34 import net.sf.basedb.core.Sample;
2933 14 Nov 14 olle 35 import net.sf.basedb.core.SessionControl;
2933 14 Nov 14 olle 36 import net.sf.basedb.core.snapshot.SnapshotManager;
2933 14 Nov 14 olle 37 import net.sf.basedb.meludi.JsonUtil;
2933 14 Nov 14 olle 38 import net.sf.basedb.meludi.Meludi;
2933 14 Nov 14 olle 39 import net.sf.basedb.meludi.Site;
2933 14 Nov 14 olle 40 import net.sf.basedb.meludi.dao.Annotationtype;
3532 05 Oct 15 olle 41 import net.sf.basedb.meludi.dao.Barcode;
3207 23 Mar 15 olle 42 import net.sf.basedb.meludi.dao.BioplateType;
4796 08 May 18 olle 43 import net.sf.basedb.meludi.dao.Blood;
4796 08 May 18 olle 44 import net.sf.basedb.meludi.dao.BloodDna;
2933 14 Nov 14 olle 45 import net.sf.basedb.meludi.dao.Case;
4780 20 Apr 18 olle 46 import net.sf.basedb.meludi.dao.Consent;
3028 11 Dec 14 olle 47 import net.sf.basedb.meludi.dao.Dna;
4912 13 Jul 18 olle 48 import net.sf.basedb.meludi.dao.FfpeBlock;
5093 14 Nov 18 olle 49 import net.sf.basedb.meludi.dao.Histology;
2933 14 Nov 14 olle 50 import net.sf.basedb.meludi.dao.Patient;
3207 23 Mar 15 olle 51 import net.sf.basedb.meludi.dao.ReactionPlate;
3028 11 Dec 14 olle 52 import net.sf.basedb.meludi.dao.Rna;
2933 14 Nov 14 olle 53 import net.sf.basedb.meludi.dao.SpecimenTube;
3434 29 Jun 15 olle 54 import net.sf.basedb.meludi.dao.StartList;
3207 23 Mar 15 olle 55 import net.sf.basedb.meludi.dao.Subtype;
2933 14 Nov 14 olle 56 import net.sf.basedb.util.Values;
2933 14 Nov 14 olle 57 import net.sf.basedb.util.error.ThrowableUtil;
2933 14 Nov 14 olle 58
2933 14 Nov 14 olle 59
2933 14 Nov 14 olle 60 public class CaseSummaryServlet 
2933 14 Nov 14 olle 61   extends HttpServlet 
2933 14 Nov 14 olle 62 {
2933 14 Nov 14 olle 63
2933 14 Nov 14 olle 64   
2933 14 Nov 14 olle 65   private static final long serialVersionUID = 856434104316596495L;
2933 14 Nov 14 olle 66
2933 14 Nov 14 olle 67   public CaseSummaryServlet()
2933 14 Nov 14 olle 68   {}
2933 14 Nov 14 olle 69
2933 14 Nov 14 olle 70   @SuppressWarnings("unchecked")
2933 14 Nov 14 olle 71   @Override
2933 14 Nov 14 olle 72   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
2933 14 Nov 14 olle 73     throws ServletException, IOException 
2933 14 Nov 14 olle 74   {
2933 14 Nov 14 olle 75
2933 14 Nov 14 olle 76     String ID = req.getParameter("ID");
2933 14 Nov 14 olle 77     String cmd = req.getParameter("cmd");
2933 14 Nov 14 olle 78     JsonUtil.setJsonResponseHeaders(resp);
2933 14 Nov 14 olle 79     
2933 14 Nov 14 olle 80     JSONObject json = new JSONObject();
2933 14 Nov 14 olle 81     json.put("status", "ok");
2933 14 Nov 14 olle 82   
5468 04 Jun 19 olle 83     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 84     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 85     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
2933 14 Nov 14 olle 86     DbControl dc = null;
2933 14 Nov 14 olle 87     try
2933 14 Nov 14 olle 88     {
2933 14 Nov 14 olle 89       if ("GetCaseInfo".equals(cmd))
2933 14 Nov 14 olle 90       {
2933 14 Nov 14 olle 91         /*
2933 14 Nov 14 olle 92           Load information about a single case when given the case name/barcode
2933 14 Nov 14 olle 93           If a case is found we will load annotations, the patient it is associated with
2933 14 Nov 14 olle 94           and specimen tubes for the case. 
2933 14 Nov 14 olle 95         */
2933 14 Nov 14 olle 96         dc = sc.newDbControl();
4241 16 Nov 16 olle 97
2933 14 Nov 14 olle 98         String caseName = req.getParameter("caseName");
2933 14 Nov 14 olle 99         // Check if input 'caseName' is valid, or is personal number
4151 04 Oct 16 olle 100         boolean validCaseName = caseNameIsValid(caseName, sc.getActiveProjectId());
2933 14 Nov 14 olle 101         Patient patient = null;
2933 14 Nov 14 olle 102         Case theCase = null;
2933 14 Nov 14 olle 103         List<Case> caseList = null;
4913 13 Jul 18 olle 104         // Use stored annotation snapshots for performance reasons
4913 13 Jul 18 olle 105         SnapshotManager manager = new SnapshotManager();        
2933 14 Nov 14 olle 106         if (!validCaseName)
2933 14 Nov 14 olle 107         {
4913 13 Jul 18 olle 108           String itemName = caseName;
4913 13 Jul 18 olle 109           // Check if input string is personal number
4913 13 Jul 18 olle 110           String potentialPnr = itemName;
2933 14 Nov 14 olle 111           // Find cases by personal number if user has permission to do so
2933 14 Nov 14 olle 112           try
2933 14 Nov 14 olle 113           {
2933 14 Nov 14 olle 114             patient = Patient.findByPersonalNumber(dc, potentialPnr);
2933 14 Nov 14 olle 115           }
2933 14 Nov 14 olle 116           catch (PermissionDeniedException | ItemNotFoundException ex)
2933 14 Nov 14 olle 117           {}
4913 13 Jul 18 olle 118           // Check if input string is patient ID
4913 13 Jul 18 olle 119           if (patient == null)
4913 13 Jul 18 olle 120           {
4913 13 Jul 18 olle 121             if (itemName.startsWith(Meludi.fetchPatientItemPrefix(sc.getActiveProjectId())))
4913 13 Jul 18 olle 122             {
4913 13 Jul 18 olle 123               patient = Patient.findByName(dc, itemName);
4913 13 Jul 18 olle 124             }
4913 13 Jul 18 olle 125           }
2933 14 Nov 14 olle 126           if (patient != null)
2933 14 Nov 14 olle 127           {
2933 14 Nov 14 olle 128             caseList = Case.findByPatient(dc, patient);
2933 14 Nov 14 olle 129             if (caseList != null && caseList.size() > 0)
2933 14 Nov 14 olle 130             {
2933 14 Nov 14 olle 131               // Use lowest case name (a patient can have at most 2 cases)
2933 14 Nov 14 olle 132               theCase = (Case) caseList.get(0);
2933 14 Nov 14 olle 133             }
2933 14 Nov 14 olle 134             if (theCase != null)
2933 14 Nov 14 olle 135             {
2933 14 Nov 14 olle 136               // Use case name found from personal number
2933 14 Nov 14 olle 137               caseName = theCase.getName();
2933 14 Nov 14 olle 138             }
2933 14 Nov 14 olle 139           }
4988 28 Sep 18 olle 140           // Check if input string is referral ID or blood tube label
4913 13 Jul 18 olle 141           if (theCase == null)
4913 13 Jul 18 olle 142           {
4913 13 Jul 18 olle 143             if (itemName.startsWith(Meludi.fetchReferralPrefix(sc.getActiveProjectId())))
4913 13 Jul 18 olle 144             {
4913 13 Jul 18 olle 145               // Find case by referral ID if user has permission to do so
4913 13 Jul 18 olle 146               try
4913 13 Jul 18 olle 147               {
4988 28 Sep 18 olle 148                 // Remove optional suffix starting with dot "." if blood tube label
4988 28 Sep 18 olle 149                 String baseName = "" + itemName;
4988 28 Sep 18 olle 150                 int dotIndex = itemName.indexOf(".");
4988 28 Sep 18 olle 151                 if (dotIndex >= 0)
4988 28 Sep 18 olle 152                 {
4988 28 Sep 18 olle 153                   // Remove part starting with first dot
4988 28 Sep 18 olle 154                   baseName = baseName.substring(0, dotIndex);
4988 28 Sep 18 olle 155                 }
4988 28 Sep 18 olle 156                 theCase = Case.findByReferralId(dc, baseName);
4913 13 Jul 18 olle 157               }
4913 13 Jul 18 olle 158               catch (PermissionDeniedException | ItemNotFoundException ex)
4913 13 Jul 18 olle 159               {}
4913 13 Jul 18 olle 160             }
4913 13 Jul 18 olle 161           }
4913 13 Jul 18 olle 162           // Check if input string is blood ID
4913 13 Jul 18 olle 163           if (theCase == null)
4913 13 Jul 18 olle 164           {
4913 13 Jul 18 olle 165             if (itemName.startsWith(Meludi.fetchBloodItemPrefix(sc.getActiveProjectId())))
4913 13 Jul 18 olle 166             {
4987 28 Sep 18 olle 167               Blood blood = Blood.getByName(dc, itemName);
4913 13 Jul 18 olle 168               if (blood != null)
4913 13 Jul 18 olle 169               {
4913 13 Jul 18 olle 170                 // Get CASE_ID annotation
4913 13 Jul 18 olle 171                 String bloodCaseName = (String) Annotationtype.CASE_ID.getAnnotationValue(dc, manager, blood.getSample());
4913 13 Jul 18 olle 172                 if (bloodCaseName != null)
4913 13 Jul 18 olle 173                 {
4913 13 Jul 18 olle 174                   theCase = Case.findByName(dc, bloodCaseName);
4913 13 Jul 18 olle 175                 }
4913 13 Jul 18 olle 176               }
4913 13 Jul 18 olle 177             }
4913 13 Jul 18 olle 178           }
4913 13 Jul 18 olle 179           // Check if input string is FFPE block ID
4913 13 Jul 18 olle 180           if (theCase == null)
4913 13 Jul 18 olle 181           {
4913 13 Jul 18 olle 182             if (itemName.startsWith(Meludi.fetchFfpeBlockItemPrefix(sc.getActiveProjectId())))
4913 13 Jul 18 olle 183             {
4913 13 Jul 18 olle 184               // Remove optional suffix starting with dot "."
4913 13 Jul 18 olle 185               String baseName = "" + itemName;
4913 13 Jul 18 olle 186               int dotIndex = itemName.indexOf(".");
4913 13 Jul 18 olle 187               if (dotIndex >= 0)
4913 13 Jul 18 olle 188               {
4913 13 Jul 18 olle 189                 // Remove part starting with first dot
4913 13 Jul 18 olle 190                 baseName = baseName.substring(0, dotIndex);
4913 13 Jul 18 olle 191               }
4913 13 Jul 18 olle 192               FfpeBlock ffpeBlock = FfpeBlock.getByName(dc, baseName);
4913 13 Jul 18 olle 193               if (ffpeBlock != null)
4913 13 Jul 18 olle 194               {
4913 13 Jul 18 olle 195                 // Get CASE_ID annotation
4913 13 Jul 18 olle 196                 String ffpeBlockCaseName = (String) Annotationtype.CASE_ID.getAnnotationValue(dc, manager, ffpeBlock.getSample());
4913 13 Jul 18 olle 197                 if (ffpeBlockCaseName != null)
4913 13 Jul 18 olle 198                 {
4913 13 Jul 18 olle 199                   theCase = Case.findByName(dc, ffpeBlockCaseName);
4913 13 Jul 18 olle 200                 }
4913 13 Jul 18 olle 201               }
4913 13 Jul 18 olle 202             }
4913 13 Jul 18 olle 203           }
4913 13 Jul 18 olle 204           // If case item found, get case name for report
4913 13 Jul 18 olle 205           if (theCase != null)
4913 13 Jul 18 olle 206           {
4913 13 Jul 18 olle 207             // Use case name found from personal number
4913 13 Jul 18 olle 208             caseName = theCase.getName();
4913 13 Jul 18 olle 209           }
2933 14 Nov 14 olle 210         }
2933 14 Nov 14 olle 211         
2933 14 Nov 14 olle 212         JSONArray jsonSections = new JSONArray();
2933 14 Nov 14 olle 213         json.put("sections", jsonSections);
2933 14 Nov 14 olle 214         
4241 16 Nov 16 olle 215         // Load project information
4241 16 Nov 16 olle 216         Project project = Project.getById(dc, sc.getActiveProjectId());
4241 16 Nov 16 olle 217         if (project != null)
4241 16 Nov 16 olle 218         {
4241 16 Nov 16 olle 219           JSONObject jsonProj = new JSONObject();
4241 16 Nov 16 olle 220           jsonProj.put("id", project.getId());
4241 16 Nov 16 olle 221           jsonProj.put("name", project.getName());
4241 16 Nov 16 olle 222           jsonProj.put("description", project.getDescription());
4241 16 Nov 16 olle 223           jsonSections.add(loadSectionInfo(sc, "project"));
4241 16 Nov 16 olle 224           json.put("projInfo", jsonProj);
4241 16 Nov 16 olle 225         }
4241 16 Nov 16 olle 226
2933 14 Nov 14 olle 227         // Load case (null if not found) and patient
2933 14 Nov 14 olle 228         if (theCase == null)
2933 14 Nov 14 olle 229         {
2933 14 Nov 14 olle 230           theCase = Case.findByName(dc, caseName);
2933 14 Nov 14 olle 231         }
2933 14 Nov 14 olle 232         // Load mutation analysis list string
2933 14 Nov 14 olle 233         if (theCase != null)
2933 14 Nov 14 olle 234         {
2933 14 Nov 14 olle 235           String mutationAnalysisListString = fetchMutationAnalysisListString(dc, manager, theCase.getSample());
2933 14 Nov 14 olle 236           theCase.setAnnotation("mutationAnalysis", mutationAnalysisListString);
2933 14 Nov 14 olle 237         }
2933 14 Nov 14 olle 238         // Load site information
2933 14 Nov 14 olle 239         Site site = Site.UNKNOWN;
2933 14 Nov 14 olle 240         if (theCase != null)
2933 14 Nov 14 olle 241         {
2933 14 Nov 14 olle 242           site = fetchSite(dc, manager, theCase.getSample());
2933 14 Nov 14 olle 243         }
2933 14 Nov 14 olle 244         if (site != null)
2933 14 Nov 14 olle 245         {
2933 14 Nov 14 olle 246           json.put("site", site.asJSONObject());
2933 14 Nov 14 olle 247         }
2933 14 Nov 14 olle 248         // Load project focus string
2933 14 Nov 14 olle 249         if (theCase != null)
2933 14 Nov 14 olle 250         {
2933 14 Nov 14 olle 251           String projectFocus = fetchProjectFocus(dc, manager, theCase.getSample());
2933 14 Nov 14 olle 252           theCase.setAnnotation("projectFocus", projectFocus);
2933 14 Nov 14 olle 253         }
4732 04 Apr 18 olle 254         // Load metastasis site string
2933 14 Nov 14 olle 255         if (theCase != null)
2933 14 Nov 14 olle 256         {
4732 04 Apr 18 olle 257           String metastasisSite = fetchMetastasisSite(dc, manager, theCase.getSample());
4732 04 Apr 18 olle 258           theCase.setAnnotation("metastasisSite", metastasisSite);
4732 04 Apr 18 olle 259         }
4732 04 Apr 18 olle 260         if (theCase != null)
4732 04 Apr 18 olle 261         {
3028 11 Dec 14 olle 262           String tubeContentType = fetchTubeContentType(dc, manager, theCase.getSample());
3028 11 Dec 14 olle 263           theCase.setAnnotation("tubeContentType", tubeContentType);
2933 14 Nov 14 olle 264         }
2933 14 Nov 14 olle 265         if (theCase != null) 
2933 14 Nov 14 olle 266         {
2933 14 Nov 14 olle 267           loadCaseInfo(dc, theCase);
2933 14 Nov 14 olle 268           jsonSections.add(loadSectionInfo(sc, "case"));
2933 14 Nov 14 olle 269           json.put("caseInfo", theCase.asJSONObject());
2933 14 Nov 14 olle 270           if (patient == null)
2933 14 Nov 14 olle 271           {
2933 14 Nov 14 olle 272             patient = Patient.findByCase(dc, theCase);
2933 14 Nov 14 olle 273           }
2933 14 Nov 14 olle 274         }
2933 14 Nov 14 olle 275
4796 08 May 18 olle 276         // Load blood (and patient if no case was found)
4796 08 May 18 olle 277         List<Blood> blood = null;
4831 05 Jun 18 olle 278         blood = Blood.findAllByCaseName(dc, caseName);
4831 05 Jun 18 olle 279 /*
4796 08 May 18 olle 280         if (patient == null)
4796 08 May 18 olle 281         {
4796 08 May 18 olle 282           blood = Blood.findAllByCaseName(dc, caseName);
4796 08 May 18 olle 283           if (blood.size() > 0) patient = Patient.findByBlood(dc, blood.get(0));
4796 08 May 18 olle 284         }
2933 14 Nov 14 olle 285         if (patient != null)
2933 14 Nov 14 olle 286         {
4796 08 May 18 olle 287           blood = Blood.findByPatient(dc, patient);
4796 08 May 18 olle 288         }
4831 05 Jun 18 olle 289 */
4796 08 May 18 olle 290     
4796 08 May 18 olle 291         if (blood.size() > 0) 
4796 08 May 18 olle 292         {
4796 08 May 18 olle 293           JSONArray jsonBlood = new JSONArray();
4796 08 May 18 olle 294           for (Blood b: blood)
4796 08 May 18 olle 295           {
4796 08 May 18 olle 296             loadBloodInfo(dc, b);
4796 08 May 18 olle 297             jsonBlood.add(b.asJSONObject());
4796 08 May 18 olle 298           }
4831 05 Jun 18 olle 299           //jsonSections.add(loadSectionInfo(sc, "blood"));
4796 08 May 18 olle 300           json.put("blood", jsonBlood);
4796 08 May 18 olle 301         }
4796 08 May 18 olle 302         
4912 13 Jul 18 olle 303         // Load FFPE block info
4912 13 Jul 18 olle 304         List<FfpeBlock> ffpeBlock = null;
4912 13 Jul 18 olle 305         ffpeBlock = FfpeBlock.findAllByCaseName(dc, caseName);
4912 13 Jul 18 olle 306     
4912 13 Jul 18 olle 307         if (ffpeBlock.size() > 0) 
4912 13 Jul 18 olle 308         {
4912 13 Jul 18 olle 309           JSONArray jsonFfpeBlock = new JSONArray();
4912 13 Jul 18 olle 310           for (FfpeBlock b: ffpeBlock)
4912 13 Jul 18 olle 311           {
4912 13 Jul 18 olle 312             loadFfpeBlockInfo(dc, b);
4912 13 Jul 18 olle 313             jsonFfpeBlock.add(b.asJSONObject());
4912 13 Jul 18 olle 314           }
4912 13 Jul 18 olle 315           //jsonSections.add(loadSectionInfo(sc, "ffpeblock"));
4912 13 Jul 18 olle 316           json.put("ffpeblock", jsonFfpeBlock);
4912 13 Jul 18 olle 317         }
4912 13 Jul 18 olle 318         
4796 08 May 18 olle 319         if (patient != null)
4796 08 May 18 olle 320         {
2933 14 Nov 14 olle 321           loadPatientInfo(dc, patient);
2933 14 Nov 14 olle 322           List<String> allCases = Case.findAllCaseNumbersByPatient(dc, patient);
2933 14 Nov 14 olle 323           patient.setAnnotation("allCases", allCases);
2933 14 Nov 14 olle 324           jsonSections.add(loadSectionInfo(sc, "patient"));
2933 14 Nov 14 olle 325           json.put("patient", patient.asJSONObject());
2933 14 Nov 14 olle 326         }
2933 14 Nov 14 olle 327         
2933 14 Nov 14 olle 328         // Load specimen tubes
2966 20 Nov 14 olle 329         List<SpecimenTube> specimen = SpecimenTube.findByCaseName(dc, caseName);
2933 14 Nov 14 olle 330         if (specimen.size() > 0)
2933 14 Nov 14 olle 331         {
2933 14 Nov 14 olle 332           JSONArray jsonSpecimen = new JSONArray();
2933 14 Nov 14 olle 333           for (SpecimenTube s : specimen)
2933 14 Nov 14 olle 334           {
2933 14 Nov 14 olle 335             loadSpecimenInfo(dc, manager, s);
3207 23 Mar 15 olle 336             //jsonSpecimen.add(s.asJSONObject());
3207 23 Mar 15 olle 337             JSONObject jsonSpec = s.asJSONObject();
3434 29 Jun 15 olle 338             ItemList startList = fetchStartList(dc, s);
3434 29 Jun 15 olle 339             if (startList != null)
3207 23 Mar 15 olle 340             {
3434 29 Jun 15 olle 341               JSONObject jsonItemList = JsonUtil.getItemListAsJSON(startList);
3434 29 Jun 15 olle 342               jsonSpec.put("startList", jsonItemList);
3207 23 Mar 15 olle 343             }
3207 23 Mar 15 olle 344             jsonSpecimen.add(jsonSpec);
2933 14 Nov 14 olle 345           }
2933 14 Nov 14 olle 346           jsonSections.add(loadSectionInfo(sc, "specimen"));
2933 14 Nov 14 olle 347           json.put("specimen", jsonSpecimen);
2933 14 Nov 14 olle 348         }
2933 14 Nov 14 olle 349
5093 14 Nov 18 olle 350         List<Histology> histology = Histology.findByCaseName(dc, caseName);
5093 14 Nov 18 olle 351         if (histology.size() > 0)
5093 14 Nov 18 olle 352         {
5093 14 Nov 18 olle 353           JSONArray jsonHistology = new JSONArray();
5093 14 Nov 18 olle 354           for (Histology his : histology)
5093 14 Nov 18 olle 355           {
5093 14 Nov 18 olle 356             loadHistologyInfo(dc, his);
5093 14 Nov 18 olle 357             jsonHistology.add(his.asJSONObject());
5093 14 Nov 18 olle 358           }
5093 14 Nov 18 olle 359           jsonSections.add(loadSectionInfo(sc, "histology"));
5093 14 Nov 18 olle 360           json.put("histology", jsonHistology);
5093 14 Nov 18 olle 361         }
5093 14 Nov 18 olle 362         
2933 14 Nov 14 olle 363         List<Rna> rna = Rna.findByCaseName(dc, caseName);
2933 14 Nov 14 olle 364         if (rna.size() > 0)
2933 14 Nov 14 olle 365         {
2933 14 Nov 14 olle 366           JSONArray jsonRna = new JSONArray();
2933 14 Nov 14 olle 367           for (Rna r : rna)
2933 14 Nov 14 olle 368           {
3318 08 May 15 olle 369             loadRnaInfo(dc, manager, r);
3237 10 Apr 15 olle 370             //jsonRna.add(r.asJSONObject());
3237 10 Apr 15 olle 371             JSONObject jsonR = r.asJSONObject();
3237 10 Apr 15 olle 372             if (itemIsInputItem(r.getName()))
3237 10 Apr 15 olle 373             {
3434 29 Jun 15 olle 374               ItemList startList = fetchStartList(dc, r.getName());
3434 29 Jun 15 olle 375               if (startList != null)
3237 10 Apr 15 olle 376               {
3434 29 Jun 15 olle 377                 JSONObject jsonItemList = JsonUtil.getItemListAsJSON(startList);
3434 29 Jun 15 olle 378                 jsonR.put("startList", jsonItemList);
3434 29 Jun 15 olle 379               }
3237 10 Apr 15 olle 380             }
3237 10 Apr 15 olle 381             jsonRna.add(jsonR);
2933 14 Nov 14 olle 382           }
2933 14 Nov 14 olle 383           jsonSections.add(loadSectionInfo(sc, "rna"));
2933 14 Nov 14 olle 384           json.put("rna", jsonRna);
2933 14 Nov 14 olle 385         }
2933 14 Nov 14 olle 386
2933 14 Nov 14 olle 387         List<Dna> dna = Dna.findByCaseName(dc, caseName);
2933 14 Nov 14 olle 388         if (dna.size() > 0)
2933 14 Nov 14 olle 389         {
2933 14 Nov 14 olle 390           JSONArray jsonDna = new JSONArray();
3526 05 Oct 15 olle 391           JSONArray jsonLib = new JSONArray();
2933 14 Nov 14 olle 392           for (Dna d : dna)
2933 14 Nov 14 olle 393           {
3318 08 May 15 olle 394             loadDnaInfo(dc, manager, d);
3237 10 Apr 15 olle 395             //jsonDna.add(d.asJSONObject());
3237 10 Apr 15 olle 396             JSONObject jsonD = d.asJSONObject();
3237 10 Apr 15 olle 397             if (itemIsInputItem(d.getName()))
3237 10 Apr 15 olle 398             {
3434 29 Jun 15 olle 399               ItemList startList = fetchStartList(dc, d.getName());
3434 29 Jun 15 olle 400               if (startList != null)
3237 10 Apr 15 olle 401               {
3434 29 Jun 15 olle 402                 JSONObject jsonItemList = JsonUtil.getItemListAsJSON(startList);
3434 29 Jun 15 olle 403                 jsonD.put("startList", jsonItemList);
3434 29 Jun 15 olle 404               }
3237 10 Apr 15 olle 405             }
3526 05 Oct 15 olle 406             if (d.getName().endsWith(".d"))
3526 05 Oct 15 olle 407             {
3526 05 Oct 15 olle 408               jsonDna.add(jsonD);
3526 05 Oct 15 olle 409             }
3526 05 Oct 15 olle 410             else if (d.getName().endsWith(".fpa") || d.getName().endsWith(".fpb"))
3526 05 Oct 15 olle 411             {
3660 09 Dec 15 olle 412               // Get DNA extract info
3660 09 Dec 15 olle 413               BioMaterial parentBioMaterial = d.getExtract().getParent();
3660 09 Dec 15 olle 414               Extract dnaExtract = d.getExtract();
3660 09 Dec 15 olle 415               BioMaterialEvent creationEvent = dnaExtract.getCreationEvent();
3660 09 Dec 15 olle 416               jsonD.put("libPrepProtocol", JsonUtil.getProtocolAsJSON(creationEvent.getProtocol()));
3660 09 Dec 15 olle 417               jsonD.put("libPrepKit", JsonUtil.getKitAsJSON(creationEvent.getKit()));
3660 09 Dec 15 olle 418
3526 05 Oct 15 olle 419               // Get BioWell row and column (0-based)
3526 05 Oct 15 olle 420               JSONObject jsonWell = new JSONObject();
3526 05 Oct 15 olle 421               JSONObject jsonDnaTmp = (JSONObject) d.asJSONObject();
3526 05 Oct 15 olle 422               jsonWell = (JSONObject) jsonDnaTmp.get("bioWell");
3532 05 Oct 15 olle 423               // Add barcode (Illumina index) info
3526 05 Oct 15 olle 424               if (jsonWell != null)
3526 05 Oct 15 olle 425               {
3532 05 Oct 15 olle 426                 Integer row = (Integer) jsonWell.get("row");
3532 05 Oct 15 olle 427                 Integer col = (Integer) jsonWell.get("column");
3532 05 Oct 15 olle 428                 if (row != null)
3526 05 Oct 15 olle 429                 {
3532 05 Oct 15 olle 430                   String rowStr = "A50" + (row + 1);
3532 05 Oct 15 olle 431                   Integer barcodeTagId = null;
3532 05 Oct 15 olle 432                   Barcode barcode = Barcode.findByName(dc, rowStr);
3532 05 Oct 15 olle 433                   if (barcode != null)
3532 05 Oct 15 olle 434                   {
3532 05 Oct 15 olle 435                     barcodeTagId = barcode.getTag().getId();
3532 05 Oct 15 olle 436                   }
3532 05 Oct 15 olle 437                   JSONObject jsonIndex2 = new JSONObject();
3532 05 Oct 15 olle 438                   jsonIndex2.put("name", rowStr);
3532 05 Oct 15 olle 439                   jsonIndex2.put("id", barcodeTagId);
3532 05 Oct 15 olle 440                   // Add Illumina Index 2 (row) barcode info
3532 05 Oct 15 olle 441                   jsonD.put("index2", jsonIndex2);
3526 05 Oct 15 olle 442                 }
3532 05 Oct 15 olle 443                 if (col != null)
3532 05 Oct 15 olle 444                 {
3532 05 Oct 15 olle 445                   String colStr = "A70" + (col + 1);
3532 05 Oct 15 olle 446                   if (col > 8)
3532 05 Oct 15 olle 447                   {
3532 05 Oct 15 olle 448                     colStr = "A7" + (col + 1);
3532 05 Oct 15 olle 449                   }
3532 05 Oct 15 olle 450                   Integer barcodeTagId = null;
3532 05 Oct 15 olle 451                   Barcode barcode = Barcode.findByName(dc, colStr);
3532 05 Oct 15 olle 452                   if (barcode != null)
3532 05 Oct 15 olle 453                   {
3532 05 Oct 15 olle 454                     barcodeTagId = barcode.getTag().getId();
3532 05 Oct 15 olle 455                   }
3532 05 Oct 15 olle 456                   JSONObject jsonIndex1 = new JSONObject();
3532 05 Oct 15 olle 457                   jsonIndex1.put("name", colStr);
3532 05 Oct 15 olle 458                   jsonIndex1.put("id", barcodeTagId);
3532 05 Oct 15 olle 459                   // Add Illumina Index 1 (column) barcode info
3532 05 Oct 15 olle 460                   jsonD.put("index1", jsonIndex1);
3532 05 Oct 15 olle 461                 }
3526 05 Oct 15 olle 462               }
3526 05 Oct 15 olle 463               jsonLib.add(jsonD);
3526 05 Oct 15 olle 464             }
2933 14 Nov 14 olle 465           }
2933 14 Nov 14 olle 466           jsonSections.add(loadSectionInfo(sc, "dna"));
2933 14 Nov 14 olle 467           json.put("dna", jsonDna);
4796 08 May 18 olle 468
4796 08 May 18 olle 469           List<BloodDna> bdna = BloodDna.findByCaseName(dc, caseName);
4796 08 May 18 olle 470           if (bdna.size() > 0)
4796 08 May 18 olle 471           {
4796 08 May 18 olle 472             JSONArray jsonBloodDna = new JSONArray();
4796 08 May 18 olle 473             for (BloodDna d : bdna)
4796 08 May 18 olle 474             {
4796 08 May 18 olle 475               loadBloodDnaInfo(dc, d);
4796 08 May 18 olle 476               jsonBloodDna.add(d.asJSONObject());
4796 08 May 18 olle 477             }
4796 08 May 18 olle 478             jsonSections.add(loadSectionInfo(sc, "bdna"));
4796 08 May 18 olle 479             json.put("bdna", jsonBloodDna);
4796 08 May 18 olle 480           }
4796 08 May 18 olle 481           
3526 05 Oct 15 olle 482           jsonSections.add(loadSectionInfo(sc, "lib"));
3526 05 Oct 15 olle 483           json.put("lib", jsonLib);
2933 14 Nov 14 olle 484         }
4151 04 Oct 16 olle 485         String sampleItemPrefix = Meludi.fetchSampleItemPrefix(sc.getActiveProjectId());
4221 09 Nov 16 olle 486         Integer sampleItemNumDigits = Meludi.fetchSampleItemNumDigits(sc.getActiveProjectId());
4151 04 Oct 16 olle 487         json.put("sampleItemPrefix", sampleItemPrefix);
4221 09 Nov 16 olle 488         json.put("sampleItemNumDigits", sampleItemNumDigits);
2933 14 Nov 14 olle 489
3028 11 Dec 14 olle 490 /*
2933 14 Nov 14 olle 491         List<MRna> mrna = MRna.findByCaseName(dc, caseName);
2933 14 Nov 14 olle 492         if (mrna.size() > 0)
2933 14 Nov 14 olle 493         {
2933 14 Nov 14 olle 494           JSONArray jsonMRna = new JSONArray();
2933 14 Nov 14 olle 495           for (MRna r : mrna)
2933 14 Nov 14 olle 496           {
2933 14 Nov 14 olle 497             loadMRnaInfo(dc, r);
2933 14 Nov 14 olle 498             jsonMRna.add(r.asJSONObject());
2933 14 Nov 14 olle 499           }
2933 14 Nov 14 olle 500           jsonSections.add(loadSectionInfo(sc, "mrna"));
2933 14 Nov 14 olle 501           json.put("mrna", jsonMRna);
2933 14 Nov 14 olle 502         }
2933 14 Nov 14 olle 503         
2933 14 Nov 14 olle 504         List<CDna> cdna = CDna.findByCaseName(dc, caseName);
2933 14 Nov 14 olle 505         if (cdna.size() > 0)
2933 14 Nov 14 olle 506         {
2933 14 Nov 14 olle 507           JSONArray jsonCDna = new JSONArray();
2933 14 Nov 14 olle 508           for (CDna r : cdna)
2933 14 Nov 14 olle 509           {
2933 14 Nov 14 olle 510             loadCDnaInfo(dc, r);
2933 14 Nov 14 olle 511             jsonCDna.add(r.asJSONObject());
2933 14 Nov 14 olle 512           }
2933 14 Nov 14 olle 513           jsonSections.add(loadSectionInfo(sc, "cdna"));
2933 14 Nov 14 olle 514           json.put("cdna", jsonCDna);
2933 14 Nov 14 olle 515         }
2933 14 Nov 14 olle 516
2933 14 Nov 14 olle 517         List<Library> lib = Library.findByCaseName(dc, caseName);
2933 14 Nov 14 olle 518         if (lib.size() > 0)
2933 14 Nov 14 olle 519         {
2933 14 Nov 14 olle 520           JSONArray jsonLib = new JSONArray();
2933 14 Nov 14 olle 521           for (Library r : lib)
2933 14 Nov 14 olle 522           {
2933 14 Nov 14 olle 523             // Ignore *.dil items with '0' remaining quantity
2933 14 Nov 14 olle 524             if (!r.getName().endsWith(".dil") || r.getExtract().getRemainingQuantity() > 0)
2933 14 Nov 14 olle 525             {
2933 14 Nov 14 olle 526               loadLibraryInfo(dc, r);
2933 14 Nov 14 olle 527               jsonLib.add(r.asJSONObject());
2933 14 Nov 14 olle 528             }
2933 14 Nov 14 olle 529           }
2933 14 Nov 14 olle 530           jsonSections.add(loadSectionInfo(sc, "lib"));
2933 14 Nov 14 olle 531           json.put("lib", jsonLib);
2933 14 Nov 14 olle 532         }
2933 14 Nov 14 olle 533
2933 14 Nov 14 olle 534         List<SequencingRun> sequencingRuns = SequencingRun.findByCaseName(dc, caseName);
2933 14 Nov 14 olle 535         if (sequencingRuns.size() > 0)
2933 14 Nov 14 olle 536         {
2933 14 Nov 14 olle 537           JSONArray jsonSequencingRun = new JSONArray();
2933 14 Nov 14 olle 538           for (SequencingRun sr : sequencingRuns)
2933 14 Nov 14 olle 539           {
2933 14 Nov 14 olle 540             loadSequencingRunInfo(dc, sr);
2933 14 Nov 14 olle 541             jsonSequencingRun.add(sr.asJSONObject());
2933 14 Nov 14 olle 542           }
2933 14 Nov 14 olle 543           jsonSections.add(loadSectionInfo(sc, "sequencingRun"));
2933 14 Nov 14 olle 544           json.put("sequencingRun", jsonSequencingRun);
2933 14 Nov 14 olle 545         }
2933 14 Nov 14 olle 546 */
2933 14 Nov 14 olle 547       }
2933 14 Nov 14 olle 548     }
2933 14 Nov 14 olle 549     catch (Throwable t)
2933 14 Nov 14 olle 550     {
2933 14 Nov 14 olle 551       t.printStackTrace();
2933 14 Nov 14 olle 552       json.clear();
2933 14 Nov 14 olle 553       json.put("status", "error");
2933 14 Nov 14 olle 554       json.put("message", t.getMessage());
2933 14 Nov 14 olle 555       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2933 14 Nov 14 olle 556     }
2933 14 Nov 14 olle 557     finally
2933 14 Nov 14 olle 558     {
2933 14 Nov 14 olle 559       if (dc != null) dc.close();
2933 14 Nov 14 olle 560       json.writeJSONString(resp.getWriter());
2933 14 Nov 14 olle 561     }
2933 14 Nov 14 olle 562   }
2933 14 Nov 14 olle 563
2933 14 Nov 14 olle 564   @SuppressWarnings("unchecked")
2933 14 Nov 14 olle 565   @Override
2933 14 Nov 14 olle 566   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
2933 14 Nov 14 olle 567     throws ServletException, IOException 
2933 14 Nov 14 olle 568   {  
2933 14 Nov 14 olle 569     String ID = req.getParameter("ID");
2933 14 Nov 14 olle 570     String cmd = req.getParameter("cmd");
2933 14 Nov 14 olle 571     JsonUtil.setJsonResponseHeaders(resp);
2933 14 Nov 14 olle 572     
2933 14 Nov 14 olle 573     JSONObject json = new JSONObject();
2933 14 Nov 14 olle 574     json.put("status", "ok");
2933 14 Nov 14 olle 575     
2933 14 Nov 14 olle 576     JSONArray jsonMessages = new JSONArray();
5468 04 Jun 19 olle 577     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 578     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 579     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
2933 14 Nov 14 olle 580     DbControl dc = null;
2933 14 Nov 14 olle 581         
2933 14 Nov 14 olle 582     try
2933 14 Nov 14 olle 583     {
2933 14 Nov 14 olle 584       if ("SetSectionVisibility".equals(cmd))
2933 14 Nov 14 olle 585       {
2933 14 Nov 14 olle 586         String section = req.getParameter("section");
2933 14 Nov 14 olle 587         boolean isHidden = Values.getBoolean(req.getParameter("hidden"));
2945 17 Nov 14 olle 588         String key = "net.sf.basedb.meludi.case-summary."+section+".hidden-details";
2933 14 Nov 14 olle 589         sc.setUserClientSetting(key, isHidden ? "1" : "0");
2933 14 Nov 14 olle 590         jsonMessages.add(key + "=" + sc.getUserClientSetting(key));
2933 14 Nov 14 olle 591       }
2933 14 Nov 14 olle 592       json.put("messages", jsonMessages);
2933 14 Nov 14 olle 593     }
2933 14 Nov 14 olle 594     catch (Throwable t)
2933 14 Nov 14 olle 595     {
2933 14 Nov 14 olle 596       t.printStackTrace();
2933 14 Nov 14 olle 597       json.clear();
2933 14 Nov 14 olle 598       json.put("status", "error");
2933 14 Nov 14 olle 599       json.put("message", t.getMessage());
2933 14 Nov 14 olle 600       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2933 14 Nov 14 olle 601     }
2933 14 Nov 14 olle 602     finally
2933 14 Nov 14 olle 603     {
2933 14 Nov 14 olle 604       if (dc != null) dc.close();
2933 14 Nov 14 olle 605       json.writeJSONString(resp.getWriter());
2933 14 Nov 14 olle 606     }
2933 14 Nov 14 olle 607   }
2933 14 Nov 14 olle 608
2933 14 Nov 14 olle 609   /**
4220 09 Nov 16 olle 610     Verify that the 'case' name consists of sample item prefix followed by a number of digits.
2933 14 Nov 14 olle 611   
2933 14 Nov 14 olle 612     @param caseName String Input case name to check.
4151 04 Oct 16 olle 613     @param activeProjectId int ID of the active project for which the case name should be validated.
2933 14 Nov 14 olle 614     @return boolean True if potential case name (not necessarily in database), false otherwise.
2933 14 Nov 14 olle 615   */
4151 04 Oct 16 olle 616   private boolean caseNameIsValid(String caseName, int activeProjectId)
2933 14 Nov 14 olle 617   {
2933 14 Nov 14 olle 618     boolean isValid = false;
2933 14 Nov 14 olle 619     if (caseName != null)
2933 14 Nov 14 olle 620     {
4151 04 Oct 16 olle 621       Pattern pattern = Meludi.fetchCaseNamePattern(activeProjectId);
2933 14 Nov 14 olle 622       Matcher matcher = pattern.matcher(caseName);
2933 14 Nov 14 olle 623       isValid = matcher.matches();
2933 14 Nov 14 olle 624     }
2933 14 Nov 14 olle 625     return isValid;
2933 14 Nov 14 olle 626   }
2933 14 Nov 14 olle 627
2933 14 Nov 14 olle 628
2933 14 Nov 14 olle 629   /**
2933 14 Nov 14 olle 630    * Returns a comma-separated list string of mutation analysis strings
2933 14 Nov 14 olle 631    * for the sample, based on the latter's mutation analysis annotation.
2933 14 Nov 14 olle 632    * 
2933 14 Nov 14 olle 633    * @param dc DbControl The DbControl to use.
2933 14 Nov 14 olle 634    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
2933 14 Nov 14 olle 635    * @param s Sample The sample to find the mutation analysis for.
2933 14 Nov 14 olle 636    * @return String A comma-separated list string of mutation analysis strings.
2933 14 Nov 14 olle 637    */
3028 11 Dec 14 olle 638   @SuppressWarnings("unchecked")
2933 14 Nov 14 olle 639   private String fetchMutationAnalysisListString(DbControl dc, SnapshotManager manager, Sample s)
2933 14 Nov 14 olle 640   {
2933 14 Nov 14 olle 641     String mutationAnalysisStr = null;
2933 14 Nov 14 olle 642     // Load mutation analysis string list
2933 14 Nov 14 olle 643     List<String> mutationAnalysisStringList = (List<String>) Annotationtype.MUTATION_ANALYSIS.getAnnotationValues(dc, s);
2933 14 Nov 14 olle 644     if (mutationAnalysisStringList != null && mutationAnalysisStringList.size() > 0)
2933 14 Nov 14 olle 645     {
2933 14 Nov 14 olle 646       mutationAnalysisStr = "";
2933 14 Nov 14 olle 647       for (int i = 0; i < mutationAnalysisStringList.size(); i++)
2933 14 Nov 14 olle 648       {
2933 14 Nov 14 olle 649         String listPart = (String) mutationAnalysisStringList.get(i);
2933 14 Nov 14 olle 650         if (i > 0)
2933 14 Nov 14 olle 651         {
2933 14 Nov 14 olle 652           mutationAnalysisStr += ", ";
2933 14 Nov 14 olle 653         }
2933 14 Nov 14 olle 654         mutationAnalysisStr += listPart;
2933 14 Nov 14 olle 655       }
2933 14 Nov 14 olle 656     }
2933 14 Nov 14 olle 657     return mutationAnalysisStr;
2933 14 Nov 14 olle 658   }
2933 14 Nov 14 olle 659
2933 14 Nov 14 olle 660
2933 14 Nov 14 olle 661   /**
2933 14 Nov 14 olle 662    * Returns the site for the sample, based on the latter's site annotation.
2933 14 Nov 14 olle 663    * 
2933 14 Nov 14 olle 664    * @param dc DbControl The DbControl to use.
2933 14 Nov 14 olle 665    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
2933 14 Nov 14 olle 666    * @param s Sample The sample to find the site for.
2933 14 Nov 14 olle 667    * @return Site The site object found for the sample or UNKNOWN.
2933 14 Nov 14 olle 668    */
2933 14 Nov 14 olle 669   private Site fetchSite(DbControl dc, SnapshotManager manager, Sample s)
2933 14 Nov 14 olle 670   {
2933 14 Nov 14 olle 671     Site site = Site.UNKNOWN;
2933 14 Nov 14 olle 672     String prefix = (String) Annotationtype.SITE.getAnnotationValue(dc, manager, s);
2933 14 Nov 14 olle 673     if (prefix != null)
2933 14 Nov 14 olle 674     {
2933 14 Nov 14 olle 675       site = Site.findByPrefix(prefix);
2933 14 Nov 14 olle 676     }
2933 14 Nov 14 olle 677     return site;
2933 14 Nov 14 olle 678   }
2933 14 Nov 14 olle 679
2933 14 Nov 14 olle 680
2933 14 Nov 14 olle 681   /**
2933 14 Nov 14 olle 682    * Returns project focus for the sample, based on the latter's ProjectFocus annotation.
2933 14 Nov 14 olle 683    * If a project focus annotation is found, a more readable string of this
2933 14 Nov 14 olle 684    * content is returned.
2933 14 Nov 14 olle 685    * 
2933 14 Nov 14 olle 686    * @param dc DbControl The DbControl to use.
2933 14 Nov 14 olle 687    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
2933 14 Nov 14 olle 688    * @param s Sample The sample to find the project focus for.
2933 14 Nov 14 olle 689    * @return String The project focus for the sample.
2933 14 Nov 14 olle 690    */
2933 14 Nov 14 olle 691   private String fetchProjectFocus(DbControl dc, SnapshotManager manager, Sample s)
2933 14 Nov 14 olle 692   {
2933 14 Nov 14 olle 693     String projectFocus = (String) Annotationtype.PROJECT_FOCUS.getAnnotationValue(dc, manager, s);
2933 14 Nov 14 olle 694     if (projectFocus != null)
2933 14 Nov 14 olle 695     {
3058 19 Dec 14 olle 696       if (projectFocus.equals(InstallServlet.PROJECTFOCUS_UNKNOWN))
2933 14 Nov 14 olle 697       {
2933 14 Nov 14 olle 698         projectFocus = "unknown";
2933 14 Nov 14 olle 699       }
3058 19 Dec 14 olle 700       else if (projectFocus.equals(InstallServlet.PROJECTFOCUS_MELANOMA))
2933 14 Nov 14 olle 701       {
2933 14 Nov 14 olle 702         projectFocus = "Melanoma";
2933 14 Nov 14 olle 703       }
3058 19 Dec 14 olle 704       else if (projectFocus.equals(InstallServlet.PROJECTFOCUS_LUNG_CANCER))
2933 14 Nov 14 olle 705       {
2933 14 Nov 14 olle 706         projectFocus = "Lung cancer";
2933 14 Nov 14 olle 707       }
3058 19 Dec 14 olle 708       else if (projectFocus.equals(InstallServlet.PROJECTFOCUS_COLON_CANCER))
3058 19 Dec 14 olle 709       {
3058 19 Dec 14 olle 710         projectFocus = "Colon cancer";
3058 19 Dec 14 olle 711       }
3139 16 Feb 15 olle 712       else if (projectFocus.equals(InstallServlet.PROJECTFOCUS_GIST))
3139 16 Feb 15 olle 713       {
3139 16 Feb 15 olle 714         projectFocus = "GIST (Gastrointestinal stromal tumour)";
3139 16 Feb 15 olle 715       }
4196 31 Oct 16 olle 716       else if (projectFocus.equals(InstallServlet.PROJECTFOCUS_BREAST_CANCER))
4196 31 Oct 16 olle 717       {
4196 31 Oct 16 olle 718         projectFocus = "Breast cancer";
4196 31 Oct 16 olle 719       }
2933 14 Nov 14 olle 720     }
2933 14 Nov 14 olle 721     return projectFocus;
2933 14 Nov 14 olle 722   }
2933 14 Nov 14 olle 723
2933 14 Nov 14 olle 724
2933 14 Nov 14 olle 725   /**
4732 04 Apr 18 olle 726    * Returns metastasis site for the sample, based on the latter's MetastasisSite annotation.
4732 04 Apr 18 olle 727    * If a metastasis site annotation is found, a more readable string of this
4732 04 Apr 18 olle 728    * content is returned.
4732 04 Apr 18 olle 729    * 
4732 04 Apr 18 olle 730    * @param dc DbControl The DbControl to use.
4732 04 Apr 18 olle 731    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
4732 04 Apr 18 olle 732    * @param s Sample The sample to find the metastasis site for.
4732 04 Apr 18 olle 733    * @return String The metastasis site for the sample.
4732 04 Apr 18 olle 734    */
4732 04 Apr 18 olle 735   private String fetchMetastasisSite(DbControl dc, SnapshotManager manager, Sample s)
4732 04 Apr 18 olle 736   {
4732 04 Apr 18 olle 737     String metastasisSite = (String) Annotationtype.METASTASIS_SITE.getAnnotationValue(dc, manager, s);
4732 04 Apr 18 olle 738     if (metastasisSite != null)
4732 04 Apr 18 olle 739     {
4732 04 Apr 18 olle 740       if (metastasisSite.equals(""))
4732 04 Apr 18 olle 741       {
4732 04 Apr 18 olle 742         metastasisSite = "unknown";
4732 04 Apr 18 olle 743       }
4732 04 Apr 18 olle 744       else if (metastasisSite.equals("liver"))
4732 04 Apr 18 olle 745       {
4732 04 Apr 18 olle 746         metastasisSite = "Liver";
4732 04 Apr 18 olle 747       }
4732 04 Apr 18 olle 748       else if (metastasisSite.equals("skeleton"))
4732 04 Apr 18 olle 749       {
4732 04 Apr 18 olle 750         metastasisSite = "Skeleton";
4732 04 Apr 18 olle 751       }
4732 04 Apr 18 olle 752       else if (metastasisSite.equals("bone"))
4732 04 Apr 18 olle 753       {
4732 04 Apr 18 olle 754         metastasisSite = "Bone";
4732 04 Apr 18 olle 755       }
4732 04 Apr 18 olle 756     }
4732 04 Apr 18 olle 757     return metastasisSite;
4732 04 Apr 18 olle 758   }
4732 04 Apr 18 olle 759
4732 04 Apr 18 olle 760
4732 04 Apr 18 olle 761   /**
3028 11 Dec 14 olle 762    * Returns tube content type for the sample, based on the latter's tubeContentType annotation.
3028 11 Dec 14 olle 763    * If a tube content type annotation is found, a more readable string of this
2933 14 Nov 14 olle 764    * content is returned.
2933 14 Nov 14 olle 765    * 
2933 14 Nov 14 olle 766    * @param dc DbControl The DbControl to use.
2933 14 Nov 14 olle 767    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
3028 11 Dec 14 olle 768    * @param s Sample The sample to find the input tube content type for.
3028 11 Dec 14 olle 769    * @return String The input tube content type for the sample.
2933 14 Nov 14 olle 770    */
3028 11 Dec 14 olle 771   private String fetchTubeContentType(DbControl dc, SnapshotManager manager, Sample s)
2933 14 Nov 14 olle 772   {
3028 11 Dec 14 olle 773     String tubeContentType = (String) Annotationtype.TUBE_CONTENT_TYPE.getAnnotationValue(dc, manager, s);
3028 11 Dec 14 olle 774     if (tubeContentType != null)
2933 14 Nov 14 olle 775     {
3028 11 Dec 14 olle 776       if (tubeContentType.equals(""))
2933 14 Nov 14 olle 777       {
3028 11 Dec 14 olle 778         tubeContentType = "unknown";
2933 14 Nov 14 olle 779       }
3028 11 Dec 14 olle 780       else if (tubeContentType.equals("Specimen"))
2933 14 Nov 14 olle 781       {
3028 11 Dec 14 olle 782         tubeContentType = "Specimen";
2933 14 Nov 14 olle 783       }
3028 11 Dec 14 olle 784       else if (tubeContentType.equals("DNA"))
2933 14 Nov 14 olle 785       {
3028 11 Dec 14 olle 786         tubeContentType = "DNA";
2933 14 Nov 14 olle 787       }
3028 11 Dec 14 olle 788       else if (tubeContentType.equals("RNA"))
3028 11 Dec 14 olle 789       {
3028 11 Dec 14 olle 790         tubeContentType = "RNA";
3028 11 Dec 14 olle 791       }
3028 11 Dec 14 olle 792       else if (tubeContentType.equals("DNA_RNA"))
3028 11 Dec 14 olle 793       {
3028 11 Dec 14 olle 794         tubeContentType = "DNA + RNA";
3028 11 Dec 14 olle 795       }
2933 14 Nov 14 olle 796     }
3028 11 Dec 14 olle 797     return tubeContentType;
2933 14 Nov 14 olle 798   }
2933 14 Nov 14 olle 799
2933 14 Nov 14 olle 800
2933 14 Nov 14 olle 801   /**
2933 14 Nov 14 olle 802    * Returns specimen type for the sample, based on the latter's specimenType annotation.
2933 14 Nov 14 olle 803    * If a specimen type annotation is found, a more readable string of this
2933 14 Nov 14 olle 804    * content is returned.
2933 14 Nov 14 olle 805    * 
2933 14 Nov 14 olle 806    * @param dc DbControl The DbControl to use.
2933 14 Nov 14 olle 807    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
2933 14 Nov 14 olle 808    * @param s Sample The sample to find the specimen type for.
2933 14 Nov 14 olle 809    * @return String The specimen type for the sample.
2933 14 Nov 14 olle 810    */
2933 14 Nov 14 olle 811   private String fetchSpecimenType(DbControl dc, SnapshotManager manager, Sample s)
2933 14 Nov 14 olle 812   {
2933 14 Nov 14 olle 813     String specimenType = (String) Annotationtype.SPECIMEN_TYPE.getAnnotationValue(dc, manager, s);
3318 08 May 15 olle 814     specimenType = fetchSpecimenType(specimenType);
3318 08 May 15 olle 815     return specimenType;
3318 08 May 15 olle 816   }
3318 08 May 15 olle 817
3318 08 May 15 olle 818
3318 08 May 15 olle 819   /**
4195 31 Oct 16 olle 820    * Returns specimen input type for the sample, based on the latter's specimenInputType annotation.
4195 31 Oct 16 olle 821    * If a specimen input type annotation is found, a more readable string of this
4195 31 Oct 16 olle 822    * content is returned.
4195 31 Oct 16 olle 823    * 
4195 31 Oct 16 olle 824    * @param dc DbControl The DbControl to use.
4195 31 Oct 16 olle 825    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
4195 31 Oct 16 olle 826    * @param s Sample The sample to find the specimen input type for.
4195 31 Oct 16 olle 827    * @return String The specimen input type for the sample.
4195 31 Oct 16 olle 828    */
4195 31 Oct 16 olle 829   private String fetchSpecimenInputType(DbControl dc, SnapshotManager manager, Sample s)
4195 31 Oct 16 olle 830   {
4195 31 Oct 16 olle 831     String specimenInputType = (String) Annotationtype.SPECIMEN_INPUT_TYPE.getAnnotationValue(dc, manager, s);
4195 31 Oct 16 olle 832     specimenInputType = fetchSpecimenInputType(specimenInputType);
4195 31 Oct 16 olle 833     return specimenInputType;
4195 31 Oct 16 olle 834   }
4195 31 Oct 16 olle 835
4195 31 Oct 16 olle 836
4195 31 Oct 16 olle 837   /**
3318 08 May 15 olle 838    * Returns specimen type for the extract, based on the latter's specimenTypeExtract annotation.
3318 08 May 15 olle 839    * If a specimen type annotation is found, a more readable string of this
3318 08 May 15 olle 840    * content is returned.
3318 08 May 15 olle 841    * 
3318 08 May 15 olle 842    * @param dc DbControl The DbControl to use.
3318 08 May 15 olle 843    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
3318 08 May 15 olle 844    * @param e Extract The extract to find the specimen type for.
3318 08 May 15 olle 845    * @return String The specimen type for the extract.
3318 08 May 15 olle 846    */
3318 08 May 15 olle 847   private String fetchSpecimenTypeExtract(DbControl dc, SnapshotManager manager, Extract e)
3318 08 May 15 olle 848   {
3318 08 May 15 olle 849     String specimenType = (String) Annotationtype.SPECIMEN_TYPE_EXTRACT.getAnnotationValue(dc, manager, e);
3318 08 May 15 olle 850     specimenType = fetchSpecimenType(specimenType);
3318 08 May 15 olle 851     return specimenType;
3318 08 May 15 olle 852   }
3318 08 May 15 olle 853
3318 08 May 15 olle 854
3318 08 May 15 olle 855   /**
3318 08 May 15 olle 856    * Returns a more readable string of a specimen type.
3318 08 May 15 olle 857    * 
3318 08 May 15 olle 858    * @param dc DbControl The DbControl to use.
3318 08 May 15 olle 859    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
3318 08 May 15 olle 860    * @param rawSpecimenType String The original specimen type string.
3318 08 May 15 olle 861    * @return String A more readable string of the input specimen type string.
3318 08 May 15 olle 862    */
3318 08 May 15 olle 863   private String fetchSpecimenType(String rawSpecimenType)
3318 08 May 15 olle 864   {
3318 08 May 15 olle 865     String specimenType = rawSpecimenType;
2933 14 Nov 14 olle 866     if (specimenType != null)
2933 14 Nov 14 olle 867     {
2933 14 Nov 14 olle 868       if (specimenType.equals(""))
2933 14 Nov 14 olle 869       {
2933 14 Nov 14 olle 870         specimenType = "unknown";
2933 14 Nov 14 olle 871       }
2933 14 Nov 14 olle 872       else if (specimenType.equals("Primary"))
2933 14 Nov 14 olle 873       {
2933 14 Nov 14 olle 874         specimenType = "Primary tumour";
2933 14 Nov 14 olle 875       }
2933 14 Nov 14 olle 876       else if (specimenType.equals("MetastasisDistant"))
2933 14 Nov 14 olle 877       {
2933 14 Nov 14 olle 878         specimenType = "Metastasis";
2933 14 Nov 14 olle 879       }
2933 14 Nov 14 olle 880     }
2933 14 Nov 14 olle 881     return specimenType;
2933 14 Nov 14 olle 882   }
2933 14 Nov 14 olle 883
2933 14 Nov 14 olle 884
4195 31 Oct 16 olle 885   /**
4195 31 Oct 16 olle 886    * Returns a more readable string of a specimen input type.
4195 31 Oct 16 olle 887    * 
4195 31 Oct 16 olle 888    * @param dc DbControl The DbControl to use.
4195 31 Oct 16 olle 889    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
4195 31 Oct 16 olle 890    * @param rawSpecimenInputType String The original specimen input type string.
4195 31 Oct 16 olle 891    * @return String A more readable string of the input specimen input type string.
4195 31 Oct 16 olle 892    */
4195 31 Oct 16 olle 893   private String fetchSpecimenInputType(String rawSpecimenInputType)
4195 31 Oct 16 olle 894   {
4195 31 Oct 16 olle 895     String specimenInputType = rawSpecimenInputType;
4195 31 Oct 16 olle 896     if (specimenInputType != null)
4195 31 Oct 16 olle 897     {
4195 31 Oct 16 olle 898       if (specimenInputType.equals(""))
4195 31 Oct 16 olle 899       {
4195 31 Oct 16 olle 900         specimenInputType = "unknown";
4195 31 Oct 16 olle 901       }
4195 31 Oct 16 olle 902       else if (specimenInputType.equals("fresh_frozen"))
4195 31 Oct 16 olle 903       {
4195 31 Oct 16 olle 904         specimenInputType = "Fresh frozen";
4195 31 Oct 16 olle 905       }
4729 04 Apr 18 olle 906       else if (specimenInputType.equals("rna_later"))
4729 04 Apr 18 olle 907       {
4729 04 Apr 18 olle 908         specimenInputType = "RNAlater";
4729 04 Apr 18 olle 909       }
4729 04 Apr 18 olle 910       else if (specimenInputType.equals("ffpe_block"))
4729 04 Apr 18 olle 911       {
4729 04 Apr 18 olle 912         specimenInputType = "FFPE block";
4729 04 Apr 18 olle 913       }
4195 31 Oct 16 olle 914       else if (specimenInputType.equals("ffpe_section"))
4195 31 Oct 16 olle 915       {
4195 31 Oct 16 olle 916         specimenInputType = "FFPE section";
4195 31 Oct 16 olle 917       }
4195 31 Oct 16 olle 918       else if (specimenInputType.equals("ffpe_punch"))
4195 31 Oct 16 olle 919       {
4195 31 Oct 16 olle 920         specimenInputType = "FFPE punch";
4195 31 Oct 16 olle 921       }
4195 31 Oct 16 olle 922       else if (specimenInputType.equals("ffpe_slide"))
4195 31 Oct 16 olle 923       {
4195 31 Oct 16 olle 924         specimenInputType = "FFPE slide";
4195 31 Oct 16 olle 925       }
4195 31 Oct 16 olle 926       else if (specimenInputType.equals("cell_line"))
4195 31 Oct 16 olle 927       {
4195 31 Oct 16 olle 928         specimenInputType = "Cell line";
4195 31 Oct 16 olle 929       }
4195 31 Oct 16 olle 930     }
4195 31 Oct 16 olle 931     return specimenInputType;
4195 31 Oct 16 olle 932   }
4195 31 Oct 16 olle 933
4195 31 Oct 16 olle 934
2933 14 Nov 14 olle 935   @SuppressWarnings("unchecked")
2933 14 Nov 14 olle 936   private JSONObject loadSectionInfo(SessionControl sc, String section)
2933 14 Nov 14 olle 937   {
2945 17 Nov 14 olle 938     String keyBase = "net.sf.basedb.meludi.case-summary."+section;
2933 14 Nov 14 olle 939     JSONObject json = new JSONObject();
2933 14 Nov 14 olle 940     json.put("id", section);
2933 14 Nov 14 olle 941     json.put("hiddenDetails", Values.getInt(sc.getUserClientSetting(keyBase+".hidden-details")));
2933 14 Nov 14 olle 942     return json;
2933 14 Nov 14 olle 943   }
2933 14 Nov 14 olle 944   
2933 14 Nov 14 olle 945   private void loadCaseInfo(DbControl dc, Case theCase)
2933 14 Nov 14 olle 946   {
2933 14 Nov 14 olle 947     Sample s = theCase.getItem();
2933 14 Nov 14 olle 948     // Properties
2933 14 Nov 14 olle 949     theCase.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(s.getEntryDate()));
2933 14 Nov 14 olle 950     theCase.setAnnotation("editable", s.hasPermission(Permission.WRITE));
2933 14 Nov 14 olle 951     
4780 20 Apr 18 olle 952     // Annotations and consent
4780 20 Apr 18 olle 953     Consent.loadConsentAnnotations(dc, theCase);
4841 08 Jun 18 olle 954     theCase.loadAnnotations(dc, "referralId", Annotationtype.REFERRAL_ID, null);
4879 27 Jun 18 olle 955     theCase.loadAnnotations(dc, "diagnosisConfirmed", Annotationtype.DIAGNOSIS_CONFIRMED, null);
5259 22 Jan 19 olle 956     theCase.loadAnnotations(dc, "ffpeBlockOrderDate", Annotationtype.FFPE_BLOCK_ORDER_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3028 11 Dec 14 olle 957     theCase.loadAnnotations(dc, "nofTubes", Annotationtype.NOF_DELIVERED_TUBES, null);
3028 11 Dec 14 olle 958     theCase.loadAnnotations(dc, "otherPathNote", Annotationtype.OTHER_PATH_NOTE, null);
3028 11 Dec 14 olle 959     theCase.setAnnotation("description", s.getDescription());
2933 14 Nov 14 olle 960   }
2933 14 Nov 14 olle 961   
4796 08 May 18 olle 962   private void loadBloodInfo(DbControl dc, Blood blood)
4796 08 May 18 olle 963   {
4796 08 May 18 olle 964     Sample s = blood.getItem();
4796 08 May 18 olle 965     
4831 05 Jun 18 olle 966     blood.loadBioPlateLocation();
4796 08 May 18 olle 967     blood.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(s.getEntryDate()));
4796 08 May 18 olle 968     blood.setAnnotation("editable", s.hasPermission(Permission.WRITE));
4796 08 May 18 olle 969     
4796 08 May 18 olle 970     Consent.loadConsentAnnotations(dc, blood);
4831 05 Jun 18 olle 971     //blood.loadAnnotations(dc, "samplingDate", Annotationtype.BLOOD_SAMPLING_DATETIME, Meludi.CONVERTER_DATETIME_TO_STRING);
4831 05 Jun 18 olle 972     blood.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5027 15 Oct 18 olle 973     blood.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5034 17 Oct 18 olle 974     blood.loadAnnotations(dc, "arrivalOperator", Annotationtype.ARRIVAL_OPERATOR, null);
4796 08 May 18 olle 975 /*
4796 08 May 18 olle 976     blood.loadAnnotations(dc, "freezerDate", Annotationtype.BLOOD_FREEZER_DATETIME, Meludi.CONVERTER_DATETIME_TO_STRING);
4796 08 May 18 olle 977     blood.loadAnnotations(dc, "serum", Annotationtype.BLOOD_SERUM, null);
4796 08 May 18 olle 978     blood.loadAnnotations(dc, "bloodSample", Annotationtype.BLOOD_SAMPLE, null);
4796 08 May 18 olle 979 */
4986 28 Sep 18 olle 980     blood.loadAnnotations(dc, "tubeLabel", Annotationtype.TUBE_LABEL, null);
4796 08 May 18 olle 981     blood.setAnnotation("comment", s.getDescription());
4796 08 May 18 olle 982   }
4796 08 May 18 olle 983   
4912 13 Jul 18 olle 984   private void loadFfpeBlockInfo(DbControl dc, FfpeBlock ffpeBlock)
4912 13 Jul 18 olle 985   {
4912 13 Jul 18 olle 986     Sample s = ffpeBlock.getItem();
4912 13 Jul 18 olle 987     
4912 13 Jul 18 olle 988     //ffpeBlock.loadBioPlateLocation();
4912 13 Jul 18 olle 989     ffpeBlock.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(s.getEntryDate()));
4912 13 Jul 18 olle 990     ffpeBlock.setAnnotation("editable", s.hasPermission(Permission.WRITE));
4912 13 Jul 18 olle 991     
4912 13 Jul 18 olle 992     Consent.loadConsentAnnotations(dc, ffpeBlock);
4912 13 Jul 18 olle 993     ffpeBlock.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
5074 06 Nov 18 olle 994     ffpeBlock.loadAnnotations(dc, "materialNumber", Annotationtype.MATERIAL_NUMBER, null);
5160 30 Nov 18 olle 995     ffpeBlock.loadAnnotations(dc, "localisation", Annotationtype.LOCALISATION, null);
4912 13 Jul 18 olle 996     ffpeBlock.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5044 19 Oct 18 olle 997     ffpeBlock.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5135 22 Nov 18 olle 998     ffpeBlock.loadAnnotations(dc, "returnedDate", Annotationtype.RETURNED_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5031 16 Oct 18 olle 999     ffpeBlock.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
4912 13 Jul 18 olle 1000     //ffpeBlock.setAnnotation("comment", s.getDescription());
5044 19 Oct 18 olle 1001     ffpeBlock.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT, null);
4912 13 Jul 18 olle 1002   }
4912 13 Jul 18 olle 1003   
2933 14 Nov 14 olle 1004   private void loadPatientInfo(DbControl dc, Patient patient)
2933 14 Nov 14 olle 1005   {
2933 14 Nov 14 olle 1006     BioSource b = patient.getBioSource();
2933 14 Nov 14 olle 1007     
2933 14 Nov 14 olle 1008     patient.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(b.getEntryDate()));
2933 14 Nov 14 olle 1009     patient.setAnnotation("editable", b.hasPermission(Permission.WRITE));
2933 14 Nov 14 olle 1010     
2933 14 Nov 14 olle 1011     patient.loadDefaultAnnotations(dc);
2933 14 Nov 14 olle 1012   }
2933 14 Nov 14 olle 1013   
2933 14 Nov 14 olle 1014   private void loadSpecimenInfo(DbControl dc, SnapshotManager manager, SpecimenTube specimen)
2933 14 Nov 14 olle 1015   {
2933 14 Nov 14 olle 1016     Sample s = specimen.getSample();
2933 14 Nov 14 olle 1017     specimen.loadBioPlateLocation();
2933 14 Nov 14 olle 1018     specimen.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(s.getEntryDate()));
2933 14 Nov 14 olle 1019     specimen.setAnnotation("editable", s.hasPermission(Permission.WRITE));
3028 11 Dec 14 olle 1020     specimen.setAnnotation("specimenType", fetchSpecimenType(dc, manager, s));
4195 31 Oct 16 olle 1021     specimen.setAnnotation("specimenInputType", fetchSpecimenInputType(dc, manager, s));
2933 14 Nov 14 olle 1022     
2933 14 Nov 14 olle 1023     specimen.setAnnotation("originalQuantity", s.getOriginalQuantity());
2933 14 Nov 14 olle 1024     specimen.setAnnotation("remainingQuantity", s.getRemainingQuantity());
2933 14 Nov 14 olle 1025
2933 14 Nov 14 olle 1026     specimen.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE,  Meludi.CONVERTER_DATE_TO_STRING);
2933 14 Nov 14 olle 1027     specimen.loadAnnotations(dc, "plNumber", Annotationtype.PL_NUMBER, null);
2933 14 Nov 14 olle 1028     specimen.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
3028 11 Dec 14 olle 1029     specimen.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE,  Meludi.CONVERTER_DATE_TO_STRING);
3028 11 Dec 14 olle 1030     specimen.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT, null);
3067 08 Jan 15 olle 1031     specimen.loadAnnotations(dc, "viableTumourCellsPercent", Annotationtype.VIABLE_TUMOUR_CELLS_PERCENT, null);
3028 11 Dec 14 olle 1032     specimen.loadAnnotations(dc, "nofSections", Annotationtype.NOF_SECTIONS, null);
5031 16 Oct 18 olle 1033     specimen.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
3028 11 Dec 14 olle 1034     specimen.setAnnotation("description", s.getDescription());
2933 14 Nov 14 olle 1035   }
2933 14 Nov 14 olle 1036
2933 14 Nov 14 olle 1037   @SuppressWarnings("unchecked")
5093 14 Nov 18 olle 1038   private void loadHistologyInfo(DbControl dc, Histology his)
5093 14 Nov 18 olle 1039   {
5093 14 Nov 18 olle 1040     Sample s = his.getSample();
5093 14 Nov 18 olle 1041     BioMaterialEvent created = s.getCreationEvent();
5093 14 Nov 18 olle 1042     
5093 14 Nov 18 olle 1043     his.loadBioPlateLocation();
5093 14 Nov 18 olle 1044     his.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(s.getEntryDate()));
5093 14 Nov 18 olle 1045     his.setAnnotation("editable", s.hasPermission(Permission.WRITE));
5093 14 Nov 18 olle 1046     //his.loadDoNotUseAnnotations(dc, null);
5093 14 Nov 18 olle 1047
5093 14 Nov 18 olle 1048     //his.loadAnnotations(dc, "partitionDate", Annotationtype.PARTITION_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5093 14 Nov 18 olle 1049     his.setAnnotation("originalQuantity", s.getOriginalQuantity());
5093 14 Nov 18 olle 1050     his.setAnnotation("remainingQuantity", s.getRemainingQuantity());
5093 14 Nov 18 olle 1051     
5093 14 Nov 18 olle 1052     if (created.getEventDate() != null)
5093 14 Nov 18 olle 1053     {
5093 14 Nov 18 olle 1054       his.setAnnotation("embedDate", Meludi.CONVERTER_DATE_TO_STRING.convert(created.getEventDate()));
5115 20 Nov 18 olle 1055 /*
5093 14 Nov 18 olle 1056       his.setAnnotation("embedProtocol", JsonUtil.getProtocolAsJSON(created.getProtocol()));
5115 20 Nov 18 olle 1057 */
5093 14 Nov 18 olle 1058     }
5093 14 Nov 18 olle 1059
5093 14 Nov 18 olle 1060     ItemQuery<Sample> stainedQuery = s.getChildSamples();
5093 14 Nov 18 olle 1061     Subtype.STAINED.addFilter(dc, stainedQuery);
5093 14 Nov 18 olle 1062     stainedQuery.include(Meludi.INCLUDE_IN_CURRENT_PROJECT);
5093 14 Nov 18 olle 1063     List<Sample> stained = stainedQuery.list(dc);
5115 20 Nov 18 olle 1064 System.out.println("CaseSummaryServlet::loadHistologyInfo(): stained.size() = " + stained.size());
5093 14 Nov 18 olle 1065     if (stained.size() > 0)
5093 14 Nov 18 olle 1066     {
5115 20 Nov 18 olle 1067 /*
5093 14 Nov 18 olle 1068       Sample good = stained.get(0);
5093 14 Nov 18 olle 1069       boolean goodIsGood = false;
5093 14 Nov 18 olle 1070       for (Sample he : stained)
5093 14 Nov 18 olle 1071       {
5093 14 Nov 18 olle 1072         if (Boolean.TRUE.equals(Annotationtype.GOOD_STAIN.getAnnotationValue(dc, he)))
5093 14 Nov 18 olle 1073         {
5093 14 Nov 18 olle 1074           good = he;
5093 14 Nov 18 olle 1075           goodIsGood = true;
5093 14 Nov 18 olle 1076           break;
5093 14 Nov 18 olle 1077         }
5093 14 Nov 18 olle 1078       }
5093 14 Nov 18 olle 1079       BioMaterialEvent heCreated = good.getCreationEvent();
5093 14 Nov 18 olle 1080       his.setAnnotation("stainDate", Meludi.CONVERTER_DATE_TO_STRING.convert(heCreated.getEventDate()));
5093 14 Nov 18 olle 1081       his.setAnnotation("stainProtocol", JsonUtil.getProtocolAsJSON(good.getProtocol()));
5093 14 Nov 18 olle 1082       his.setAnnotation("numStains", stained.size());
5093 14 Nov 18 olle 1083       if (goodIsGood)
5093 14 Nov 18 olle 1084       {
5093 14 Nov 18 olle 1085         // Image
5093 14 Nov 18 olle 1086         File imageFile = null;
5093 14 Nov 18 olle 1087         AnyToAny imageLink = null;
5093 14 Nov 18 olle 1088         try
5093 14 Nov 18 olle 1089         {
5093 14 Nov 18 olle 1090           imageLink = AnyToAny.getByName(dc, good, "image");
5093 14 Nov 18 olle 1091           if (imageLink.getToType() == Item.FILE)
5093 14 Nov 18 olle 1092           {
5093 14 Nov 18 olle 1093             // This is linked to an existing file already, replace it
5093 14 Nov 18 olle 1094             imageFile = (File)imageLink.getTo();
5093 14 Nov 18 olle 1095           }
5093 14 Nov 18 olle 1096         }
5093 14 Nov 18 olle 1097         catch (RuntimeException ex)
5093 14 Nov 18 olle 1098         {}
5093 14 Nov 18 olle 1099         
5093 14 Nov 18 olle 1100         JSONObject jsonGood = new JSONObject();
5093 14 Nov 18 olle 1101         jsonGood.put("id", good.getId());
5093 14 Nov 18 olle 1102         jsonGood.put("name", good.getName());
5093 14 Nov 18 olle 1103         jsonGood.put("bioWell", JsonUtil.getBioWellAsJSON(good.getBioWell(), true));
5093 14 Nov 18 olle 1104         jsonGood.put("comments", good.getDescription());
5093 14 Nov 18 olle 1105         
5093 14 Nov 18 olle 1106         jsonGood.put("ScoreComplete", Annotationtype.SCORE_COMPLETE.getAnnotationValue(dc, good));
5093 14 Nov 18 olle 1107         jsonGood.put("ScoreInvasiveCancer", Annotationtype.SCORE_INVASIVE_CANCER.getAnnotationValue(dc, good));
5093 14 Nov 18 olle 1108         jsonGood.put("ScoreInsituCancer", Annotationtype.SCORE_INSITU_CANCER.getAnnotationValue(dc, good));
5093 14 Nov 18 olle 1109         jsonGood.put("ScoreLymphocytes", Annotationtype.SCORE_LYMPHOCYTES.getAnnotationValue(dc, good));
5093 14 Nov 18 olle 1110         jsonGood.put("ScoreNormal", Annotationtype.SCORE_NORMAL.getAnnotationValue(dc, good));
5093 14 Nov 18 olle 1111         jsonGood.put("ScoreStroma", Annotationtype.SCORE_STROMA.getAnnotationValue(dc, good));
5093 14 Nov 18 olle 1112         jsonGood.put("ScoreFat", Annotationtype.SCORE_FAT.getAnnotationValue(dc, good));
5093 14 Nov 18 olle 1113         if (imageFile != null)
5093 14 Nov 18 olle 1114         {
5093 14 Nov 18 olle 1115           JSONObject jsonImage = new JSONObject();
5093 14 Nov 18 olle 1116           jsonImage.put("id", imageFile.getId());
5093 14 Nov 18 olle 1117           jsonImage.put("path", imageFile.getPath().toString());
5093 14 Nov 18 olle 1118           jsonGood.put("image", jsonImage);
5093 14 Nov 18 olle 1119         }
5093 14 Nov 18 olle 1120         
5093 14 Nov 18 olle 1121         his.setAnnotation("bestStain", jsonGood);
5093 14 Nov 18 olle 1122       }
5115 20 Nov 18 olle 1123 */
5115 20 Nov 18 olle 1124
5115 20 Nov 18 olle 1125       his.setAnnotation("numStains", stained.size());
5115 20 Nov 18 olle 1126       if (stained.size() > 1)
5115 20 Nov 18 olle 1127       {
5115 20 Nov 18 olle 1128         for (int i = 0; i < 2; i++)
5115 20 Nov 18 olle 1129         {
5115 20 Nov 18 olle 1130           Sample he = (Sample)stained.get(i);
5115 20 Nov 18 olle 1131           Integer tumourCellsPct = (Integer)Annotationtype.TUMOUR_CELLS_PERCENT.getAnnotationValue(dc, he);
5115 20 Nov 18 olle 1132           String heName = he.getName();
5115 20 Nov 18 olle 1133           if (heName.contains(".F"))
5115 20 Nov 18 olle 1134           {
5115 20 Nov 18 olle 1135             JSONObject jsonF = new JSONObject();
5115 20 Nov 18 olle 1136             jsonF.put("id", he.getId());
5115 20 Nov 18 olle 1137             jsonF.put("name", he.getName());
5115 20 Nov 18 olle 1138             jsonF.put("bioWell", JsonUtil.getBioWellAsJSON(he.getBioWell(), true));
5115 20 Nov 18 olle 1139             jsonF.put("tumourCellsPct", tumourCellsPct);
5115 20 Nov 18 olle 1140             his.setAnnotation("FStain", jsonF);
5115 20 Nov 18 olle 1141           }
5115 20 Nov 18 olle 1142           else if (heName.contains(".E"))
5115 20 Nov 18 olle 1143           {
5115 20 Nov 18 olle 1144             JSONObject jsonE = new JSONObject();
5115 20 Nov 18 olle 1145             jsonE.put("id", he.getId());
5115 20 Nov 18 olle 1146             jsonE.put("name", he.getName());
5115 20 Nov 18 olle 1147             jsonE.put("bioWell", JsonUtil.getBioWellAsJSON(he.getBioWell(), true));
5115 20 Nov 18 olle 1148             jsonE.put("tumourCellsPct", tumourCellsPct);
5115 20 Nov 18 olle 1149             his.setAnnotation("EStain", jsonE);
5115 20 Nov 18 olle 1150           }
5115 20 Nov 18 olle 1151         }
5115 20 Nov 18 olle 1152       }
5093 14 Nov 18 olle 1153     }
5093 14 Nov 18 olle 1154   }
5093 14 Nov 18 olle 1155   
5093 14 Nov 18 olle 1156   @SuppressWarnings("unchecked")
3318 08 May 15 olle 1157   private void loadRnaInfo(DbControl dc, SnapshotManager manager, Rna rna)
2933 14 Nov 14 olle 1158   {
2933 14 Nov 14 olle 1159     Extract e = rna.getExtract();
2933 14 Nov 14 olle 1160     BioMaterialEvent created = e.getCreationEvent();
2933 14 Nov 14 olle 1161     
2933 14 Nov 14 olle 1162     rna.loadBioPlateLocation();
2933 14 Nov 14 olle 1163     rna.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
3318 08 May 15 olle 1164     rna.setAnnotation("specimenType", fetchSpecimenTypeExtract(dc, manager, e));
3050 17 Dec 14 olle 1165 /*
2933 14 Nov 14 olle 1166     rna.setAnnotation("flag", (String) Annotationtype.FLAG.getAnnotationValue(dc, e));
3050 17 Dec 14 olle 1167 */
2933 14 Nov 14 olle 1168     rna.setAnnotation("editable", e.hasPermission(Permission.WRITE));  
3028 11 Dec 14 olle 1169     Float usedQuantity = null;
3028 11 Dec 14 olle 1170     if (e.getParent() instanceof Sample)
3028 11 Dec 14 olle 1171     {
3028 11 Dec 14 olle 1172       usedQuantity = created.getUsedQuantity((Sample)e.getParent());
3028 11 Dec 14 olle 1173     }
3028 11 Dec 14 olle 1174     else if (e.getParent() instanceof Extract)
3028 11 Dec 14 olle 1175     {
3028 11 Dec 14 olle 1176       usedQuantity = created.getUsedQuantity((Extract)e.getParent());
3028 11 Dec 14 olle 1177     }
3028 11 Dec 14 olle 1178     rna.setAnnotation("usedQuantity", usedQuantity);
2933 14 Nov 14 olle 1179     rna.setAnnotation("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(created.getEventDate()));
2933 14 Nov 14 olle 1180     rna.setAnnotation("extractionProtocol", JsonUtil.getProtocolAsJSON(created.getProtocol()));
2933 14 Nov 14 olle 1181     rna.setAnnotation("originalQuantity", e.getOriginalQuantity());
2933 14 Nov 14 olle 1182     rna.setAnnotation("remainingQuantity", e.getRemainingQuantity());
3318 08 May 15 olle 1183
3318 08 May 15 olle 1184     rna.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE_EXTRACT,  Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 1185     rna.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE_EXTRACT,  Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 1186     rna.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT_EXTRACT, null);
3318 08 May 15 olle 1187     rna.loadAnnotations(dc, "viableTumourCellsPercent", Annotationtype.VIABLE_TUMOUR_CELLS_PERCENT_EXTRACT, null);
3028 11 Dec 14 olle 1188     rna.setAnnotation("description", e.getDescription());
3028 11 Dec 14 olle 1189 /*
2933 14 Nov 14 olle 1190     rna.loadAnnotations(dc, "ndConc", Annotationtype.ND_CONC, null);
3028 11 Dec 14 olle 1191 */
3028 11 Dec 14 olle 1192
3028 11 Dec 14 olle 1193 /*
2933 14 Nov 14 olle 1194     ItemQuery<Extract> rnaqcQuery = e.getChildExtracts();
2933 14 Nov 14 olle 1195     Subtype.RNAQC.addFilter(dc, rnaqcQuery);
2933 14 Nov 14 olle 1196     rnaqcQuery.include(Meludi.INCLUDE_IN_CURRENT_PROJECT);
2933 14 Nov 14 olle 1197     List<Extract> rnaqc = rnaqcQuery.list(dc);
2933 14 Nov 14 olle 1198     if (rnaqc.size() > 0)
2933 14 Nov 14 olle 1199     {
2933 14 Nov 14 olle 1200       Extract lastQc = rnaqc.get(0);
2933 14 Nov 14 olle 1201       BioPlate lastQcPlate = null;
2933 14 Nov 14 olle 1202       Date lastQcDate = null;
2933 14 Nov 14 olle 1203       // Try to find the last QC run on either Caliper or BioAnalyzer
2933 14 Nov 14 olle 1204       // This should be stored in QC_RUN_DATE annotation on the BioPlate
2933 14 Nov 14 olle 1205       // If no bioplate is associated with the RNA QC item, use the
2933 14 Nov 14 olle 1206       // creation date
2933 14 Nov 14 olle 1207       
2933 14 Nov 14 olle 1208       for (Extract qc : rnaqc)
2933 14 Nov 14 olle 1209       {
2933 14 Nov 14 olle 1210         Float rqs = (Float)Annotationtype.CA_RQS.getAnnotationValue(dc, qc);
2933 14 Nov 14 olle 1211         Float rin = (Float)Annotationtype.BA_RIN.getAnnotationValue(dc, qc);
2933 14 Nov 14 olle 1212         // Skip RNAQC items without any quality score since it may
2933 14 Nov 14 olle 1213         // be a new NanoDrop measurement
2933 14 Nov 14 olle 1214         if (rqs == null && rin == null) continue;
2933 14 Nov 14 olle 1215         
2933 14 Nov 14 olle 1216         BioPlate plate = null;
2933 14 Nov 14 olle 1217         BioWell well = qc.getBioWell();
2933 14 Nov 14 olle 1218         Date qcDate = null;
2933 14 Nov 14 olle 1219         if (well != null)
2933 14 Nov 14 olle 1220         {
2933 14 Nov 14 olle 1221           plate = well.getPlate();
2933 14 Nov 14 olle 1222           qcDate = (Date)Annotationtype.QC_RUN_DATE.getAnnotationValue(dc, plate);
2933 14 Nov 14 olle 1223         }
2933 14 Nov 14 olle 1224         if (qcDate == null)
2933 14 Nov 14 olle 1225         {
2933 14 Nov 14 olle 1226           qcDate = qc.getCreationEvent().getEventDate();
2933 14 Nov 14 olle 1227         }
2933 14 Nov 14 olle 1228         if ((lastQcDate == null && qcDate != null) || (qcDate != null && qcDate.after(lastQcDate)))
2933 14 Nov 14 olle 1229         {
2933 14 Nov 14 olle 1230           lastQcDate = qcDate;
2933 14 Nov 14 olle 1231           lastQc = qc;
2933 14 Nov 14 olle 1232           lastQcPlate = plate;
2933 14 Nov 14 olle 1233         }
2933 14 Nov 14 olle 1234       }
2933 14 Nov 14 olle 1235       
2933 14 Nov 14 olle 1236       BioMaterialEvent lastCreated = lastQc.getCreationEvent();
2933 14 Nov 14 olle 1237       rna.setAnnotation("nofQc", rnaqc.size());
2933 14 Nov 14 olle 1238       rna.setAnnotation("qcProtocol", JsonUtil.getProtocolAsJSON(lastQc.getProtocol()));
2933 14 Nov 14 olle 1239       rna.setAnnotation("qcPlate", JsonUtil.getBioWellAsJSON(lastQc.getBioWell(), true));
3028 11 Dec 14 olle 1240       rna.setAnnotation("qcDescription", lastQc.getDescription());
2933 14 Nov 14 olle 1241
2933 14 Nov 14 olle 1242       if (lastQcPlate != null)
2933 14 Nov 14 olle 1243       {
2933 14 Nov 14 olle 1244         try
2933 14 Nov 14 olle 1245         {
2933 14 Nov 14 olle 1246           AnyToAny pdf = AnyToAny.getByName(dc, lastQcPlate, "PDF printout");
2933 14 Nov 14 olle 1247           File pdfFile = File.getById(dc, pdf.getToId());
2933 14 Nov 14 olle 1248           JSONObject jsonPdf = new JSONObject();
2933 14 Nov 14 olle 1249           jsonPdf.put("id", pdfFile.getId());
2933 14 Nov 14 olle 1250           jsonPdf.put("name", pdfFile.getName());
2933 14 Nov 14 olle 1251           rna.setAnnotation("qcPdf", jsonPdf);
2933 14 Nov 14 olle 1252         }
2933 14 Nov 14 olle 1253         catch (RuntimeException ex)
2933 14 Nov 14 olle 1254         {} // Typically, ItemNotFound if no PDF exists
2933 14 Nov 14 olle 1255       }
2933 14 Nov 14 olle 1256       
2933 14 Nov 14 olle 1257       rna.setAnnotation("qcDate", Meludi.CONVERTER_DATE_TO_STRING.convert(lastQcDate));
2933 14 Nov 14 olle 1258       Float rqs = (Float)Annotationtype.CA_RQS.getAnnotationValue(dc, lastQc);
2933 14 Nov 14 olle 1259       if (rqs != null)
2933 14 Nov 14 olle 1260       {
2933 14 Nov 14 olle 1261         rna.setAnnotation("qcRqs", rqs);
2933 14 Nov 14 olle 1262       }
2933 14 Nov 14 olle 1263       else
2933 14 Nov 14 olle 1264       {
2933 14 Nov 14 olle 1265         Float rin = (Float)Annotationtype.BA_RIN.getAnnotationValue(dc, lastQc);
2933 14 Nov 14 olle 1266         if (rin != null)
2933 14 Nov 14 olle 1267         {
2933 14 Nov 14 olle 1268           rna.setAnnotation("qcRin", rin);
2933 14 Nov 14 olle 1269         }
2933 14 Nov 14 olle 1270       }
2933 14 Nov 14 olle 1271     }
3028 11 Dec 14 olle 1272 */
2933 14 Nov 14 olle 1273   }
2933 14 Nov 14 olle 1274
3318 08 May 15 olle 1275   private void loadDnaInfo(DbControl dc, SnapshotManager manager, Dna dna)
2933 14 Nov 14 olle 1276   {
2933 14 Nov 14 olle 1277     Extract e = dna.getExtract();
2933 14 Nov 14 olle 1278     BioMaterialEvent created = e.getCreationEvent();
2933 14 Nov 14 olle 1279     
2933 14 Nov 14 olle 1280     dna.loadBioPlateLocation();
2933 14 Nov 14 olle 1281     dna.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
3028 11 Dec 14 olle 1282     dna.setAnnotation("editable", e.hasPermission(Permission.WRITE));
3318 08 May 15 olle 1283     dna.setAnnotation("specimenType", fetchSpecimenTypeExtract(dc, manager, e));
3028 11 Dec 14 olle 1284     Float usedQuantity = null;
3028 11 Dec 14 olle 1285     if (e.getParent() instanceof Sample)
3028 11 Dec 14 olle 1286     {
3028 11 Dec 14 olle 1287       usedQuantity = created.getUsedQuantity((Sample)e.getParent());
3028 11 Dec 14 olle 1288     }
3028 11 Dec 14 olle 1289     else if (e.getParent() instanceof Extract)
3028 11 Dec 14 olle 1290     {
3028 11 Dec 14 olle 1291       usedQuantity = created.getUsedQuantity((Extract)e.getParent());
3028 11 Dec 14 olle 1292     }
3028 11 Dec 14 olle 1293     dna.setAnnotation("usedQuantity", usedQuantity);
2933 14 Nov 14 olle 1294     dna.setAnnotation("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(created.getEventDate()));
2933 14 Nov 14 olle 1295     dna.setAnnotation("extractionProtocol", JsonUtil.getProtocolAsJSON(created.getProtocol()));
2933 14 Nov 14 olle 1296     dna.setAnnotation("originalQuantity", e.getOriginalQuantity());
2933 14 Nov 14 olle 1297     dna.setAnnotation("remainingQuantity", e.getRemainingQuantity());
3318 08 May 15 olle 1298
3318 08 May 15 olle 1299     dna.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE_EXTRACT,  Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 1300     dna.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE_EXTRACT,  Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 1301     dna.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT_EXTRACT, null);
3318 08 May 15 olle 1302     dna.loadAnnotations(dc, "viableTumourCellsPercent", Annotationtype.VIABLE_TUMOUR_CELLS_PERCENT_EXTRACT, null);
3028 11 Dec 14 olle 1303     dna.setAnnotation("description", e.getDescription());
3028 11 Dec 14 olle 1304 /*
2933 14 Nov 14 olle 1305     dna.loadAnnotations(dc, "ndConc", Annotationtype.ND_CONC, null);
3028 11 Dec 14 olle 1306 */
2933 14 Nov 14 olle 1307   }
4796 08 May 18 olle 1308
4796 08 May 18 olle 1309   private void loadBloodDnaInfo(DbControl dc, BloodDna dna)
4796 08 May 18 olle 1310   {
4796 08 May 18 olle 1311     Extract e = dna.getExtract();
4796 08 May 18 olle 1312     BioMaterialEvent created = e.getCreationEvent();
4796 08 May 18 olle 1313     
4796 08 May 18 olle 1314     dna.loadBioPlateLocation();
4796 08 May 18 olle 1315     dna.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
4796 08 May 18 olle 1316     dna.setAnnotation("editable", e.hasPermission(Permission.WRITE));
4796 08 May 18 olle 1317     dna.setAnnotation("usedQuantity", created.getUsedQuantity((MeasuredBioMaterial)e.getParent()));
4796 08 May 18 olle 1318     dna.setAnnotation("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(created.getEventDate()));
4796 08 May 18 olle 1319     dna.setAnnotation("originalQuantity", e.getOriginalQuantity());
4796 08 May 18 olle 1320     dna.setAnnotation("remainingQuantity", e.getRemainingQuantity());
4796 08 May 18 olle 1321     dna.setAnnotation("comment", e.getDescription());
4796 08 May 18 olle 1322     dna.loadAnnotations(dc, "ndConc", Annotationtype.ND_CONC, null);
4796 08 May 18 olle 1323   }
3028 11 Dec 14 olle 1324   
3028 11 Dec 14 olle 1325 /*
2933 14 Nov 14 olle 1326   private void loadMRnaInfo(DbControl dc, MRna mrna)
2933 14 Nov 14 olle 1327   {
2933 14 Nov 14 olle 1328     Extract e = mrna.getExtract();
2933 14 Nov 14 olle 1329     BioMaterialEvent created = e.getCreationEvent();
2933 14 Nov 14 olle 1330     
2933 14 Nov 14 olle 1331     mrna.loadBioPlateLocation();
2933 14 Nov 14 olle 1332     JSONObject jsonPdf = mrna.loadBioPlatePdf(dc);
2933 14 Nov 14 olle 1333     if (jsonPdf != null)
2933 14 Nov 14 olle 1334     {
2933 14 Nov 14 olle 1335       mrna.setAnnotation("platePdf", jsonPdf);
2933 14 Nov 14 olle 1336     }
2933 14 Nov 14 olle 1337     mrna.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
2933 14 Nov 14 olle 1338     mrna.setAnnotation("editable", e.hasPermission(Permission.WRITE));  
2933 14 Nov 14 olle 1339     mrna.setAnnotation("usedQuantity", created.getUsedQuantity((Extract)e.getParent()));
3028 11 Dec 14 olle 1340     mrna.setAnnotation("description", e.getDescription());
2933 14 Nov 14 olle 1341     
2933 14 Nov 14 olle 1342     // Get BioPlate annotations
2933 14 Nov 14 olle 1343     BioPlate plate = getBioPlate(e);
2933 14 Nov 14 olle 1344     if (plate != null)
2933 14 Nov 14 olle 1345     {
2933 14 Nov 14 olle 1346       Date cleanupDate = (Date) Annotationtype.CLEANUP_DATE.getAnnotationValue(dc, plate);
2933 14 Nov 14 olle 1347       mrna.setAnnotation("cleanupDate", Meludi.CONVERTER_DATE_TO_STRING.convert(cleanupDate));
2933 14 Nov 14 olle 1348       // Get BioPlate PlateProcessResult annotation
2933 14 Nov 14 olle 1349       String result = (String) Annotationtype.PLATE_PROCESS_RESULT.getAnnotationValue(dc, plate);
2933 14 Nov 14 olle 1350       mrna.setAnnotation("result", result);
2933 14 Nov 14 olle 1351     }
2933 14 Nov 14 olle 1352   }
2933 14 Nov 14 olle 1353
2933 14 Nov 14 olle 1354   private void loadCDnaInfo(DbControl dc, CDna cdna)
2933 14 Nov 14 olle 1355   {
2933 14 Nov 14 olle 1356     Extract e = cdna.getExtract();
2933 14 Nov 14 olle 1357     
2933 14 Nov 14 olle 1358     cdna.loadBioPlateLocation();
2933 14 Nov 14 olle 1359     cdna.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
2933 14 Nov 14 olle 1360     cdna.setAnnotation("editable", e.hasPermission(Permission.WRITE));  
3028 11 Dec 14 olle 1361     cdna.setAnnotation("description", e.getDescription());
2933 14 Nov 14 olle 1362     
2933 14 Nov 14 olle 1363     // Get BioPlate annotations
2933 14 Nov 14 olle 1364     BioPlate plate = getBioPlate(e);
2933 14 Nov 14 olle 1365     if (plate != null)
2933 14 Nov 14 olle 1366     {
2933 14 Nov 14 olle 1367       Date synthesisDate = (Date) Annotationtype.SYNTHESIS_DATE.getAnnotationValue(dc, plate);
2933 14 Nov 14 olle 1368       cdna.setAnnotation("synthesisDate", Meludi.CONVERTER_DATE_TO_STRING.convert(synthesisDate));
2933 14 Nov 14 olle 1369       // Get BioPlate PlateProcessResult annotation
2933 14 Nov 14 olle 1370       String result = (String) Annotationtype.PLATE_PROCESS_RESULT.getAnnotationValue(dc, plate);
2933 14 Nov 14 olle 1371       cdna.setAnnotation("result", result);
2933 14 Nov 14 olle 1372     }
2933 14 Nov 14 olle 1373   }
2933 14 Nov 14 olle 1374
2933 14 Nov 14 olle 1375   private void loadLibraryInfo(DbControl dc, Library lib)
2933 14 Nov 14 olle 1376   {
2933 14 Nov 14 olle 1377     Extract e = lib.getExtract();
2933 14 Nov 14 olle 1378     
2933 14 Nov 14 olle 1379     lib.loadBioPlateLocation();
2933 14 Nov 14 olle 1380     lib.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
2933 14 Nov 14 olle 1381     lib.setAnnotation("editable", e.hasPermission(Permission.WRITE));  
2933 14 Nov 14 olle 1382     lib.setAnnotation("originalQuantity", e.getOriginalQuantity());
2933 14 Nov 14 olle 1383     lib.setAnnotation("remainingQuantity", e.getRemainingQuantity());
2933 14 Nov 14 olle 1384     lib.loadAnnotations(dc, "ca_size", Annotationtype.CA_SIZE, null);
2933 14 Nov 14 olle 1385     lib.loadAnnotations(dc, "library_molarity_est", Annotationtype.LIBRARY_MOLARITY_EST, null);
2933 14 Nov 14 olle 1386     lib.loadAnnotations(dc, "library_frac_adpt", Annotationtype.LIBRARY_FRAC_ADPT, null);
2933 14 Nov 14 olle 1387     lib.loadAnnotations(dc, "qubitconc", Annotationtype.QUBIT_CONC, null);
3028 11 Dec 14 olle 1388     lib.setAnnotation("description", e.getDescription());
2933 14 Nov 14 olle 1389     
2933 14 Nov 14 olle 1390     // Get BioPlate annotations
2933 14 Nov 14 olle 1391     BioPlate plate = getBioPlate(e);
2933 14 Nov 14 olle 1392     if (plate != null)
2933 14 Nov 14 olle 1393     {
2933 14 Nov 14 olle 1394       Date cleanupDate = (Date) Annotationtype.CLEANUP_DATE.getAnnotationValue(dc, plate);
2933 14 Nov 14 olle 1395       lib.setAnnotation("cleanupDate", Meludi.CONVERTER_DATE_TO_STRING.convert(cleanupDate));
2933 14 Nov 14 olle 1396       // Get BioPlate PlateProcessResult annotation
2933 14 Nov 14 olle 1397       String result = (String) Annotationtype.PLATE_PROCESS_RESULT.getAnnotationValue(dc, plate);
2933 14 Nov 14 olle 1398       lib.setAnnotation("result", result);
2933 14 Nov 14 olle 1399     }
2933 14 Nov 14 olle 1400   }
2933 14 Nov 14 olle 1401
2933 14 Nov 14 olle 1402   private void loadSequencingRunInfo(DbControl dc, SequencingRun sr)
2933 14 Nov 14 olle 1403   {
2933 14 Nov 14 olle 1404     DerivedBioAssay dba = sr.getDerivedBioAssay();
2933 14 Nov 14 olle 1405     
2933 14 Nov 14 olle 1406     sr.setAnnotation("editable", dba.hasPermission(Permission.WRITE));
2933 14 Nov 14 olle 1407     sr.loadAnnotations(dc, "startDate", Annotationtype.SEQUENCING_START, Meludi.CONVERTER_DATE_TO_STRING);
2933 14 Nov 14 olle 1408     sr.loadAnnotations(dc, "endDate", Annotationtype.SEQUENCING_END, Meludi.CONVERTER_DATE_TO_STRING);
2933 14 Nov 14 olle 1409     sr.loadAnnotations(dc, "result", Annotationtype.SEQUENCING_RESULT, null);
3028 11 Dec 14 olle 1410     sr.setAnnotation("description", dba.getDescription());
2933 14 Nov 14 olle 1411     
2933 14 Nov 14 olle 1412     FlowCell fc = FlowCell.getBySequencingRun(dc, sr);
2933 14 Nov 14 olle 1413     PhysicalBioAssay pba = fc.getPhysicalBioAssay();
2933 14 Nov 14 olle 1414     
2933 14 Nov 14 olle 1415     fc.setAnnotation("editable", pba.hasPermission(Permission.WRITE));
2933 14 Nov 14 olle 1416     fc.loadAnnotations(dc, "clusterDate", Annotationtype.CLUSTER_START, Meludi.CONVERTER_DATE_TO_STRING);
2933 14 Nov 14 olle 1417     sr.setAnnotation("flowCell", fc.asJSONObject());
2933 14 Nov 14 olle 1418   }
2933 14 Nov 14 olle 1419 */
2933 14 Nov 14 olle 1420
3237 10 Apr 15 olle 1421   /**
3237 10 Apr 15 olle 1422    * Checks if an item is an input item by checking if
3237 10 Apr 15 olle 1423    * its name only contains a single dot. Case names
3237 10 Apr 15 olle 1424    * without dots are not regarded as input items here.
3237 10 Apr 15 olle 1425    * 
3237 10 Apr 15 olle 1426    * @param itemName String The name of the item to check.
3237 10 Apr 15 olle 1427    * @return boolean True if the item is an input item, else false.
3237 10 Apr 15 olle 1428    */
3237 10 Apr 15 olle 1429   private boolean itemIsInputItem(String itemName)
3237 10 Apr 15 olle 1430   {
3237 10 Apr 15 olle 1431     boolean result = false;
3237 10 Apr 15 olle 1432     if (itemName != null && !itemName.equals(""))
3237 10 Apr 15 olle 1433     {
3237 10 Apr 15 olle 1434       // Check if only one dot exists in name, which indicates an input item
3237 10 Apr 15 olle 1435       int lastDotIndex = itemName.lastIndexOf(".");
3237 10 Apr 15 olle 1436       if (lastDotIndex >= 0)
3237 10 Apr 15 olle 1437       {
3237 10 Apr 15 olle 1438         // Remove part after and including last dot
3237 10 Apr 15 olle 1439         String restStr = itemName.substring(0, lastDotIndex);
3237 10 Apr 15 olle 1440         // Check if any more dot exists in item name
3237 10 Apr 15 olle 1441         int extraDotIndex = restStr.indexOf(".");
3237 10 Apr 15 olle 1442         if (extraDotIndex == -1)
3237 10 Apr 15 olle 1443         {
3237 10 Apr 15 olle 1444           result = true;
3237 10 Apr 15 olle 1445         }
3237 10 Apr 15 olle 1446       }
3237 10 Apr 15 olle 1447     }
3237 10 Apr 15 olle 1448     return result;
3237 10 Apr 15 olle 1449   }
3237 10 Apr 15 olle 1450
2933 14 Nov 14 olle 1451   private BioPlate getBioPlate(MeasuredBioMaterial bioMaterial)
2933 14 Nov 14 olle 1452   {
2933 14 Nov 14 olle 1453     BioWell well = bioMaterial.getBioWell();
2933 14 Nov 14 olle 1454     return well == null ? null : well.getPlate();
2933 14 Nov 14 olle 1455   }
3207 23 Mar 15 olle 1456
3207 23 Mar 15 olle 1457   /**
3207 23 Mar 15 olle 1458    * Finds the (first) start DNA reaction plate with the input specimen name
3237 10 Apr 15 olle 1459    * contained in annotation list Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING, or `null`
3207 23 Mar 15 olle 1460    * if none found.
3207 23 Mar 15 olle 1461    * 
3207 23 Mar 15 olle 1462    * @param dc DbControl The DbControl to use.
3207 23 Mar 15 olle 1463    * @param st SpecimenTube The specimenTube to find startPlate for.
3207 23 Mar 15 olle 1464    * @return BioPlate The (first) BioPlate assigned to the specimen, or `null`, if none found.
3207 23 Mar 15 olle 1465    */
3207 23 Mar 15 olle 1466   private BioPlate fetchStartPlate(DbControl dc, SpecimenTube st)
3207 23 Mar 15 olle 1467   {
3207 23 Mar 15 olle 1468     Sample s = st.getSample();
3207 23 Mar 15 olle 1469     String name =  s.getName();
3237 10 Apr 15 olle 1470     BioPlate bioPlate = fetchStartPlate(dc, name);
3237 10 Apr 15 olle 1471     return bioPlate;
3237 10 Apr 15 olle 1472   }
3237 10 Apr 15 olle 1473
3237 10 Apr 15 olle 1474   /**
3237 10 Apr 15 olle 1475    * Finds the (first) start DNA reaction plate with the input item name
3237 10 Apr 15 olle 1476    * contained in annotation list Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING, or `null`
3237 10 Apr 15 olle 1477    * if none found.
3237 10 Apr 15 olle 1478    * 
3237 10 Apr 15 olle 1479    * @param dc DbControl The DbControl to use.
3237 10 Apr 15 olle 1480    * @param name String The name of the input item to find startPlate for.
3237 10 Apr 15 olle 1481    * @return BioPlate The (first) BioPlate assigned to the input item, or `null`, if none found.
3237 10 Apr 15 olle 1482    */
3237 10 Apr 15 olle 1483   private BioPlate fetchStartPlate(DbControl dc, String name)
3237 10 Apr 15 olle 1484   {
3237 10 Apr 15 olle 1485     BioPlate bioPlate = null;
3207 23 Mar 15 olle 1486     Subtype subtype = null;
4200 01 Nov 16 olle 1487     String prefix = Meludi.fetchStartPlateItemPrefix(dc.getSessionControl().getActiveProjectId());
3207 23 Mar 15 olle 1488     int numFreeWells = 0;
3207 23 Mar 15 olle 1489     Annotationtype runDateAnnotation = null;
3207 23 Mar 15 olle 1490     List<ReactionPlate> startPlates = ReactionPlate.findByBioPlateType(dc, 
3207 23 Mar 15 olle 1491         BioplateType.DNA, subtype, prefix, numFreeWells, runDateAnnotation);
3207 23 Mar 15 olle 1492     
3207 23 Mar 15 olle 1493     for (ReactionPlate p : startPlates)
3207 23 Mar 15 olle 1494     {
3233 09 Apr 15 olle 1495       List<String> sourceItemStringList = (List<String>) Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING.getAnnotationValues(dc, p.getBioPlate(), true, true);
3207 23 Mar 15 olle 1496       if (sourceItemStringList != null && sourceItemStringList.size() > 0)
3207 23 Mar 15 olle 1497       {
3207 23 Mar 15 olle 1498         if (sourceItemStringList.contains(name))
3207 23 Mar 15 olle 1499         {
3207 23 Mar 15 olle 1500           bioPlate = (BioPlate)p.getBioPlate();
3207 23 Mar 15 olle 1501           break;
3207 23 Mar 15 olle 1502         }
3207 23 Mar 15 olle 1503       }
3207 23 Mar 15 olle 1504     }
3207 23 Mar 15 olle 1505     return bioPlate;
3207 23 Mar 15 olle 1506   }
3434 29 Jun 15 olle 1507
3434 29 Jun 15 olle 1508   /**
3434 29 Jun 15 olle 1509    * Finds the (first) start item list with the input specimen name
3434 29 Jun 15 olle 1510    * contained in annotation list Annotationtype.LIST_SOURCE_ITEM_NAMES_LIST_STRING, or `null`
3434 29 Jun 15 olle 1511    * if none found.
3434 29 Jun 15 olle 1512    * 
3434 29 Jun 15 olle 1513    * @param dc DbControl The DbControl to use.
3434 29 Jun 15 olle 1514    * @param st SpecimenTube The specimenTube to find start item list for.
3434 29 Jun 15 olle 1515    * @return ItemList The (first) ItemList assigned to the specimen, or `null`, if none found.
3434 29 Jun 15 olle 1516    */
3434 29 Jun 15 olle 1517   private ItemList fetchStartList(DbControl dc, SpecimenTube st)
3434 29 Jun 15 olle 1518   {
3434 29 Jun 15 olle 1519     Sample s = st.getSample();
3434 29 Jun 15 olle 1520     String name =  s.getName();
3434 29 Jun 15 olle 1521     ItemList itemList = fetchStartList(dc, name);
3434 29 Jun 15 olle 1522     return itemList;
3434 29 Jun 15 olle 1523   }
3434 29 Jun 15 olle 1524
3434 29 Jun 15 olle 1525   /**
3434 29 Jun 15 olle 1526    * Finds the first start item list with the input item name
3434 29 Jun 15 olle 1527    * represented in the member item list, or, in case of a specimen,
3434 29 Jun 15 olle 1528    * as a parent item to one of the member items, or `null`
3434 29 Jun 15 olle 1529    * if none found.
3434 29 Jun 15 olle 1530    * 
3434 29 Jun 15 olle 1531    * @param dc DbControl The DbControl to use.
3434 29 Jun 15 olle 1532    * @param name String The name of the input item to find start item list for.
3434 29 Jun 15 olle 1533    * @return ItemList The (first) ItemList assigned to the input item, or `null`, if none found.
3434 29 Jun 15 olle 1534    */
3434 29 Jun 15 olle 1535   private ItemList fetchStartList(DbControl dc, String name)
3434 29 Jun 15 olle 1536   {
3434 29 Jun 15 olle 1537     ItemList itemList = null;
3434 29 Jun 15 olle 1538     Subtype subtype = null;
4151 04 Oct 16 olle 1539     String prefix = Meludi.fetchStartListItemPrefix(dc.getSessionControl().getActiveProjectId());
3434 29 Jun 15 olle 1540     List<ItemList> startLists = StartList.findByPrefix(dc, prefix, false);
3434 29 Jun 15 olle 1541     
3434 29 Jun 15 olle 1542     for (ItemList l : startLists)
3434 29 Jun 15 olle 1543     {
3434 29 Jun 15 olle 1544       StartList sl = new StartList(l);
3434 29 Jun 15 olle 1545       List<String> sourceItemStringList = sl.fetchExtractSourceNameListFromItemList(dc);
3434 29 Jun 15 olle 1546       if (sourceItemStringList != null && sourceItemStringList.size() > 0)
3434 29 Jun 15 olle 1547       {
3434 29 Jun 15 olle 1548         if (sourceItemStringList.contains(name))
3434 29 Jun 15 olle 1549         {
3434 29 Jun 15 olle 1550           itemList = l;
3434 29 Jun 15 olle 1551           break;
3434 29 Jun 15 olle 1552         }
3434 29 Jun 15 olle 1553       }
3434 29 Jun 15 olle 1554     }
3434 29 Jun 15 olle 1555     return itemList;
3434 29 Jun 15 olle 1556   }
2933 14 Nov 14 olle 1557 }