extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/ExtractionServlet.java

Code
Comments
Other
Rev Date Author Line
1637 07 May 12 nicklas 1 package net.sf.basedb.reggie.servlet;
1637 07 May 12 nicklas 2
1637 07 May 12 nicklas 3 import java.io.IOException;
1639 09 May 12 nicklas 4 import java.io.PrintWriter;
1660 22 May 12 nicklas 5 import java.util.ArrayList;
2903 06 Nov 14 nicklas 6 import java.util.Arrays;
1639 09 May 12 nicklas 7 import java.util.Date;
1637 07 May 12 nicklas 8 import java.util.List;
1637 07 May 12 nicklas 9
1637 07 May 12 nicklas 10 import javax.servlet.ServletException;
1637 07 May 12 nicklas 11 import javax.servlet.http.HttpServlet;
1637 07 May 12 nicklas 12 import javax.servlet.http.HttpServletRequest;
1637 07 May 12 nicklas 13 import javax.servlet.http.HttpServletResponse;
1637 07 May 12 nicklas 14
1637 07 May 12 nicklas 15 import org.json.simple.JSONArray;
1637 07 May 12 nicklas 16 import org.json.simple.JSONObject;
1637 07 May 12 nicklas 17
1639 09 May 12 nicklas 18 import net.sf.basedb.core.BioMaterialEvent;
2903 06 Nov 14 nicklas 19 import net.sf.basedb.core.BioMaterialEventSource;
4089 08 Sep 16 nicklas 20 import net.sf.basedb.core.BioPlate;
4092 09 Sep 16 nicklas 21 import net.sf.basedb.core.BioPlateEvent;
4092 09 Sep 16 nicklas 22 import net.sf.basedb.core.BioPlateEventParticipant;
4092 09 Sep 16 nicklas 23 import net.sf.basedb.core.BioPlateEventType;
3247 14 Apr 15 nicklas 24 import net.sf.basedb.core.ItemList;
1639 09 May 12 nicklas 25 import net.sf.basedb.core.BioWell;
1637 07 May 12 nicklas 26 import net.sf.basedb.core.DbControl;
1639 09 May 12 nicklas 27 import net.sf.basedb.core.Extract;
1639 09 May 12 nicklas 28 import net.sf.basedb.core.ItemQuery;
2903 06 Nov 14 nicklas 29 import net.sf.basedb.core.ItemSubtype;
1639 09 May 12 nicklas 30 import net.sf.basedb.core.Protocol;
1637 07 May 12 nicklas 31 import net.sf.basedb.core.SessionControl;
1639 09 May 12 nicklas 32 import net.sf.basedb.core.Type;
2900 05 Nov 14 nicklas 33 import net.sf.basedb.core.query.Annotations;
1639 09 May 12 nicklas 34 import net.sf.basedb.core.query.Expressions;
1639 09 May 12 nicklas 35 import net.sf.basedb.core.query.Hql;
3168 05 Mar 15 nicklas 36 import net.sf.basedb.core.query.Orders;
1639 09 May 12 nicklas 37 import net.sf.basedb.core.query.Restrictions;
2598 22 Aug 14 nicklas 38 import net.sf.basedb.reggie.JsonUtil;
1639 09 May 12 nicklas 39 import net.sf.basedb.reggie.Reggie;
5384 26 Apr 19 nicklas 40 import net.sf.basedb.reggie.activity.ActivityDef;
3059 19 Dec 14 nicklas 41 import net.sf.basedb.reggie.counter.CounterService;
1639 09 May 12 nicklas 42 import net.sf.basedb.reggie.dao.Annotationtype;
2901 05 Nov 14 nicklas 43 import net.sf.basedb.reggie.dao.BiomaterialList;
5361 15 Apr 19 nicklas 44 import net.sf.basedb.reggie.dao.BioplateType;
5356 12 Apr 19 nicklas 45 import net.sf.basedb.reggie.dao.Dna;
5791 16 Dec 19 nicklas 46 import net.sf.basedb.reggie.dao.DoNotUse;
1637 07 May 12 nicklas 47 import net.sf.basedb.reggie.dao.Lysate;
5361 15 Apr 19 nicklas 48 import net.sf.basedb.reggie.dao.ReactionPlate;
4905 11 Jul 18 nicklas 49 import net.sf.basedb.reggie.dao.ReggieItem;
2161 09 Dec 13 nicklas 50 import net.sf.basedb.reggie.dao.ReggieRole;
2901 05 Nov 14 nicklas 51 import net.sf.basedb.reggie.dao.Rna;
3740 11 Feb 16 nicklas 52 import net.sf.basedb.reggie.dao.SpecimenTube;
1639 09 May 12 nicklas 53 import net.sf.basedb.reggie.dao.StoragePlate;
1639 09 May 12 nicklas 54 import net.sf.basedb.reggie.dao.Subtype;
2690 24 Sep 14 nicklas 55 import net.sf.basedb.util.Values;
1637 07 May 12 nicklas 56 import net.sf.basedb.util.error.ThrowableUtil;
5371 16 Apr 19 nicklas 57 import net.sf.basedb.util.excel.XlsxTableWriter;
5371 16 Apr 19 nicklas 58 import net.sf.basedb.util.excel.XlsxToCsvUtil;
1639 09 May 12 nicklas 59 import net.sf.basedb.util.export.TableWriter;
1637 07 May 12 nicklas 60
1637 07 May 12 nicklas 61
1637 07 May 12 nicklas 62 public class ExtractionServlet 
1637 07 May 12 nicklas 63   extends HttpServlet 
1637 07 May 12 nicklas 64 {
1637 07 May 12 nicklas 65
1637 07 May 12 nicklas 66
1637 07 May 12 nicklas 67   private static final long serialVersionUID = 5374097270488331091L;
1637 07 May 12 nicklas 68
1637 07 May 12 nicklas 69   public ExtractionServlet()
1637 07 May 12 nicklas 70   {}
1637 07 May 12 nicklas 71
1637 07 May 12 nicklas 72   @Override
1637 07 May 12 nicklas 73   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1637 07 May 12 nicklas 74     throws ServletException, IOException 
1637 07 May 12 nicklas 75   {
1637 07 May 12 nicklas 76     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 77     JsonUtil.setJsonResponseHeaders(resp);
1637 07 May 12 nicklas 78     
1637 07 May 12 nicklas 79     JSONObject json = new JSONObject();
1637 07 May 12 nicklas 80     json.put("status", "ok");
1637 07 May 12 nicklas 81   
3975 26 May 16 nicklas 82     final SessionControl sc = Reggie.getSessionControl(req);
1637 07 May 12 nicklas 83     DbControl dc = null;
1637 07 May 12 nicklas 84     try
1637 07 May 12 nicklas 85     {
3059 19 Dec 14 nicklas 86       if ("GetUnprocessedLysates".equals(cmd))
1637 07 May 12 nicklas 87       {
1637 07 May 12 nicklas 88         /*
1637 07 May 12 nicklas 89           Find Lysate items that have no created date. Order by
1637 07 May 12 nicklas 90           bioplate position.
1637 07 May 12 nicklas 91         */
6330 14 Jun 21 nicklas 92         dc = sc.newDbControl(":Extraction wizard");
4093 12 Sep 16 nicklas 93         boolean includeReprocess = Values.getBoolean(req.getParameter("includeReprocess"));
1637 07 May 12 nicklas 94         
4093 12 Sep 16 nicklas 95         List<Lysate> lysates = Lysate.findUnprocessedLysates(dc, includeReprocess);
4093 12 Sep 16 nicklas 96         JSONArray jsonLysate = new JSONArray();        
1637 07 May 12 nicklas 97         for (Lysate l : lysates)
1637 07 May 12 nicklas 98         {
1637 07 May 12 nicklas 99           l.loadBioPlateLocation();
3469 27 Aug 15 nicklas 100           l.loadAnnotations(dc, "ReservedBy", Annotationtype.RESERVED_BY, null);
4093 12 Sep 16 nicklas 101           l.loadAnnotations(dc, "AutoProcessing", Annotationtype.AUTO_PROCESSING, null);
6723 04 May 22 nicklas 102           l.loadAnnotations(dc, "label", Annotationtype.TUBE_LABEL, null);
4094 12 Sep 16 nicklas 103           l.setAnnotation("remainingQuantity", l.getItem().getRemainingQuantity());
4094 12 Sep 16 nicklas 104           l.setAnnotation("comment", l.getItem().getDescription());
3740 11 Feb 16 nicklas 105           SpecimenTube specimen = l.getSpecimen();
3740 11 Feb 16 nicklas 106           specimen.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
3740 11 Feb 16 nicklas 107           l.setAnnotation("specimen", specimen.asJSONObject());
1637 07 May 12 nicklas 108           jsonLysate.add(l.asJSONObject());
1637 07 May 12 nicklas 109         }
1637 07 May 12 nicklas 110         
1637 07 May 12 nicklas 111         json.put("lysates", jsonLysate);
4093 12 Sep 16 nicklas 112         json.put("AutoProcessing", Annotationtype.AUTO_PROCESSING.asJSONObject(dc));
3469 27 Aug 15 nicklas 113         json.put("ReservedBy", Annotationtype.RESERVED_BY.asJSONObject(dc));
3740 11 Feb 16 nicklas 114         json.put("YellowLabel", Annotationtype.YELLOW_LABEL.asJSONObject(dc));
6723 04 May 22 nicklas 115         json.put("TubeLabel", Annotationtype.TUBE_LABEL.asJSONObject(dc));
1684 04 Jun 12 nicklas 116       }      
4089 08 Sep 16 nicklas 117       else if ("GetLysatesForPicking".equals(cmd))
4089 08 Sep 16 nicklas 118       {
4089 08 Sep 16 nicklas 119         /*
4089 08 Sep 16 nicklas 120           Find Lysate items that have been selected for re-extraction
4089 08 Sep 16 nicklas 121           but not yet picked from the freezers.
4089 08 Sep 16 nicklas 122         */
6330 14 Jun 21 nicklas 123         dc = sc.newDbControl(":Pick lysate for re-extraction");
4089 08 Sep 16 nicklas 124         ItemQuery<Extract> query = Extract.getQuery();
4089 08 Sep 16 nicklas 125         Subtype.LYSATE.addFilter(dc, query);
4089 08 Sep 16 nicklas 126         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
6722 04 May 22 nicklas 127         // Must NOT be in a ExtraLys* box (moved to ExtraLys* after picking)
6722 04 May 22 nicklas 128         query.restrict(Restrictions.not(Restrictions.like(Hql.property("bioWell.bioPlate.name"), Expressions.string("ExtraLys%"))));
4089 08 Sep 16 nicklas 129         // Join AutoProcessing annotation
4089 08 Sep 16 nicklas 130         query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
4089 08 Sep 16 nicklas 131         query.restrict(Restrictions.eq(Hql.alias("ap"), Expressions.string("ReProcess")));
4089 08 Sep 16 nicklas 132         
4089 08 Sep 16 nicklas 133         List<Lysate> lysates = Lysate.toLysate(query.list(dc));
4089 08 Sep 16 nicklas 134         
4089 08 Sep 16 nicklas 135         JSONArray jsonLysate = new JSONArray();
4089 08 Sep 16 nicklas 136         for (Lysate l : lysates)
4089 08 Sep 16 nicklas 137         {
4089 08 Sep 16 nicklas 138           l.loadBioPlateLocation();
4089 08 Sep 16 nicklas 139           l.setAnnotation("remainingQuantity", l.getItem().getRemainingQuantity());
6722 04 May 22 nicklas 140           l.loadAnnotations(dc, "label", Annotationtype.TUBE_LABEL, null);
4089 08 Sep 16 nicklas 141           SpecimenTube specimen = l.getSpecimen();
4089 08 Sep 16 nicklas 142           specimen.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
4089 08 Sep 16 nicklas 143           l.setAnnotation("specimen", specimen.asJSONObject());
4089 08 Sep 16 nicklas 144           jsonLysate.add(l.asJSONObject());
4089 08 Sep 16 nicklas 145         }
4089 08 Sep 16 nicklas 146         
4089 08 Sep 16 nicklas 147         json.put("lysates", jsonLysate);
4089 08 Sep 16 nicklas 148         json.put("YellowLabel", Annotationtype.YELLOW_LABEL.asJSONObject(dc));
4089 08 Sep 16 nicklas 149       }      
4089 08 Sep 16 nicklas 150       else if ("GetNewLysateBoxPositions".equals(cmd))
4089 08 Sep 16 nicklas 151       {
4089 08 Sep 16 nicklas 152         // Find a given number of storage positions for the lysate items. 
4089 08 Sep 16 nicklas 153         // Use the same procedure as we use for locating specimen
4089 08 Sep 16 nicklas 154         // boxes
6330 14 Jun 21 nicklas 155         dc = sc.newDbControl(":Pick lysate for re-extraction");
4089 08 Sep 16 nicklas 156         int numLysates = Values.getInt(req.getParameter("numLysates"));
4089 08 Sep 16 nicklas 157         
4089 08 Sep 16 nicklas 158         // Find the lysate (in ExtraLys** box) with the highest plate/row/column combination
4089 08 Sep 16 nicklas 159         ItemQuery<Extract> query = Extract.getQuery();
4089 08 Sep 16 nicklas 160         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
4089 08 Sep 16 nicklas 161         Subtype.LYSATE.addFilter(dc, query);
4089 08 Sep 16 nicklas 162         query.join(Hql.innerJoin("bioWell", "bw"));
4089 08 Sep 16 nicklas 163         query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
4089 08 Sep 16 nicklas 164         query.order(Orders.desc(Hql.property("bp", "name")));
4089 08 Sep 16 nicklas 165         query.order(Orders.desc(Hql.property("bw", "row")));
4089 08 Sep 16 nicklas 166         query.order(Orders.desc(Hql.property("bw", "column")));
4089 08 Sep 16 nicklas 167         // Must be in a ExtraLys* box
4089 08 Sep 16 nicklas 168         query.restrict(Restrictions.like(Hql.property("bp", "name"), Expressions.string("ExtraLys%")));
4089 08 Sep 16 nicklas 169         query.setMaxResults(1);
4089 08 Sep 16 nicklas 170         List<Extract> lastPlacedLysate = query.list(dc);
4089 08 Sep 16 nicklas 171         
4089 08 Sep 16 nicklas 172         BioPlate lastPlate = lastPlacedLysate.size() > 0 ? lastPlacedLysate.get(0).getBioWell().getPlate() : null;
4089 08 Sep 16 nicklas 173         
4089 08 Sep 16 nicklas 174         ItemQuery<BioPlate> extraLysBoxQuery = BioPlate.getQuery();
4089 08 Sep 16 nicklas 175         extraLysBoxQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string("ExtraLys%")));
4089 08 Sep 16 nicklas 176         if (lastPlate != null)
4089 08 Sep 16 nicklas 177         {
4089 08 Sep 16 nicklas 178           // Only load Sp boxes with the same or later name than the last sample
4089 08 Sep 16 nicklas 179           extraLysBoxQuery.restrict(Restrictions.gteq(Hql.property("name"), Expressions.string(lastPlate.getName())));
4089 08 Sep 16 nicklas 180         }
4089 08 Sep 16 nicklas 181         extraLysBoxQuery.order(Orders.asc(Hql.property("name")));
4089 08 Sep 16 nicklas 182         List<StoragePlate> extraLysBoxes = StoragePlate.toStoragePlates(extraLysBoxQuery.list(dc));
4089 08 Sep 16 nicklas 183         
4089 08 Sep 16 nicklas 184         JSONArray jsonWells = new JSONArray();
4089 08 Sep 16 nicklas 185         for (StoragePlate box : extraLysBoxes)
4089 08 Sep 16 nicklas 186         {
4089 08 Sep 16 nicklas 187           BioWell free = box.getFirstFreeWell();
4089 08 Sep 16 nicklas 188           while (free != null)
4089 08 Sep 16 nicklas 189           {
4089 08 Sep 16 nicklas 190             jsonWells.add(JsonUtil.getBioWellAsJSON(free, true));
4089 08 Sep 16 nicklas 191             if (jsonWells.size() == numLysates) break;
4089 08 Sep 16 nicklas 192             free = box.getNextFreeWell(free);
4089 08 Sep 16 nicklas 193           }
4089 08 Sep 16 nicklas 194           if (jsonWells.size() == numLysates) break;
4089 08 Sep 16 nicklas 195         }
4089 08 Sep 16 nicklas 196         
4089 08 Sep 16 nicklas 197         json.put("wells", jsonWells);
4089 08 Sep 16 nicklas 198         if (lastPlate != null) json.put("lastExtraLys", lastPlate.getName());
4095 13 Sep 16 nicklas 199       }
4095 13 Sep 16 nicklas 200       else if ("GetChildItemNames".equals(cmd))
4095 13 Sep 16 nicklas 201       {
4095 13 Sep 16 nicklas 202         // Auto-generate names for child RNA/DNA/FlowThrough
6330 14 Jun 21 nicklas 203         dc = sc.newDbControl(":Extraction wizard");
4095 13 Sep 16 nicklas 204         Integer[] lysates = Values.getInt(req.getParameter("lysates").split(","));
4095 13 Sep 16 nicklas 205         
4095 13 Sep 16 nicklas 206         JSONArray jsonChildNames = new JSONArray();
4095 13 Sep 16 nicklas 207         for (Integer id : lysates)
4095 13 Sep 16 nicklas 208         {
4095 13 Sep 16 nicklas 209           if (id == null) continue;
4095 13 Sep 16 nicklas 210           
4095 13 Sep 16 nicklas 211           Lysate lys = Lysate.getById(dc, id);
4905 11 Jul 18 nicklas 212           jsonChildNames.add(getChildNames(dc, lys)); 
4095 13 Sep 16 nicklas 213         }
4095 13 Sep 16 nicklas 214         json.put("childNames", jsonChildNames);
4906 11 Jul 18 nicklas 215     
4095 13 Sep 16 nicklas 216       }
1639 09 May 12 nicklas 217       else if ("GetNextQiacubeRunNo".equals(cmd))
1639 09 May 12 nicklas 218       {
1639 09 May 12 nicklas 219         /*
1639 09 May 12 nicklas 220           Get the next qiacube run number for the given date
1639 09 May 12 nicklas 221         */
1639 09 May 12 nicklas 222         Date date = Reggie.CONVERTER_STRING_TO_DATE.convert(req.getParameter("qiacubeDate"));
6330 14 Jun 21 nicklas 223         dc = sc.newDbControl(":Extraction wizard");
1639 09 May 12 nicklas 224         
1639 09 May 12 nicklas 225         // Find existing RNA with QiacubeDate
1639 09 May 12 nicklas 226         ItemQuery<Extract> query = Extract.getQuery();
1639 09 May 12 nicklas 227         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1639 09 May 12 nicklas 228         // Filter on RNA subtype
1639 09 May 12 nicklas 229         Subtype.RNA.addFilter(dc, query);
1639 09 May 12 nicklas 230         
1639 09 May 12 nicklas 231         // Filter on given date
1639 09 May 12 nicklas 232         query.join(Hql.innerJoin("creationEvent", "ce"));
1639 09 May 12 nicklas 233         query.restrict(Restrictions.eq(Hql.property("ce", "eventDate"), Expressions.parameter("created", date, Type.DATE)));
1639 09 May 12 nicklas 234         
1639 09 May 12 nicklas 235         int nextQiacubeRunNumber = 1;
1639 09 May 12 nicklas 236         for (Extract e : query.list(dc))
1639 09 May 12 nicklas 237         {
1639 09 May 12 nicklas 238           Integer qiacubeRunNumber = (Integer)Annotationtype.QIACUBE_RUN_NO.getAnnotationValue(dc, e);
1639 09 May 12 nicklas 239           if (qiacubeRunNumber != null && qiacubeRunNumber >= nextQiacubeRunNumber)
1639 09 May 12 nicklas 240           {
1639 09 May 12 nicklas 241             nextQiacubeRunNumber = qiacubeRunNumber + 1;
1639 09 May 12 nicklas 242           }
1639 09 May 12 nicklas 243           
1639 09 May 12 nicklas 244         }
1639 09 May 12 nicklas 245         json.put("nextQiacubeRunNo", nextQiacubeRunNumber);
1639 09 May 12 nicklas 246       }
3168 05 Mar 15 nicklas 247       else if ("GetLatestReagentLotNumbers".equals(cmd))
3168 05 Mar 15 nicklas 248       {
6330 14 Jun 21 nicklas 249         dc = sc.newDbControl(":Extraction wizard");
3168 05 Mar 15 nicklas 250         
3168 05 Mar 15 nicklas 251         // Find existing RNA with QiacubeDate
3168 05 Mar 15 nicklas 252         ItemQuery<Extract> query = Extract.getQuery();
3168 05 Mar 15 nicklas 253         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
3168 05 Mar 15 nicklas 254         // Filter on RNA subtype
3168 05 Mar 15 nicklas 255         Subtype.RNA.addFilter(dc, query);
6973 12 Jan 23 nicklas 256         // The QiacubeRunId must be empty since it is only used for external labs
6973 12 Jan 23 nicklas 257         query.join(Annotations.leftJoin(Annotationtype.QIACUBE_RUN_ID.load(dc), "qid"));
6973 12 Jan 23 nicklas 258         query.restrict(Restrictions.eq(Hql.alias("qid"), null));
3168 05 Mar 15 nicklas 259         // 
6892 24 Nov 22 nicklas 260         query.join(Annotations.innerJoin(Annotationtype.QIACUBE_DATE.load(dc), "qdate"));
3168 05 Mar 15 nicklas 261         query.join(Annotations.leftJoin(Annotationtype.QIACUBE_POSITION.load(dc), "qpos"));
3168 05 Mar 15 nicklas 262         query.join(Annotations.leftJoin(Annotationtype.QIACUBE_RUN_NO.load(dc), "qrun"));
3168 05 Mar 15 nicklas 263         // Order by date (descending), run number and position
6892 24 Nov 22 nicklas 264         query.order(Orders.desc(Hql.alias("qdate")));
3168 05 Mar 15 nicklas 265         query.order(Orders.desc(Hql.alias("qrun")));
3168 05 Mar 15 nicklas 266         query.order(Orders.desc(Hql.alias("qpos")));
3168 05 Mar 15 nicklas 267         query.setMaxResults(1);
3168 05 Mar 15 nicklas 268         
3168 05 Mar 15 nicklas 269         List<Extract> result = query.list(dc);
3168 05 Mar 15 nicklas 270         if (result.size() > 0)
3168 05 Mar 15 nicklas 271         {
3168 05 Mar 15 nicklas 272           Rna rna = Rna.get(result.get(0));
3168 05 Mar 15 nicklas 273           rna.loadAnnotations(dc, "allPrepMiniKit", Annotationtype.ALLPREP_MINI_KIT, null);
3168 05 Mar 15 nicklas 274           rna.loadAnnotations(dc, "etoh70", Annotationtype.ETOH70, null);
3168 05 Mar 15 nicklas 275           rna.loadAnnotations(dc, "bufferRw1", Annotationtype.BUFFER_RW1, null);
3168 05 Mar 15 nicklas 276           rna.loadAnnotations(dc, "bufferRpe", Annotationtype.BUFFER_RPE, null);
3168 05 Mar 15 nicklas 277           rna.loadAnnotations(dc, "bufferAw1", Annotationtype.BUFFER_AW1, null);
3168 05 Mar 15 nicklas 278           rna.loadAnnotations(dc, "bufferAw2", Annotationtype.BUFFER_AW2, null);
3168 05 Mar 15 nicklas 279           rna.loadAnnotations(dc, "bufferEb", Annotationtype.BUFFER_EB, null);
3168 05 Mar 15 nicklas 280           rna.loadAnnotations(dc, "rnaseFreeWater", Annotationtype.RNASE_FREE_WATER, null);
3168 05 Mar 15 nicklas 281           rna.loadAnnotations(dc, "allPrepDnaColumn", Annotationtype.ALLPREP_DNA_COL, null);
3168 05 Mar 15 nicklas 282           rna.loadAnnotations(dc, "rneasyColumn", Annotationtype.RNEASY_COL, null);          
3168 05 Mar 15 nicklas 283           rna.loadAnnotations(dc, "QiaCubeDate", Annotationtype.QIACUBE_DATE, Reggie.CONVERTER_DATE_TO_STRING);
3168 05 Mar 15 nicklas 284           rna.loadAnnotations(dc, "QiaCubePosition", Annotationtype.QIACUBE_POSITION, null);
3168 05 Mar 15 nicklas 285           json.put("rna", rna.asJSONObject());
3168 05 Mar 15 nicklas 286           
3168 05 Mar 15 nicklas 287           Lysate lysate = rna.getLysate();
6892 24 Nov 22 nicklas 288           if (lysate != null)
6892 24 Nov 22 nicklas 289           {
6892 24 Nov 22 nicklas 290             lysate.loadAnnotations(dc, "rltPlusBuffer", Annotationtype.RLT_PLUS, null);
6892 24 Nov 22 nicklas 291             lysate.loadAnnotations(dc, "betaMercaptoetanol", Annotationtype.BETA_MERCA, null);
6892 24 Nov 22 nicklas 292             lysate.loadAnnotations(dc, "reagentDx", Annotationtype.REAGENT_DX, null);
6892 24 Nov 22 nicklas 293             lysate.loadAnnotations(dc, "qiaShredderColumn", Annotationtype.QIA_SHREDDER, null);
6892 24 Nov 22 nicklas 294             lysate.setAnnotation("lysisDate", Reggie.CONVERTER_DATE_TO_STRING.convert(lysate.getItem().getCreationEvent().getEventDate()));
6892 24 Nov 22 nicklas 295             json.put("lysate", lysate.asJSONObject());
6892 24 Nov 22 nicklas 296           }
3168 05 Mar 15 nicklas 297         }
3168 05 Mar 15 nicklas 298         
3168 05 Mar 15 nicklas 299       }
1639 09 May 12 nicklas 300       
4097 13 Sep 16 nicklas 301       else if ("DownloadLabelFile".equals(cmd))
4097 13 Sep 16 nicklas 302       {
4097 13 Sep 16 nicklas 303         json = null; // No JSON output
4097 13 Sep 16 nicklas 304         
6330 14 Jun 21 nicklas 305         dc = sc.newDbControl(":Pick lysate for re-extraction");
4097 13 Sep 16 nicklas 306         Integer[] lysates = Values.getInt(req.getParameter("lysates").split(","));
5304 15 Feb 19 nicklas 307         String format = Values.getString(req.getParameter("format"), "csv");
5304 15 Feb 19 nicklas 308       
5304 15 Feb 19 nicklas 309         String baseFilename = "reextract-" + Reggie.CONVERTER_DATE_TO_STRING_WITH_SEPARATOR.convert(new Date());
4097 13 Sep 16 nicklas 310
5304 15 Feb 19 nicklas 311         TableWriter tw = null;
5304 15 Feb 19 nicklas 312         XlsxTableWriter xls = null;
5304 15 Feb 19 nicklas 313         if ("xlsx".equals(format))
5304 15 Feb 19 nicklas 314         {
5304 15 Feb 19 nicklas 315           resp.setHeader("Content-Disposition", "attachment; filename="+baseFilename+".xlsx");
5371 16 Apr 19 nicklas 316           resp.setContentType(XlsxToCsvUtil.XLSX_MIME_TYPE);
5306 15 Feb 19 nicklas 317           xls = new XlsxTableWriter("ReExtractionLabels");
5304 15 Feb 19 nicklas 318           tw = xls;
5304 15 Feb 19 nicklas 319         }
5304 15 Feb 19 nicklas 320         else
5304 15 Feb 19 nicklas 321         {
5304 15 Feb 19 nicklas 322           resp.setHeader("Content-Disposition", "attachment; filename="+baseFilename+".csv");
5304 15 Feb 19 nicklas 323           resp.setContentType("text/plain");
5304 15 Feb 19 nicklas 324           resp.setCharacterEncoding("UTF-8");
5304 15 Feb 19 nicklas 325           tw = new TableWriter(resp.getWriter());
5304 15 Feb 19 nicklas 326           // The LABEL header is only required in the CSV format
5304 15 Feb 19 nicklas 327           tw.tablePrintData("LABEL");
5304 15 Feb 19 nicklas 328         }
5304 15 Feb 19 nicklas 329
4097 13 Sep 16 nicklas 330         for (Integer id : lysates)
4097 13 Sep 16 nicklas 331         {
4097 13 Sep 16 nicklas 332           if (id == null) continue;
4097 13 Sep 16 nicklas 333           
4097 13 Sep 16 nicklas 334           Lysate lysate = Lysate.getById(dc, id);
4097 13 Sep 16 nicklas 335           JSONObject jsonNames = getChildNames(dc, lysate);
5304 15 Feb 19 nicklas 336           tw.tablePrintData(jsonNames.get("rna"));
5304 15 Feb 19 nicklas 337           tw.tablePrintData(jsonNames.get("preNormalizedRna"));
5304 15 Feb 19 nicklas 338           tw.tablePrintData(jsonNames.get("dna"));
5304 15 Feb 19 nicklas 339           tw.tablePrintData(jsonNames.get("ft"));
5304 15 Feb 19 nicklas 340         }
5304 15 Feb 19 nicklas 341         tw.flush();
5304 15 Feb 19 nicklas 342
5304 15 Feb 19 nicklas 343         if (xls != null) 
5304 15 Feb 19 nicklas 344         {
5304 15 Feb 19 nicklas 345           xls.getSheet().setColumnWidth(0, 18*256);
5304 15 Feb 19 nicklas 346           xls.saveTo(resp.getOutputStream());
5304 15 Feb 19 nicklas 347           xls.close();
5304 15 Feb 19 nicklas 348         }
5304 15 Feb 19 nicklas 349
4097 13 Sep 16 nicklas 350       }        
1639 09 May 12 nicklas 351       else if ("DownloadNanoDropSampleIdFile".equals(cmd))
1639 09 May 12 nicklas 352       {
1639 09 May 12 nicklas 353         json = null; // No JSON output
1639 09 May 12 nicklas 354         
1639 09 May 12 nicklas 355         String qiacubeDate = req.getParameter("qiacubeDate");
1639 09 May 12 nicklas 356         String qiacubeRunNo = req.getParameter("qiacubeRunNo");
4095 13 Sep 16 nicklas 357         String[] rnaNames = req.getParameter("rnaNames").split(",");
4095 13 Sep 16 nicklas 358         String[] dnaNames = req.getParameter("dnaNames").split(",");
1639 09 May 12 nicklas 359         
1643 11 May 12 nicklas 360         resp.setHeader("Content-Disposition", "attachment; filename=nd-sample-id-" + qiacubeDate + "-" + qiacubeRunNo+ ".txt");
1643 11 May 12 nicklas 361         resp.setContentType("text/plain");
1639 09 May 12 nicklas 362         resp.setCharacterEncoding("UTF-8");
1639 09 May 12 nicklas 363         
1639 09 May 12 nicklas 364         PrintWriter out = resp.getWriter();
1639 09 May 12 nicklas 365         TableWriter tw = new TableWriter(out);
1639 09 May 12 nicklas 366         
3468 26 Aug 15 nicklas 367         // Samples names in NanoDrop [row][column]
3468 26 Aug 15 nicklas 368         String[][] ndWells = new String[8][12];  
3468 26 Aug 15 nicklas 369         int ndRow = 0;
3468 26 Aug 15 nicklas 370         int ndCol = 0;
4095 13 Sep 16 nicklas 371         int rnaOffset = dnaNames.length > 7 ? 2 : 1; // RNA starts in column 3 if more than 7 lysates, otherwise in column 2.
4095 13 Sep 16 nicklas 372         for (int i = 0; i < dnaNames.length; i++)
1639 09 May 12 nicklas 373         {
4095 13 Sep 16 nicklas 374           ndWells[ndRow][ndCol] = dnaNames[i];
4095 13 Sep 16 nicklas 375           ndWells[ndRow][ndCol+rnaOffset] = rnaNames[i];
3468 26 Aug 15 nicklas 376           ndRow++;
3468 26 Aug 15 nicklas 377           if (ndRow == 8)
1639 09 May 12 nicklas 378           {
3468 26 Aug 15 nicklas 379             ndRow = 0;
3468 26 Aug 15 nicklas 380             ndCol++;
1639 09 May 12 nicklas 381           }
1639 09 May 12 nicklas 382         }
3468 26 Aug 15 nicklas 383         ndWells[ndRow][ndCol] = "blank";
3468 26 Aug 15 nicklas 384         ndWells[ndRow][ndCol+rnaOffset] = "blank";
1639 09 May 12 nicklas 385         
3468 26 Aug 15 nicklas 386         // Print out row by row
3468 26 Aug 15 nicklas 387         for (int row = 0; row < 8; ++row)
3468 26 Aug 15 nicklas 388         {
3468 26 Aug 15 nicklas 389           tw.tablePrintData((Object[])ndWells[row]);
3468 26 Aug 15 nicklas 390         }
3468 26 Aug 15 nicklas 391         
1639 09 May 12 nicklas 392         out.flush();
1639 09 May 12 nicklas 393         out.close();
1639 09 May 12 nicklas 394         
1639 09 May 12 nicklas 395       }
2903 06 Nov 14 nicklas 396       else if ("GetRNAToNormalize".equals(cmd) || "GetRNAInfoForNormalize".equals(cmd))
2903 06 Nov 14 nicklas 397       {
2903 06 Nov 14 nicklas 398         /*
2903 06 Nov 14 nicklas 399           Find RNA items that have been selected for pre-normalization.
2903 06 Nov 14 nicklas 400         */
6330 14 Jun 21 nicklas 401         dc = sc.newDbControl(":RNA normalization wizard");
2903 06 Nov 14 nicklas 402         // Create a query that load all Lysate extracts without created date
2903 06 Nov 14 nicklas 403         ItemQuery<Extract> query = Extract.getQuery();
2903 06 Nov 14 nicklas 404         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2903 06 Nov 14 nicklas 405         // Filter on RNA subtype
2903 06 Nov 14 nicklas 406         Subtype.RNA.addFilter(dc, query);
2903 06 Nov 14 nicklas 407         
2903 06 Nov 14 nicklas 408         if ("GetRNAToNormalize".equals(cmd))
2903 06 Nov 14 nicklas 409         {
2903 06 Nov 14 nicklas 410           // Join AutoProcessing annotation
2903 06 Nov 14 nicklas 411           query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
2903 06 Nov 14 nicklas 412           query.restrict(Restrictions.eq(Hql.alias("ap"), Expressions.string(Rna.AUTO_PROCESSING_PRE_NORMALIZE)));
2903 06 Nov 14 nicklas 413         }
2903 06 Nov 14 nicklas 414         else
2903 06 Nov 14 nicklas 415         {
2903 06 Nov 14 nicklas 416           String[] selected = req.getParameter("rnaIds").split(",");
2903 06 Nov 14 nicklas 417           query.restrict(Restrictions.in(Hql.property("id"), Expressions.parameter("selected")));
2903 06 Nov 14 nicklas 418           query.setParameter("selected", Arrays.asList(Values.getInt(selected)), Type.INT);
2903 06 Nov 14 nicklas 419         }
2903 06 Nov 14 nicklas 420
2903 06 Nov 14 nicklas 421         JSONArray jsonRna = new JSONArray();
2903 06 Nov 14 nicklas 422         List<Rna> rna = Rna.toRna(query.list(dc));
2903 06 Nov 14 nicklas 423         
2903 06 Nov 14 nicklas 424         for (Rna r : rna)
2903 06 Nov 14 nicklas 425         {
3741 12 Feb 16 nicklas 426           Lysate l = r.getLysate();
3741 12 Feb 16 nicklas 427           SpecimenTube sp = l != null ? l.getSpecimen() : null;
2903 06 Nov 14 nicklas 428           r.loadBioPlateLocation();
2903 06 Nov 14 nicklas 429           r.setAnnotation("remainingQuantity", r.getItem().getRemainingQuantity());
4905 11 Jul 18 nicklas 430           r.setAnnotation("nextAliquotName", r.getNextNormalizedAliquotName(dc));
6726 04 May 22 nicklas 431           r.loadAnnotations(dc, "label", Annotationtype.TUBE_LABEL, null);
2903 06 Nov 14 nicklas 432           r.loadAnnotations(dc, "NDConc", Annotationtype.ND_CONC, null);
6219 20 Apr 21 nicklas 433           r.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
6219 20 Apr 21 nicklas 434           r.coalesceInto("conc", "NDConc", "QubitConc");
2903 06 Nov 14 nicklas 435           r.loadAnnotations(dc, "QiacubeDate", Annotationtype.QIACUBE_DATE, Reggie.CONVERTER_DATE_TO_STRING);
2903 06 Nov 14 nicklas 436           r.loadAnnotations(dc, "QiacubeRunNo", Annotationtype.QIACUBE_RUN_NO, null);
2903 06 Nov 14 nicklas 437           r.loadAnnotations(dc, "QiacubePosition", Annotationtype.QIACUBE_POSITION, null);
3741 12 Feb 16 nicklas 438           if (sp != null) 
3741 12 Feb 16 nicklas 439           {
3741 12 Feb 16 nicklas 440             sp.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
3741 12 Feb 16 nicklas 441             r.setAnnotation("specimen", sp.asJSONObject());
3741 12 Feb 16 nicklas 442           }
2903 06 Nov 14 nicklas 443           jsonRna.add(r.asJSONObject());
2903 06 Nov 14 nicklas 444         }
2903 06 Nov 14 nicklas 445         
2903 06 Nov 14 nicklas 446         json.put("rnaExtracts", jsonRna);
2903 06 Nov 14 nicklas 447       }      
5359 12 Apr 19 nicklas 448       else if ("GetDNAToNormalize".equals(cmd) || "GetDNAInfoForNormalize".equals(cmd))
5359 12 Apr 19 nicklas 449       {
5359 12 Apr 19 nicklas 450         /*
5359 12 Apr 19 nicklas 451           Find DNA items that have been selected for pre-normalization.
5359 12 Apr 19 nicklas 452         */
6330 14 Jun 21 nicklas 453         dc = sc.newDbControl(":DNA normalization wizard");
5359 12 Apr 19 nicklas 454         // Create a query that load all Lysate extracts without created date
5359 12 Apr 19 nicklas 455         ItemQuery<Extract> query = Extract.getQuery();
5359 12 Apr 19 nicklas 456         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5359 12 Apr 19 nicklas 457         // Filter on DNA subtype
5359 12 Apr 19 nicklas 458         Subtype.DNA.addFilter(dc, query);
5359 12 Apr 19 nicklas 459         
5359 12 Apr 19 nicklas 460         if ("GetDNAToNormalize".equals(cmd))
5359 12 Apr 19 nicklas 461         {
5359 12 Apr 19 nicklas 462           // Join AutoProcessing annotation
5359 12 Apr 19 nicklas 463           query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
5359 12 Apr 19 nicklas 464           query.restrict(Restrictions.eq(Hql.alias("ap"), Expressions.string(Dna.AUTO_PROCESSING_PRE_NORMALIZE)));
5359 12 Apr 19 nicklas 465         }
5359 12 Apr 19 nicklas 466         else
5359 12 Apr 19 nicklas 467         {
5359 12 Apr 19 nicklas 468           String[] selected = req.getParameter("dnaIds").split(",");
5359 12 Apr 19 nicklas 469           query.restrict(Restrictions.in(Hql.property("id"), Expressions.parameter("selected")));
5359 12 Apr 19 nicklas 470           query.setParameter("selected", Arrays.asList(Values.getInt(selected)), Type.INT);
5359 12 Apr 19 nicklas 471         }
5359 12 Apr 19 nicklas 472
5359 12 Apr 19 nicklas 473         JSONArray jsonDna = new JSONArray();
5359 12 Apr 19 nicklas 474         List<Dna> dna = Dna.toDna(query.list(dc));
5359 12 Apr 19 nicklas 475         
5359 12 Apr 19 nicklas 476         for (Dna d : dna)
5359 12 Apr 19 nicklas 477         {
5359 12 Apr 19 nicklas 478           Lysate l = d.getLysate();
5359 12 Apr 19 nicklas 479           SpecimenTube sp = l != null ? l.getSpecimen() : null;
5359 12 Apr 19 nicklas 480           d.loadBioPlateLocation();
5359 12 Apr 19 nicklas 481           d.setAnnotation("remainingQuantity", d.getItem().getRemainingQuantity());
5359 12 Apr 19 nicklas 482           d.setAnnotation("nextAliquotName", d.getNextNormalizedAliquotName(dc));
6726 04 May 22 nicklas 483           d.loadAnnotations(dc, "label", Annotationtype.TUBE_LABEL, null);
5359 12 Apr 19 nicklas 484           d.loadAnnotations(dc, "NDConc", Annotationtype.ND_CONC, null);
6219 20 Apr 21 nicklas 485           d.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
6219 20 Apr 21 nicklas 486           d.coalesceInto("conc", "NDConc", "QubitConc");
5359 12 Apr 19 nicklas 487           d.loadAnnotations(dc, "QiacubeDate", Annotationtype.QIACUBE_DATE, Reggie.CONVERTER_DATE_TO_STRING);
5359 12 Apr 19 nicklas 488           d.loadAnnotations(dc, "QiacubeRunNo", Annotationtype.QIACUBE_RUN_NO, null);
5359 12 Apr 19 nicklas 489           d.loadAnnotations(dc, "QiacubePosition", Annotationtype.QIACUBE_POSITION, null);
5359 12 Apr 19 nicklas 490           if (sp != null) 
5359 12 Apr 19 nicklas 491           {
5359 12 Apr 19 nicklas 492             sp.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
5359 12 Apr 19 nicklas 493             d.setAnnotation("specimen", sp.asJSONObject());
5359 12 Apr 19 nicklas 494           }
5359 12 Apr 19 nicklas 495           jsonDna.add(d.asJSONObject());
5359 12 Apr 19 nicklas 496         }
5359 12 Apr 19 nicklas 497         
5359 12 Apr 19 nicklas 498         json.put("dnaExtracts", jsonDna);
5361 15 Apr 19 nicklas 499       }
5361 15 Apr 19 nicklas 500       else if ("GetActivePreNormalizedDNAPlates".equals(cmd))
5361 15 Apr 19 nicklas 501       {
5361 15 Apr 19 nicklas 502         /*
5361 15 Apr 19 nicklas 503           Find bioplates that is currently being filled
5361 15 Apr 19 nicklas 504           with PreNormalzied DNA aliquots. We look for plates that has a 
5456 29 May 19 nicklas 505           'PreNormalizedDNA' type, at least 'numFreeWells' free well,
5456 29 May 19 nicklas 506           PlateProcessing=AddAliquots and are not marked as destroyed.
5361 15 Apr 19 nicklas 507           We also generate a name for the next plate in case a new plate is needed.
5361 15 Apr 19 nicklas 508         */
5361 15 Apr 19 nicklas 509         int numFreeWells = Values.getInt(req.getParameter("numFreeWells"), 1);
5407 08 May 19 nicklas 510
6330 14 Jun 21 nicklas 511         dc = sc.newDbControl(":DNA normalization wizard");
5407 08 May 19 nicklas 512
5407 08 May 19 nicklas 513         ItemQuery<BioPlate> query = BioPlate.getQuery();
5407 08 May 19 nicklas 514         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5407 08 May 19 nicklas 515         BioplateType.PRE_NORMALIZED_DNA.addFilter(dc, query, true);
5407 08 May 19 nicklas 516         query.restrict(Restrictions.gteq(Hql.property("freeWells"), Expressions.integer(numFreeWells)));
5407 08 May 19 nicklas 517         query.join(Annotations.innerJoin(Annotationtype.PLATE_PROCESSING.load(dc), "ps"));
5407 08 May 19 nicklas 518         query.restrict(Restrictions.eq(Hql.alias("ps"), Expressions.string("AddAliquots")));
5407 08 May 19 nicklas 519         query.order(Orders.asc(Hql.property("name")));
5361 15 Apr 19 nicklas 520         
5407 08 May 19 nicklas 521         List<ReactionPlate> plates = ReactionPlate.toList(BioplateType.PRE_NORMALIZED_DNA, query.list(dc));
5407 08 May 19 nicklas 522         
5361 15 Apr 19 nicklas 523         JSONArray jsonPlates = new JSONArray();
5361 15 Apr 19 nicklas 524         for (ReactionPlate plate : plates)
5361 15 Apr 19 nicklas 525         {
5361 15 Apr 19 nicklas 526           plate.loadBioWells(dc, false);
5361 15 Apr 19 nicklas 527           jsonPlates.add(plate.asJSONObject());
5361 15 Apr 19 nicklas 528         }
5361 15 Apr 19 nicklas 529         json.put("plates", jsonPlates);
5407 08 May 19 nicklas 530         ReactionPlate newPlate = ReactionPlate.getNew(dc, BioplateType.PRE_NORMALIZED_DNA);
5407 08 May 19 nicklas 531         newPlate.getBioPlate().setName(BioplateType.PRE_NORMALIZED_DNA.getNextPlateName(dc, false));
5361 15 Apr 19 nicklas 532         newPlate.loadBioWells(dc, false);
5361 15 Apr 19 nicklas 533         jsonPlates.add(newPlate.asJSONObject());
5361 15 Apr 19 nicklas 534       }
5361 15 Apr 19 nicklas 535
1637 07 May 12 nicklas 536     }
1637 07 May 12 nicklas 537     catch (Throwable t)
1637 07 May 12 nicklas 538     {
1637 07 May 12 nicklas 539       t.printStackTrace();
1639 09 May 12 nicklas 540       if (json != null)
1639 09 May 12 nicklas 541       {
1639 09 May 12 nicklas 542         json.clear();
1639 09 May 12 nicklas 543         json.put("status", "error");
1639 09 May 12 nicklas 544         json.put("message", t.getMessage());
1639 09 May 12 nicklas 545         json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1639 09 May 12 nicklas 546       }
1639 09 May 12 nicklas 547       else
1639 09 May 12 nicklas 548       {
1639 09 May 12 nicklas 549         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());
1639 09 May 12 nicklas 550       }
1637 07 May 12 nicklas 551     }
1637 07 May 12 nicklas 552     finally
1637 07 May 12 nicklas 553     {
1637 07 May 12 nicklas 554       if (dc != null) dc.close();
1639 09 May 12 nicklas 555       if (json != null)
1639 09 May 12 nicklas 556       {
1639 09 May 12 nicklas 557         json.writeJSONString(resp.getWriter());
1639 09 May 12 nicklas 558       }
1637 07 May 12 nicklas 559     }
1637 07 May 12 nicklas 560     
1637 07 May 12 nicklas 561   }
1637 07 May 12 nicklas 562
1637 07 May 12 nicklas 563   @Override
1637 07 May 12 nicklas 564   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
1637 07 May 12 nicklas 565     throws ServletException, IOException 
1637 07 May 12 nicklas 566   {
1637 07 May 12 nicklas 567     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 568     JsonUtil.setJsonResponseHeaders(resp);
1637 07 May 12 nicklas 569     
1637 07 May 12 nicklas 570     JSONObject json = new JSONObject();
1637 07 May 12 nicklas 571     json.put("status", "ok");
1637 07 May 12 nicklas 572     
1637 07 May 12 nicklas 573     JSONArray jsonMessages = new JSONArray();
1637 07 May 12 nicklas 574   
3975 26 May 16 nicklas 575     final SessionControl sc = Reggie.getSessionControl(req);
1637 07 May 12 nicklas 576     DbControl dc = null;
1637 07 May 12 nicklas 577     try
1637 07 May 12 nicklas 578     {
1637 07 May 12 nicklas 579       
1639 09 May 12 nicklas 580       if ("RegisterDnaRnaFt".equals(cmd))
1639 09 May 12 nicklas 581       {
6330 14 Jun 21 nicklas 582         dc = sc.newDbControl(":Extraction registration wizard");
1639 09 May 12 nicklas 583         
2161 09 Dec 13 nicklas 584         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 585         
3752 17 Feb 16 nicklas 586         JSONObject jsonReq = JsonUtil.parseRequest(req);
1639 09 May 12 nicklas 587         JSONArray jsonLysates = (JSONArray)jsonReq.get("lysates");
1660 22 May 12 nicklas 588         List<Extract> newRna = new ArrayList<Extract>(jsonLysates.size());
5357 12 Apr 19 nicklas 589         List<Extract> newDna = new ArrayList<Extract>(jsonLysates.size());
1639 09 May 12 nicklas 590         
1639 09 May 12 nicklas 591         // Lysate properties
1639 09 May 12 nicklas 592         Date lysisDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("lysisDate"));
1639 09 May 12 nicklas 593         Number lysisProtocolId = (Number)jsonReq.get("lysisProtocol");
1639 09 May 12 nicklas 594         Protocol lysisProtocol = lysisProtocolId != null ? Protocol.getById(dc, lysisProtocolId.intValue()) : null;
1639 09 May 12 nicklas 595         
1639 09 May 12 nicklas 596         // Qiacube properties
1639 09 May 12 nicklas 597         Date qiacubeDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("qiacubeDate"));
1639 09 May 12 nicklas 598         Number qiacubeRunNo = (Number)jsonReq.get("qiacubeRunNo");
1639 09 May 12 nicklas 599         String qiacubeOperator = (String)jsonReq.get("qiacubeOperator");
1639 09 May 12 nicklas 600         Number dnaProtocolId = (Number)jsonReq.get("dnaProtocol");
1639 09 May 12 nicklas 601         Number rnaProtocolId = (Number)jsonReq.get("rnaProtocol");
1639 09 May 12 nicklas 602         Number ftProtocolId = (Number)jsonReq.get("ftProtocol");
1639 09 May 12 nicklas 603         Protocol dnaProtocol = dnaProtocolId != null ? Protocol.getById(dc, dnaProtocolId.intValue()) : null;
1639 09 May 12 nicklas 604         Protocol rnaProtocol = rnaProtocolId != null ? Protocol.getById(dc, rnaProtocolId.intValue()) : null;
1639 09 May 12 nicklas 605         Protocol ftProtocol = ftProtocolId != null ? Protocol.getById(dc, ftProtocolId.intValue()) : null;
1639 09 May 12 nicklas 606         
2690 24 Sep 14 nicklas 607         String d_rltPlusBuffer = Values.getStringOrNull((String)jsonReq.get("rltPlusBuffer"));
2690 24 Sep 14 nicklas 608         String d_betaMercaptoetanol = Values.getStringOrNull((String)jsonReq.get("betaMercaptoetanol"));
2690 24 Sep 14 nicklas 609         String d_reagentDx = Values.getStringOrNull((String)jsonReq.get("reagentDx"));
2690 24 Sep 14 nicklas 610         String d_qiaShredderColumn = Values.getStringOrNull((String)jsonReq.get("qiaShredderColumn"));
2690 24 Sep 14 nicklas 611
2690 24 Sep 14 nicklas 612         String d_allPrepMiniKit = Values.getStringOrNull((String)jsonReq.get("allPrepMiniKit"));
2690 24 Sep 14 nicklas 613         String d_etoh70 = Values.getStringOrNull((String)jsonReq.get("etoh70"));
2690 24 Sep 14 nicklas 614         String d_bufferRw1 = Values.getStringOrNull((String)jsonReq.get("bufferRw1"));
2690 24 Sep 14 nicklas 615         String d_bufferRpe = Values.getStringOrNull((String)jsonReq.get("bufferRpe"));
2690 24 Sep 14 nicklas 616         String d_bufferAw1 = Values.getStringOrNull((String)jsonReq.get("bufferAw1"));
2690 24 Sep 14 nicklas 617         String d_bufferAw2 = Values.getStringOrNull((String)jsonReq.get("bufferAw2"));
2690 24 Sep 14 nicklas 618         String d_bufferEb = Values.getStringOrNull((String)jsonReq.get("bufferEb"));
2690 24 Sep 14 nicklas 619         String d_rnaseFreeWater = Values.getStringOrNull((String)jsonReq.get("rnaseFreeWater"));
2690 24 Sep 14 nicklas 620         String d_allPrepDnaColumn = Values.getStringOrNull((String)jsonReq.get("allPrepDnaColumn"));
2690 24 Sep 14 nicklas 621         String d_rneasyColumn = Values.getStringOrNull((String)jsonReq.get("rneasyColumn"));
2690 24 Sep 14 nicklas 622
3247 14 Apr 15 nicklas 623         ItemList flaggedRna = BiomaterialList.FLAGGED_RNA.load(dc);
5357 12 Apr 19 nicklas 624         ItemList flaggedDna = BiomaterialList.FLAGGED_DNA.load(dc);
5357 12 Apr 19 nicklas 625         int numFlaggedRNA = 0;
5357 12 Apr 19 nicklas 626         int numFlaggedDNA = 0;
2901 05 Nov 14 nicklas 627       
1639 09 May 12 nicklas 628         for (int i = 0; i < jsonLysates.size(); ++i)
1639 09 May 12 nicklas 629         {
1639 09 May 12 nicklas 630           JSONObject jsonLysate = (JSONObject)jsonLysates.get(i);
1639 09 May 12 nicklas 631           Number lysateId = (Number)jsonLysate.get("id");
1639 09 May 12 nicklas 632           float lysateProcessedQuantity = ((Number)jsonLysate.get("processedQuantity")).floatValue();
1639 09 May 12 nicklas 633           
1639 09 May 12 nicklas 634           Extract lysate = Extract.getById(dc, lysateId.intValue());
1639 09 May 12 nicklas 635           BioMaterialEvent lysisEvent = lysate.getCreationEvent();
4094 12 Sep 16 nicklas 636           boolean isReExtract = lysisEvent.getEventDate() != null;
4094 12 Sep 16 nicklas 637           if (!isReExtract)
4094 12 Sep 16 nicklas 638           {
4094 12 Sep 16 nicklas 639             float lysateOriginalQuantity = ((Number)jsonLysate.get("originalQuantity")).floatValue();
4094 12 Sep 16 nicklas 640             lysisEvent.setEventDate(lysisDate);
4094 12 Sep 16 nicklas 641             lysisEvent.setProtocol(lysisProtocol);
4094 12 Sep 16 nicklas 642             lysate.setOriginalQuantity(lysateOriginalQuantity);
4094 12 Sep 16 nicklas 643           }
1642 10 May 12 nicklas 644           lysate.setDescription((String)jsonLysate.get("comment"));
1639 09 May 12 nicklas 645           BioWell lysateWell = lysate.getBioWell();
4100 14 Sep 16 nicklas 646           Annotationtype.RESERVED_BY.setAnnotationValue(dc, lysate, null);
4152 05 Oct 16 nicklas 647           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, lysate, null);
2690 24 Sep 14 nicklas 648
2690 24 Sep 14 nicklas 649           // Reagents
2690 24 Sep 14 nicklas 650           String rltPlusBuffer = Values.getString((String)jsonLysate.get("rltPlusBuffer"), d_rltPlusBuffer);
2690 24 Sep 14 nicklas 651           String betaMercaptoetanol = Values.getString((String)jsonLysate.get("betaMercaptoetanol"), d_betaMercaptoetanol);
2690 24 Sep 14 nicklas 652           String reagentDx = Values.getString((String)jsonLysate.get("reagentDx"), d_reagentDx);
2690 24 Sep 14 nicklas 653           String qiaShredderColumn = Values.getString((String)jsonLysate.get("qiaShredderColumn"), d_qiaShredderColumn);
2690 24 Sep 14 nicklas 654
2690 24 Sep 14 nicklas 655           String allPrepMiniKit = Values.getString((String)jsonLysate.get("allPrepMiniKit"), d_allPrepMiniKit);
2690 24 Sep 14 nicklas 656           String etoh70 = Values.getString((String)jsonLysate.get("etoh70"), d_etoh70);
2690 24 Sep 14 nicklas 657           String bufferRw1 = Values.getString((String)jsonLysate.get("bufferRw1"), d_bufferRw1);
2690 24 Sep 14 nicklas 658           String bufferRpe = Values.getString((String)jsonLysate.get("bufferRpe"), d_bufferRpe);
2690 24 Sep 14 nicklas 659           String bufferAw1 = Values.getString((String)jsonLysate.get("bufferAw1"), d_bufferAw1);
2690 24 Sep 14 nicklas 660           String bufferAw2 = Values.getString((String)jsonLysate.get("bufferAw2"), d_bufferAw2);
2690 24 Sep 14 nicklas 661           String bufferEb = Values.getString((String)jsonLysate.get("bufferEb"), d_bufferEb);
2690 24 Sep 14 nicklas 662           String rnaseFreeWater = Values.getString((String)jsonLysate.get("rnaseFreeWater"), d_rnaseFreeWater);
2690 24 Sep 14 nicklas 663           String allPrepDnaColumn = Values.getString((String)jsonLysate.get("allPrepDnaColumn"), d_allPrepDnaColumn);
2690 24 Sep 14 nicklas 664           String rneasyColumn = Values.getString((String)jsonLysate.get("rneasyColumn"), d_rneasyColumn);
1639 09 May 12 nicklas 665           
4094 12 Sep 16 nicklas 666           if (!isReExtract)
4094 12 Sep 16 nicklas 667           {
4094 12 Sep 16 nicklas 668             Annotationtype.RLT_PLUS.setAnnotationValue(dc, lysate, rltPlusBuffer);
4094 12 Sep 16 nicklas 669             Annotationtype.BETA_MERCA.setAnnotationValue(dc, lysate, betaMercaptoetanol);
4094 12 Sep 16 nicklas 670             Annotationtype.REAGENT_DX.setAnnotationValue(dc, lysate, reagentDx);
4094 12 Sep 16 nicklas 671             Annotationtype.QIA_SHREDDER.setAnnotationValue(dc, lysate, qiaShredderColumn);
4094 12 Sep 16 nicklas 672           }
2690 24 Sep 14 nicklas 673           
1639 09 May 12 nicklas 674           // Qiacube
1639 09 May 12 nicklas 675           Number qiacubePosition = (Number)jsonLysate.get("qiacubePosition");
1639 09 May 12 nicklas 676           
1639 09 May 12 nicklas 677           // DNA
1639 09 May 12 nicklas 678           JSONObject jsonDna = (JSONObject)jsonLysate.get("dna");
1639 09 May 12 nicklas 679           Extract dna = Extract.getNew(dc);
1639 09 May 12 nicklas 680           dna.setItemSubtype(Subtype.DNA.load(dc));
4905 11 Jul 18 nicklas 681           dna.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.DNA, (String)jsonDna.get("name")));
1642 10 May 12 nicklas 682           dna.setDescription((String)jsonDna.get("comment"));
1639 09 May 12 nicklas 683           BioMaterialEvent dnaEvent = dna.getCreationEvent();
1639 09 May 12 nicklas 684           dnaEvent.setEventDate(qiacubeDate);
1639 09 May 12 nicklas 685           dnaEvent.setProtocol(dnaProtocol);
1639 09 May 12 nicklas 686           dnaEvent.setSource(lysate).setUsedQuantity(lysateProcessedQuantity / 3);
5791 16 Dec 19 nicklas 687           DoNotUse.copyDoNotUseAnnotations(dc, lysate, dna, false);
1639 09 May 12 nicklas 688           float dnaVolume = ((Number)jsonDna.get("volume")).floatValue();
1639 09 May 12 nicklas 689           float dnaNdConc = ((Number)jsonDna.get("ndConc")).floatValue();
3470 27 Aug 15 nicklas 690           dna.setOriginalQuantity(Math.max(0, dnaVolume * dnaNdConc / 1000));
1639 09 May 12 nicklas 691           dna.setBioWell(StoragePlate.getChildBioWell(dc, lysateWell, "Lys", "DNA"));
5356 12 Apr 19 nicklas 692           boolean preNormalizeDna = Boolean.TRUE.equals(jsonDna.get("preNormalize"));
5357 12 Apr 19 nicklas 693           boolean dnaIsFlagged = jsonDna.get("flag") != null;
5356 12 Apr 19 nicklas 694           if (preNormalizeDna)
5356 12 Apr 19 nicklas 695           {
5356 12 Apr 19 nicklas 696             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, dna, Dna.AUTO_PROCESSING_PRE_NORMALIZE);
5356 12 Apr 19 nicklas 697           }
5357 12 Apr 19 nicklas 698           else if (dnaIsFlagged)
5356 12 Apr 19 nicklas 699           {
5356 12 Apr 19 nicklas 700             Annotationtype.FLAG.setAnnotationValue(dc, dna, jsonDna.get("flag"));
5356 12 Apr 19 nicklas 701             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, dna, "Disable");
5357 12 Apr 19 nicklas 702             numFlaggedDNA++;
5357 12 Apr 19 nicklas 703             flaggedDna.add(dna);
5356 12 Apr 19 nicklas 704           }
5356 12 Apr 19 nicklas 705
1639 09 May 12 nicklas 706           Annotationtype.QIACUBE_DATE.setAnnotationValue(dc, dna, qiacubeDate);
1639 09 May 12 nicklas 707           Annotationtype.QIACUBE_RUN_NO.setAnnotationValue(dc, dna, qiacubeRunNo);
1639 09 May 12 nicklas 708           Annotationtype.QIACUBE_OPERATOR.setAnnotationValue(dc, dna, qiacubeOperator);
1639 09 May 12 nicklas 709           Annotationtype.QIACUBE_POSITION.setAnnotationValue(dc, dna, qiacubePosition);
1639 09 May 12 nicklas 710           Annotationtype.ND_CONC.setAnnotationValue(dc, dna, dnaNdConc);
1639 09 May 12 nicklas 711           Annotationtype.ND_260_BY_280.setAnnotationValue(dc, dna, jsonDna.get("nd260by280"));
1639 09 May 12 nicklas 712           Annotationtype.ND_260_BY_230.setAnnotationValue(dc, dna, jsonDna.get("nd260by230"));
2690 24 Sep 14 nicklas 713           // Reagents
2690 24 Sep 14 nicklas 714           Annotationtype.ALLPREP_MINI_KIT.setAnnotationValue(dc, dna, allPrepMiniKit);
2690 24 Sep 14 nicklas 715           Annotationtype.ETOH70.setAnnotationValue(dc, dna, etoh70);
2690 24 Sep 14 nicklas 716           Annotationtype.BUFFER_RW1.setAnnotationValue(dc, dna, bufferRw1);
2690 24 Sep 14 nicklas 717           Annotationtype.BUFFER_RPE.setAnnotationValue(dc, dna, bufferRpe);
2690 24 Sep 14 nicklas 718           Annotationtype.BUFFER_AW1.setAnnotationValue(dc, dna, bufferAw1);
2690 24 Sep 14 nicklas 719           Annotationtype.BUFFER_AW2.setAnnotationValue(dc, dna, bufferAw2);
2690 24 Sep 14 nicklas 720           Annotationtype.BUFFER_EB.setAnnotationValue(dc, dna, bufferEb);
2690 24 Sep 14 nicklas 721           Annotationtype.RNASE_FREE_WATER.setAnnotationValue(dc, dna, rnaseFreeWater);
2690 24 Sep 14 nicklas 722           Annotationtype.ALLPREP_DNA_COL.setAnnotationValue(dc, dna, allPrepDnaColumn);
2690 24 Sep 14 nicklas 723           Annotationtype.RNEASY_COL.setAnnotationValue(dc, dna, rneasyColumn);          
1639 09 May 12 nicklas 724           dc.saveItem(dna);
5357 12 Apr 19 nicklas 725           newDna.add(dna);
1639 09 May 12 nicklas 726           
1639 09 May 12 nicklas 727           // RNA
1639 09 May 12 nicklas 728           JSONObject jsonRna = (JSONObject)jsonLysate.get("rna");
1639 09 May 12 nicklas 729           Extract rna = Extract.getNew(dc);
1639 09 May 12 nicklas 730           rna.setItemSubtype(Subtype.RNA.load(dc));
4905 11 Jul 18 nicklas 731           rna.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.RNA, (String)jsonRna.get("name")));
1642 10 May 12 nicklas 732           rna.setDescription((String)jsonRna.get("comment"));
1639 09 May 12 nicklas 733           BioMaterialEvent rnaEvent = rna.getCreationEvent();
1639 09 May 12 nicklas 734           rnaEvent.setEventDate(qiacubeDate);
1639 09 May 12 nicklas 735           rnaEvent.setProtocol(rnaProtocol);
1639 09 May 12 nicklas 736           rnaEvent.setSource(lysate).setUsedQuantity(lysateProcessedQuantity / 3);
5791 16 Dec 19 nicklas 737           DoNotUse.copyDoNotUseAnnotations(dc, lysate, rna, false);
1639 09 May 12 nicklas 738           float rnaVolume = ((Number)jsonRna.get("volume")).floatValue();
1639 09 May 12 nicklas 739           float rnaNdConc = ((Number)jsonRna.get("ndConc")).floatValue();
3470 27 Aug 15 nicklas 740           rna.setOriginalQuantity(Math.max(0, rnaVolume * rnaNdConc / 1000));
1639 09 May 12 nicklas 741           rna.setBioWell(StoragePlate.getChildBioWell(dc, lysateWell, "Lys", "RNA"));
5356 12 Apr 19 nicklas 742           boolean preNormalizeRna = Boolean.TRUE.equals(jsonRna.get("preNormalize"));
5357 12 Apr 19 nicklas 743           boolean rnaIsflagged = jsonRna.get("flag") != null;
5356 12 Apr 19 nicklas 744           if (preNormalizeRna)
2900 05 Nov 14 nicklas 745           {
2902 06 Nov 14 nicklas 746             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rna, Rna.AUTO_PROCESSING_PRE_NORMALIZE);
2900 05 Nov 14 nicklas 747           }
5357 12 Apr 19 nicklas 748           else if (rnaIsflagged)
2901 05 Nov 14 nicklas 749           {
3040 15 Dec 14 nicklas 750             Annotationtype.FLAG.setAnnotationValue(dc, rna, jsonRna.get("flag"));
2901 05 Nov 14 nicklas 751             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rna, "Disable");
5357 12 Apr 19 nicklas 752             numFlaggedRNA++;
2901 05 Nov 14 nicklas 753             flaggedRna.add(rna);
2901 05 Nov 14 nicklas 754           }
5785 12 Dec 19 nicklas 755           else if (Reggie.isExternalItem(rna.getName()))
5785 12 Dec 19 nicklas 756           {
5785 12 Dec 19 nicklas 757             // External items are paused at this stage
5785 12 Dec 19 nicklas 758             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rna, "Disable");
5785 12 Dec 19 nicklas 759           }
1639 09 May 12 nicklas 760           Annotationtype.QIACUBE_DATE.setAnnotationValue(dc, rna, qiacubeDate);
1639 09 May 12 nicklas 761           Annotationtype.QIACUBE_RUN_NO.setAnnotationValue(dc, rna, qiacubeRunNo);
1639 09 May 12 nicklas 762           Annotationtype.QIACUBE_OPERATOR.setAnnotationValue(dc, rna, qiacubeOperator);
1639 09 May 12 nicklas 763           Annotationtype.QIACUBE_POSITION.setAnnotationValue(dc, rna, qiacubePosition);
1639 09 May 12 nicklas 764           Annotationtype.ND_CONC.setAnnotationValue(dc, rna, rnaNdConc);
1639 09 May 12 nicklas 765           Annotationtype.ND_260_BY_280.setAnnotationValue(dc, rna, jsonRna.get("nd260by280"));
1639 09 May 12 nicklas 766           Annotationtype.ND_260_BY_230.setAnnotationValue(dc, rna, jsonRna.get("nd260by230"));
2690 24 Sep 14 nicklas 767           // Reagents
2690 24 Sep 14 nicklas 768           Annotationtype.ALLPREP_MINI_KIT.setAnnotationValue(dc, rna, allPrepMiniKit);
2690 24 Sep 14 nicklas 769           Annotationtype.ETOH70.setAnnotationValue(dc, rna, etoh70);
2690 24 Sep 14 nicklas 770           Annotationtype.BUFFER_RW1.setAnnotationValue(dc, rna, bufferRw1);
2690 24 Sep 14 nicklas 771           Annotationtype.BUFFER_RPE.setAnnotationValue(dc, rna, bufferRpe);
2690 24 Sep 14 nicklas 772           Annotationtype.BUFFER_AW1.setAnnotationValue(dc, rna, bufferAw1);
2690 24 Sep 14 nicklas 773           Annotationtype.BUFFER_AW2.setAnnotationValue(dc, rna, bufferAw2);
2690 24 Sep 14 nicklas 774           Annotationtype.BUFFER_EB.setAnnotationValue(dc, rna, bufferEb);
2690 24 Sep 14 nicklas 775           Annotationtype.RNASE_FREE_WATER.setAnnotationValue(dc, rna, rnaseFreeWater);
2690 24 Sep 14 nicklas 776           Annotationtype.ALLPREP_DNA_COL.setAnnotationValue(dc, rna, allPrepDnaColumn);
2690 24 Sep 14 nicklas 777           Annotationtype.RNEASY_COL.setAnnotationValue(dc, rna, rneasyColumn);
1639 09 May 12 nicklas 778           dc.saveItem(rna);
1660 22 May 12 nicklas 779           newRna.add(rna);
1639 09 May 12 nicklas 780           
1639 09 May 12 nicklas 781           // FT
1639 09 May 12 nicklas 782           JSONObject jsonFt = (JSONObject)jsonLysate.get("ft");
1639 09 May 12 nicklas 783           Extract ft = Extract.getNew(dc);
1639 09 May 12 nicklas 784           ft.setItemSubtype(Subtype.FLOW_THROUGH.load(dc));
4905 11 Jul 18 nicklas 785           ft.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.FLOW_THROUGH, (String)jsonFt.get("name")));
1642 10 May 12 nicklas 786           ft.setDescription((String)jsonFt.get("comment"));
1639 09 May 12 nicklas 787           BioMaterialEvent ftEvent = ft.getCreationEvent();
1639 09 May 12 nicklas 788           ftEvent.setEventDate(qiacubeDate);
1639 09 May 12 nicklas 789           ftEvent.setProtocol(ftProtocol);
1639 09 May 12 nicklas 790           ftEvent.setSource(lysate).setUsedQuantity(lysateProcessedQuantity / 3);
5791 16 Dec 19 nicklas 791           DoNotUse.copyDoNotUseAnnotations(dc, lysate, ft, false);
1639 09 May 12 nicklas 792           ft.setOriginalQuantity(((Number)jsonFt.get("volume")).floatValue());
1639 09 May 12 nicklas 793           ft.setBioWell(StoragePlate.getChildBioWell(dc, lysateWell, "Lys", "FT"));
2095 23 Oct 13 nicklas 794           Annotationtype.QIACUBE_DATE.setAnnotationValue(dc, ft, qiacubeDate);
2095 23 Oct 13 nicklas 795           Annotationtype.QIACUBE_RUN_NO.setAnnotationValue(dc, ft, qiacubeRunNo);
2095 23 Oct 13 nicklas 796           Annotationtype.QIACUBE_OPERATOR.setAnnotationValue(dc, ft, qiacubeOperator);
2095 23 Oct 13 nicklas 797           Annotationtype.QIACUBE_POSITION.setAnnotationValue(dc, ft, qiacubePosition);
2690 24 Sep 14 nicklas 798           // Reagents
2690 24 Sep 14 nicklas 799           Annotationtype.ALLPREP_MINI_KIT.setAnnotationValue(dc, ft, allPrepMiniKit);
2690 24 Sep 14 nicklas 800           Annotationtype.ETOH70.setAnnotationValue(dc, ft, etoh70);
2690 24 Sep 14 nicklas 801           Annotationtype.BUFFER_RW1.setAnnotationValue(dc, ft, bufferRw1);
2690 24 Sep 14 nicklas 802           Annotationtype.BUFFER_RPE.setAnnotationValue(dc, ft, bufferRpe);
2690 24 Sep 14 nicklas 803           Annotationtype.BUFFER_AW1.setAnnotationValue(dc, ft, bufferAw1);
2690 24 Sep 14 nicklas 804           Annotationtype.BUFFER_AW2.setAnnotationValue(dc, ft, bufferAw2);
2690 24 Sep 14 nicklas 805           Annotationtype.BUFFER_EB.setAnnotationValue(dc, ft, bufferEb);
2690 24 Sep 14 nicklas 806           Annotationtype.RNASE_FREE_WATER.setAnnotationValue(dc, ft, rnaseFreeWater);
2690 24 Sep 14 nicklas 807           Annotationtype.ALLPREP_DNA_COL.setAnnotationValue(dc, ft, allPrepDnaColumn);
2690 24 Sep 14 nicklas 808           Annotationtype.RNEASY_COL.setAnnotationValue(dc, ft, rneasyColumn);
1639 09 May 12 nicklas 809           dc.saveItem(ft);
1639 09 May 12 nicklas 810           
3040 15 Dec 14 nicklas 811           String msg = "DNA, RNA and FlowThrough created from " + lysate.getName();
5356 12 Apr 19 nicklas 812           List<String> notes = new ArrayList<>();
5357 12 Apr 19 nicklas 813           if (rnaIsflagged || dnaIsFlagged)
3040 15 Dec 14 nicklas 814           {
5356 12 Apr 19 nicklas 815             notes.add("Flagged");
3040 15 Dec 14 nicklas 816           }
5356 12 Apr 19 nicklas 817           if (!preNormalizeRna)
3040 15 Dec 14 nicklas 818           {
5356 12 Apr 19 nicklas 819             notes.add("No RNA normalization");
3040 15 Dec 14 nicklas 820           }
5356 12 Apr 19 nicklas 821           if (!preNormalizeDna)
5356 12 Apr 19 nicklas 822           {
5356 12 Apr 19 nicklas 823             notes.add("No DNA normalization");
5356 12 Apr 19 nicklas 824           }
5356 12 Apr 19 nicklas 825           if (notes.size() > 0) msg += " (" + Values.getString(notes, "; ", true) +")";
3040 15 Dec 14 nicklas 826           jsonMessages.add(msg);
1639 09 May 12 nicklas 827         }
1639 09 May 12 nicklas 828         
5357 12 Apr 19 nicklas 829         if (numFlaggedRNA > 0)
2901 05 Nov 14 nicklas 830         {
5357 12 Apr 19 nicklas 831           jsonMessages.add(numFlaggedRNA + " RNA added to " + flaggedRna.getName() + " list");
2901 05 Nov 14 nicklas 832         }
5357 12 Apr 19 nicklas 833         if (numFlaggedDNA > 0)
5357 12 Apr 19 nicklas 834         {
5357 12 Apr 19 nicklas 835           jsonMessages.add(numFlaggedDNA + " DNA added to " + flaggedDna.getName() + " list");
5357 12 Apr 19 nicklas 836         }
5377 23 Apr 19 nicklas 837         if (jsonLysates.size() > 0)
5377 23 Apr 19 nicklas 838         {
5421 13 May 19 nicklas 839           ActivityDef.EXTRACTED_XNA.create(dc, jsonLysates.size());
5377 23 Apr 19 nicklas 840         }
4101 14 Sep 16 nicklas 841         dc.commit();
1660 22 May 12 nicklas 842         
5357 12 Apr 19 nicklas 843         JSONArray jsonCreatedRna = new JSONArray();
1660 22 May 12 nicklas 844         for (Extract rna : newRna)
1660 22 May 12 nicklas 845         {
1660 22 May 12 nicklas 846           jsonCreatedRna.add(rna.getId());
1660 22 May 12 nicklas 847         }
1660 22 May 12 nicklas 848         json.put("createdRna", jsonCreatedRna);
5357 12 Apr 19 nicklas 849         
5357 12 Apr 19 nicklas 850         JSONArray jsonCreatedDna = new JSONArray();
5357 12 Apr 19 nicklas 851         for (Extract dna : newDna)
5357 12 Apr 19 nicklas 852         {
5357 12 Apr 19 nicklas 853           jsonCreatedDna.add(dna.getId());
5357 12 Apr 19 nicklas 854         }
5357 12 Apr 19 nicklas 855         json.put("createdDna", jsonCreatedDna);
1639 09 May 12 nicklas 856       }
2903 06 Nov 14 nicklas 857       else if ("RegisterNormalizedRNA".equals(cmd))
2903 06 Nov 14 nicklas 858       {
6330 14 Jun 21 nicklas 859         dc = sc.newDbControl(":RNA normalization wizard");
2903 06 Nov 14 nicklas 860
2903 06 Nov 14 nicklas 861         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
2903 06 Nov 14 nicklas 862
3752 17 Feb 16 nicklas 863         JSONObject jsonReq = JsonUtil.parseRequest(req);
2903 06 Nov 14 nicklas 864         JSONArray jsonRnaAliquots = (JSONArray)jsonReq.get("rnaAliquots");
2903 06 Nov 14 nicklas 865         
2903 06 Nov 14 nicklas 866         ItemSubtype subtypeNormalized = Subtype.RNA_NORMALIZED_ALIQUOT.load(dc);
2903 06 Nov 14 nicklas 867         
2903 06 Nov 14 nicklas 868         Date normalizationDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("normalizationDate"));
2903 06 Nov 14 nicklas 869         String operator = (String)jsonReq.get("normalizationOperator");
3269 23 Apr 15 nicklas 870         Number normalizationProtocolId = (Number)jsonReq.get("normalizationProtocol");
3269 23 Apr 15 nicklas 871         Protocol normalizationProtocol = normalizationProtocolId != null ? Protocol.getById(dc, normalizationProtocolId.intValue()) : null;
2903 06 Nov 14 nicklas 872
3269 23 Apr 15 nicklas 873         ItemList flaggedRna = BiomaterialList.FLAGGED_RNA.load(dc);
3269 23 Apr 15 nicklas 874         int numFlagged = 0;
5377 23 Apr 19 nicklas 875         int numNormalized = 0;
2903 06 Nov 14 nicklas 876         for (int i = 0; i < jsonRnaAliquots.size(); ++i)
2903 06 Nov 14 nicklas 877         {
2903 06 Nov 14 nicklas 878           JSONObject jsonRnaAliquot = (JSONObject)jsonRnaAliquots.get(i);
2903 06 Nov 14 nicklas 879           
3269 23 Apr 15 nicklas 880           String flag = (String)jsonRnaAliquot.get("flag");
3269 23 Apr 15 nicklas 881           if (flag != null)
3269 23 Apr 15 nicklas 882           {
3269 23 Apr 15 nicklas 883             // Flag the RNA
3269 23 Apr 15 nicklas 884             Number rnaId = (Number)jsonRnaAliquot.get("id");
3269 23 Apr 15 nicklas 885             Extract rna = Extract.getById(dc, rnaId.intValue());
3269 23 Apr 15 nicklas 886             
3269 23 Apr 15 nicklas 887             Annotationtype.FLAG.setAnnotationValue(dc, rna, flag);
3269 23 Apr 15 nicklas 888             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rna, "Disable");
3269 23 Apr 15 nicklas 889             numFlagged++;
3269 23 Apr 15 nicklas 890             flaggedRna.add(rna);
3269 23 Apr 15 nicklas 891             jsonMessages.add("Flagged RNA: " + rna.getName());
3269 23 Apr 15 nicklas 892           }
3269 23 Apr 15 nicklas 893           else
3269 23 Apr 15 nicklas 894           {
3269 23 Apr 15 nicklas 895             // Get the RNA extract which is the parent
3269 23 Apr 15 nicklas 896             Number parentRnaId = (Number)jsonRnaAliquot.get("parentId");
3269 23 Apr 15 nicklas 897             Extract parentRna = Extract.getById(dc, parentRnaId.intValue());
3269 23 Apr 15 nicklas 898             
3269 23 Apr 15 nicklas 899             // Disable further processing of parent RNA
3269 23 Apr 15 nicklas 900             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, parentRna, "Disable");
3269 23 Apr 15 nicklas 901             
5785 12 Dec 19 nicklas 902             // Create a child RNANormalizedAliquot extract
3269 23 Apr 15 nicklas 903             Extract normalizedRna = Extract.getNew(dc);
3269 23 Apr 15 nicklas 904             normalizedRna.setItemSubtype(subtypeNormalized);
4905 11 Jul 18 nicklas 905             normalizedRna.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.RNA_NORMALIZED_ALIQUOT, (String)jsonRnaAliquot.get("name")));
3269 23 Apr 15 nicklas 906             
3269 23 Apr 15 nicklas 907             Annotationtype.DILUTION_DATE.setAnnotationValue(dc, normalizedRna, normalizationDate);
3269 23 Apr 15 nicklas 908             Annotationtype.DILUTION_OPERATOR.setAnnotationValue(dc, normalizedRna, operator);
3269 23 Apr 15 nicklas 909   
5785 12 Dec 19 nicklas 910             if (Reggie.isExternalItem(normalizedRna.getName())) 
5785 12 Dec 19 nicklas 911             {
5785 12 Dec 19 nicklas 912               // External items are paused at this stage
5785 12 Dec 19 nicklas 913               Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, normalizedRna, "Disable");
5785 12 Dec 19 nicklas 914             }
5785 12 Dec 19 nicklas 915             
3269 23 Apr 15 nicklas 916             BioMaterialEvent creationEvent = normalizedRna.getCreationEvent();
3269 23 Apr 15 nicklas 917             creationEvent.setEventDate(normalizationDate);
3269 23 Apr 15 nicklas 918             creationEvent.setProtocol(normalizationProtocol);
3269 23 Apr 15 nicklas 919   
3269 23 Apr 15 nicklas 920             Number usedQuantity = (Number)jsonRnaAliquot.get("usedQuantity");
3269 23 Apr 15 nicklas 921             normalizedRna.setOriginalQuantity(usedQuantity.floatValue());
3269 23 Apr 15 nicklas 922             BioMaterialEventSource evtSrc = creationEvent.setSource(parentRna);
5791 16 Dec 19 nicklas 923             DoNotUse.copyDoNotUseAnnotations(dc, parentRna, normalizedRna, false);
3269 23 Apr 15 nicklas 924             evtSrc.setUsedQuantity(usedQuantity.floatValue());
3269 23 Apr 15 nicklas 925             
6219 20 Apr 21 nicklas 926             Number conc = (Number)jsonRnaAliquot.get("conc");
6219 20 Apr 21 nicklas 927             Annotationtype concType = Annotationtype.getByCName((String)jsonRnaAliquot.get("ConcType"));
6219 20 Apr 21 nicklas 928             concType.setAnnotationValue(dc, normalizedRna, conc.floatValue());
3269 23 Apr 15 nicklas 929             
5377 23 Apr 19 nicklas 930             numNormalized++;
3831 06 Apr 16 nicklas 931             jsonMessages.add("Created '" + normalizedRna.getName() + "' normalized RNA (" + Values.formatNumber(usedQuantity.floatValue()*1000, 0, "ng") + ")");
3269 23 Apr 15 nicklas 932             dc.saveItem(normalizedRna);
3269 23 Apr 15 nicklas 933           }
2903 06 Nov 14 nicklas 934         }
2903 06 Nov 14 nicklas 935         
3269 23 Apr 15 nicklas 936         if (numFlagged > 0)
3269 23 Apr 15 nicklas 937         {
3269 23 Apr 15 nicklas 938           jsonMessages.add(numFlagged + " RNA added to " + flaggedRna.getName() + " list");
3269 23 Apr 15 nicklas 939         }
3269 23 Apr 15 nicklas 940         
5377 23 Apr 19 nicklas 941         if (numNormalized > 0)
5377 23 Apr 19 nicklas 942         {
5421 13 May 19 nicklas 943           ActivityDef.NORMALIZED_RNA.merge(dc, numNormalized);
5377 23 Apr 19 nicklas 944         }
2903 06 Nov 14 nicklas 945         dc.commit();
2903 06 Nov 14 nicklas 946       }
5362 15 Apr 19 nicklas 947       else if ("RegisterNormalizedDNA".equals(cmd))
5362 15 Apr 19 nicklas 948       {
6330 14 Jun 21 nicklas 949         dc = sc.newDbControl(":DNA normalization wizard");
5362 15 Apr 19 nicklas 950
5362 15 Apr 19 nicklas 951         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
5362 15 Apr 19 nicklas 952
5362 15 Apr 19 nicklas 953         JSONObject jsonReq = JsonUtil.parseRequest(req);
5362 15 Apr 19 nicklas 954         JSONArray jsonDnaAliquots = (JSONArray)jsonReq.get("dnaAliquots");
5362 15 Apr 19 nicklas 955         
5362 15 Apr 19 nicklas 956         ItemSubtype subtypeNormalized = Subtype.DNA_NORMALIZED_ALIQUOT.load(dc);
5362 15 Apr 19 nicklas 957         
5362 15 Apr 19 nicklas 958         Date normalizationDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("normalizationDate"));
5362 15 Apr 19 nicklas 959         String operator = (String)jsonReq.get("normalizationOperator");
5362 15 Apr 19 nicklas 960         Number normalizationProtocolId = (Number)jsonReq.get("normalizationProtocol");
5362 15 Apr 19 nicklas 961         Protocol normalizationProtocol = normalizationProtocolId != null ? Protocol.getById(dc, normalizationProtocolId.intValue()) : null;
5601 13 Sep 19 nicklas 962         String mipsPanel = (String)Annotationtype.MIPS_PANEL.getAnnotationValue(dc, normalizationProtocol);
5362 15 Apr 19 nicklas 963         Number normalizedPlateId = (Number)jsonReq.get("normalizedPlate");
5362 15 Apr 19 nicklas 964         BioPlate normalizedPlate;
5362 15 Apr 19 nicklas 965         if (normalizedPlateId != null && normalizedPlateId.intValue() > 0)
5362 15 Apr 19 nicklas 966         {
5362 15 Apr 19 nicklas 967           normalizedPlate = BioPlate.getById(dc, normalizedPlateId.intValue());
5362 15 Apr 19 nicklas 968           jsonMessages.add("Use existing plate: " + normalizedPlate.getName());
5362 15 Apr 19 nicklas 969         }
5362 15 Apr 19 nicklas 970         else
5362 15 Apr 19 nicklas 971         {
5362 15 Apr 19 nicklas 972           normalizedPlate = ReactionPlate.getNew(dc, BioplateType.PRE_NORMALIZED_DNA).getBioPlate();
5362 15 Apr 19 nicklas 973           normalizedPlate.setName(BioplateType.PRE_NORMALIZED_DNA.getNextPlateName(dc, true));
5456 29 May 19 nicklas 974           normalizedPlate.setEventDate(normalizationDate);
5407 08 May 19 nicklas 975           Annotationtype.PLATE_PROCESSING.setAnnotationValue(dc, normalizedPlate, "AddAliquots");
5407 08 May 19 nicklas 976           
5362 15 Apr 19 nicklas 977           dc.saveItem(normalizedPlate);
5362 15 Apr 19 nicklas 978           jsonMessages.add("Created new plate: " + normalizedPlate.getName());
5362 15 Apr 19 nicklas 979         }
5362 15 Apr 19 nicklas 980
5362 15 Apr 19 nicklas 981         ItemList flaggedDna = BiomaterialList.FLAGGED_DNA.load(dc);
5362 15 Apr 19 nicklas 982         int numFlagged = 0;
5377 23 Apr 19 nicklas 983         int numNormalized = 0;
5362 15 Apr 19 nicklas 984         for (int i = 0; i < jsonDnaAliquots.size(); ++i)
5362 15 Apr 19 nicklas 985         {
5362 15 Apr 19 nicklas 986           JSONObject jsonDnaAliquot = (JSONObject)jsonDnaAliquots.get(i);
5362 15 Apr 19 nicklas 987           
5362 15 Apr 19 nicklas 988           String flag = (String)jsonDnaAliquot.get("flag");
5362 15 Apr 19 nicklas 989           if (flag != null)
5362 15 Apr 19 nicklas 990           {
5362 15 Apr 19 nicklas 991             // Flag the DNA
5362 15 Apr 19 nicklas 992             Number dnaId = (Number)jsonDnaAliquot.get("id");
5362 15 Apr 19 nicklas 993             Extract dna = Extract.getById(dc, dnaId.intValue());
5362 15 Apr 19 nicklas 994             
5362 15 Apr 19 nicklas 995             Annotationtype.FLAG.setAnnotationValue(dc, dna, flag);
5362 15 Apr 19 nicklas 996             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, dna, "Disable");
5362 15 Apr 19 nicklas 997             numFlagged++;
5362 15 Apr 19 nicklas 998             flaggedDna.add(dna);
5362 15 Apr 19 nicklas 999             jsonMessages.add("Flagged DNA: " + dna.getName());
5362 15 Apr 19 nicklas 1000           }
5362 15 Apr 19 nicklas 1001           else
5362 15 Apr 19 nicklas 1002           {
5362 15 Apr 19 nicklas 1003             // Get the DNA extract which is the parent
5362 15 Apr 19 nicklas 1004             Number parentDnaId = (Number)jsonDnaAliquot.get("parentId");
5362 15 Apr 19 nicklas 1005             Extract parentDna = Extract.getById(dc, parentDnaId.intValue());
5362 15 Apr 19 nicklas 1006             
5362 15 Apr 19 nicklas 1007             // Disable further processing of parent DNA
5362 15 Apr 19 nicklas 1008             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, parentDna, "Disable");
5362 15 Apr 19 nicklas 1009             
5362 15 Apr 19 nicklas 1010             // Create a child normalized extract
5362 15 Apr 19 nicklas 1011             Extract normalizedDna = Extract.getNew(dc);
5362 15 Apr 19 nicklas 1012             normalizedDna.setItemSubtype(subtypeNormalized);
5362 15 Apr 19 nicklas 1013             normalizedDna.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.DNA_NORMALIZED_ALIQUOT, (String)jsonDnaAliquot.get("name")));
5362 15 Apr 19 nicklas 1014             
5362 15 Apr 19 nicklas 1015             Annotationtype.DILUTION_DATE.setAnnotationValue(dc, normalizedDna, normalizationDate);
5362 15 Apr 19 nicklas 1016             Annotationtype.DILUTION_OPERATOR.setAnnotationValue(dc, normalizedDna, operator);
5601 13 Sep 19 nicklas 1017             Annotationtype.MIPS_PANEL.setAnnotationValue(dc, normalizedDna, mipsPanel);
5362 15 Apr 19 nicklas 1018   
5362 15 Apr 19 nicklas 1019             BioMaterialEvent creationEvent = normalizedDna.getCreationEvent();
5362 15 Apr 19 nicklas 1020             creationEvent.setEventDate(normalizationDate);
5362 15 Apr 19 nicklas 1021             creationEvent.setProtocol(normalizationProtocol);
5362 15 Apr 19 nicklas 1022   
5362 15 Apr 19 nicklas 1023             Number usedQuantity = (Number)jsonDnaAliquot.get("usedQuantity");
5362 15 Apr 19 nicklas 1024             normalizedDna.setOriginalQuantity(usedQuantity.floatValue());
5362 15 Apr 19 nicklas 1025             BioMaterialEventSource evtSrc = creationEvent.setSource(parentDna);
5791 16 Dec 19 nicklas 1026             DoNotUse.copyDoNotUseAnnotations(dc, parentDna, normalizedDna, false);
5362 15 Apr 19 nicklas 1027             evtSrc.setUsedQuantity(usedQuantity.floatValue());
5362 15 Apr 19 nicklas 1028             
6219 20 Apr 21 nicklas 1029             Number conc = (Number)jsonDnaAliquot.get("conc");
6219 20 Apr 21 nicklas 1030             Annotationtype concType = Annotationtype.getByCName((String)jsonDnaAliquot.get("ConcType"));
6219 20 Apr 21 nicklas 1031             concType.setAnnotationValue(dc, normalizedDna, conc.floatValue());
5362 15 Apr 19 nicklas 1032             
5362 15 Apr 19 nicklas 1033             Number row = (Number)jsonDnaAliquot.get("row");
5362 15 Apr 19 nicklas 1034             Number column = (Number)jsonDnaAliquot.get("column");
5362 15 Apr 19 nicklas 1035             BioWell well = normalizedPlate.getBioWell(row.intValue(), column.intValue());
5362 15 Apr 19 nicklas 1036             normalizedDna.setBioWell(well);
5362 15 Apr 19 nicklas 1037             
5377 23 Apr 19 nicklas 1038             numNormalized++;
5362 15 Apr 19 nicklas 1039             jsonMessages.add("Created '" + normalizedDna.getName() + "' normalized DNA (" + 
5362 15 Apr 19 nicklas 1040                 Values.formatNumber(usedQuantity.floatValue()*1000, 0, "ng") + "): " + well.getCoordinate());
5362 15 Apr 19 nicklas 1041             dc.saveItem(normalizedDna);
5362 15 Apr 19 nicklas 1042           }
5362 15 Apr 19 nicklas 1043         }
5362 15 Apr 19 nicklas 1044         
5362 15 Apr 19 nicklas 1045         if (numFlagged > 0)
5362 15 Apr 19 nicklas 1046         {
5362 15 Apr 19 nicklas 1047           jsonMessages.add(numFlagged + " DNA added to " + flaggedDna.getName() + " list");
5362 15 Apr 19 nicklas 1048         }
5377 23 Apr 19 nicklas 1049         if (numNormalized > 0)
5377 23 Apr 19 nicklas 1050         {
5421 13 May 19 nicklas 1051           ActivityDef.NORMALIZED_DNA.merge(dc, numNormalized);
5377 23 Apr 19 nicklas 1052         }
5362 15 Apr 19 nicklas 1053         
5362 15 Apr 19 nicklas 1054         dc.commit();
5362 15 Apr 19 nicklas 1055       }
4092 09 Sep 16 nicklas 1056       else if ("RegisterLysatesPickedForReextraction".equals(cmd))
4092 09 Sep 16 nicklas 1057       {
6330 14 Jun 21 nicklas 1058         dc = sc.newDbControl(":Pick lysate for re-extraction");
4092 09 Sep 16 nicklas 1059         
4092 09 Sep 16 nicklas 1060         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
4092 09 Sep 16 nicklas 1061         
4092 09 Sep 16 nicklas 1062         JSONObject jsonReq = JsonUtil.parseRequest(req);
4092 09 Sep 16 nicklas 1063         JSONArray jsonLysates = (JSONArray)jsonReq.get("lysates");
2903 06 Nov 14 nicklas 1064
4092 09 Sep 16 nicklas 1065         // Create a MOVE bioplate event to represent the picking
4092 09 Sep 16 nicklas 1066         BioPlateEventType moveType = BioPlateEventType.getById(dc, BioPlateEventType.MOVE);
4092 09 Sep 16 nicklas 1067         BioPlateEvent pickEvent = BioPlateEvent.getNew(dc, moveType);
4092 09 Sep 16 nicklas 1068         pickEvent.setName("Pick lysates for re-extraction");
4092 09 Sep 16 nicklas 1069         pickEvent.setEventDate(new Date());
4092 09 Sep 16 nicklas 1070         dc.saveItem(pickEvent);
4092 09 Sep 16 nicklas 1071         
4092 09 Sep 16 nicklas 1072         int numOldPlates = 0;
4092 09 Sep 16 nicklas 1073         int numNewPlates = 0;
4092 09 Sep 16 nicklas 1074         for (int i = 0; i < jsonLysates.size(); ++i)
4092 09 Sep 16 nicklas 1075         {
4092 09 Sep 16 nicklas 1076           JSONObject jsonLysate = (JSONObject)jsonLysates.get(i);
4092 09 Sep 16 nicklas 1077           Number lysateId = (Number)jsonLysate.get("id");
4092 09 Sep 16 nicklas 1078           Number newWellId = (Number)jsonLysate.get("newWell");
4092 09 Sep 16 nicklas 1079           
4092 09 Sep 16 nicklas 1080           Extract lysate = Extract.getById(dc, lysateId.intValue());
4092 09 Sep 16 nicklas 1081
4092 09 Sep 16 nicklas 1082           BioWell oldWell = lysate.getBioWell();
4092 09 Sep 16 nicklas 1083           BioPlate oldPlate = oldWell.getPlate();
4092 09 Sep 16 nicklas 1084           BioPlateEventParticipant oldParticipant = pickEvent.getParticipant(oldPlate);
4092 09 Sep 16 nicklas 1085           if (oldParticipant == null)
4092 09 Sep 16 nicklas 1086           {
4092 09 Sep 16 nicklas 1087             numOldPlates++;
4092 09 Sep 16 nicklas 1088             oldParticipant = pickEvent.addParticipant(oldPlate, "source", numOldPlates);
4092 09 Sep 16 nicklas 1089           }
4092 09 Sep 16 nicklas 1090
4092 09 Sep 16 nicklas 1091           BioWell newWell = BioWell.getById(dc, newWellId.intValue());
4092 09 Sep 16 nicklas 1092           BioPlate newPlate = newWell.getPlate();
4092 09 Sep 16 nicklas 1093           BioPlateEventParticipant newParticipant = pickEvent.getParticipant(newPlate);
4092 09 Sep 16 nicklas 1094           if (newParticipant == null)
4092 09 Sep 16 nicklas 1095           {
4092 09 Sep 16 nicklas 1096             numNewPlates++;
4092 09 Sep 16 nicklas 1097             newParticipant = pickEvent.addParticipant(newPlate, "destination", numNewPlates);
4092 09 Sep 16 nicklas 1098           }
4092 09 Sep 16 nicklas 1099           
4092 09 Sep 16 nicklas 1100           BioMaterialEvent moveEvent = lysate.newEvent(newParticipant);
4092 09 Sep 16 nicklas 1101           moveEvent.setComment("Moved from " + oldPlate.getName() + " [" + oldWell.getCoordinate() + 
4092 09 Sep 16 nicklas 1102             "] to " + newPlate.getName() + " [" + newWell.getCoordinate() + "] for re-extraction");
4092 09 Sep 16 nicklas 1103           dc.saveItem(moveEvent);
4092 09 Sep 16 nicklas 1104           
4092 09 Sep 16 nicklas 1105           lysate.setBioWell(newWell);
4100 14 Sep 16 nicklas 1106           Annotationtype.RESERVED_BY.setAnnotationValue(dc, lysate, null);
4092 09 Sep 16 nicklas 1107           jsonMessages.add(lysate.getName() + " " + moveEvent.getComment());
4092 09 Sep 16 nicklas 1108         }
4092 09 Sep 16 nicklas 1109         
4092 09 Sep 16 nicklas 1110         dc.commit();
4092 09 Sep 16 nicklas 1111       }
4092 09 Sep 16 nicklas 1112
1637 07 May 12 nicklas 1113       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 1114       CounterService.getInstance().setForceCount();      
1637 07 May 12 nicklas 1115     }
1637 07 May 12 nicklas 1116     catch (Throwable t)
1637 07 May 12 nicklas 1117     {
1637 07 May 12 nicklas 1118       t.printStackTrace();
1637 07 May 12 nicklas 1119       json.clear();
1637 07 May 12 nicklas 1120       json.put("status", "error");
1637 07 May 12 nicklas 1121       json.put("message", t.getMessage());
1637 07 May 12 nicklas 1122       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1637 07 May 12 nicklas 1123     }
1637 07 May 12 nicklas 1124     finally
1637 07 May 12 nicklas 1125     {
1637 07 May 12 nicklas 1126       if (dc != null) dc.close();
1637 07 May 12 nicklas 1127       json.writeJSONString(resp.getWriter());
1637 07 May 12 nicklas 1128     }
1637 07 May 12 nicklas 1129     
1637 07 May 12 nicklas 1130   }
1637 07 May 12 nicklas 1131   
4097 13 Sep 16 nicklas 1132   private JSONObject getChildNames(DbControl dc, Lysate lysate)
4097 13 Sep 16 nicklas 1133   {
4905 11 Jul 18 nicklas 1134     String rna = lysate.getNextChildName(dc, Subtype.RNA);
4905 11 Jul 18 nicklas 1135     String dna = lysate.getNextChildName(dc, Subtype.DNA);
4905 11 Jul 18 nicklas 1136     String ft = lysate.getNextChildName(dc, Subtype.FLOW_THROUGH);
4097 13 Sep 16 nicklas 1137     JSONObject jsonNames = new JSONObject();
4097 13 Sep 16 nicklas 1138     jsonNames.put("rna", rna);
4097 13 Sep 16 nicklas 1139     jsonNames.put("preNormalizedRna", rna + ".n");
4097 13 Sep 16 nicklas 1140     jsonNames.put("dna", dna);
5359 12 Apr 19 nicklas 1141     jsonNames.put("preNormalizedDna", dna + ".n");
4097 13 Sep 16 nicklas 1142     jsonNames.put("ft", ft);
4097 13 Sep 16 nicklas 1143     return jsonNames;
4097 13 Sep 16 nicklas 1144   }
1637 07 May 12 nicklas 1145 }