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

Code
Comments
Other
Rev Date Author Line
3449 28 Jul 15 olle 1 package net.sf.basedb.meludi.servlet;
3449 28 Jul 15 olle 2
3449 28 Jul 15 olle 3 import java.io.IOException;
3449 28 Jul 15 olle 4 import java.util.ArrayList;
3449 28 Jul 15 olle 5 import java.util.Date;
3449 28 Jul 15 olle 6 import java.util.HashMap;
3449 28 Jul 15 olle 7 import java.util.List;
3449 28 Jul 15 olle 8 import java.util.Map;
3449 28 Jul 15 olle 9
3449 28 Jul 15 olle 10 import javax.servlet.ServletException;
3449 28 Jul 15 olle 11 import javax.servlet.http.HttpServlet;
3449 28 Jul 15 olle 12 import javax.servlet.http.HttpServletRequest;
3449 28 Jul 15 olle 13 import javax.servlet.http.HttpServletResponse;
3449 28 Jul 15 olle 14
3449 28 Jul 15 olle 15 import org.json.simple.JSONArray;
3449 28 Jul 15 olle 16 import org.json.simple.JSONObject;
3449 28 Jul 15 olle 17 import org.json.simple.parser.JSONParser;
3449 28 Jul 15 olle 18
3449 28 Jul 15 olle 19 import net.sf.basedb.core.AnyToAny;
3449 28 Jul 15 olle 20 import net.sf.basedb.core.Application;
3652 08 Dec 15 olle 21 import net.sf.basedb.core.BioMaterial;
3449 28 Jul 15 olle 22 import net.sf.basedb.core.BioMaterialEvent;
3449 28 Jul 15 olle 23 import net.sf.basedb.core.BioMaterialEventSource;
3449 28 Jul 15 olle 24 import net.sf.basedb.core.ItemList;
3449 28 Jul 15 olle 25 import net.sf.basedb.core.BioPlate;
3449 28 Jul 15 olle 26 import net.sf.basedb.core.BioPlateEvent;
3449 28 Jul 15 olle 27 import net.sf.basedb.core.BioPlateEventParticipant;
3449 28 Jul 15 olle 28 import net.sf.basedb.core.BioPlateEventType;
3652 08 Dec 15 olle 29 import net.sf.basedb.core.BioPlateType;
3449 28 Jul 15 olle 30 import net.sf.basedb.core.BioWell;
3449 28 Jul 15 olle 31 import net.sf.basedb.core.DbControl;
3449 28 Jul 15 olle 32 import net.sf.basedb.core.Extract;
3449 28 Jul 15 olle 33 import net.sf.basedb.core.File;
3449 28 Jul 15 olle 34 import net.sf.basedb.core.Include;
3449 28 Jul 15 olle 35 import net.sf.basedb.core.InvalidDataException;
3449 28 Jul 15 olle 36 import net.sf.basedb.core.ItemQuery;
3449 28 Jul 15 olle 37 import net.sf.basedb.core.ItemResultIterator;
3449 28 Jul 15 olle 38 import net.sf.basedb.core.ItemResultList;
3449 28 Jul 15 olle 39 import net.sf.basedb.core.ItemSubtype;
3628 26 Nov 15 olle 40 import net.sf.basedb.core.Kit;
3449 28 Jul 15 olle 41 import net.sf.basedb.core.Path;
3449 28 Jul 15 olle 42 import net.sf.basedb.core.PlateGeometry;
3449 28 Jul 15 olle 43 import net.sf.basedb.core.Protocol;
3449 28 Jul 15 olle 44 import net.sf.basedb.core.SessionControl;
3449 28 Jul 15 olle 45 import net.sf.basedb.core.Tag;
3449 28 Jul 15 olle 46 import net.sf.basedb.core.Type;
3449 28 Jul 15 olle 47 import net.sf.basedb.core.query.Annotations;
3449 28 Jul 15 olle 48 import net.sf.basedb.core.query.Expression;
3449 28 Jul 15 olle 49 import net.sf.basedb.core.query.Expressions;
3449 28 Jul 15 olle 50 import net.sf.basedb.core.query.Hql;
3449 28 Jul 15 olle 51 import net.sf.basedb.core.query.Orders;
3449 28 Jul 15 olle 52 import net.sf.basedb.core.query.Restrictions;
3449 28 Jul 15 olle 53 import net.sf.basedb.core.snapshot.SnapshotManager;
3449 28 Jul 15 olle 54 import net.sf.basedb.meludi.JsonUtil;
3449 28 Jul 15 olle 55 import net.sf.basedb.meludi.Meludi;
3449 28 Jul 15 olle 56 import net.sf.basedb.meludi.counter.CounterService;
3449 28 Jul 15 olle 57 import net.sf.basedb.meludi.dao.Annotationtype;
3449 28 Jul 15 olle 58 import net.sf.basedb.meludi.dao.BiomaterialList;
3449 28 Jul 15 olle 59 import net.sf.basedb.meludi.dao.BioplateType;
3449 28 Jul 15 olle 60 import net.sf.basedb.meludi.dao.Dna;
3449 28 Jul 15 olle 61 /*
3449 28 Jul 15 olle 62 import net.sf.basedb.meludi.dao.CDna;
3449 28 Jul 15 olle 63 import net.sf.basedb.meludi.dao.MRna;
3449 28 Jul 15 olle 64 */
3449 28 Jul 15 olle 65 import net.sf.basedb.meludi.dao.ReactionPlate;
3449 28 Jul 15 olle 66 import net.sf.basedb.meludi.dao.MeludiRole;
3449 28 Jul 15 olle 67 import net.sf.basedb.meludi.dao.Rna;
3449 28 Jul 15 olle 68 import net.sf.basedb.meludi.dao.Subtype;
3449 28 Jul 15 olle 69 import net.sf.basedb.util.Values;
3449 28 Jul 15 olle 70 import net.sf.basedb.util.error.ThrowableUtil;
3449 28 Jul 15 olle 71
3449 28 Jul 15 olle 72
3449 28 Jul 15 olle 73 public class DnaServlet 
3449 28 Jul 15 olle 74   extends HttpServlet 
3449 28 Jul 15 olle 75 {
3449 28 Jul 15 olle 76
3449 28 Jul 15 olle 77   private static final long serialVersionUID = -8104884012462693394L;
3449 28 Jul 15 olle 78
3449 28 Jul 15 olle 79   public DnaServlet()
3449 28 Jul 15 olle 80   {}
3449 28 Jul 15 olle 81
3449 28 Jul 15 olle 82   @SuppressWarnings("unchecked")
3449 28 Jul 15 olle 83   @Override
3449 28 Jul 15 olle 84   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
3449 28 Jul 15 olle 85     throws ServletException, IOException 
3449 28 Jul 15 olle 86   {
3449 28 Jul 15 olle 87
3449 28 Jul 15 olle 88     String ID = req.getParameter("ID");
3449 28 Jul 15 olle 89     String cmd = req.getParameter("cmd");
3449 28 Jul 15 olle 90     JsonUtil.setJsonResponseHeaders(resp);
3450 28 Jul 15 olle 91 //System.out.println(new Date() + " DnaServlet::doGet(): cmd = \"" + cmd + "\"");
3449 28 Jul 15 olle 92     
3449 28 Jul 15 olle 93     JSONObject json = new JSONObject();
3449 28 Jul 15 olle 94     json.put("status", "ok");
3449 28 Jul 15 olle 95     
5468 04 Jun 19 olle 96     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 97     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 98     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
3449 28 Jul 15 olle 99     DbControl dc = null;
3449 28 Jul 15 olle 100     try
3449 28 Jul 15 olle 101     {
3449 28 Jul 15 olle 102       if ("GetNextAutoGeneratedPlateName".equals(cmd))
3449 28 Jul 15 olle 103       {
3449 28 Jul 15 olle 104         /*
3449 28 Jul 15 olle 105           Eg. MRnaPlate0001, MRnaPlate0002 and so on
3449 28 Jul 15 olle 106          */
3449 28 Jul 15 olle 107         BioplateType bioPlateType = BioplateType.getByCName(req.getParameter("bioPlateType"));
3449 28 Jul 15 olle 108         String platePrefix = req.getParameter("bioPlatePrefix");
4225 10 Nov 16 olle 109         Integer plateNumDigits = Meludi.fetchStartPlateItemNumDigits(sc.getActiveProjectId());
3449 28 Jul 15 olle 110         if (platePrefix != null && !platePrefix.equals(""))
3449 28 Jul 15 olle 111         {
3449 28 Jul 15 olle 112           bioPlateType.setPlateNamePrefix(platePrefix);
4225 10 Nov 16 olle 113           bioPlateType.setNumDigitsInName(plateNumDigits);
3449 28 Jul 15 olle 114         }
3449 28 Jul 15 olle 115         dc = sc.newDbControl();
3449 28 Jul 15 olle 116         json.put("name", bioPlateType.generateNextName(dc, 1));
3449 28 Jul 15 olle 117       }
3449 28 Jul 15 olle 118       else if ("GetStratagene".equals(cmd))
3449 28 Jul 15 olle 119       {
3449 28 Jul 15 olle 120         dc = sc.newDbControl();
3449 28 Jul 15 olle 121         List<Rna> stratagene = Rna.findStratagene(dc);
3449 28 Jul 15 olle 122         
3449 28 Jul 15 olle 123         JSONArray jsonStratagene = new JSONArray();
3449 28 Jul 15 olle 124         for (Rna s : stratagene)
3449 28 Jul 15 olle 125         {
3449 28 Jul 15 olle 126           s.loadBioPlateLocation();
3449 28 Jul 15 olle 127           jsonStratagene.add(s.asJSONObject());
3449 28 Jul 15 olle 128         }
3449 28 Jul 15 olle 129         json.put("stratagene", jsonStratagene);
3449 28 Jul 15 olle 130         
3449 28 Jul 15 olle 131       }
3652 08 Dec 15 olle 132       else if ("GetStartPlates".equals(cmd))
3652 08 Dec 15 olle 133       {
3652 08 Dec 15 olle 134         dc = sc.newDbControl();
3652 08 Dec 15 olle 135         BioplateType bioPlateType = BioplateType.getByCName(req.getParameter("bioPlateType"));
3652 08 Dec 15 olle 136         String platePrefix = req.getParameter("bioPlatePrefix");
4225 10 Nov 16 olle 137         Integer plateNumDigits = Meludi.fetchStartPlateItemNumDigits(sc.getActiveProjectId());
3652 08 Dec 15 olle 138         if (platePrefix != null && !platePrefix.equals(""))
3652 08 Dec 15 olle 139         {
3652 08 Dec 15 olle 140           bioPlateType.setPlateNamePrefix(platePrefix);
4225 10 Nov 16 olle 141           bioPlateType.setNumDigitsInName(plateNumDigits);
3652 08 Dec 15 olle 142         }
3652 08 Dec 15 olle 143         
3652 08 Dec 15 olle 144         // Find plates of the given 'plate type' with 'creation date' and no 'destroyed' flag set
3652 08 Dec 15 olle 145         ItemQuery<BioPlate> query = BioPlate.getQuery();
3652 08 Dec 15 olle 146         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3652 08 Dec 15 olle 147 /*
3652 08 Dec 15 olle 148         query.restrict(Restrictions.neq(Hql.property("eventDate"), null));
3652 08 Dec 15 olle 149 */
3652 08 Dec 15 olle 150         bioPlateType.addFilter(dc, query, true);
3652 08 Dec 15 olle 151         
3652 08 Dec 15 olle 152         query.order(Orders.desc(Hql.property("id")));
3652 08 Dec 15 olle 153         
4204 02 Nov 16 olle 154         String sampleItemPrefix = Meludi.fetchSampleItemPrefix(sc.getActiveProjectId());
3652 08 Dec 15 olle 155         List<BioPlate> result = query.list(dc);
3652 08 Dec 15 olle 156         JSONArray jsonPlates = new JSONArray();
3652 08 Dec 15 olle 157         for (BioPlate plate: result)
3652 08 Dec 15 olle 158         {
3652 08 Dec 15 olle 159           // Get protocol and kit from bio plate creation event
3652 08 Dec 15 olle 160           Protocol protocol = null;
3652 08 Dec 15 olle 161           Kit kit = null;
3652 08 Dec 15 olle 162           ItemQuery<BioPlateEvent> plateEventQuery = plate.getEvents();
3652 08 Dec 15 olle 163           List<BioPlateEvent> plateEventList = plateEventQuery.list(dc);
3652 08 Dec 15 olle 164           for (BioPlateEvent bioPlateEvent: plateEventList)
3652 08 Dec 15 olle 165           {
3652 08 Dec 15 olle 166             protocol = bioPlateEvent.getProtocol();
3652 08 Dec 15 olle 167             kit = bioPlateEvent.getKit();
3652 08 Dec 15 olle 168             if (protocol != null)
3652 08 Dec 15 olle 169             {
3652 08 Dec 15 olle 170               break;
3652 08 Dec 15 olle 171             }
3652 08 Dec 15 olle 172           }
3652 08 Dec 15 olle 173           Integer protocolId = null;
3652 08 Dec 15 olle 174           String protocolName = null;
3652 08 Dec 15 olle 175           if (protocol != null)
3652 08 Dec 15 olle 176           {
3652 08 Dec 15 olle 177             protocolId = protocol.getId();
3652 08 Dec 15 olle 178             protocolName = protocol.getName();
3652 08 Dec 15 olle 179           }
3652 08 Dec 15 olle 180           Integer kitId = null;
3652 08 Dec 15 olle 181           String kitName = null;
3652 08 Dec 15 olle 182           if (kit != null)
3652 08 Dec 15 olle 183           {
3652 08 Dec 15 olle 184             kitId = kit.getId();
3652 08 Dec 15 olle 185             kitName = kit.getName();
3652 08 Dec 15 olle 186           }
3652 08 Dec 15 olle 187           // Add plate data to JSONObject
3652 08 Dec 15 olle 188           JSONObject jsonPlate = new JSONObject();
3652 08 Dec 15 olle 189           jsonPlate.put("id", plate.getId());
3652 08 Dec 15 olle 190           jsonPlate.put("name", plate.getName());
3652 08 Dec 15 olle 191           jsonPlate.put("columns", plate.getColumns());
3652 08 Dec 15 olle 192           jsonPlate.put("rows", plate.getRows());
3652 08 Dec 15 olle 193           jsonPlate.put("comments", plate.getDescription());
3652 08 Dec 15 olle 194           jsonPlate.put("protocolId", protocolId);
3652 08 Dec 15 olle 195           jsonPlate.put("protocolName", protocolName);
3652 08 Dec 15 olle 196           jsonPlate.put("kitId", kitId);
3652 08 Dec 15 olle 197           jsonPlate.put("kitName", kitName);
3652 08 Dec 15 olle 198           jsonPlate.put("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(plate.getEntryDate()));
3652 08 Dec 15 olle 199           jsonPlate.put("eventDate", Meludi.CONVERTER_DATE_TO_STRING.convert(plate.getEventDate()));
3652 08 Dec 15 olle 200           if (bioPlateType == BioplateType.DNA)
3652 08 Dec 15 olle 201           {
3652 08 Dec 15 olle 202             jsonPlate.put("DilutionDate", Meludi.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, plate)));
3652 08 Dec 15 olle 203             jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, plate));
3652 08 Dec 15 olle 204           }
3652 08 Dec 15 olle 205           jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, plate));
3652 08 Dec 15 olle 206 //System.out.println(new Date() + " DnaServlet::doGet(): cmd = \"" + cmd + "\" id = " + plate.getId() + " name = " + plate.getName() + " registrationDate = " + plate.getEntryDate() + " eventdate = " + plate.getEventDate());
3652 08 Dec 15 olle 207           int numMeludiItems = 0;
3652 08 Dec 15 olle 208           int numExtraItems = 0;
3652 08 Dec 15 olle 209           JSONObject jsonWells = new JSONObject();
3667 15 Dec 15 olle 210           for (int col = 0; col < 12; col++)
3652 08 Dec 15 olle 211           {
3652 08 Dec 15 olle 212             for (int row = 0; row < 8; row++)
3652 08 Dec 15 olle 213             {
3652 08 Dec 15 olle 214               BioWell bioWell = plate.getBioWell(row, col);
3652 08 Dec 15 olle 215               if (bioWell != null)
3652 08 Dec 15 olle 216               {
3652 08 Dec 15 olle 217                 String location = bioWell.getCoordinate();
3652 08 Dec 15 olle 218                 BioMaterial bioMaterial = bioWell.getBioMaterial();
3652 08 Dec 15 olle 219                 if (bioMaterial != null)
3652 08 Dec 15 olle 220                 {
3652 08 Dec 15 olle 221                   int bioMaterialId = bioMaterial.getId();
3652 08 Dec 15 olle 222                   String bioMaterialName = bioMaterial.getName();
3667 15 Dec 15 olle 223                   // Only count extract source items for FPA aliquots
3667 15 Dec 15 olle 224                   if (bioMaterialName.endsWith(".fpa"))
3652 08 Dec 15 olle 225                   {
4204 02 Nov 16 olle 226                     if (bioMaterialName.startsWith(sampleItemPrefix))
3667 15 Dec 15 olle 227                     {
3667 15 Dec 15 olle 228                       numMeludiItems++;
3667 15 Dec 15 olle 229                     }
3667 15 Dec 15 olle 230                     else
3667 15 Dec 15 olle 231                     {
3667 15 Dec 15 olle 232                       numExtraItems++;
3667 15 Dec 15 olle 233                     }
3652 08 Dec 15 olle 234                   }
3652 08 Dec 15 olle 235                   ItemSubtype itemSubtype = bioMaterial.getItemSubtype();
3652 08 Dec 15 olle 236                   Extract extract = Extract.getById(dc, bioMaterialId);
3652 08 Dec 15 olle 237                   BioMaterial parentBioMaterial = extract.getParent();
3652 08 Dec 15 olle 238                   Extract parentExtract = Extract.getById(dc, parentBioMaterial.getId());
3652 08 Dec 15 olle 239                   int parentId = parentExtract.getId();
3693 15 Jan 16 olle 240                   int id = extract.getId();
3667 15 Dec 15 olle 241                   String name = extract.getName();
3652 08 Dec 15 olle 242                   String parentName = parentExtract.getName();
3652 08 Dec 15 olle 243                   Float parentQubitConc = (Float) Annotationtype.QUBIT_CONC.getAnnotationValue(dc, parentExtract);
3652 08 Dec 15 olle 244                   Float parentDeltaCt = (Float) Annotationtype.DELTA_CT.getAnnotationValue(dc, parentExtract);
3652 08 Dec 15 olle 245                   Float parentOriginalQuantity = parentExtract.getOriginalQuantity();
3652 08 Dec 15 olle 246                   String parentPlate = null;
3652 08 Dec 15 olle 247                   String parentPlateCoord = null;
3652 08 Dec 15 olle 248                   String parentLocation = null;
3652 08 Dec 15 olle 249                   BioWell parentBioWell = parentExtract.getBioWell();
3652 08 Dec 15 olle 250                   if (parentBioWell != null)
3652 08 Dec 15 olle 251                   {
3652 08 Dec 15 olle 252                     parentPlate = parentBioWell.getPlate().getName();
3652 08 Dec 15 olle 253                     parentPlateCoord = parentBioWell.getCoordinate();
3652 08 Dec 15 olle 254                     parentLocation = parentPlate + "[" + parentPlateCoord + "]";
3652 08 Dec 15 olle 255                   }
3652 08 Dec 15 olle 256                   JSONObject jsonWell = new JSONObject();
3652 08 Dec 15 olle 257                   JSONObject jsonExtract = new JSONObject();
3652 08 Dec 15 olle 258                   JSONObject jsonInfo = new JSONObject();
3652 08 Dec 15 olle 259                   JSONObject jsonBioWell = new JSONObject();
3667 15 Dec 15 olle 260                   jsonBioWell.put("origLocation", parentLocation);
3667 15 Dec 15 olle 261                   jsonInfo.put("origId", parentId);
3667 15 Dec 15 olle 262                   jsonInfo.put("origQubitConc", parentQubitConc);
3667 15 Dec 15 olle 263                   jsonInfo.put("origDeltaCt", parentDeltaCt);
3652 08 Dec 15 olle 264                   jsonInfo.put("originalQuantity", parentOriginalQuantity);
3667 15 Dec 15 olle 265                   jsonInfo.put("origBioWell", jsonBioWell);
3652 08 Dec 15 olle 266                   jsonExtract.put("info", jsonInfo);
3693 15 Jan 16 olle 267                   jsonExtract.put("id", id);
3667 15 Dec 15 olle 268                   jsonExtract.put("name", name);
3667 15 Dec 15 olle 269                   jsonExtract.put("origName", parentName);
3652 08 Dec 15 olle 270                   jsonWell.put("location", location);
3652 08 Dec 15 olle 271                   jsonWell.put("extract", jsonExtract);
3652 08 Dec 15 olle 272                   jsonWells.put(location, jsonWell);
3652 08 Dec 15 olle 273 //System.out.println(new Date() + " DnaServlet::doGet(): cmd = \"" + cmd + "\" id = " + plate.getId() + " name = " + plate.getName() + " row = " + row + " col = " + col + " bioMaterialName = " + bioMaterialName + " parentName = " + parentName + " parentQubitConc = " + parentQubitConc + " parentDeltaCt = " + parentDeltaCt + " parentOriginalQuantity = " + parentOriginalQuantity + " parentLocation = " + parentLocation + " parentPlate = " + parentPlate + " parentPlateLoc = " + parentPlateCoord);
3652 08 Dec 15 olle 274 //System.out.println(new Date() + " DnaServlet::doGet(): cmd = \"" + cmd + "\" id = " + plate.getId() + " name = " + plate.getName() + " row = " + row + " col = " + col + " bioMaterialName = " + bioMaterialName + " dna = " + dna.asJSONObject());
3652 08 Dec 15 olle 275                 }
3652 08 Dec 15 olle 276               }
3652 08 Dec 15 olle 277             }
3652 08 Dec 15 olle 278           }
3652 08 Dec 15 olle 279           jsonPlate.put("numMeludiItems", numMeludiItems);
3652 08 Dec 15 olle 280           jsonPlate.put("numExtraItems", numExtraItems);
3652 08 Dec 15 olle 281           jsonPlate.put("wells", jsonWells);
3652 08 Dec 15 olle 282           //
3652 08 Dec 15 olle 283           jsonPlates.add(jsonPlate);
3652 08 Dec 15 olle 284         }
3652 08 Dec 15 olle 285         json.put("bioplates", jsonPlates);
3652 08 Dec 15 olle 286       }
3449 28 Jul 15 olle 287       else if ("GetUnprocessedPlates".equals(cmd))
3449 28 Jul 15 olle 288       {
3449 28 Jul 15 olle 289         dc = sc.newDbControl();
3449 28 Jul 15 olle 290         BioplateType plateType = BioplateType.getByCName(req.getParameter("plateType"));
3449 28 Jul 15 olle 291         
3449 28 Jul 15 olle 292         // Find plates of the given 'plate type' without 'creation date' and no 'destroyed' flag set
3449 28 Jul 15 olle 293         ItemQuery<BioPlate> query = BioPlate.getQuery();
3449 28 Jul 15 olle 294         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 295         query.restrict(Restrictions.eq(Hql.property("eventDate"), null));
3449 28 Jul 15 olle 296         plateType.addFilter(dc, query, true);
3449 28 Jul 15 olle 297         
3449 28 Jul 15 olle 298         if (req.getParameter("onlyDiluted") != null)
3449 28 Jul 15 olle 299         {
3449 28 Jul 15 olle 300           // Must have a DILUTION_DATE annotation OR AUTO_PROCESSING = PreNormalizeRNA
3449 28 Jul 15 olle 301           query.join(Annotations.leftJoin(null, Annotationtype.DILUTION_DATE.load(dc), "dd"));
3449 28 Jul 15 olle 302 /*
3449 28 Jul 15 olle 303           query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
3449 28 Jul 15 olle 304 */
3449 28 Jul 15 olle 305 /*
3449 28 Jul 15 olle 306           query.restrict(
3449 28 Jul 15 olle 307             Restrictions.or(
3449 28 Jul 15 olle 308               Restrictions.neq(Hql.alias("dd"), null),
3449 28 Jul 15 olle 309               Restrictions.eq(Hql.alias("ap"), Expressions.string(Rna.AUTO_PROCESSING_PRE_NORMALIZE))
3449 28 Jul 15 olle 310             ));
3449 28 Jul 15 olle 311 */
3449 28 Jul 15 olle 312           query.restrict(
3449 28 Jul 15 olle 313               Restrictions.or(
3449 28 Jul 15 olle 314                 Restrictions.neq(Hql.alias("dd"), null)
3449 28 Jul 15 olle 315               ));
3449 28 Jul 15 olle 316         }
3449 28 Jul 15 olle 317         else if (req.getParameter("onlyUndiluted") != null)
3449 28 Jul 15 olle 318         {
3449 28 Jul 15 olle 319           // Must not have a DILUTION_DATE annotation AND no AUTO_PROCESSING annotation
3449 28 Jul 15 olle 320           query.join(Annotations.leftJoin(null, Annotationtype.DILUTION_DATE.load(dc), "dd"));
3449 28 Jul 15 olle 321 /*
3449 28 Jul 15 olle 322           query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
3449 28 Jul 15 olle 323 */
3449 28 Jul 15 olle 324           query.restrict(Restrictions.eq(Hql.alias("dd"), null));
3449 28 Jul 15 olle 325           query.restrict(Restrictions.eq(Hql.alias("ap"), null));
3449 28 Jul 15 olle 326         }
3449 28 Jul 15 olle 327         
3449 28 Jul 15 olle 328         query.order(Orders.desc(Hql.property("id")));
3449 28 Jul 15 olle 329         
3449 28 Jul 15 olle 330         List<BioPlate> result = query.list(dc);
3449 28 Jul 15 olle 331         JSONArray jsonPlates = new JSONArray();
3449 28 Jul 15 olle 332         for (BioPlate plate: result)
3449 28 Jul 15 olle 333         {
3449 28 Jul 15 olle 334           JSONObject jsonPlate = new JSONObject();
3449 28 Jul 15 olle 335           jsonPlate.put("id", plate.getId());
3449 28 Jul 15 olle 336           jsonPlate.put("name", plate.getName());
3449 28 Jul 15 olle 337           jsonPlate.put("columns", plate.getColumns());
3449 28 Jul 15 olle 338           jsonPlate.put("rows", plate.getRows());
3449 28 Jul 15 olle 339           jsonPlate.put("comments", plate.getDescription());
3449 28 Jul 15 olle 340 /*
3449 28 Jul 15 olle 341           if (plateType == BioplateType.MRNA)
3449 28 Jul 15 olle 342           {
3449 28 Jul 15 olle 343             jsonPlate.put("AutoProcessing", Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 344             jsonPlate.put("DilutionDate", Meludi.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, plate)));
3449 28 Jul 15 olle 345             jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 346           }
3449 28 Jul 15 olle 347 */
3449 28 Jul 15 olle 348           if (plateType == BioplateType.DNA)
3449 28 Jul 15 olle 349           {
3449 28 Jul 15 olle 350 /*
3449 28 Jul 15 olle 351             jsonPlate.put("AutoProcessing", Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 352 */
3449 28 Jul 15 olle 353             jsonPlate.put("DilutionDate", Meludi.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, plate)));
3449 28 Jul 15 olle 354             jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 355           }
3449 28 Jul 15 olle 356           jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 357           jsonPlates.add(jsonPlate);
3449 28 Jul 15 olle 358         }
3449 28 Jul 15 olle 359         json.put("bioplates", jsonPlates);
3449 28 Jul 15 olle 360       }
3449 28 Jul 15 olle 361       else if ("GetDnaInfoForPlate".equals(cmd))
3449 28 Jul 15 olle 362       {
3449 28 Jul 15 olle 363         int dnaPlateId = Values.getInt(req.getParameter("bioplate"));
3449 28 Jul 15 olle 364         dc = sc.newDbControl();
3449 28 Jul 15 olle 365
3449 28 Jul 15 olle 366         BioPlate dnaPlate = BioPlate.getById(dc, dnaPlateId);
3449 28 Jul 15 olle 367
3449 28 Jul 15 olle 368         JSONObject jsonPlate = new JSONObject();
3449 28 Jul 15 olle 369         jsonPlate.put("id", dnaPlate.getId());
3449 28 Jul 15 olle 370         jsonPlate.put("name", dnaPlate.getName());
3449 28 Jul 15 olle 371         jsonPlate.put("columns", dnaPlate.getColumns());
3449 28 Jul 15 olle 372         jsonPlate.put("rows", dnaPlate.getRows());
3449 28 Jul 15 olle 373         jsonPlate.put("comments", dnaPlate.getDescription());
3449 28 Jul 15 olle 374         jsonPlate.put("DilutionDate", Meludi.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, dnaPlate)));
3449 28 Jul 15 olle 375         jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, dnaPlate));
3449 28 Jul 15 olle 376         jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, dnaPlate));
3449 28 Jul 15 olle 377         
3449 28 Jul 15 olle 378         ItemQuery<Extract> query = Extract.getQuery();
3449 28 Jul 15 olle 379         query.join(Hql.innerJoin(null, "bioWell", "bw", true));
3449 28 Jul 15 olle 380         query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
3449 28 Jul 15 olle 381         query.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entity(dnaPlate)));
3449 28 Jul 15 olle 382         query.order(Orders.asc(Hql.property("bw", "column")));
3449 28 Jul 15 olle 383         query.order(Orders.asc(Hql.property("bw", "row")));
3449 28 Jul 15 olle 384
3449 28 Jul 15 olle 385         List<Dna> dnaList = Dna.toList(query.list(dc));
3449 28 Jul 15 olle 386 /*
3449 28 Jul 15 olle 387         List<Extract> extractList = query.list(dc);
3449 28 Jul 15 olle 388         List<Dna> dnaList = new ArrayList<Dna>();
3449 28 Jul 15 olle 389         for (Extract e: extractList)
3449 28 Jul 15 olle 390         {
3449 28 Jul 15 olle 391           Dna dna = Dna.get(e);
3449 28 Jul 15 olle 392           dnaList.add(dna);
3449 28 Jul 15 olle 393         }
3449 28 Jul 15 olle 394 */
3449 28 Jul 15 olle 395         JSONArray jsonDna = new JSONArray();
3449 28 Jul 15 olle 396         
3449 28 Jul 15 olle 397         for (Dna d : dnaList)
3449 28 Jul 15 olle 398         {
3450 28 Jul 15 olle 399 //System.out.println(new Date() + " DnaServlet::doGet(): cmd = \"" + cmd + "\" d.getName() = " + d.getName());
3449 28 Jul 15 olle 400           d.loadBioPlateLocation();
3449 28 Jul 15 olle 401           d.loadAnnotations(dc, "DilutionConc", Annotationtype.DILUTION_CONC, null);
3449 28 Jul 15 olle 402 /*
3449 28 Jul 15 olle 403           r.loadAnnotations(dc, "UseForQC", Annotationtype.USE_FOR_QC, null);
3449 28 Jul 15 olle 404 */
3449 28 Jul 15 olle 405 /*
3449 28 Jul 15 olle 406           Dna dna = r.getDna();
3449 28 Jul 15 olle 407 */
3449 28 Jul 15 olle 408           Dna dna = d;
3449 28 Jul 15 olle 409 /*
3449 28 Jul 15 olle 410           boolean preNormalized = dna.isPreNormalized(dc);
3449 28 Jul 15 olle 411           dna.setAnnotation("preNormalized", preNormalized);
3449 28 Jul 15 olle 412           if (!preNormalized)
3449 28 Jul 15 olle 413           {
3449 28 Jul 15 olle 414 */
3449 28 Jul 15 olle 415             dna.loadBioPlateLocation();
3449 28 Jul 15 olle 416 /*
3449 28 Jul 15 olle 417           }
3449 28 Jul 15 olle 418 */
3449 28 Jul 15 olle 419
3449 28 Jul 15 olle 420 /*
3449 28 Jul 15 olle 421           if (Dna.isSpecialRna(r.getName()))
3449 28 Jul 15 olle 422           {
3449 28 Jul 15 olle 423             // Makes it possible to track some info about external DNA in MeLuDI
3449 28 Jul 15 olle 424             dna.setAnnotation("QubitConc", Annotationtype.QUBIT_CONC.getAnnotationValue(dc, r.getExtract()));
3449 28 Jul 15 olle 425           }
3449 28 Jul 15 olle 426           else
3449 28 Jul 15 olle 427           {
3449 28 Jul 15 olle 428 */
3449 28 Jul 15 olle 429             dna.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
3449 28 Jul 15 olle 430 /*
3449 28 Jul 15 olle 431           }
3449 28 Jul 15 olle 432 */
3449 28 Jul 15 olle 433           
3449 28 Jul 15 olle 434           Float used = d.getItem().getCreationEvent().getUsedQuantity(dna.getItem());
3449 28 Jul 15 olle 435           Float remain = dna.getItem().getRemainingQuantity();
3449 28 Jul 15 olle 436           dna.setAnnotation("usedQuantity", used);
3449 28 Jul 15 olle 437           if (used != null && remain != null)
3449 28 Jul 15 olle 438           {
3449 28 Jul 15 olle 439             dna.setAnnotation("remainingQuantity", remain+used);
3449 28 Jul 15 olle 440           }
3449 28 Jul 15 olle 441           
3449 28 Jul 15 olle 442           d.setAnnotation("dna", dna.asJSONObject());
3449 28 Jul 15 olle 443           d.setAnnotation("comment", d.getItem().getDescription());
3449 28 Jul 15 olle 444           jsonDna.add(d.asJSONObject());
3449 28 Jul 15 olle 445           
3449 28 Jul 15 olle 446         }
3449 28 Jul 15 olle 447                 
3449 28 Jul 15 olle 448         json.put("dna", jsonDna);
3449 28 Jul 15 olle 449         json.put("plate", jsonPlate);
3449 28 Jul 15 olle 450       }
3449 28 Jul 15 olle 451       else if ("GetMRnaInfoForPlate".equals(cmd))
3449 28 Jul 15 olle 452       {
3449 28 Jul 15 olle 453         int mRnaPlateId = Values.getInt(req.getParameter("bioplate"));
3449 28 Jul 15 olle 454         dc = sc.newDbControl();
3449 28 Jul 15 olle 455
3449 28 Jul 15 olle 456         BioPlate mRnaPlate = BioPlate.getById(dc, mRnaPlateId);
3449 28 Jul 15 olle 457
3449 28 Jul 15 olle 458         JSONObject jsonPlate = new JSONObject();
3449 28 Jul 15 olle 459         jsonPlate.put("id", mRnaPlate.getId());
3449 28 Jul 15 olle 460         jsonPlate.put("name", mRnaPlate.getName());
3449 28 Jul 15 olle 461         jsonPlate.put("columns", mRnaPlate.getColumns());
3449 28 Jul 15 olle 462         jsonPlate.put("rows", mRnaPlate.getRows());
3449 28 Jul 15 olle 463         jsonPlate.put("comments", mRnaPlate.getDescription());
3449 28 Jul 15 olle 464         jsonPlate.put("DilutionDate", Meludi.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, mRnaPlate)));
3449 28 Jul 15 olle 465         jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, mRnaPlate));
3449 28 Jul 15 olle 466         jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, mRnaPlate));
3449 28 Jul 15 olle 467         
3449 28 Jul 15 olle 468         ItemQuery<Extract> query = Extract.getQuery();
3449 28 Jul 15 olle 469         query.join(Hql.innerJoin(null, "bioWell", "bw", true));
3449 28 Jul 15 olle 470         query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
3449 28 Jul 15 olle 471         query.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entity(mRnaPlate)));
3449 28 Jul 15 olle 472         query.order(Orders.asc(Hql.property("bw", "column")));
3449 28 Jul 15 olle 473         query.order(Orders.asc(Hql.property("bw", "row")));
3449 28 Jul 15 olle 474
3449 28 Jul 15 olle 475 /*
3449 28 Jul 15 olle 476         List<Dna> dnaList = Dna.toList(query.list(dc));
3449 28 Jul 15 olle 477 */
3449 28 Jul 15 olle 478         List<Extract> extractList = query.list(dc);
3449 28 Jul 15 olle 479         List<Dna> dnaList = new ArrayList<Dna>();
3449 28 Jul 15 olle 480         for (Extract e: extractList)
3449 28 Jul 15 olle 481         {
3449 28 Jul 15 olle 482           Dna dna = Dna.get(e);
3449 28 Jul 15 olle 483           dnaList.add(dna);
3449 28 Jul 15 olle 484         }
3449 28 Jul 15 olle 485         JSONArray jsonDna = new JSONArray();
3449 28 Jul 15 olle 486         
3449 28 Jul 15 olle 487         for (Dna r : dnaList)
3449 28 Jul 15 olle 488         {
3449 28 Jul 15 olle 489           r.loadBioPlateLocation();
3449 28 Jul 15 olle 490           r.loadAnnotations(dc, "DilutionConc", Annotationtype.DILUTION_CONC, null);
3449 28 Jul 15 olle 491 /*
3449 28 Jul 15 olle 492           r.loadAnnotations(dc, "UseForQC", Annotationtype.USE_FOR_QC, null);
3449 28 Jul 15 olle 493 */
3449 28 Jul 15 olle 494 /*
3449 28 Jul 15 olle 495           Dna dna = r.getDna();
3449 28 Jul 15 olle 496 */
3449 28 Jul 15 olle 497           Dna dna = r;
3449 28 Jul 15 olle 498 /*
3449 28 Jul 15 olle 499           boolean preNormalized = dna.isPreNormalized(dc);
3449 28 Jul 15 olle 500           dna.setAnnotation("preNormalized", preNormalized);
3449 28 Jul 15 olle 501           if (!preNormalized)
3449 28 Jul 15 olle 502           {
3449 28 Jul 15 olle 503 */
3449 28 Jul 15 olle 504             dna.loadBioPlateLocation();
3449 28 Jul 15 olle 505 /*
3449 28 Jul 15 olle 506           }
3449 28 Jul 15 olle 507 */
3449 28 Jul 15 olle 508
3449 28 Jul 15 olle 509 /*
3449 28 Jul 15 olle 510           if (Dna.isSpecialRna(r.getName()))
3449 28 Jul 15 olle 511           {
3449 28 Jul 15 olle 512             // Makes it possible to track some info about external DNA in MeLuDI
3449 28 Jul 15 olle 513             dna.setAnnotation("QubitConc", Annotationtype.QUBIT_CONC.getAnnotationValue(dc, r.getExtract()));
3449 28 Jul 15 olle 514           }
3449 28 Jul 15 olle 515           else
3449 28 Jul 15 olle 516           {
3449 28 Jul 15 olle 517 */
3449 28 Jul 15 olle 518             dna.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
3449 28 Jul 15 olle 519 /*
3449 28 Jul 15 olle 520           }
3449 28 Jul 15 olle 521 */
3449 28 Jul 15 olle 522           
3449 28 Jul 15 olle 523           Float used = r.getItem().getCreationEvent().getUsedQuantity(dna.getItem());
3449 28 Jul 15 olle 524           Float remain = dna.getItem().getRemainingQuantity();
3449 28 Jul 15 olle 525           dna.setAnnotation("usedQuantity", used);
3449 28 Jul 15 olle 526           if (used != null && remain != null)
3449 28 Jul 15 olle 527           {
3449 28 Jul 15 olle 528             dna.setAnnotation("remainingQuantity", remain+used);
3449 28 Jul 15 olle 529           }
3449 28 Jul 15 olle 530           
3449 28 Jul 15 olle 531           r.setAnnotation("dna", dna.asJSONObject());
3449 28 Jul 15 olle 532           r.setAnnotation("comment", r.getItem().getDescription());
3449 28 Jul 15 olle 533           jsonDna.add(r.asJSONObject());
3449 28 Jul 15 olle 534           
3449 28 Jul 15 olle 535         }
3449 28 Jul 15 olle 536                 
3449 28 Jul 15 olle 537         json.put("dna", jsonDna);
3449 28 Jul 15 olle 538         json.put("plate", jsonPlate);
3449 28 Jul 15 olle 539       }
3449 28 Jul 15 olle 540
3449 28 Jul 15 olle 541       
3449 28 Jul 15 olle 542     }
3449 28 Jul 15 olle 543     catch (Throwable t)
3449 28 Jul 15 olle 544     {
3449 28 Jul 15 olle 545       t.printStackTrace();
3449 28 Jul 15 olle 546       json.clear();
3449 28 Jul 15 olle 547       json.put("status", "error");
3449 28 Jul 15 olle 548       json.put("message", t.getMessage());
3449 28 Jul 15 olle 549       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3449 28 Jul 15 olle 550     }
3449 28 Jul 15 olle 551     finally
3449 28 Jul 15 olle 552     {
3450 28 Jul 15 olle 553 //System.out.println(new Date() + " DnaServlet::doGet(): cmd = \"" + cmd + "\" End");
3449 28 Jul 15 olle 554       if (dc != null) dc.close();
3449 28 Jul 15 olle 555       json.writeJSONString(resp.getWriter());
3449 28 Jul 15 olle 556     }
3449 28 Jul 15 olle 557     
3449 28 Jul 15 olle 558   }
3449 28 Jul 15 olle 559
3449 28 Jul 15 olle 560   @SuppressWarnings("unchecked")
3449 28 Jul 15 olle 561   @Override
3449 28 Jul 15 olle 562   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
3449 28 Jul 15 olle 563     throws ServletException, IOException 
3449 28 Jul 15 olle 564   {
3449 28 Jul 15 olle 565     String ID = req.getParameter("ID");
3449 28 Jul 15 olle 566     String cmd = req.getParameter("cmd");
3449 28 Jul 15 olle 567     JsonUtil.setJsonResponseHeaders(resp);
3450 28 Jul 15 olle 568 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\"");
3449 28 Jul 15 olle 569     
3449 28 Jul 15 olle 570     JSONObject json = new JSONObject();
3449 28 Jul 15 olle 571     json.put("status", "ok");
3449 28 Jul 15 olle 572     
3449 28 Jul 15 olle 573     JSONArray jsonMessages = new JSONArray();
3449 28 Jul 15 olle 574   
5468 04 Jun 19 olle 575     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 576     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 577     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
3449 28 Jul 15 olle 578     DbControl dc = null;
3449 28 Jul 15 olle 579     try
3449 28 Jul 15 olle 580     {
3449 28 Jul 15 olle 581 /*
3449 28 Jul 15 olle 582       if ("GetRnaInfoFromNames".equals(cmd))
3449 28 Jul 15 olle 583 */
3449 28 Jul 15 olle 584       if ("GetDnaInfoFromNames".equals(cmd))
3449 28 Jul 15 olle 585       {
3449 28 Jul 15 olle 586         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 587         JSONArray names = (JSONArray)jsonReq.get("names");
3449 28 Jul 15 olle 588         
3449 28 Jul 15 olle 589         dc = sc.newDbControl();
3449 28 Jul 15 olle 590         ItemQuery<Extract> query = Extract.getQuery();
3449 28 Jul 15 olle 591         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 592 /*
3449 28 Jul 15 olle 593         query.restrict(
3449 28 Jul 15 olle 594           Restrictions.or(
3449 28 Jul 15 olle 595             Subtype.RNA.restriction(dc, null),  
3449 28 Jul 15 olle 596             Subtype.RNA_NORMALIZED_ALIQUOT.restriction(dc, null)
3449 28 Jul 15 olle 597         ));
3449 28 Jul 15 olle 598 */
3449 28 Jul 15 olle 599         Subtype.DNA.addFilter(dc, query);
3449 28 Jul 15 olle 600         
3449 28 Jul 15 olle 601         query.restrict(Restrictions.in(Hql.property("name"), Expressions.parameter("names")));
3449 28 Jul 15 olle 602         query.setParameter("names", names, Type.STRING);
3449 28 Jul 15 olle 603
3449 28 Jul 15 olle 604 /*
3449 28 Jul 15 olle 605         List<Dna> dna = Dna.toRna(query.list(dc));
3449 28 Jul 15 olle 606 */
3449 28 Jul 15 olle 607         List<Extract> extractList = query.list(dc);
3449 28 Jul 15 olle 608         List<Dna> dna = new ArrayList<Dna>();
3449 28 Jul 15 olle 609         for (Extract e: extractList)
3449 28 Jul 15 olle 610         {
3449 28 Jul 15 olle 611           Dna d = Dna.get(e);
3449 28 Jul 15 olle 612           dna.add(d);
3450 28 Jul 15 olle 613 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" d.getName() = " + d.getName());
3449 28 Jul 15 olle 614         }
3449 28 Jul 15 olle 615         JSONArray jsonDna = new JSONArray();
3449 28 Jul 15 olle 616         SnapshotManager manager = new SnapshotManager();
3449 28 Jul 15 olle 617         for (Dna r : dna)
3449 28 Jul 15 olle 618         {
3449 28 Jul 15 olle 619 /*
3449 28 Jul 15 olle 620           boolean preNormalized = r.isPreNormalized(dc);
3449 28 Jul 15 olle 621           r.setAnnotation("preNormalized", preNormalized);
3449 28 Jul 15 olle 622 */
3449 28 Jul 15 olle 623           r.setAnnotation("remainingQuantity", r.getExtract().getRemainingQuantity());
3449 28 Jul 15 olle 624           r.loadAnnotations(dc, manager, "QubitConc", Annotationtype.QUBIT_CONC, null);
3449 28 Jul 15 olle 625           r.loadAnnotations(dc, manager, "deltaCt", Annotationtype.DELTA_CT, null);
3449 28 Jul 15 olle 626 /*
3449 28 Jul 15 olle 627           r.loadAnnotations(dc, manager, "AutoProcessing", Annotationtype.AUTO_PROCESSING, null);
3449 28 Jul 15 olle 628 */
3449 28 Jul 15 olle 629 /*
3449 28 Jul 15 olle 630           if (preNormalized)
3449 28 Jul 15 olle 631           {
3449 28 Jul 15 olle 632             r.loadAnnotations(dc, manager, "DilutionDate", Annotationtype.DILUTION_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3449 28 Jul 15 olle 633           }
3449 28 Jul 15 olle 634           else
3449 28 Jul 15 olle 635           {
3449 28 Jul 15 olle 636 */
3449 28 Jul 15 olle 637             r.loadAnnotations(dc, manager, "QiacubeDate", Annotationtype.QIACUBE_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3449 28 Jul 15 olle 638             //r.loadBioPlateLocation();
3449 28 Jul 15 olle 639 /*
3449 28 Jul 15 olle 640             r.loadDnaQc(dc, manager);
3449 28 Jul 15 olle 641 */
3449 28 Jul 15 olle 642 /*
3449 28 Jul 15 olle 643           }
3449 28 Jul 15 olle 644 */
3449 28 Jul 15 olle 645           r.setAnnotation("comment", r.getExtract().getDescription());
3449 28 Jul 15 olle 646 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" r.asJSONObject() = " + r.asJSONObject());
3449 28 Jul 15 olle 647 /*
3449 28 Jul 15 olle 648           jsonDna.add(r.asJSONObject());
3449 28 Jul 15 olle 649 */
3449 28 Jul 15 olle 650           JSONObject jsonDnaSingle = r.asJSONObject();
3449 28 Jul 15 olle 651           jsonDnaSingle.put("bioWell", r.loadBioPlateLocation());
3449 28 Jul 15 olle 652           jsonDna.add(jsonDnaSingle);
3450 28 Jul 15 olle 653 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" jsonDnaSingle = " + jsonDnaSingle);
3449 28 Jul 15 olle 654         }
3449 28 Jul 15 olle 655         
3449 28 Jul 15 olle 656         json.put("dna", jsonDna);
3449 28 Jul 15 olle 657       }
3449 28 Jul 15 olle 658       else if ("AutoSelectDna".equals(cmd))
3449 28 Jul 15 olle 659       {
3449 28 Jul 15 olle 660         dc = sc.newDbControl();
3449 28 Jul 15 olle 661         SnapshotManager manager = new SnapshotManager();
3449 28 Jul 15 olle 662         
3449 28 Jul 15 olle 663         int numToSelect = Values.getInt(req.getParameter("numToSelect"));
3449 28 Jul 15 olle 664         
3449 28 Jul 15 olle 665         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 666         JSONArray jsonIgnore = (JSONArray)jsonReq.get("ignore");
3449 28 Jul 15 olle 667         
3449 28 Jul 15 olle 668         boolean preNormalized = jsonReq.get("preNormalized") != null;
3449 28 Jul 15 olle 669
3449 28 Jul 15 olle 670         String startDateStr = (String)jsonReq.get("startDate");
3449 28 Jul 15 olle 671         Date startDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("startDate"));
3449 28 Jul 15 olle 672         Float remainingQuantityLimit = preNormalized ? null : JsonUtil.getAsFloat(jsonReq, "remainingQuantity");
3449 28 Jul 15 olle 673         Float qualityScoreLimit = preNormalized ? null : JsonUtil.getAsFloat(jsonReq, "qualityScore");
3449 28 Jul 15 olle 674         boolean flagNotSelected = !preNormalized && jsonReq.get("flag") != null;
3449 28 Jul 15 olle 675         boolean reProcess = !preNormalized && jsonReq.get("reProcess") != null;
3450 28 Jul 15 olle 676 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" startDate = " + startDateStr + " remainingQuantityLimit = " + remainingQuantityLimit + " qualityScoreLmit = " + qualityScoreLimit + " flagNotSelected = " + flagNotSelected + " reProcess = " + reProcess);
3449 28 Jul 15 olle 677         
3449 28 Jul 15 olle 678         ItemQuery<Extract> query = Extract.getQuery();
3449 28 Jul 15 olle 679         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 680 /*
3449 28 Jul 15 olle 681         if (preNormalized)
3449 28 Jul 15 olle 682         {
3449 28 Jul 15 olle 683           Subtype.RNA_NORMALIZED_ALIQUOT.addFilter(dc, query);        
3449 28 Jul 15 olle 684           Number protocolId = (Number)jsonReq.get("normalizationProtocol");
3449 28 Jul 15 olle 685           query.restrict(Restrictions.eq(Hql.property("creationEvent.protocol.id"), Expressions.integer(protocolId.intValue())));
3449 28 Jul 15 olle 686         }
3449 28 Jul 15 olle 687         else
3449 28 Jul 15 olle 688         {
3449 28 Jul 15 olle 689 */
3449 28 Jul 15 olle 690           Subtype.DNA.addFilter(dc, query);
3449 28 Jul 15 olle 691 /*
3449 28 Jul 15 olle 692         }
3449 28 Jul 15 olle 693 */
3449 28 Jul 15 olle 694         
3449 28 Jul 15 olle 695         // Filter for DNA already included on the plate
3449 28 Jul 15 olle 696         if (jsonIgnore != null && jsonIgnore.size() > 0)
3449 28 Jul 15 olle 697         {
3449 28 Jul 15 olle 698           query.restrict(Restrictions.not(Restrictions.in(Hql.property("id"), Expressions.parameter("ignore"))));
3449 28 Jul 15 olle 699           query.setParameter("ignore", jsonIgnore, Type.LONG);
3449 28 Jul 15 olle 700         }
3449 28 Jul 15 olle 701         
3449 28 Jul 15 olle 702         // Filter for non-empty creation date or creation date after startdate
3449 28 Jul 15 olle 703         query.join(Hql.innerJoin("creationEvent", "ce"));
3449 28 Jul 15 olle 704         if (startDate == null)
3449 28 Jul 15 olle 705         {
3449 28 Jul 15 olle 706           query.restrict(Restrictions.neq(Hql.property("ce", "eventDate"), null));
3449 28 Jul 15 olle 707         }
3449 28 Jul 15 olle 708         else
3449 28 Jul 15 olle 709         {
3449 28 Jul 15 olle 710           query.restrict(Restrictions.gteq(Hql.property("ce", "eventDate"), Expressions.parameter("startDate", startDate, Type.DATE)));
3449 28 Jul 15 olle 711         }
3449 28 Jul 15 olle 712         
3449 28 Jul 15 olle 713         // Filter on parent != null (to get rid of 'Stratagene')
3449 28 Jul 15 olle 714         query.restrict(Restrictions.neq(Hql.property("parent"), null));
3449 28 Jul 15 olle 715
3449 28 Jul 15 olle 716 /*
3449 28 Jul 15 olle 717         // Join AUTO_PROCESSING annotation
3449 28 Jul 15 olle 718         query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
3449 28 Jul 15 olle 719 */
3449 28 Jul 15 olle 720
3449 28 Jul 15 olle 721 /*
3449 28 Jul 15 olle 722         // Create a subquery that load all RNA extracts that has at least one mRNA child extract
3449 28 Jul 15 olle 723         ItemQuery<Extract> mrnaQuery = Extract.getQuery();
3449 28 Jul 15 olle 724         mrnaQuery.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 725         // Filter on RNA subtype
3449 28 Jul 15 olle 726         if (preNormalized)
3449 28 Jul 15 olle 727         {
3449 28 Jul 15 olle 728           Subtype.RNA_NORMALIZED_ALIQUOT.addFilter(dc, mrnaQuery);
3449 28 Jul 15 olle 729         }
3449 28 Jul 15 olle 730         else
3449 28 Jul 15 olle 731         {
3449 28 Jul 15 olle 732           Subtype.RNA.addFilter(dc, mrnaQuery);
3449 28 Jul 15 olle 733         }
3449 28 Jul 15 olle 734         
3449 28 Jul 15 olle 735         // Join child items and filter on mRNA/Library subtype
3449 28 Jul 15 olle 736         mrnaQuery.join(Hql.innerJoin("childCreationEvents", "cce"));
3449 28 Jul 15 olle 737         mrnaQuery.join(Hql.innerJoin("cce", "event", "evt", null, false));
3449 28 Jul 15 olle 738         mrnaQuery.join(Hql.innerJoin("evt", "bioMaterial", "bm", null, false));
3449 28 Jul 15 olle 739         mrnaQuery.restrict(Restrictions.or(
3449 28 Jul 15 olle 740           Subtype.MRNA.restriction(dc, "bm"),
3449 28 Jul 15 olle 741           Subtype.LIBRARY.restriction(dc, "bm")
3449 28 Jul 15 olle 742         ));
3449 28 Jul 15 olle 743
3449 28 Jul 15 olle 744         // Create filter: AUTO_PROCESS==ReProcess OR (AUTO_PROCESS=null AND <no mrna/library children>)
3449 28 Jul 15 olle 745         Expression autoProcessing = Hql.alias("ap");
3449 28 Jul 15 olle 746         query.restrict(
3449 28 Jul 15 olle 747           Restrictions.nullSafeOr(
3449 28 Jul 15 olle 748             reProcess ? Restrictions.eq(autoProcessing, Expressions.string("ReProcess")) : null,
3449 28 Jul 15 olle 749             Restrictions.and(
3449 28 Jul 15 olle 750               Restrictions.eq(autoProcessing, null),
3449 28 Jul 15 olle 751               Restrictions.neq(Hql.property("id"), Expressions.all(mrnaQuery))
3449 28 Jul 15 olle 752             )
3449 28 Jul 15 olle 753           )
3449 28 Jul 15 olle 754         );
3449 28 Jul 15 olle 755 */
3449 28 Jul 15 olle 756 /*
3449 28 Jul 15 olle 757         if (qualityScoreLimit != null)
3449 28 Jul 15 olle 758         {
3449 28 Jul 15 olle 759           // Join DELTA_CT annotation
3449 28 Jul 15 olle 760           query.join(Annotations.leftJoin(null, Annotationtype.DELTA_CT.load(dc), "dct"));
3449 28 Jul 15 olle 761
3449 28 Jul 15 olle 762           // Create filter: DELTA_CT != null AND DELTA_CT <= qualityScoreLimit
3449 28 Jul 15 olle 763           Expression deltaCt = Hql.alias("dct");
3449 28 Jul 15 olle 764           query.restrict(
3449 28 Jul 15 olle 765             Restrictions.and(
3449 28 Jul 15 olle 766               Restrictions.neq(deltaCt, null),
3449 28 Jul 15 olle 767               Restrictions.lteq(deltaCt, Expressions.aFloat(qualityScoreLimit))
3449 28 Jul 15 olle 768             )
3449 28 Jul 15 olle 769           );
3449 28 Jul 15 olle 770         }
3449 28 Jul 15 olle 771 */
3449 28 Jul 15 olle 772
3449 28 Jul 15 olle 773         // Order by DNA plate and location: plate - row - column
3449 28 Jul 15 olle 774 /*
3449 28 Jul 15 olle 775         if (preNormalized)
3449 28 Jul 15 olle 776         {
3449 28 Jul 15 olle 777           query.join(Hql.innerJoin(null, "parent", "rna", true));
3449 28 Jul 15 olle 778           query.join(Hql.innerJoin("rna", "bioWell", "w", true));
3449 28 Jul 15 olle 779         }
3449 28 Jul 15 olle 780         else
3449 28 Jul 15 olle 781         {
3449 28 Jul 15 olle 782 */
3449 28 Jul 15 olle 783           query.join(Hql.innerJoin(null, "bioWell", "w", true));
3449 28 Jul 15 olle 784 /*
3449 28 Jul 15 olle 785         }
3449 28 Jul 15 olle 786 */
3449 28 Jul 15 olle 787         query.join(Hql.innerJoin("w", "bioPlate", "bp", true));
3449 28 Jul 15 olle 788         query.order(Orders.asc(Hql.property("bp", "name")));
3449 28 Jul 15 olle 789         query.order(Orders.asc(Hql.property("w", "row")));
3449 28 Jul 15 olle 790         query.order(Orders.asc(Hql.property("w", "column")));
3449 28 Jul 15 olle 791 /*
3449 28 Jul 15 olle 792         query.setMaxResults(numToSelect * 2);
3449 28 Jul 15 olle 793 */
3449 28 Jul 15 olle 794 /*
3449 28 Jul 15 olle 795         query.setMaxResults(numToSelect);
3449 28 Jul 15 olle 796 */
3449 28 Jul 15 olle 797         
3449 28 Jul 15 olle 798         // Debug
3449 28 Jul 15 olle 799         //json.put("query", query.toQl(dc));
3449 28 Jul 15 olle 800
3449 28 Jul 15 olle 801         ItemResultList<Extract> qList = query.list(dc);
3450 28 Jul 15 olle 802 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" qList.size() = " + qList.size());
3449 28 Jul 15 olle 803         JSONArray jsonDna = new JSONArray();
3449 28 Jul 15 olle 804         JSONArray jsonFlagged = new JSONArray();
3449 28 Jul 15 olle 805         ItemResultIterator<Extract> it = query.iterate(dc);
3449 28 Jul 15 olle 806         try
3449 28 Jul 15 olle 807         {
3449 28 Jul 15 olle 808           String autoProcess = null;
3449 28 Jul 15 olle 809           while (it.hasNext() && jsonDna.size() < numToSelect)
3449 28 Jul 15 olle 810           {
3449 28 Jul 15 olle 811             Extract dna = it.next();
3449 28 Jul 15 olle 812             boolean okToProcess = true;
3449 28 Jul 15 olle 813             Float remainingQuantity = dna.getRemainingQuantity();
3449 28 Jul 15 olle 814             
3449 28 Jul 15 olle 815             Dna r = Dna.get(dna);
3449 28 Jul 15 olle 816 /*
3449 28 Jul 15 olle 817             r.setAnnotation("preNormalized", preNormalized);
3449 28 Jul 15 olle 818 */
3449 28 Jul 15 olle 819             r.setAnnotation("remainingQuantity", remainingQuantity);
3449 28 Jul 15 olle 820             r.loadAnnotations(dc, manager, "QubitConc", Annotationtype.QUBIT_CONC, null);
3449 28 Jul 15 olle 821             r.loadAnnotations(dc, manager, "deltaCt", Annotationtype.DELTA_CT, null);
3449 28 Jul 15 olle 822             
3449 28 Jul 15 olle 823             Float qualityScore = null;
3449 28 Jul 15 olle 824             if (preNormalized)
3449 28 Jul 15 olle 825             {
3449 28 Jul 15 olle 826               r.loadAnnotations(dc, manager, "DilutionDate", Annotationtype.DILUTION_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3449 28 Jul 15 olle 827             }
3449 28 Jul 15 olle 828             else
3449 28 Jul 15 olle 829             {
3449 28 Jul 15 olle 830               //r.loadBioPlateLocation();
3449 28 Jul 15 olle 831 /*
3449 28 Jul 15 olle 832               qualityScore = r.loadDnaQc(dc, manager);
3449 28 Jul 15 olle 833               if (qualityScore == null) continue; // QC has not been measuered; ignore
3449 28 Jul 15 olle 834 */
3449 28 Jul 15 olle 835               r.loadAnnotations(dc, manager, "QiacubeDate", Annotationtype.QIACUBE_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3449 28 Jul 15 olle 836             }
3449 28 Jul 15 olle 837 /*
3449 28 Jul 15 olle 838             if (reProcess)
3449 28 Jul 15 olle 839             {
3449 28 Jul 15 olle 840               autoProcess = (String)Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, manager, rna);
3449 28 Jul 15 olle 841               r.setAnnotation("AutoProcessing", autoProcess);
3449 28 Jul 15 olle 842             }
3449 28 Jul 15 olle 843             
3449 28 Jul 15 olle 844             if (autoProcess == null)
3449 28 Jul 15 olle 845             {
3449 28 Jul 15 olle 846 */
3449 28 Jul 15 olle 847               if (remainingQuantityLimit != null)
3449 28 Jul 15 olle 848               {
3449 28 Jul 15 olle 849                 if (remainingQuantity == null || remainingQuantity < remainingQuantityLimit)
3449 28 Jul 15 olle 850                 {
3449 28 Jul 15 olle 851                   okToProcess = false;
3449 28 Jul 15 olle 852 /*
3449 28 Jul 15 olle 853                   r.setAnnotation("flag", Rna.FLAG_NOT_ENOUGH_REMAINING_QUANTITY);
3449 28 Jul 15 olle 854 */
3449 28 Jul 15 olle 855                 }
3449 28 Jul 15 olle 856               }
3449 28 Jul 15 olle 857               if (okToProcess && qualityScoreLimit != null)
3449 28 Jul 15 olle 858               {
3449 28 Jul 15 olle 859 /*
3449 28 Jul 15 olle 860                 if (qualityScore < qualityScoreLimit)
3449 28 Jul 15 olle 861                 {
3449 28 Jul 15 olle 862                   okToProcess = false;
3449 28 Jul 15 olle 863                   r.setAnnotation("flag", Rna.FLAG_LOW_QUALITY_SCORE);
3449 28 Jul 15 olle 864                 }
3449 28 Jul 15 olle 865 */
3449 28 Jul 15 olle 866                 qualityScore = (Float) Annotationtype.DELTA_CT.getAnnotationValue(dc, manager, dna);
3449 28 Jul 15 olle 867                 if (qualityScore == null || qualityScore > qualityScoreLimit)
3449 28 Jul 15 olle 868                 {
3449 28 Jul 15 olle 869                   okToProcess = false;
3449 28 Jul 15 olle 870                   r.setAnnotation("flag", Rna.FLAG_LOW_QUALITY_SCORE);
3449 28 Jul 15 olle 871                 }
3449 28 Jul 15 olle 872               }
3449 28 Jul 15 olle 873 /*
3449 28 Jul 15 olle 874             }
3449 28 Jul 15 olle 875 */
3450 28 Jul 15 olle 876 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" name = " + r.getName() + " remainingQuantity = " + remainingQuantity + " qualityScore = " + qualityScore + " okToProcess = " + okToProcess);
3449 28 Jul 15 olle 877             if (okToProcess)
3449 28 Jul 15 olle 878             {
3449 28 Jul 15 olle 879 /*
3449 28 Jul 15 olle 880               jsonDna.add(r.asJSONObject());
3449 28 Jul 15 olle 881 */
3449 28 Jul 15 olle 882               JSONObject jsonDnaSingle = r.asJSONObject();
3449 28 Jul 15 olle 883               jsonDnaSingle.put("bioWell", r.loadBioPlateLocation());
3449 28 Jul 15 olle 884               jsonDna.add(jsonDnaSingle);
3450 28 Jul 15 olle 885 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" jsonDnaSingle = " + jsonDnaSingle);
3449 28 Jul 15 olle 886             }
3449 28 Jul 15 olle 887             else if (flagNotSelected)
3449 28 Jul 15 olle 888             {
3449 28 Jul 15 olle 889               jsonFlagged.add(r.asJSONObject());
3449 28 Jul 15 olle 890             }
3449 28 Jul 15 olle 891           }
3449 28 Jul 15 olle 892         }
3449 28 Jul 15 olle 893         finally
3449 28 Jul 15 olle 894         {
3449 28 Jul 15 olle 895           it.close();
3449 28 Jul 15 olle 896         }
3449 28 Jul 15 olle 897         
3449 28 Jul 15 olle 898         json.put("dna", jsonDna);
3449 28 Jul 15 olle 899         json.put("flagged", jsonFlagged);
3449 28 Jul 15 olle 900         
3449 28 Jul 15 olle 901       }
3449 28 Jul 15 olle 902       else if ("CreateDnaOrLibPlate".equals(cmd))
3449 28 Jul 15 olle 903       {
3449 28 Jul 15 olle 904         dc = sc.newDbControl();
3449 28 Jul 15 olle 905
3536 13 Oct 15 olle 906         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3449 28 Jul 15 olle 907
3449 28 Jul 15 olle 908         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 909         JSONObject jsonPlate = (JSONObject)jsonReq.get("bioplate");
3449 28 Jul 15 olle 910         JSONArray jsonFlagged = (JSONArray)jsonReq.get("flagged");
3449 28 Jul 15 olle 911         
3619 23 Nov 15 olle 912         Number libraryPreparationProtocolId = (Number)jsonReq.get("libraryPreparationProtocol");
3619 23 Nov 15 olle 913         Protocol libraryPreparationProtocol = libraryPreparationProtocolId != null ? Protocol.getById(dc, libraryPreparationProtocolId.intValue()) : null;
3619 23 Nov 15 olle 914         String libraryPreparationProtocolName = (String)jsonReq.get("libraryPreparationProtocolName");
3619 23 Nov 15 olle 915
3449 28 Jul 15 olle 916         String plateName = (String)jsonPlate.get("name");
3449 28 Jul 15 olle 917         JSONArray jsonWells = (JSONArray)jsonPlate.get("wells");
3449 28 Jul 15 olle 918         
3449 28 Jul 15 olle 919         BioplateType plateType = BioplateType.getByCName((String)jsonPlate.get("plateType"));
3449 28 Jul 15 olle 920         PlateGeometry geometry = plateType.getPlateGeometry(dc);
3449 28 Jul 15 olle 921         boolean plateIsStartPlate = false;
4200 01 Nov 16 olle 922         String startPlatePrefix = Meludi.fetchStartPlateItemPrefix(sc.getActiveProjectId());
4200 01 Nov 16 olle 923         if (plateName.startsWith(startPlatePrefix))
3449 28 Jul 15 olle 924         {
3449 28 Jul 15 olle 925           plateIsStartPlate = true;
3449 28 Jul 15 olle 926         }
3628 26 Nov 15 olle 927         int kitId = Values.getInt((String)jsonPlate.get("kitId"));
3628 26 Nov 15 olle 928         Kit kit = kitId > 0 ? Kit.getById(dc, kitId) : null;
3449 28 Jul 15 olle 929         
3449 28 Jul 15 olle 930         BioPlate workPlate = BioPlate.getNew(dc, geometry, plateType.load(dc));
3449 28 Jul 15 olle 931         workPlate.setName(plateName);
3449 28 Jul 15 olle 932         workPlate.setDescription(Values.getStringOrNull((String)jsonPlate.get("comments")));
3449 28 Jul 15 olle 933         Annotationtype.POOL_SCHEMA.setAnnotationValue(dc, workPlate, jsonPlate.get("poolSchema"));
3449 28 Jul 15 olle 934         Annotationtype.BARCODE_VARIANT.setAnnotationValue(dc, workPlate, jsonPlate.get("barcodeVariant"));
3449 28 Jul 15 olle 935         dc.saveItem(workPlate);
3449 28 Jul 15 olle 936         
3449 28 Jul 15 olle 937         BioPlateEventType eventType = BioPlateEventType.getById(dc, BioPlateEventType.CREATE_BIOMATERIAL);
3449 28 Jul 15 olle 938         BioPlateEvent event = BioPlateEvent.getNew(dc, eventType);
3449 28 Jul 15 olle 939         event.setName("Create child " + plateType.getName());
3641 02 Dec 15 olle 940         event.setProtocol(libraryPreparationProtocol);
3628 26 Nov 15 olle 941         event.setKit(kit);
3449 28 Jul 15 olle 942         dc.saveItem(event);
3449 28 Jul 15 olle 943         BioPlateEventParticipant destPlate = event.addParticipant(workPlate, "destination", 1);
3449 28 Jul 15 olle 944         dc.saveItem(destPlate);
3449 28 Jul 15 olle 945
3449 28 Jul 15 olle 946         // DNA or Library
3449 28 Jul 15 olle 947         Subtype childType = plateType.getSubtype();
3449 28 Jul 15 olle 948         ItemSubtype extractType = childType.load(dc);
3449 28 Jul 15 olle 949         Map<Extract, Integer> childCount = new HashMap<Extract, Integer>();
3449 28 Jul 15 olle 950
3449 28 Jul 15 olle 951 /*
3449 28 Jul 15 olle 952         boolean allArePreNormalized = true;
3449 28 Jul 15 olle 953         ItemSubtype preNormalizedType = Subtype.DNA_NORMALIZED_ALIQUOT.get(dc);
3449 28 Jul 15 olle 954         ItemList flagged = BiomaterialList.FLAGGED_RNA.load(dc);
3449 28 Jul 15 olle 955 */
3449 28 Jul 15 olle 956         
3449 28 Jul 15 olle 957         for (int i = 0; i < jsonWells.size(); i++)
3449 28 Jul 15 olle 958         {
3449 28 Jul 15 olle 959           JSONObject jsonWell = (JSONObject)jsonWells.get(i);
3449 28 Jul 15 olle 960           JSONObject jsonDna = (JSONObject)jsonWell.get("dna");
3449 28 Jul 15 olle 961           JSONObject jsonBarcode = (JSONObject)jsonDna.get("barcode");
3449 28 Jul 15 olle 962           
3449 28 Jul 15 olle 963           Number row = (Number)jsonWell.get("row");
3449 28 Jul 15 olle 964           Number col = (Number)jsonWell.get("column");
3449 28 Jul 15 olle 965           
3449 28 Jul 15 olle 966           Number dnaId = (Number)jsonDna.get("id");
3449 28 Jul 15 olle 967           Extract dna = Extract.getById(dc, dnaId.intValue());
3449 28 Jul 15 olle 968 /*
3449 28 Jul 15 olle 969           if ("ReProcess".equals(Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, dna)))
3449 28 Jul 15 olle 970           {
3449 28 Jul 15 olle 971             // Remove auto-processing and flag annotations and remove the DNA from the flagged list
3449 28 Jul 15 olle 972             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, dna, null);
3449 28 Jul 15 olle 973             Annotationtype.FLAG.setAnnotationValue(dc, dna, null);
3449 28 Jul 15 olle 974             flagged.remove(dna);
3449 28 Jul 15 olle 975           }
3449 28 Jul 15 olle 976           boolean isPreNormalized = preNormalizedType.equals(dna.getItemSubtype());
3449 28 Jul 15 olle 977           allArePreNormalized &= isPreNormalized;
3449 28 Jul 15 olle 978 */
3449 28 Jul 15 olle 979           
3449 28 Jul 15 olle 980           Number usedQuantity = (Number)jsonDna.get("usedQuantity");
3450 28 Jul 15 olle 981 //System.out.println(new Date() + " DnaServlet::doPost(): cmd = \"" + cmd + "\" row = " + row + " col = " + col + " dna.name = " + dna.getName() + " usedQuantity = " + usedQuantity);
3449 28 Jul 15 olle 982           
3449 28 Jul 15 olle 983           Extract childExtract = Extract.getNew(dc, destPlate);
3449 28 Jul 15 olle 984           childExtract.setItemSubtype(extractType);
3449 28 Jul 15 olle 985           childExtract.setOriginalQuantity(usedQuantity.floatValue());
3449 28 Jul 15 olle 986           childExtract.setDescription((String)jsonDna.get("comment"));
3449 28 Jul 15 olle 987 /*
3449 28 Jul 15 olle 988           if (Boolean.TRUE.equals(jsonDna.get("qc")))
3449 28 Jul 15 olle 989           {
3449 28 Jul 15 olle 990             Annotationtype.USE_FOR_QC.setAnnotationValue(dc, childExtract, true);
3449 28 Jul 15 olle 991           }
3449 28 Jul 15 olle 992 */
3449 28 Jul 15 olle 993           Annotationtype.DILUTION_CONC.setAnnotationValue(dc, childExtract, jsonDna.get("dilutionConc"));
3449 28 Jul 15 olle 994
3449 28 Jul 15 olle 995           if (jsonBarcode != null)
3449 28 Jul 15 olle 996           {
3449 28 Jul 15 olle 997             Number barcodeId = (Number)jsonBarcode.get("id");
3449 28 Jul 15 olle 998             childExtract.setTag(Tag.getById(dc, barcodeId.intValue()));
3449 28 Jul 15 olle 999           }
3449 28 Jul 15 olle 1000           
3449 28 Jul 15 olle 1001           int childNum;
3449 28 Jul 15 olle 1002           if (childCount.containsKey(dna))
3449 28 Jul 15 olle 1003           {
3449 28 Jul 15 olle 1004             childNum = childCount.get(dna)+1;
3449 28 Jul 15 olle 1005             childCount.put(dna, childNum);
3449 28 Jul 15 olle 1006           }
3449 28 Jul 15 olle 1007           else
3449 28 Jul 15 olle 1008           {
3449 28 Jul 15 olle 1009             ItemQuery<Extract> query = dna.getChildExtracts();
3449 28 Jul 15 olle 1010             childType.addFilter(dc, query);
3449 28 Jul 15 olle 1011             query.include(Include.ALL);
3449 28 Jul 15 olle 1012             childNum = (int)query.count(dc)+1;
3449 28 Jul 15 olle 1013             childCount.put(dna, childNum);
3449 28 Jul 15 olle 1014           }
3449 28 Jul 15 olle 1015
3449 28 Jul 15 olle 1016           if (plateIsStartPlate)
3449 28 Jul 15 olle 1017           {
3449 28 Jul 15 olle 1018             // Set suffix "fpa" or "fpb", depending on which side of the plate the well is located
3449 28 Jul 15 olle 1019             String suffix = "fpa";
3449 28 Jul 15 olle 1020             if (col.intValue() > 5)
3449 28 Jul 15 olle 1021             {
3449 28 Jul 15 olle 1022               suffix = "fpb";
3449 28 Jul 15 olle 1023             }
3449 28 Jul 15 olle 1024             childExtract.setName(dna.getName()+"." + suffix);
3619 23 Nov 15 olle 1025             BioMaterialEvent childExtractEvent = childExtract.getCreationEvent();
3619 23 Nov 15 olle 1026             childExtractEvent.setProtocol(libraryPreparationProtocol);
3628 26 Nov 15 olle 1027             childExtractEvent.setKit(kit);
3449 28 Jul 15 olle 1028           }
3449 28 Jul 15 olle 1029           else
3449 28 Jul 15 olle 1030           {
3449 28 Jul 15 olle 1031             childExtract.setName(dna.getName()+"." + childType.getItemSuffix() + (childNum == 1 ? "" : childNum));
3449 28 Jul 15 olle 1032           }
3449 28 Jul 15 olle 1033           BioMaterialEventSource evtSrc = childExtract.getCreationEvent().setSource(dna);
3449 28 Jul 15 olle 1034           evtSrc.setUsedQuantity(usedQuantity.floatValue());
3449 28 Jul 15 olle 1035           
3449 28 Jul 15 olle 1036           BioWell well = workPlate.getBioWell(row.intValue(), col.intValue());
3449 28 Jul 15 olle 1037           childExtract.setBioWell(well);
3449 28 Jul 15 olle 1038           
3449 28 Jul 15 olle 1039           float remainingQuantityBefore = dna.getRemainingQuantity();
3449 28 Jul 15 olle 1040           float remainingQuantityAfter = remainingQuantityBefore - usedQuantity.floatValue();
3449 28 Jul 15 olle 1041 /*
3449 28 Jul 15 olle 1042           if (isPreNormalized && remainingQuantityAfter > 0.01)
3449 28 Jul 15 olle 1043           {
3449 28 Jul 15 olle 1044             // We discard any remaining pre-normalized DNA
3449 28 Jul 15 olle 1045             BioMaterialEvent discardedDNA = dna.newEvent();
3449 28 Jul 15 olle 1046             discardedDNA.setUsedQuantity(remainingQuantityAfter);
3449 28 Jul 15 olle 1047             discardedDNA.setComment("Remaining pre-normalized DNA is discarded after it has been used");
3449 28 Jul 15 olle 1048             dc.saveItem(discardedDNA);
3449 28 Jul 15 olle 1049           }
3449 28 Jul 15 olle 1050 */
3449 28 Jul 15 olle 1051           
3449 28 Jul 15 olle 1052           dc.saveItem(childExtract);
3449 28 Jul 15 olle 1053         }
3449 28 Jul 15 olle 1054         
3449 28 Jul 15 olle 1055         if (jsonFlagged != null && jsonFlagged.size() > 0)
3449 28 Jul 15 olle 1056         {
3449 28 Jul 15 olle 1057           int numFlagged = 0;
3449 28 Jul 15 olle 1058           
3449 28 Jul 15 olle 1059           for (int i = 0; i < jsonFlagged.size(); ++i)
3449 28 Jul 15 olle 1060           {
3449 28 Jul 15 olle 1061             JSONObject jsonDna = (JSONObject)jsonFlagged.get(i);
3449 28 Jul 15 olle 1062             Number dnaId = (Number)jsonDna.get("id");
3449 28 Jul 15 olle 1063             Extract dna = Extract.getById(dc, dnaId.intValue());
3449 28 Jul 15 olle 1064             dna.setDescription((String)jsonDna.get("comment"));
3449 28 Jul 15 olle 1065
3449 28 Jul 15 olle 1066 /*
3449 28 Jul 15 olle 1067             // Safety check in case a flagged DNA is also on the plate
3449 28 Jul 15 olle 1068             if (!childCount.containsKey(dna))
3449 28 Jul 15 olle 1069             {
3449 28 Jul 15 olle 1070               flagged.add(dna);
3449 28 Jul 15 olle 1071               Annotationtype.FLAG.setAnnotationValue(dc, dna, jsonDna.get("flag"));
3449 28 Jul 15 olle 1072               Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, dna, "Disable");
3449 28 Jul 15 olle 1073               numFlagged++;
3449 28 Jul 15 olle 1074             }
3449 28 Jul 15 olle 1075 */
3449 28 Jul 15 olle 1076           }
3449 28 Jul 15 olle 1077 /*
3449 28 Jul 15 olle 1078           if (numFlagged > 0)
3449 28 Jul 15 olle 1079           {
3449 28 Jul 15 olle 1080             jsonMessages.add("Added " + numFlagged + " DNA items to '" + flagged.getName() + "' list.");
3449 28 Jul 15 olle 1081           }
3449 28 Jul 15 olle 1082 */
3449 28 Jul 15 olle 1083         }
3449 28 Jul 15 olle 1084         
3449 28 Jul 15 olle 1085         jsonMessages.add("Created " + workPlate.getName() + " with " + jsonWells.size() + " child items");
3449 28 Jul 15 olle 1086 /*
3449 28 Jul 15 olle 1087         // Set AutoProcessing annotation to PreNormalizeDNA
3449 28 Jul 15 olle 1088         if (allArePreNormalized)
3449 28 Jul 15 olle 1089         {
3449 28 Jul 15 olle 1090           jsonMessages.add("All DNA is pre-normalized");
3449 28 Jul 15 olle 1091           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, workPlate, Dna.AUTO_PROCESSING_PRE_NORMALIZE);
3449 28 Jul 15 olle 1092         }
3449 28 Jul 15 olle 1093 */
3573 05 Nov 15 olle 1094
3449 28 Jul 15 olle 1095         dc.commit();
3449 28 Jul 15 olle 1096       }
3449 28 Jul 15 olle 1097       else if ("RegisterDNADilution".equals(cmd))
3449 28 Jul 15 olle 1098       {
3449 28 Jul 15 olle 1099         dc = sc.newDbControl();
3449 28 Jul 15 olle 1100
3449 28 Jul 15 olle 1101         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3449 28 Jul 15 olle 1102
3449 28 Jul 15 olle 1103         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 1104         
3449 28 Jul 15 olle 1105         Number bioPlateId = (Number)jsonReq.get("bioplate");
3449 28 Jul 15 olle 1106
3449 28 Jul 15 olle 1107         // Load DNA plate and the creation event
3449 28 Jul 15 olle 1108         ReactionPlate dnaReactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.DNA);
3449 28 Jul 15 olle 1109         BioPlate dnaPlate = dnaReactionPlate.getBioPlate();
3449 28 Jul 15 olle 1110         
3449 28 Jul 15 olle 1111         // Dates and operators 
3449 28 Jul 15 olle 1112         Annotationtype.DILUTION_DATE.setAnnotationValue(dc, dnaPlate, Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("dilutionDate")));
3449 28 Jul 15 olle 1113         Annotationtype.DILUTION_OPERATOR.setAnnotationValue(dc, dnaPlate, Values.getStringOrNull((String)jsonReq.get("dilutionOperator")));
3449 28 Jul 15 olle 1114     
3449 28 Jul 15 olle 1115         // Comment about the plate
3449 28 Jul 15 olle 1116         dnaPlate.setDescription((String)jsonReq.get("comments"));
3449 28 Jul 15 olle 1117
3449 28 Jul 15 olle 1118         jsonMessages.add("DNA dilution for DNA plate '" + dnaPlate.getName() + "' registered successfully.");
3449 28 Jul 15 olle 1119         
3449 28 Jul 15 olle 1120         dc.commit();
3449 28 Jul 15 olle 1121       }
3719 22 Jan 16 olle 1122       else if ("UpdateDnaQpcrBranchFlag".equals(cmd))
3719 22 Jan 16 olle 1123       {
3719 22 Jan 16 olle 1124         dc = sc.newDbControl();
3449 28 Jul 15 olle 1125
3719 22 Jan 16 olle 1126         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3719 22 Jan 16 olle 1127
3719 22 Jan 16 olle 1128         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3719 22 Jan 16 olle 1129
3719 22 Jan 16 olle 1130         int numDnaUpdated = 0;
3719 22 Jan 16 olle 1131         JSONArray jsonItems = (JSONArray)jsonReq.get("items");
3719 22 Jan 16 olle 1132         if (jsonItems != null)
3719 22 Jan 16 olle 1133         {
3719 22 Jan 16 olle 1134           for (int i = 0; i < jsonItems.size(); i++)
3719 22 Jan 16 olle 1135           {
3719 22 Jan 16 olle 1136             JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3719 22 Jan 16 olle 1137             if (jsonItem != null)
3719 22 Jan 16 olle 1138             {
3719 22 Jan 16 olle 1139               Number id = (Number)jsonItem.get("id");
3719 22 Jan 16 olle 1140               if (id != null)
3719 22 Jan 16 olle 1141               {
3719 22 Jan 16 olle 1142                 Extract e = Extract.getById(dc, id.intValue());
3719 22 Jan 16 olle 1143                 if (e != null)
3719 22 Jan 16 olle 1144                 {
3719 22 Jan 16 olle 1145                   Boolean qPcrBranchCur = (Boolean) Annotationtype.QPCR_BRANCH.getAnnotationValue(dc, e);
3719 22 Jan 16 olle 1146                   if (qPcrBranchCur == null)
3719 22 Jan 16 olle 1147                   {
3719 22 Jan 16 olle 1148                     qPcrBranchCur = false;
3719 22 Jan 16 olle 1149                   }
3719 22 Jan 16 olle 1150                   Boolean qPcrBranchNew = (Boolean)jsonItem.get("qpcrBranch");
3719 22 Jan 16 olle 1151                   if (qPcrBranchNew == null)
3719 22 Jan 16 olle 1152                   {
3719 22 Jan 16 olle 1153                     qPcrBranchNew = false;
3719 22 Jan 16 olle 1154                   }
3719 22 Jan 16 olle 1155                   // Check if qPCR branch settings changed
3719 22 Jan 16 olle 1156                   if (qPcrBranchCur != qPcrBranchNew)
3719 22 Jan 16 olle 1157                   {
3719 22 Jan 16 olle 1158                     numDnaUpdated++;
3719 22 Jan 16 olle 1159                     Annotationtype.QPCR_BRANCH.setAnnotationValue(dc, e, qPcrBranchNew);                    
3719 22 Jan 16 olle 1160                     jsonMessages.add("Extract '" + e.getName() + "' qPCR branch setting updated to " + qPcrBranchNew + ".");
3719 22 Jan 16 olle 1161                   }
3719 22 Jan 16 olle 1162                 }
3719 22 Jan 16 olle 1163               }
3719 22 Jan 16 olle 1164             }
3719 22 Jan 16 olle 1165           }
3719 22 Jan 16 olle 1166         }
3719 22 Jan 16 olle 1167         if (numDnaUpdated > 0)
3719 22 Jan 16 olle 1168         {
3719 22 Jan 16 olle 1169           dc.commit();
3719 22 Jan 16 olle 1170         }
3719 22 Jan 16 olle 1171         else
3719 22 Jan 16 olle 1172         {
3719 22 Jan 16 olle 1173           jsonMessages.add("No extract items updated.");
3719 22 Jan 16 olle 1174         }
3719 22 Jan 16 olle 1175       }
3719 22 Jan 16 olle 1176
3449 28 Jul 15 olle 1177 /*      
3449 28 Jul 15 olle 1178       else if ("ImportMRnaQCResults".equals(cmd))
3449 28 Jul 15 olle 1179       {
3449 28 Jul 15 olle 1180         dc = sc.newDbControl();
3449 28 Jul 15 olle 1181
3449 28 Jul 15 olle 1182         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3449 28 Jul 15 olle 1183
3449 28 Jul 15 olle 1184         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 1185         
3449 28 Jul 15 olle 1186         Number bioPlateId = (Number)jsonReq.get("bioplate");
3449 28 Jul 15 olle 1187         String pdfPath = Values.getStringOrNull((String)jsonReq.get("pdf"));
3449 28 Jul 15 olle 1188         Number mrnaProtocolId = (Number)jsonReq.get("mrnaProtocol");
3449 28 Jul 15 olle 1189         boolean failed = Boolean.TRUE.equals(jsonReq.get("failed"));
3449 28 Jul 15 olle 1190
3449 28 Jul 15 olle 1191         // Load mRNA plate and the creation event
3449 28 Jul 15 olle 1192         ReactionPlate mrnaReactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.MRNA);
3449 28 Jul 15 olle 1193         BioPlate mrnaPlate = mrnaReactionPlate.getBioPlate();
3449 28 Jul 15 olle 1194         
3449 28 Jul 15 olle 1195         // Find the creation event for the mRNA plate
3449 28 Jul 15 olle 1196         List<BioPlateEvent> events = mrnaReactionPlate.findEvents(dc, BioPlateEventType.CREATE_BIOMATERIAL, "destination");
3449 28 Jul 15 olle 1197         if (events.size() > 1)
3449 28 Jul 15 olle 1198         {
3449 28 Jul 15 olle 1199           throw new InvalidDataException("Found > 1 creation event for mRNA plate: " + mrnaReactionPlate.getName());
3449 28 Jul 15 olle 1200         }
3449 28 Jul 15 olle 1201         // ...it may not exist if a plate has been created with a batch import...
3449 28 Jul 15 olle 1202         BioPlateEvent mrnaPlateCreationEvent = events.size() == 0 ? null : events.get(0);
3449 28 Jul 15 olle 1203         
3449 28 Jul 15 olle 1204         // Set creation date and protocol
3449 28 Jul 15 olle 1205         Date mrnaDate = new Date();
3449 28 Jul 15 olle 1206         
3449 28 Jul 15 olle 1207         mrnaPlate.setEventDate(mrnaDate);
3449 28 Jul 15 olle 1208         Protocol mrnaProtocol = mrnaProtocolId == null ? null : Protocol.getById(dc, mrnaProtocolId.intValue());
3449 28 Jul 15 olle 1209         if (mrnaPlateCreationEvent != null)
3449 28 Jul 15 olle 1210         {
3449 28 Jul 15 olle 1211           // This automatically propagated to all mRNA items on the plate
3449 28 Jul 15 olle 1212           mrnaPlateCreationEvent.setEventDate(mrnaDate);
3449 28 Jul 15 olle 1213           mrnaPlateCreationEvent.setProtocol(mrnaProtocol);
3449 28 Jul 15 olle 1214         }
3449 28 Jul 15 olle 1215
3449 28 Jul 15 olle 1216         // Link PDF file with the mRNA plate
3449 28 Jul 15 olle 1217         if (pdfPath != null)
3449 28 Jul 15 olle 1218         {
3449 28 Jul 15 olle 1219           File pdf = File.getByPath(dc, new Path(pdfPath, Path.Type.FILE), false);
3449 28 Jul 15 olle 1220           AnyToAny ata = AnyToAny.getNewOrExisting(dc, mrnaPlate, "PDF printout", pdf, true);
3449 28 Jul 15 olle 1221           if (!ata.isInDatabase()) dc.saveItem(ata);
3449 28 Jul 15 olle 1222           if (pdf.getDescription() == null) pdf.setDescription("BioAnalyzer QC for " + mrnaPlate.getName());
3449 28 Jul 15 olle 1223           jsonMessages.add("Attached file '" + pdf.getName() + "' to plate " + mrnaPlate.getName());
3449 28 Jul 15 olle 1224         }
3449 28 Jul 15 olle 1225         
3449 28 Jul 15 olle 1226         // Dates and operators 
3449 28 Jul 15 olle 1227         Annotationtype.PURIFICATION_DATE.setAnnotationValue(dc, mrnaPlate, Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("purificationDate")));
3449 28 Jul 15 olle 1228         Annotationtype.PURIFICATION_OPERATOR.setAnnotationValue(dc, mrnaPlate, Values.getStringOrNull((String)jsonReq.get("purificationOperator")));
3449 28 Jul 15 olle 1229         Annotationtype.FRAGMENTATION_DATE.setAnnotationValue(dc, mrnaPlate, Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("fragmentationDate")));
3449 28 Jul 15 olle 1230         Annotationtype.FRAGMENTATION_OPERATOR.setAnnotationValue(dc, mrnaPlate, Values.getStringOrNull((String)jsonReq.get("fragmentationOperator")));
3449 28 Jul 15 olle 1231         Annotationtype.CLEANUP_DATE.setAnnotationValue(dc, mrnaPlate, Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("cleanupDate")));
3449 28 Jul 15 olle 1232         Annotationtype.CLEANUP_OPERATOR.setAnnotationValue(dc, mrnaPlate, Values.getStringOrNull((String)jsonReq.get("cleanupOperator")));
3449 28 Jul 15 olle 1233     
3449 28 Jul 15 olle 1234         // Comment about the plate
3449 28 Jul 15 olle 1235         mrnaPlate.setDescription((String)jsonReq.get("comments"));
3449 28 Jul 15 olle 1236
3449 28 Jul 15 olle 1237         // Flags to indicate that this plate has been processed, successfully or not
3449 28 Jul 15 olle 1238         mrnaPlate.setDestroyed(true);
3449 28 Jul 15 olle 1239         Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, mrnaPlate, failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL);
3449 28 Jul 15 olle 1240       
3449 28 Jul 15 olle 1241         BioPlate cdnaPlate = null;
3449 28 Jul 15 olle 1242         BioPlateEventParticipant cdnaCreation = null;
3449 28 Jul 15 olle 1243         ItemList flaggedList = null;
3449 28 Jul 15 olle 1244         if (!failed)
3449 28 Jul 15 olle 1245         {
3449 28 Jul 15 olle 1246           // Prepare cDNA child plate
3449 28 Jul 15 olle 1247           String cdnaPlateName = mrnaPlate.getName().replace(BioplateType.MRNA.getPlateNamePrefix(), BioplateType.CDNA.getPlateNamePrefix());
3449 28 Jul 15 olle 1248           cdnaPlate = BioPlate.getNew(dc, BioplateType.CDNA.getPlateGeometry(dc), BioplateType.CDNA.load(dc));
3449 28 Jul 15 olle 1249           cdnaPlate.setName(cdnaPlateName);
3449 28 Jul 15 olle 1250           Annotationtype.POOL_SCHEMA.copyAnnotationValues(dc, mrnaPlate, cdnaPlate, true);
3449 28 Jul 15 olle 1251   
3449 28 Jul 15 olle 1252           dc.saveItem(cdnaPlate);
3449 28 Jul 15 olle 1253           
3449 28 Jul 15 olle 1254           // Create plate event...
3449 28 Jul 15 olle 1255           BioPlateEventType eventType = BioPlateEventType.getById(dc, BioPlateEventType.CREATE_BIOMATERIAL);
3449 28 Jul 15 olle 1256           BioPlateEvent event = BioPlateEvent.getNew(dc, eventType);
3449 28 Jul 15 olle 1257           event.setName("Create cDNA child plate");
3449 28 Jul 15 olle 1258           dc.saveItem(event);
3449 28 Jul 15 olle 1259           
3449 28 Jul 15 olle 1260           //... with parent (mRNA) and child (cDNA) plate
3449 28 Jul 15 olle 1261           cdnaCreation = event.addParticipant(cdnaPlate, "child", 1);
3449 28 Jul 15 olle 1262           event.addParticipant(mrnaPlate, "parent", 1);
3449 28 Jul 15 olle 1263           dc.saveItem(cdnaCreation);
3449 28 Jul 15 olle 1264         }
3449 28 Jul 15 olle 1265         else
3449 28 Jul 15 olle 1266         {
3449 28 Jul 15 olle 1267           flaggedList = BiomaterialList.FLAGGED_RNA.load(dc);
3449 28 Jul 15 olle 1268         }
3449 28 Jul 15 olle 1269
3449 28 Jul 15 olle 1270         List<BioWell> wells = mrnaPlate.getBioWells().list(dc);
3449 28 Jul 15 olle 1271         int numCdna = 0;
3449 28 Jul 15 olle 1272         int numFlagged = 0;
3449 28 Jul 15 olle 1273         ItemSubtype cdnaType = Subtype.CDNA.load(dc);
3449 28 Jul 15 olle 1274         for (BioWell well : wells)
3449 28 Jul 15 olle 1275         {
3449 28 Jul 15 olle 1276           if (!well.isEmpty())
3449 28 Jul 15 olle 1277           {
3449 28 Jul 15 olle 1278             Extract mrna = (Extract)well.getBioMaterial();
3449 28 Jul 15 olle 1279             MRna m = MRna.get(mrna);
3449 28 Jul 15 olle 1280             
3449 28 Jul 15 olle 1281             if (mrnaPlateCreationEvent == null)
3449 28 Jul 15 olle 1282             {
3449 28 Jul 15 olle 1283               // If no plate event existed, we must set protocol and date on all mRNA items
3449 28 Jul 15 olle 1284               BioMaterialEvent evt = mrna.getCreationEvent();
3449 28 Jul 15 olle 1285               evt.setEventDate(mrnaDate);
3449 28 Jul 15 olle 1286               evt.setProtocol(mrnaProtocol);
3449 28 Jul 15 olle 1287             }
3449 28 Jul 15 olle 1288             if (!failed)
3449 28 Jul 15 olle 1289             {
3449 28 Jul 15 olle 1290               Extract cdna = Extract.getNew(dc, cdnaCreation);
3449 28 Jul 15 olle 1291               cdna.setName(mrna.getName()+".c");
3449 28 Jul 15 olle 1292               cdna.setItemSubtype(cdnaType);
3449 28 Jul 15 olle 1293               cdna.setBioWell(cdnaPlate.getBioWell(well.getPlateCoordinate()));
3449 28 Jul 15 olle 1294               cdna.getCreationEvent().setSource(mrna);
3449 28 Jul 15 olle 1295               dc.saveItem(cdna);
3449 28 Jul 15 olle 1296               numCdna++;
3449 28 Jul 15 olle 1297             }
3449 28 Jul 15 olle 1298             else
3449 28 Jul 15 olle 1299             {
3449 28 Jul 15 olle 1300               if (!Rna.isSpecialRna(mrna.getName()))
3449 28 Jul 15 olle 1301               {
3449 28 Jul 15 olle 1302                 Extract rna = m.getRna().getRnaIfPrenormalized(dc).getItem();
3449 28 Jul 15 olle 1303                 Annotationtype.FLAG.setAnnotationValue(dc, rna, Rna.FLAG_MRNA_PLATE_FAILED);
3449 28 Jul 15 olle 1304                 flaggedList.add(rna);
3449 28 Jul 15 olle 1305                 numFlagged++;
3449 28 Jul 15 olle 1306               }
3449 28 Jul 15 olle 1307             }
3449 28 Jul 15 olle 1308           }
3449 28 Jul 15 olle 1309         }
3449 28 Jul 15 olle 1310         
3449 28 Jul 15 olle 1311         if (!failed)
3449 28 Jul 15 olle 1312         {
3449 28 Jul 15 olle 1313           jsonMessages.add(numCdna + " cDNA extracts created");
3449 28 Jul 15 olle 1314           jsonMessages.add("Prepared child cDNA plate '" + cdnaPlate.getName() + "' for future processing");
3449 28 Jul 15 olle 1315         }
3449 28 Jul 15 olle 1316         else
3449 28 Jul 15 olle 1317         {
3449 28 Jul 15 olle 1318           jsonMessages.add(numFlagged + " RNA items flagged for re-processing");
3449 28 Jul 15 olle 1319           jsonMessages.add("No child cDNA plate created!");
3449 28 Jul 15 olle 1320         }
3449 28 Jul 15 olle 1321         jsonMessages.add("mRNA plate '" + mrnaPlate.getName() + "' registered with status: " + (failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL));
3449 28 Jul 15 olle 1322         
3449 28 Jul 15 olle 1323         dc.commit();
3449 28 Jul 15 olle 1324       }
3449 28 Jul 15 olle 1325 */
3449 28 Jul 15 olle 1326 /*
3449 28 Jul 15 olle 1327       else if ("ImportCDnaResults".equals(cmd))
3449 28 Jul 15 olle 1328       {
3449 28 Jul 15 olle 1329         dc = sc.newDbControl();
3449 28 Jul 15 olle 1330
3449 28 Jul 15 olle 1331         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3449 28 Jul 15 olle 1332
3449 28 Jul 15 olle 1333         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 1334         
3449 28 Jul 15 olle 1335         Number bioPlateId = (Number)jsonReq.get("bioplate");
3449 28 Jul 15 olle 1336         Number cdnaProtocolId = (Number)jsonReq.get("cdnaProtocol");
3449 28 Jul 15 olle 1337         boolean failed = Boolean.TRUE.equals(jsonReq.get("failed"));
3449 28 Jul 15 olle 1338
3449 28 Jul 15 olle 1339         // Load cDNA plate and the creation event
3449 28 Jul 15 olle 1340         ReactionPlate cdnaReactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.CDNA);
3449 28 Jul 15 olle 1341         BioPlate cdnaPlate = cdnaReactionPlate.getBioPlate();
3449 28 Jul 15 olle 1342         
3449 28 Jul 15 olle 1343         // Find the creation event for the cDNA plate
3449 28 Jul 15 olle 1344         List<BioPlateEvent> events = cdnaReactionPlate.findEvents(dc, BioPlateEventType.CREATE_BIOMATERIAL, "child");
3449 28 Jul 15 olle 1345         if (events.size() > 1)
3449 28 Jul 15 olle 1346         {
3449 28 Jul 15 olle 1347           throw new InvalidDataException("Found > 1 creation event for cDNA plate: " + cdnaReactionPlate.getName());
3449 28 Jul 15 olle 1348         }
3449 28 Jul 15 olle 1349         // ...it may not exist if a plate has been created with a batch import...
3449 28 Jul 15 olle 1350         BioPlateEvent cdnaPlateCreationEvent = events.size() == 0 ? null : events.get(0);
3449 28 Jul 15 olle 1351         
3449 28 Jul 15 olle 1352         // Set creation date and protocol
3449 28 Jul 15 olle 1353         Date cdnaDate = new Date();
3449 28 Jul 15 olle 1354         cdnaPlate.setEventDate(cdnaDate);
3449 28 Jul 15 olle 1355         Protocol cdnaProtocol = cdnaProtocolId == null ? null : Protocol.getById(dc, cdnaProtocolId.intValue());
3449 28 Jul 15 olle 1356         if (cdnaPlateCreationEvent != null)
3449 28 Jul 15 olle 1357         {
3449 28 Jul 15 olle 1358           // This automatically propagated to all cDNA items on the plate
3449 28 Jul 15 olle 1359           cdnaPlateCreationEvent.setEventDate(cdnaDate);
3449 28 Jul 15 olle 1360           cdnaPlateCreationEvent.setProtocol(cdnaProtocol);
3449 28 Jul 15 olle 1361         }
3449 28 Jul 15 olle 1362   
3449 28 Jul 15 olle 1363         // Dates and operators
3449 28 Jul 15 olle 1364         Annotationtype.SYNTHESIS_DATE.setAnnotationValue(dc, cdnaPlate, Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("synthesisDate")));
3449 28 Jul 15 olle 1365         Annotationtype.SYNTHESIS_OPERATOR.setAnnotationValue(dc, cdnaPlate, Values.getStringOrNull((String)jsonReq.get("synthesisOperator")));
3449 28 Jul 15 olle 1366         
3449 28 Jul 15 olle 1367         // Comment about the plate
3449 28 Jul 15 olle 1368         cdnaPlate.setDescription((String)jsonReq.get("comments"));
3449 28 Jul 15 olle 1369         
3449 28 Jul 15 olle 1370         // Register the success or failure of the plate
3449 28 Jul 15 olle 1371         Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, cdnaPlate, failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL);
3449 28 Jul 15 olle 1372         cdnaPlate.setDestroyed(failed);
3449 28 Jul 15 olle 1373
3449 28 Jul 15 olle 1374         List<BioWell> wells = cdnaPlate.getBioWells().list(dc);
3449 28 Jul 15 olle 1375         
3449 28 Jul 15 olle 1376         // Flag the parent RNA items
3449 28 Jul 15 olle 1377         ItemList flaggedList = BiomaterialList.FLAGGED_RNA.load(dc);
3449 28 Jul 15 olle 1378         int numFlagged = 0;
3449 28 Jul 15 olle 1379         
3449 28 Jul 15 olle 1380         for (BioWell well : wells)
3449 28 Jul 15 olle 1381         {
3449 28 Jul 15 olle 1382           if (!well.isEmpty())
3449 28 Jul 15 olle 1383           {
3449 28 Jul 15 olle 1384             Extract cdna = (Extract)well.getBioMaterial();
3449 28 Jul 15 olle 1385             CDna c = CDna.get(cdna);
3449 28 Jul 15 olle 1386             
3449 28 Jul 15 olle 1387             if (cdnaPlateCreationEvent == null)
3449 28 Jul 15 olle 1388             {
3449 28 Jul 15 olle 1389               // If no plate event existed, we must set protocol and date on all CDNA items
3449 28 Jul 15 olle 1390               BioMaterialEvent evt = cdna.getCreationEvent();
3449 28 Jul 15 olle 1391               evt.setEventDate(cdnaDate);
3449 28 Jul 15 olle 1392               evt.setProtocol(cdnaProtocol);
3449 28 Jul 15 olle 1393             }
3449 28 Jul 15 olle 1394             if (failed)
3449 28 Jul 15 olle 1395             {
3449 28 Jul 15 olle 1396               if (!Rna.isSpecialRna(cdna.getName()))
3449 28 Jul 15 olle 1397               {
3449 28 Jul 15 olle 1398                 MRna m =  c.getMRna();
3449 28 Jul 15 olle 1399                 Rna r = m.getRna().getRnaIfPrenormalized(dc);
3449 28 Jul 15 olle 1400                 Extract rna = r.getItem();
3449 28 Jul 15 olle 1401                 Annotationtype.FLAG.setAnnotationValue(dc, rna, Rna.FLAG_CDNA_PLATE_FAILED);
3449 28 Jul 15 olle 1402                 flaggedList.add(rna);
3449 28 Jul 15 olle 1403                 numFlagged++;
3449 28 Jul 15 olle 1404               }
3449 28 Jul 15 olle 1405             }
3449 28 Jul 15 olle 1406           }
3449 28 Jul 15 olle 1407         }
3449 28 Jul 15 olle 1408         
3449 28 Jul 15 olle 1409         if (numFlagged > 0)
3449 28 Jul 15 olle 1410         {
3449 28 Jul 15 olle 1411           jsonMessages.add(numFlagged + " RNA items flagged for re-processing");
3449 28 Jul 15 olle 1412         }
3449 28 Jul 15 olle 1413         
3449 28 Jul 15 olle 1414         jsonMessages.add("cDNA plate '" + cdnaPlate.getName() + "' registered with status: " + (failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL));
3449 28 Jul 15 olle 1415         dc.commit();
3449 28 Jul 15 olle 1416       }
3449 28 Jul 15 olle 1417 */
3449 28 Jul 15 olle 1418
3449 28 Jul 15 olle 1419       json.put("messages", jsonMessages);
3449 28 Jul 15 olle 1420       CounterService.getInstance().setForceCount();
3449 28 Jul 15 olle 1421     }
3449 28 Jul 15 olle 1422     catch (Throwable t)
3449 28 Jul 15 olle 1423     {
3449 28 Jul 15 olle 1424       t.printStackTrace();
3449 28 Jul 15 olle 1425       json.clear();
3449 28 Jul 15 olle 1426       json.put("status", "error");
3449 28 Jul 15 olle 1427       json.put("message", t.getMessage());
3449 28 Jul 15 olle 1428       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3449 28 Jul 15 olle 1429     }
3449 28 Jul 15 olle 1430     finally
3449 28 Jul 15 olle 1431     {
3449 28 Jul 15 olle 1432       if (dc != null) dc.close();
3449 28 Jul 15 olle 1433       json.writeJSONString(resp.getWriter());
3449 28 Jul 15 olle 1434     }
3449 28 Jul 15 olle 1435     
3449 28 Jul 15 olle 1436   }
3449 28 Jul 15 olle 1437   
3449 28 Jul 15 olle 1438   
3449 28 Jul 15 olle 1439 }