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

Code
Comments
Other
Rev Date Author Line
1742 23 Nov 12 nicklas 1 package net.sf.basedb.reggie.servlet;
1742 23 Nov 12 nicklas 2
1742 23 Nov 12 nicklas 3 import java.io.IOException;
3659 09 Dec 15 nicklas 4 import java.util.ArrayList;
3764 22 Feb 16 nicklas 5 import java.util.Collections;
1758 30 Nov 12 nicklas 6 import java.util.Date;
4885 04 Jul 18 nicklas 7 import java.util.HashSet;
1742 23 Nov 12 nicklas 8 import java.util.List;
4885 04 Jul 18 nicklas 9 import java.util.Set;
1742 23 Nov 12 nicklas 10
1742 23 Nov 12 nicklas 11 import javax.servlet.ServletException;
1742 23 Nov 12 nicklas 12 import javax.servlet.http.HttpServlet;
1742 23 Nov 12 nicklas 13 import javax.servlet.http.HttpServletRequest;
1742 23 Nov 12 nicklas 14 import javax.servlet.http.HttpServletResponse;
1742 23 Nov 12 nicklas 15
1742 23 Nov 12 nicklas 16 import org.json.simple.JSONArray;
1742 23 Nov 12 nicklas 17 import org.json.simple.JSONObject;
1742 23 Nov 12 nicklas 18
1758 30 Nov 12 nicklas 19 import net.sf.basedb.core.AnyToAny;
1758 30 Nov 12 nicklas 20 import net.sf.basedb.core.BioMaterialEvent;
1742 23 Nov 12 nicklas 21 import net.sf.basedb.core.BioMaterialEventSource;
3247 14 Apr 15 nicklas 22 import net.sf.basedb.core.ItemList;
1742 23 Nov 12 nicklas 23 import net.sf.basedb.core.BioPlate;
1742 23 Nov 12 nicklas 24 import net.sf.basedb.core.BioPlateEvent;
1742 23 Nov 12 nicklas 25 import net.sf.basedb.core.BioPlateEventParticipant;
1742 23 Nov 12 nicklas 26 import net.sf.basedb.core.BioPlateEventType;
1742 23 Nov 12 nicklas 27 import net.sf.basedb.core.BioWell;
1742 23 Nov 12 nicklas 28 import net.sf.basedb.core.DbControl;
1742 23 Nov 12 nicklas 29 import net.sf.basedb.core.Extract;
1758 30 Nov 12 nicklas 30 import net.sf.basedb.core.File;
1758 30 Nov 12 nicklas 31 import net.sf.basedb.core.InvalidDataException;
3659 09 Dec 15 nicklas 32 import net.sf.basedb.core.Item;
1742 23 Nov 12 nicklas 33 import net.sf.basedb.core.ItemQuery;
1742 23 Nov 12 nicklas 34 import net.sf.basedb.core.ItemSubtype;
2666 15 Sep 14 nicklas 35 import net.sf.basedb.core.Path;
1742 23 Nov 12 nicklas 36 import net.sf.basedb.core.PlateGeometry;
3645 03 Dec 15 nicklas 37 import net.sf.basedb.core.Project;
1758 30 Nov 12 nicklas 38 import net.sf.basedb.core.Protocol;
1742 23 Nov 12 nicklas 39 import net.sf.basedb.core.SessionControl;
3302 04 May 15 nicklas 40 import net.sf.basedb.core.Tag;
1742 23 Nov 12 nicklas 41 import net.sf.basedb.core.Type;
1803 18 Jan 13 nicklas 42 import net.sf.basedb.core.query.Annotations;
1742 23 Nov 12 nicklas 43 import net.sf.basedb.core.query.Expressions;
1742 23 Nov 12 nicklas 44 import net.sf.basedb.core.query.Hql;
1742 23 Nov 12 nicklas 45 import net.sf.basedb.core.query.Orders;
1742 23 Nov 12 nicklas 46 import net.sf.basedb.core.query.Restrictions;
3166 05 Mar 15 nicklas 47 import net.sf.basedb.core.snapshot.SnapshotManager;
1940 18 Apr 13 nicklas 48 import net.sf.basedb.reggie.JsonUtil;
1742 23 Nov 12 nicklas 49 import net.sf.basedb.reggie.Reggie;
5384 26 Apr 19 nicklas 50 import net.sf.basedb.reggie.activity.ActivityDef;
3059 19 Dec 14 nicklas 51 import net.sf.basedb.reggie.counter.CounterService;
1742 23 Nov 12 nicklas 52 import net.sf.basedb.reggie.dao.Annotationtype;
1803 18 Jan 13 nicklas 53 import net.sf.basedb.reggie.dao.BiomaterialList;
1742 23 Nov 12 nicklas 54 import net.sf.basedb.reggie.dao.BioplateType;
2906 07 Nov 14 nicklas 55 import net.sf.basedb.reggie.dao.CDna;
5791 16 Dec 19 nicklas 56 import net.sf.basedb.reggie.dao.DoNotUse;
1745 27 Nov 12 nicklas 57 import net.sf.basedb.reggie.dao.MRna;
5436 17 May 19 nicklas 58 import net.sf.basedb.reggie.dao.Pipeline;
1758 30 Nov 12 nicklas 59 import net.sf.basedb.reggie.dao.ReactionPlate;
3764 22 Feb 16 nicklas 60 import net.sf.basedb.reggie.dao.ReggieItem;
2161 09 Dec 13 nicklas 61 import net.sf.basedb.reggie.dao.ReggieRole;
1742 23 Nov 12 nicklas 62 import net.sf.basedb.reggie.dao.Rna;
3651 07 Dec 15 nicklas 63 import net.sf.basedb.reggie.dao.Rna.RnaFilterOptions;
1742 23 Nov 12 nicklas 64 import net.sf.basedb.reggie.dao.Subtype;
1742 23 Nov 12 nicklas 65 import net.sf.basedb.util.Values;
1742 23 Nov 12 nicklas 66 import net.sf.basedb.util.error.ThrowableUtil;
1742 23 Nov 12 nicklas 67
1742 23 Nov 12 nicklas 68
1742 23 Nov 12 nicklas 69 public class MRnaServlet 
1742 23 Nov 12 nicklas 70   extends HttpServlet 
1742 23 Nov 12 nicklas 71 {
1742 23 Nov 12 nicklas 72
1742 23 Nov 12 nicklas 73   private static final long serialVersionUID = -8104884012462693394L;
1742 23 Nov 12 nicklas 74
1742 23 Nov 12 nicklas 75   public MRnaServlet()
1742 23 Nov 12 nicklas 76   {}
1742 23 Nov 12 nicklas 77
1742 23 Nov 12 nicklas 78   @Override
1742 23 Nov 12 nicklas 79   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1742 23 Nov 12 nicklas 80     throws ServletException, IOException 
1742 23 Nov 12 nicklas 81   {
1742 23 Nov 12 nicklas 82     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 83     JsonUtil.setJsonResponseHeaders(resp);
1742 23 Nov 12 nicklas 84     
1742 23 Nov 12 nicklas 85     JSONObject json = new JSONObject();
1742 23 Nov 12 nicklas 86     json.put("status", "ok");
1745 27 Nov 12 nicklas 87     
3975 26 May 16 nicklas 88     final SessionControl sc = Reggie.getSessionControl(req);
1742 23 Nov 12 nicklas 89     DbControl dc = null;
1742 23 Nov 12 nicklas 90     try
1742 23 Nov 12 nicklas 91     {
1742 23 Nov 12 nicklas 92       if ("GetNextAutoGeneratedPlateName".equals(cmd))
1742 23 Nov 12 nicklas 93       {
1742 23 Nov 12 nicklas 94         /*
1899 07 Mar 13 nicklas 95           Eg. MRnaPlate0001, MRnaPlate0002 and so on
1742 23 Nov 12 nicklas 96          */
1742 23 Nov 12 nicklas 97         BioplateType bioPlateType = BioplateType.getByCName(req.getParameter("bioPlateType"));
6333 15 Jun 21 nicklas 98         dc = sc.newDbControl(":Create mRNA or Library plate");
4888 05 Jul 18 nicklas 99         json.put("name", bioPlateType.getNextPlateName(dc, false));
1742 23 Nov 12 nicklas 100       }
1938 17 Apr 13 nicklas 101       else if ("GetStratagene".equals(cmd))
1938 17 Apr 13 nicklas 102       {
6333 15 Jun 21 nicklas 103         dc = sc.newDbControl(":Create mRNA or Library plate");
1938 17 Apr 13 nicklas 104         List<Rna> stratagene = Rna.findStratagene(dc);
1938 17 Apr 13 nicklas 105         
1938 17 Apr 13 nicklas 106         JSONArray jsonStratagene = new JSONArray();
1938 17 Apr 13 nicklas 107         for (Rna s : stratagene)
1938 17 Apr 13 nicklas 108         {
1938 17 Apr 13 nicklas 109           s.loadBioPlateLocation();
1938 17 Apr 13 nicklas 110           jsonStratagene.add(s.asJSONObject());
1938 17 Apr 13 nicklas 111         }
1938 17 Apr 13 nicklas 112         json.put("stratagene", jsonStratagene);
1938 17 Apr 13 nicklas 113         
1938 17 Apr 13 nicklas 114       }
1762 03 Dec 12 nicklas 115       else if ("GetUnprocessedPlates".equals(cmd))
1745 27 Nov 12 nicklas 116       {
6333 15 Jun 21 nicklas 117         dc = sc.newDbControl(":mRNA or cDNA registration");
1762 03 Dec 12 nicklas 118         BioplateType plateType = BioplateType.getByCName(req.getParameter("plateType"));
1745 27 Nov 12 nicklas 119         
1890 01 Mar 13 nicklas 120         // Find plates of the given 'plate type' without 'creation date' and no 'destroyed' flag set
1745 27 Nov 12 nicklas 121         ItemQuery<BioPlate> query = BioPlate.getQuery();
1745 27 Nov 12 nicklas 122         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1745 27 Nov 12 nicklas 123         query.restrict(Restrictions.eq(Hql.property("eventDate"), null));
1762 03 Dec 12 nicklas 124         plateType.addFilter(dc, query, true);
2509 16 Jun 14 nicklas 125         
2509 16 Jun 14 nicklas 126         if (req.getParameter("onlyDiluted") != null)
2509 16 Jun 14 nicklas 127         {
2906 07 Nov 14 nicklas 128           // Must have a DILUTION_DATE annotation OR AUTO_PROCESSING = PreNormalizeRNA
2509 16 Jun 14 nicklas 129           query.join(Annotations.leftJoin(null, Annotationtype.DILUTION_DATE.load(dc), "dd"));
2906 07 Nov 14 nicklas 130           query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
2906 07 Nov 14 nicklas 131           query.restrict(
2906 07 Nov 14 nicklas 132             Restrictions.or(
2906 07 Nov 14 nicklas 133               Restrictions.neq(Hql.alias("dd"), null),
2906 07 Nov 14 nicklas 134               Restrictions.eq(Hql.alias("ap"), Expressions.string(Rna.AUTO_PROCESSING_PRE_NORMALIZE))
2906 07 Nov 14 nicklas 135             ));
2509 16 Jun 14 nicklas 136         }
2509 16 Jun 14 nicklas 137         else if (req.getParameter("onlyUndiluted") != null)
2509 16 Jun 14 nicklas 138         {
2906 07 Nov 14 nicklas 139           // Must not have a DILUTION_DATE annotation AND no AUTO_PROCESSING annotation
2509 16 Jun 14 nicklas 140           query.join(Annotations.leftJoin(null, Annotationtype.DILUTION_DATE.load(dc), "dd"));
2906 07 Nov 14 nicklas 141           query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
2509 16 Jun 14 nicklas 142           query.restrict(Restrictions.eq(Hql.alias("dd"), null));
2906 07 Nov 14 nicklas 143           query.restrict(Restrictions.eq(Hql.alias("ap"), null));
2509 16 Jun 14 nicklas 144         }
2509 16 Jun 14 nicklas 145         
1745 27 Nov 12 nicklas 146         query.order(Orders.desc(Hql.property("id")));
1745 27 Nov 12 nicklas 147         
1745 27 Nov 12 nicklas 148         List<BioPlate> result = query.list(dc);
1792 17 Dec 12 nicklas 149         JSONArray jsonPlates = new JSONArray();
1745 27 Nov 12 nicklas 150         for (BioPlate plate: result)
1745 27 Nov 12 nicklas 151         {
1792 17 Dec 12 nicklas 152           JSONObject jsonPlate = new JSONObject();
1792 17 Dec 12 nicklas 153           jsonPlate.put("id", plate.getId());
1792 17 Dec 12 nicklas 154           jsonPlate.put("name", plate.getName());
3299 04 May 15 nicklas 155           jsonPlate.put("columns", plate.getColumns());
3299 04 May 15 nicklas 156           jsonPlate.put("rows", plate.getRows());
1792 17 Dec 12 nicklas 157           jsonPlate.put("comments", plate.getDescription());
2509 16 Jun 14 nicklas 158           if (plateType == BioplateType.MRNA)
2509 16 Jun 14 nicklas 159           {
2906 07 Nov 14 nicklas 160             jsonPlate.put("AutoProcessing", Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, plate));
2509 16 Jun 14 nicklas 161             jsonPlate.put("DilutionDate", Reggie.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, plate)));
2509 16 Jun 14 nicklas 162             jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, plate));
2509 16 Jun 14 nicklas 163           }
1807 23 Jan 13 nicklas 164           jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, plate));
1792 17 Dec 12 nicklas 165           jsonPlates.add(jsonPlate);
1745 27 Nov 12 nicklas 166         }
1792 17 Dec 12 nicklas 167         json.put("bioplates", jsonPlates);
1745 27 Nov 12 nicklas 168       }
1745 27 Nov 12 nicklas 169       else if ("GetMRnaInfoForPlate".equals(cmd))
1745 27 Nov 12 nicklas 170       {
1745 27 Nov 12 nicklas 171         int mRnaPlateId = Values.getInt(req.getParameter("bioplate"));
6333 15 Jun 21 nicklas 172         dc = sc.newDbControl(":mRNA and cDNA lab protocols");
1745 27 Nov 12 nicklas 173
1745 27 Nov 12 nicklas 174         BioPlate mRnaPlate = BioPlate.getById(dc, mRnaPlateId);
1745 27 Nov 12 nicklas 175
2509 16 Jun 14 nicklas 176         JSONObject jsonPlate = new JSONObject();
2509 16 Jun 14 nicklas 177         jsonPlate.put("id", mRnaPlate.getId());
2509 16 Jun 14 nicklas 178         jsonPlate.put("name", mRnaPlate.getName());
3299 04 May 15 nicklas 179         jsonPlate.put("columns", mRnaPlate.getColumns());
3299 04 May 15 nicklas 180         jsonPlate.put("rows", mRnaPlate.getRows());
2509 16 Jun 14 nicklas 181         jsonPlate.put("comments", mRnaPlate.getDescription());
2509 16 Jun 14 nicklas 182         jsonPlate.put("DilutionDate", Reggie.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, mRnaPlate)));
2509 16 Jun 14 nicklas 183         jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, mRnaPlate));
2509 16 Jun 14 nicklas 184         jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, mRnaPlate));
2509 16 Jun 14 nicklas 185         
1745 27 Nov 12 nicklas 186         ItemQuery<Extract> query = Extract.getQuery();
1745 27 Nov 12 nicklas 187         query.join(Hql.innerJoin(null, "bioWell", "bw", true));
1745 27 Nov 12 nicklas 188         query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
1745 27 Nov 12 nicklas 189         query.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entity(mRnaPlate)));
1745 27 Nov 12 nicklas 190         query.order(Orders.asc(Hql.property("bw", "column")));
1745 27 Nov 12 nicklas 191         query.order(Orders.asc(Hql.property("bw", "row")));
1745 27 Nov 12 nicklas 192
1745 27 Nov 12 nicklas 193         List<MRna> mrna = MRna.toList(query.list(dc));
1745 27 Nov 12 nicklas 194         JSONArray jsonMRna = new JSONArray();
1745 27 Nov 12 nicklas 195         
1745 27 Nov 12 nicklas 196         for (MRna r : mrna)
1745 27 Nov 12 nicklas 197         {
1745 27 Nov 12 nicklas 198           r.loadBioPlateLocation();
3303 04 May 15 nicklas 199           r.loadAnnotations(dc, "DilutionConc", Annotationtype.DILUTION_CONC, null);
3832 07 Apr 16 nicklas 200           r.loadAnnotations(dc, "DilutionVolume", Annotationtype.DILUTION_VOLUME, null);
3303 04 May 15 nicklas 201           r.loadAnnotations(dc, "UseForQC", Annotationtype.USE_FOR_QC, null);
1745 27 Nov 12 nicklas 202           Rna rna = r.getRna();
6727 05 May 22 nicklas 203           rna.loadAnnotations(dc, "label", Annotationtype.TUBE_LABEL, null);
3757 18 Feb 16 nicklas 204           rna.loadYellowLabelInfo(dc, null);
2906 07 Nov 14 nicklas 205           boolean preNormalized = rna.isPreNormalized(dc);
2906 07 Nov 14 nicklas 206           rna.setAnnotation("preNormalized", preNormalized);
2906 07 Nov 14 nicklas 207           if (!preNormalized)
2906 07 Nov 14 nicklas 208           {
2906 07 Nov 14 nicklas 209             rna.loadBioPlateLocation();
2906 07 Nov 14 nicklas 210           }
1898 07 Mar 13 nicklas 211
5553 12 Aug 19 nicklas 212           rna.loadAnnotations(dc, "NDConc", Annotationtype.ND_CONC, null);
6218 20 Apr 21 nicklas 213           rna.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
6218 20 Apr 21 nicklas 214           rna.coalesceInto("conc", "NDConc", "QubitConc");
6218 20 Apr 21 nicklas 215
1745 27 Nov 12 nicklas 216           Float used = r.getItem().getCreationEvent().getUsedQuantity(rna.getItem());
1745 27 Nov 12 nicklas 217           Float remain = rna.getItem().getRemainingQuantity();
1745 27 Nov 12 nicklas 218           rna.setAnnotation("usedQuantity", used);
1745 27 Nov 12 nicklas 219           if (used != null && remain != null)
1745 27 Nov 12 nicklas 220           {
1745 27 Nov 12 nicklas 221             rna.setAnnotation("remainingQuantity", remain+used);
1745 27 Nov 12 nicklas 222           }
1745 27 Nov 12 nicklas 223           
1745 27 Nov 12 nicklas 224           r.setAnnotation("rna", rna.asJSONObject());
1816 31 Jan 13 nicklas 225           r.setAnnotation("comment", r.getItem().getDescription());
1745 27 Nov 12 nicklas 226           jsonMRna.add(r.asJSONObject());
1745 27 Nov 12 nicklas 227           
1745 27 Nov 12 nicklas 228         }
1745 27 Nov 12 nicklas 229                 
1745 27 Nov 12 nicklas 230         json.put("mrna", jsonMRna);
2509 16 Jun 14 nicklas 231         json.put("plate", jsonPlate);
1745 27 Nov 12 nicklas 232       }
3645 03 Dec 15 nicklas 233       else if ("GetRNAForAutoDesign".equals(cmd))
3645 03 Dec 15 nicklas 234       {
6333 15 Jun 21 nicklas 235         dc = sc.newDbControl(":Auto-design mRNA plate");
3645 03 Dec 15 nicklas 236         SnapshotManager manager = new SnapshotManager();
3659 09 Dec 15 nicklas 237
3659 09 Dec 15 nicklas 238         // Total number of RNA to select
3645 03 Dec 15 nicklas 239         int numToSelect = Values.getInt(req.getParameter("numToSelect"));
3659 09 Dec 15 nicklas 240         // Number of new RNA to select (default is to leave one slot for 'old' RNA)
3659 09 Dec 15 nicklas 241         int numNewToSelect = Values.getInt(req.getParameter("numNewToSelect"), numToSelect-1);
3833 08 Apr 16 nicklas 242         int protocolId = Values.getInt(req.getParameter("protocol"));
3645 03 Dec 15 nicklas 243         
3659 09 Dec 15 nicklas 244         // Find the best matching normalization protocol used on pre-normalized RNA item
3833 08 Apr 16 nicklas 245         Protocol protocol = Protocol.getById(dc, protocolId);
3833 08 Apr 16 nicklas 246         Float targetRnaAmount = (Float)Annotationtype.RNA_MINIMAL_AMOUNT.getAnnotationValue(dc, protocol);
3833 08 Apr 16 nicklas 247         if (targetRnaAmount == null)
3645 03 Dec 15 nicklas 248         {
3833 08 Apr 16 nicklas 249           targetRnaAmount = (Float)Annotationtype.RNA_TARGET_AMOUNT.getAnnotationValue(dc, protocol);
3645 03 Dec 15 nicklas 250         }
3833 08 Apr 16 nicklas 251         if (targetRnaAmount == null)
3833 08 Apr 16 nicklas 252         {
3833 08 Apr 16 nicklas 253           throw new InvalidDataException("The '" + protocol.getName() + "' doesn't specify the amount of RNA.");
3833 08 Apr 16 nicklas 254         }
3645 03 Dec 15 nicklas 255         
3651 07 Dec 15 nicklas 256         RnaFilterOptions options = new RnaFilterOptions();
3651 07 Dec 15 nicklas 257         options.normalizationProtocol = protocol;
3651 07 Dec 15 nicklas 258         options.minQualityScore = 6f;
3651 07 Dec 15 nicklas 259         options.minRemainingQuantity = targetRnaAmount;
3651 07 Dec 15 nicklas 260         options.reProcess = true;
3645 03 Dec 15 nicklas 261         
3659 09 Dec 15 nicklas 262         // Find pre-normalized RNA not yet processed
3764 22 Feb 16 nicklas 263         List<Rna>[] rna = Rna.findPreNormalizedForLibPrep(dc, manager, options, numNewToSelect, 0);
3764 22 Feb 16 nicklas 264         List<Rna> rnaSelected = rna[0]; // YellowLabel specimen!
3764 22 Feb 16 nicklas 265         List<Rna> rnaNotYellow = rna[1]; // Other specimen!
3764 22 Feb 16 nicklas 266         
3764 22 Feb 16 nicklas 267         if (rnaSelected.size() < numNewToSelect)
3651 07 Dec 15 nicklas 268         {
3659 09 Dec 15 nicklas 269           // Find regular RNA not yet processed
3764 22 Feb 16 nicklas 270           rna = Rna.findRnaForLibPrep(dc, manager, options, numNewToSelect - rnaSelected.size(), 100000);
3764 22 Feb 16 nicklas 271           rnaSelected.addAll(rna[0]); // Add more YellowLabel specimen
3764 22 Feb 16 nicklas 272           rnaNotYellow.addAll(rna[1]);
3764 22 Feb 16 nicklas 273 //          rna.addAll(Rna.findRnaForLibPrep(dc, manager, options, numNewToSelect-rna.size())[0]);
3651 07 Dec 15 nicklas 274         }
3645 03 Dec 15 nicklas 275         
3764 22 Feb 16 nicklas 276         if (rnaSelected.size() < numNewToSelect)
3764 22 Feb 16 nicklas 277         {
3764 22 Feb 16 nicklas 278           // If there are remaining places, we add some of the non-YellowLabel specimen
3764 22 Feb 16 nicklas 279           rnaSelected.addAll(rnaNotYellow.subList(0, Math.min(rnaNotYellow.size(), numNewToSelect-rnaSelected.size())));
3764 22 Feb 16 nicklas 280         }
3764 22 Feb 16 nicklas 281         
3764 22 Feb 16 nicklas 282         // Re-order the RNA
3764 22 Feb 16 nicklas 283         Collections.sort(rnaSelected, new ReggieItem.IndexOrderComparator());
3764 22 Feb 16 nicklas 284         
3659 09 Dec 15 nicklas 285         // Fill remaining positions with "old" RNA that has already been
3651 07 Dec 15 nicklas 286         // sequenced before
3764 22 Feb 16 nicklas 287         if (rnaSelected.size() < numToSelect)
3659 09 Dec 15 nicklas 288         {
3764 22 Feb 16 nicklas 289           rnaSelected.addAll(Rna.findMoreRnaForAutoDesignLibPrep(dc, manager, numToSelect-rnaSelected.size()));
3659 09 Dec 15 nicklas 290         }
3764 22 Feb 16 nicklas 291         else
3764 22 Feb 16 nicklas 292         {
3764 22 Feb 16 nicklas 293           rnaSelected = rnaSelected.subList(0, numToSelect);
3764 22 Feb 16 nicklas 294         }
3645 03 Dec 15 nicklas 295         
3645 03 Dec 15 nicklas 296         JSONArray jsonRna = new JSONArray();
3764 22 Feb 16 nicklas 297         for (Rna r : rnaSelected)
3645 03 Dec 15 nicklas 298         {
3651 07 Dec 15 nicklas 299           jsonRna.add(r.asJSONObject());
3659 09 Dec 15 nicklas 300         }
3651 07 Dec 15 nicklas 301         
3645 03 Dec 15 nicklas 302         json.put("rna", jsonRna);
3645 03 Dec 15 nicklas 303       }
3645 03 Dec 15 nicklas 304
1742 23 Nov 12 nicklas 305     }
1742 23 Nov 12 nicklas 306     catch (Throwable t)
1742 23 Nov 12 nicklas 307     {
1742 23 Nov 12 nicklas 308       t.printStackTrace();
1742 23 Nov 12 nicklas 309       json.clear();
1742 23 Nov 12 nicklas 310       json.put("status", "error");
1742 23 Nov 12 nicklas 311       json.put("message", t.getMessage());
1742 23 Nov 12 nicklas 312       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1742 23 Nov 12 nicklas 313     }
1742 23 Nov 12 nicklas 314     finally
1742 23 Nov 12 nicklas 315     {
1742 23 Nov 12 nicklas 316       if (dc != null) dc.close();
1742 23 Nov 12 nicklas 317       json.writeJSONString(resp.getWriter());
1742 23 Nov 12 nicklas 318     }
1742 23 Nov 12 nicklas 319     
1742 23 Nov 12 nicklas 320   }
1742 23 Nov 12 nicklas 321
1742 23 Nov 12 nicklas 322   @SuppressWarnings("unchecked")
1742 23 Nov 12 nicklas 323   @Override
1742 23 Nov 12 nicklas 324   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
1742 23 Nov 12 nicklas 325     throws ServletException, IOException 
1742 23 Nov 12 nicklas 326   {
1742 23 Nov 12 nicklas 327     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 328     JsonUtil.setJsonResponseHeaders(resp);
1742 23 Nov 12 nicklas 329     
1742 23 Nov 12 nicklas 330     JSONObject json = new JSONObject();
1742 23 Nov 12 nicklas 331     json.put("status", "ok");
1742 23 Nov 12 nicklas 332     
1742 23 Nov 12 nicklas 333     JSONArray jsonMessages = new JSONArray();
1742 23 Nov 12 nicklas 334   
3975 26 May 16 nicklas 335     final SessionControl sc = Reggie.getSessionControl(req);
1742 23 Nov 12 nicklas 336     DbControl dc = null;
1742 23 Nov 12 nicklas 337     try
1742 23 Nov 12 nicklas 338     {
1742 23 Nov 12 nicklas 339       if ("GetRnaInfoFromNames".equals(cmd))
1742 23 Nov 12 nicklas 340       {
3752 17 Feb 16 nicklas 341         JSONObject jsonReq = JsonUtil.parseRequest(req);
1742 23 Nov 12 nicklas 342         JSONArray names = (JSONArray)jsonReq.get("names");
1742 23 Nov 12 nicklas 343         
6333 15 Jun 21 nicklas 344         dc = sc.newDbControl(":Create mRNA plate");
1742 23 Nov 12 nicklas 345         ItemQuery<Extract> query = Extract.getQuery();
1742 23 Nov 12 nicklas 346         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2906 07 Nov 14 nicklas 347         query.restrict(
2906 07 Nov 14 nicklas 348           Restrictions.or(
2906 07 Nov 14 nicklas 349             Subtype.RNA.restriction(dc, null),  
2906 07 Nov 14 nicklas 350             Subtype.RNA_NORMALIZED_ALIQUOT.restriction(dc, null)
2906 07 Nov 14 nicklas 351         ));
1742 23 Nov 12 nicklas 352         
1742 23 Nov 12 nicklas 353         query.restrict(Restrictions.in(Hql.property("name"), Expressions.parameter("names")));
1742 23 Nov 12 nicklas 354         query.setParameter("names", names, Type.STRING);
1742 23 Nov 12 nicklas 355         
1742 23 Nov 12 nicklas 356         List<Rna> rna = Rna.toRna(query.list(dc));
1742 23 Nov 12 nicklas 357         JSONArray jsonRna = new JSONArray();
3166 05 Mar 15 nicklas 358         SnapshotManager manager = new SnapshotManager();
1742 23 Nov 12 nicklas 359         for (Rna r : rna)
1742 23 Nov 12 nicklas 360         {
2906 07 Nov 14 nicklas 361           boolean preNormalized = r.isPreNormalized(dc);
2906 07 Nov 14 nicklas 362           r.setAnnotation("preNormalized", preNormalized);
1742 23 Nov 12 nicklas 363           r.setAnnotation("remainingQuantity", r.getExtract().getRemainingQuantity());
4983 27 Sep 18 nicklas 364           r.loadDoNotUseAnnotations(dc, manager);
6727 05 May 22 nicklas 365           r.loadAnnotations(dc, "label", Annotationtype.TUBE_LABEL, null);
3166 05 Mar 15 nicklas 366           r.loadAnnotations(dc, manager, "NDConc", Annotationtype.ND_CONC, null);
6218 20 Apr 21 nicklas 367           r.loadAnnotations(dc, manager, "QubitConc", Annotationtype.QUBIT_CONC, null);
6218 20 Apr 21 nicklas 368           r.coalesceInto("conc", "NDConc", "QubitConc");
3166 05 Mar 15 nicklas 369           r.loadAnnotations(dc, manager, "AutoProcessing", Annotationtype.AUTO_PROCESSING, null);
3757 18 Feb 16 nicklas 370           r.loadYellowLabelInfo(dc, manager);
2906 07 Nov 14 nicklas 371           if (preNormalized)
2906 07 Nov 14 nicklas 372           {
3166 05 Mar 15 nicklas 373             r.loadAnnotations(dc, manager, "DilutionDate", Annotationtype.DILUTION_DATE, Reggie.CONVERTER_DATE_TO_STRING);
2906 07 Nov 14 nicklas 374           }
2906 07 Nov 14 nicklas 375           else
2906 07 Nov 14 nicklas 376           {
3166 05 Mar 15 nicklas 377             r.loadAnnotations(dc, manager, "QiacubeDate", Annotationtype.QIACUBE_DATE, Reggie.CONVERTER_DATE_TO_STRING);
2906 07 Nov 14 nicklas 378             r.loadBioPlateLocation();
3166 05 Mar 15 nicklas 379             r.loadRnaQc(dc, manager);
2906 07 Nov 14 nicklas 380           }
1946 19 Apr 13 nicklas 381           r.setAnnotation("comment", r.getExtract().getDescription());
1742 23 Nov 12 nicklas 382           jsonRna.add(r.asJSONObject());
1742 23 Nov 12 nicklas 383         }
1742 23 Nov 12 nicklas 384         
1742 23 Nov 12 nicklas 385         json.put("rna", jsonRna);
1742 23 Nov 12 nicklas 386       }
1940 18 Apr 13 nicklas 387       else if ("AutoSelectRnaForMRna".equals(cmd))
1940 18 Apr 13 nicklas 388       {
6333 15 Jun 21 nicklas 389         dc = sc.newDbControl(":Auto-select RNA for libprep");
1940 18 Apr 13 nicklas 390         int numToSelect = Values.getInt(req.getParameter("numToSelect"));
1940 18 Apr 13 nicklas 391         
3752 17 Feb 16 nicklas 392         JSONObject jsonReq = JsonUtil.parseRequest(req);
5319 28 Feb 19 nicklas 393         List<Long> jsonIgnore = (List<Long>)jsonReq.get("ignore");
2906 07 Nov 14 nicklas 394         boolean preNormalized = jsonReq.get("preNormalized") != null;
2906 07 Nov 14 nicklas 395         boolean flagNotSelected = !preNormalized && jsonReq.get("flag") != null;
1940 18 Apr 13 nicklas 396         
3659 09 Dec 15 nicklas 397         SnapshotManager manager = new SnapshotManager();
3651 07 Dec 15 nicklas 398         RnaFilterOptions options = new RnaFilterOptions();
3651 07 Dec 15 nicklas 399         options.startDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("startDate"));
3651 07 Dec 15 nicklas 400         options.ignore = jsonIgnore;
3651 07 Dec 15 nicklas 401
3764 22 Feb 16 nicklas 402         List<Rna> rnaSelected = null;
3764 22 Feb 16 nicklas 403         List<Rna> rnaNotYellow = null;
3651 07 Dec 15 nicklas 404         List<Rna> flagged = null;
2906 07 Nov 14 nicklas 405         if (preNormalized)
2906 07 Nov 14 nicklas 406         {
3270 23 Apr 15 nicklas 407           Number protocolId = (Number)jsonReq.get("normalizationProtocol");
3651 07 Dec 15 nicklas 408           options.normalizationProtocol = Protocol.getById(dc, protocolId.intValue());
3764 22 Feb 16 nicklas 409           List<Rna>[] tmp = Rna.findPreNormalizedForLibPrep(dc, manager, options, numToSelect, 0);
3764 22 Feb 16 nicklas 410           rnaSelected = tmp[0];
3764 22 Feb 16 nicklas 411           rnaNotYellow = tmp[1];
2906 07 Nov 14 nicklas 412         }
2906 07 Nov 14 nicklas 413         else
2906 07 Nov 14 nicklas 414         {
3651 07 Dec 15 nicklas 415           options.minRemainingQuantity = JsonUtil.getAsFloat(jsonReq, "remainingQuantity");
3651 07 Dec 15 nicklas 416           options.minQualityScore = JsonUtil.getAsFloat(jsonReq, "qualityScore");
3651 07 Dec 15 nicklas 417           options.reProcess = jsonReq.get("reProcess") != null;
3651 07 Dec 15 nicklas 418           
3764 22 Feb 16 nicklas 419           List<Rna>[] tmp = Rna.findRnaForLibPrep(dc, manager, options, numToSelect, 0);
3764 22 Feb 16 nicklas 420           rnaSelected = tmp[0];
3764 22 Feb 16 nicklas 421           rnaNotYellow = tmp[1];
3764 22 Feb 16 nicklas 422           flagged = tmp[2];
2906 07 Nov 14 nicklas 423         }
2906 07 Nov 14 nicklas 424         
3764 22 Feb 16 nicklas 425         if (rnaSelected.size() < numToSelect)
3764 22 Feb 16 nicklas 426         {
3764 22 Feb 16 nicklas 427           // If there are remaining places, we add some of the non-YellowLabel specimen
3764 22 Feb 16 nicklas 428           rnaSelected.addAll(rnaNotYellow.subList(0, Math.min(rnaNotYellow.size(), numToSelect-rnaSelected.size())));
3764 22 Feb 16 nicklas 429         }
3764 22 Feb 16 nicklas 430         
3764 22 Feb 16 nicklas 431         // Re-order the RNA
3764 22 Feb 16 nicklas 432         Collections.sort(rnaSelected, new ReggieItem.IndexOrderComparator());
3764 22 Feb 16 nicklas 433         
3651 07 Dec 15 nicklas 434         JSONArray jsonRna = new JSONArray();
3651 07 Dec 15 nicklas 435         JSONArray jsonFlagged = new JSONArray();
3764 22 Feb 16 nicklas 436         for (Rna r : rnaSelected)
1940 18 Apr 13 nicklas 437         {
3651 07 Dec 15 nicklas 438           jsonRna.add(r.asJSONObject());
1940 18 Apr 13 nicklas 439         }
3651 07 Dec 15 nicklas 440         if (flagNotSelected && flagged != null)
2143 19 Nov 13 nicklas 441         {
3651 07 Dec 15 nicklas 442           for (Rna r : flagged)
1940 18 Apr 13 nicklas 443           {
3651 07 Dec 15 nicklas 444             jsonFlagged.add(r.asJSONObject());
1940 18 Apr 13 nicklas 445           }
1940 18 Apr 13 nicklas 446         }
1940 18 Apr 13 nicklas 447         
1940 18 Apr 13 nicklas 448         json.put("rna", jsonRna);
1940 18 Apr 13 nicklas 449         json.put("flagged", jsonFlagged);
1940 18 Apr 13 nicklas 450         
1940 18 Apr 13 nicklas 451       }
3302 04 May 15 nicklas 452       else if ("CreateMRnaOrLibPlate".equals(cmd))
1742 23 Nov 12 nicklas 453       {
6333 15 Jun 21 nicklas 454         dc = sc.newDbControl(":Create mRNA or Library plate");
2161 09 Dec 13 nicklas 455
3645 03 Dec 15 nicklas 456         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.LIBRARY_PLATE_DESIGNER, ReggieRole.LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 457
3752 17 Feb 16 nicklas 458         JSONObject jsonReq = JsonUtil.parseRequest(req);
1742 23 Nov 12 nicklas 459         JSONObject jsonPlate = (JSONObject)jsonReq.get("bioplate");
1803 18 Jan 13 nicklas 460         JSONArray jsonFlagged = (JSONArray)jsonReq.get("flagged");
5891 07 Apr 20 nicklas 461         Number normalizationProtocolId = (Number)jsonReq.get("normalizationProtocol");
1742 23 Nov 12 nicklas 462         
1742 23 Nov 12 nicklas 463         String plateName = (String)jsonPlate.get("name");
1742 23 Nov 12 nicklas 464         JSONArray jsonWells = (JSONArray)jsonPlate.get("wells");
1742 23 Nov 12 nicklas 465         
3302 04 May 15 nicklas 466         BioplateType plateType = BioplateType.getByCName((String)jsonPlate.get("plateType"));
3302 04 May 15 nicklas 467         PlateGeometry geometry = plateType.getPlateGeometry(dc);
1742 23 Nov 12 nicklas 468         
3302 04 May 15 nicklas 469         BioPlate workPlate = BioPlate.getNew(dc, geometry, plateType.load(dc));
5886 30 Mar 20 nicklas 470         workPlate.setName(ReggieItem.ensureNonExistingItem(dc, Item.BIOPLATE, plateType.getName(), plateName));
3302 04 May 15 nicklas 471         workPlate.setDescription(Values.getStringOrNull((String)jsonPlate.get("comments")));
3302 04 May 15 nicklas 472         Annotationtype.POOL_SCHEMA.setAnnotationValue(dc, workPlate, jsonPlate.get("poolSchema"));
3302 04 May 15 nicklas 473         dc.saveItem(workPlate);
5891 07 Apr 20 nicklas 474         // Remember the selected normalization protocol for later use
5891 07 Apr 20 nicklas 475         if (normalizationProtocolId != null)
5891 07 Apr 20 nicklas 476         {
5891 07 Apr 20 nicklas 477           Protocol protocol = Protocol.getById(dc, normalizationProtocolId.intValue());
5891 07 Apr 20 nicklas 478           Annotationtype.RNA_TARGET_AMOUNT.copyAnnotationValues(dc, protocol, workPlate, false);
5891 07 Apr 20 nicklas 479           Annotationtype.RNA_TARGET_VOLUME.copyAnnotationValues(dc, protocol, workPlate, false);
5891 07 Apr 20 nicklas 480           AnyToAny protocolLink = AnyToAny.getNewOrExisting(dc, workPlate, "NormalizationProtocol", protocol, false);
5891 07 Apr 20 nicklas 481           dc.saveItem(protocolLink);
5891 07 Apr 20 nicklas 482         }
3302 04 May 15 nicklas 483         
3302 04 May 15 nicklas 484         // mRNA or Library
3302 04 May 15 nicklas 485         Subtype childType = plateType.getSubtype();
3302 04 May 15 nicklas 486         ItemSubtype extractType = childType.load(dc);
4885 04 Jul 18 nicklas 487         Set<Extract> rnaOnPlate = new HashSet<Extract>();
1742 23 Nov 12 nicklas 488         
2906 07 Nov 14 nicklas 489         boolean allArePreNormalized = true;
2906 07 Nov 14 nicklas 490         ItemSubtype preNormalizedType = Subtype.RNA_NORMALIZED_ALIQUOT.get(dc);
3247 14 Apr 15 nicklas 491         ItemList flagged = BiomaterialList.FLAGGED_RNA.load(dc);
3659 09 Dec 15 nicklas 492         ItemList autoDesign1 = BiomaterialList.RNA_AUTO_DESIGN_1.load(dc);
3659 09 Dec 15 nicklas 493         ItemList autoDesign2 = BiomaterialList.RNA_AUTO_DESIGN_2.load(dc);
5364 16 Apr 19 nicklas 494
3659 09 Dec 15 nicklas 495         List<String> removedFrom1 = new ArrayList<String>();
3659 09 Dec 15 nicklas 496         List<String> removedFrom2 = new ArrayList<String>();
1742 23 Nov 12 nicklas 497         for (int i = 0; i < jsonWells.size(); i++)
1742 23 Nov 12 nicklas 498         {
1742 23 Nov 12 nicklas 499           JSONObject jsonWell = (JSONObject)jsonWells.get(i);
1742 23 Nov 12 nicklas 500           JSONObject jsonRna = (JSONObject)jsonWell.get("rna");
3302 04 May 15 nicklas 501           JSONObject jsonBarcode = (JSONObject)jsonRna.get("barcode");
1742 23 Nov 12 nicklas 502           
1742 23 Nov 12 nicklas 503           Number row = (Number)jsonWell.get("row");
1742 23 Nov 12 nicklas 504           Number col = (Number)jsonWell.get("column");
1742 23 Nov 12 nicklas 505           
1742 23 Nov 12 nicklas 506           Number rnaId = (Number)jsonRna.get("id");
4885 04 Jul 18 nicklas 507           Rna rna = Rna.getById(dc, rnaId.intValue());
4885 04 Jul 18 nicklas 508           Extract r = rna.getItem();
4885 04 Jul 18 nicklas 509           rnaOnPlate.add(r);
4885 04 Jul 18 nicklas 510           if ("ReProcess".equals(Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, r)))
3112 04 Feb 15 nicklas 511           {
3112 04 Feb 15 nicklas 512             // Remove auto-processing and flag annotations and remove the RNA from the flagged list
4885 04 Jul 18 nicklas 513             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, r, null);
4885 04 Jul 18 nicklas 514             Annotationtype.FLAG.setAnnotationValue(dc, r, null);
5364 16 Apr 19 nicklas 515             flagged.removeItem(r);
3112 04 Feb 15 nicklas 516           }
3659 09 Dec 15 nicklas 517           
4885 04 Jul 18 nicklas 518           boolean isPreNormalized = preNormalizedType.equals(r.getItemSubtype());
3273 23 Apr 15 nicklas 519           allArePreNormalized &= isPreNormalized;
3659 09 Dec 15 nicklas 520           // Remove RNA from auto-design lists
3659 09 Dec 15 nicklas 521           if (!isPreNormalized)
3659 09 Dec 15 nicklas 522           {
5364 16 Apr 19 nicklas 523             if (autoDesign1.removeItem(r)) removedFrom1.add(rna.getName());
5364 16 Apr 19 nicklas 524             if (autoDesign2.removeItem(r)) removedFrom2.add(rna.getName());
3659 09 Dec 15 nicklas 525           }
3273 23 Apr 15 nicklas 526           
1742 23 Nov 12 nicklas 527           Number usedQuantity = (Number)jsonRna.get("usedQuantity");
1742 23 Nov 12 nicklas 528           
5886 30 Mar 20 nicklas 529           Extract childExtract = Extract.getNew(dc);
3302 04 May 15 nicklas 530           childExtract.setItemSubtype(extractType);
5436 17 May 19 nicklas 531           Pipeline.RNA_SEQ.setAnnotation(dc, childExtract);
3302 04 May 15 nicklas 532           childExtract.setDescription((String)jsonRna.get("comment"));
3303 04 May 15 nicklas 533           if (Boolean.TRUE.equals(jsonRna.get("qc")))
3303 04 May 15 nicklas 534           {
3303 04 May 15 nicklas 535             Annotationtype.USE_FOR_QC.setAnnotationValue(dc, childExtract, true);
3303 04 May 15 nicklas 536           }
3303 04 May 15 nicklas 537           Annotationtype.DILUTION_CONC.setAnnotationValue(dc, childExtract, jsonRna.get("dilutionConc"));
3832 07 Apr 16 nicklas 538           Annotationtype.DILUTION_VOLUME.setAnnotationValue(dc, childExtract, jsonRna.get("dilutionVolume"));
3303 04 May 15 nicklas 539
3302 04 May 15 nicklas 540           if (jsonBarcode != null)
3302 04 May 15 nicklas 541           {
3302 04 May 15 nicklas 542             Number barcodeId = (Number)jsonBarcode.get("id");
3302 04 May 15 nicklas 543             childExtract.setTag(Tag.getById(dc, barcodeId.intValue()));
3302 04 May 15 nicklas 544           }
3302 04 May 15 nicklas 545           
4885 04 Jul 18 nicklas 546           String childName = rna.getNextLibOrMRnaName(dc, childType);
4885 04 Jul 18 nicklas 547           childExtract.setName(childName);
4885 04 Jul 18 nicklas 548           BioMaterialEventSource evtSrc = childExtract.getCreationEvent().setSource(r);
5791 16 Dec 19 nicklas 549           DoNotUse.copyDoNotUseAnnotations(dc, r, childExtract, false);
1742 23 Nov 12 nicklas 550           evtSrc.setUsedQuantity(usedQuantity.floatValue());
1742 23 Nov 12 nicklas 551           
3302 04 May 15 nicklas 552           BioWell well = workPlate.getBioWell(row.intValue(), col.intValue());
3302 04 May 15 nicklas 553           childExtract.setBioWell(well);
1742 23 Nov 12 nicklas 554           
4885 04 Jul 18 nicklas 555           Float remainingQuantityBefore = r.getRemainingQuantity();
4377 02 Mar 17 nicklas 556           if (remainingQuantityBefore != null)
3273 23 Apr 15 nicklas 557           {
4377 02 Mar 17 nicklas 558             float remainingQuantityAfter = remainingQuantityBefore - usedQuantity.floatValue();
4377 02 Mar 17 nicklas 559             if (isPreNormalized && remainingQuantityAfter > 0.01)
4377 02 Mar 17 nicklas 560             {
4377 02 Mar 17 nicklas 561               // We discard any remaining pre-normalized RNA
4885 04 Jul 18 nicklas 562               BioMaterialEvent discardedRNA = r.newEvent();
4377 02 Mar 17 nicklas 563               discardedRNA.setUsedQuantity(remainingQuantityAfter);
4377 02 Mar 17 nicklas 564               discardedRNA.setComment("Remaining pre-normalized RNA is discarded after it has been used");
4377 02 Mar 17 nicklas 565               dc.saveItem(discardedRNA);
4377 02 Mar 17 nicklas 566             }
3273 23 Apr 15 nicklas 567           }
3273 23 Apr 15 nicklas 568           
3832 07 Apr 16 nicklas 569           //jsonMessages.add(childExtract.getName() + "; used="+usedQuantity + "; conc="+jsonRna.get("dilutionConc"));
3832 07 Apr 16 nicklas 570           
3302 04 May 15 nicklas 571           dc.saveItem(childExtract);
1742 23 Nov 12 nicklas 572         }
1742 23 Nov 12 nicklas 573         
1803 18 Jan 13 nicklas 574         if (jsonFlagged != null && jsonFlagged.size() > 0)
1803 18 Jan 13 nicklas 575         {
1803 18 Jan 13 nicklas 576           int numFlagged = 0;
1803 18 Jan 13 nicklas 577           
1803 18 Jan 13 nicklas 578           for (int i = 0; i < jsonFlagged.size(); ++i)
1803 18 Jan 13 nicklas 579           {
1803 18 Jan 13 nicklas 580             JSONObject jsonRna = (JSONObject)jsonFlagged.get(i);
1803 18 Jan 13 nicklas 581             Number rnaId = (Number)jsonRna.get("id");
1803 18 Jan 13 nicklas 582             Extract rna = Extract.getById(dc, rnaId.intValue());
1946 19 Apr 13 nicklas 583             rna.setDescription((String)jsonRna.get("comment"));
1803 18 Jan 13 nicklas 584             
1803 18 Jan 13 nicklas 585             // Safety check in case a flagged RNA is also on the plate
4885 04 Jul 18 nicklas 586             if (!rnaOnPlate.contains(rna))
1803 18 Jan 13 nicklas 587             {
1949 22 Apr 13 nicklas 588               flagged.add(rna);
1889 27 Feb 13 nicklas 589               Annotationtype.FLAG.setAnnotationValue(dc, rna, jsonRna.get("flag"));
1889 27 Feb 13 nicklas 590               Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rna, "Disable");
1889 27 Feb 13 nicklas 591               numFlagged++;
1803 18 Jan 13 nicklas 592             }
1803 18 Jan 13 nicklas 593           }
1803 18 Jan 13 nicklas 594           if (numFlagged > 0)
1803 18 Jan 13 nicklas 595           {
1803 18 Jan 13 nicklas 596             jsonMessages.add("Added " + numFlagged + " RNA items to '" + flagged.getName() + "' list.");
1803 18 Jan 13 nicklas 597           }
1803 18 Jan 13 nicklas 598         }
1803 18 Jan 13 nicklas 599         
3302 04 May 15 nicklas 600         jsonMessages.add("Created " + workPlate.getName() + " with " + jsonWells.size() + " child items");
5421 13 May 19 nicklas 601         ActivityDef.DESIGNED_MRNAPLATE.create(dc, workPlate.getName(), jsonWells.size());
5382 24 Apr 19 nicklas 602         
2906 07 Nov 14 nicklas 603         // Set AutoProcessing annotation to PreNormalizeRNA
2906 07 Nov 14 nicklas 604         if (allArePreNormalized)
2906 07 Nov 14 nicklas 605         {
2906 07 Nov 14 nicklas 606           jsonMessages.add("All RNA is pre-normalized");
3302 04 May 15 nicklas 607           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, workPlate, Rna.AUTO_PROCESSING_PRE_NORMALIZE);
2906 07 Nov 14 nicklas 608         }
3659 09 Dec 15 nicklas 609         if (removedFrom1.size() > 0)
3659 09 Dec 15 nicklas 610         {
3659 09 Dec 15 nicklas 611           jsonMessages.add("Used " + removedFrom1.size() + " RNA from '" + autoDesign1.getName() + "': "+Values.getString(removedFrom1, ", ", true));
3659 09 Dec 15 nicklas 612         }
3659 09 Dec 15 nicklas 613         if (removedFrom2.size() > 0)
3659 09 Dec 15 nicklas 614         {
3659 09 Dec 15 nicklas 615           jsonMessages.add("Used " + removedFrom2.size() + " RNA from '" + autoDesign2.getName() + "': "+Values.getString(removedFrom2, ", ", true));
3659 09 Dec 15 nicklas 616         }
2906 07 Nov 14 nicklas 617         
1742 23 Nov 12 nicklas 618         dc.commit();
1742 23 Nov 12 nicklas 619       }
2509 16 Jun 14 nicklas 620       else if ("RegisterRNADilution".equals(cmd))
2509 16 Jun 14 nicklas 621       {
6333 15 Jun 21 nicklas 622         dc = sc.newDbControl(":RNA dilution before libprep");
2509 16 Jun 14 nicklas 623
2509 16 Jun 14 nicklas 624         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
2509 16 Jun 14 nicklas 625
3752 17 Feb 16 nicklas 626         JSONObject jsonReq = JsonUtil.parseRequest(req);
2509 16 Jun 14 nicklas 627         
2509 16 Jun 14 nicklas 628         Number bioPlateId = (Number)jsonReq.get("bioplate");
2509 16 Jun 14 nicklas 629
2509 16 Jun 14 nicklas 630         // Load mRNA plate and the creation event
2509 16 Jun 14 nicklas 631         ReactionPlate mrnaReactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.MRNA);
2509 16 Jun 14 nicklas 632         BioPlate mrnaPlate = mrnaReactionPlate.getBioPlate();
2509 16 Jun 14 nicklas 633         
2509 16 Jun 14 nicklas 634         // Dates and operators 
2509 16 Jun 14 nicklas 635         Annotationtype.DILUTION_DATE.setAnnotationValue(dc, mrnaPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("dilutionDate")));
2509 16 Jun 14 nicklas 636         Annotationtype.DILUTION_OPERATOR.setAnnotationValue(dc, mrnaPlate, Values.getStringOrNull((String)jsonReq.get("dilutionOperator")));
2509 16 Jun 14 nicklas 637     
2509 16 Jun 14 nicklas 638         // Comment about the plate
2509 16 Jun 14 nicklas 639         mrnaPlate.setDescription((String)jsonReq.get("comments"));
2509 16 Jun 14 nicklas 640
2509 16 Jun 14 nicklas 641         jsonMessages.add("RNA dilution for mRNA plate '" + mrnaPlate.getName() + "' registered successfully.");
2509 16 Jun 14 nicklas 642         
2509 16 Jun 14 nicklas 643         dc.commit();
2509 16 Jun 14 nicklas 644       }
1758 30 Nov 12 nicklas 645       
1758 30 Nov 12 nicklas 646       else if ("ImportMRnaQCResults".equals(cmd))
1758 30 Nov 12 nicklas 647       {
6333 15 Jun 21 nicklas 648         dc = sc.newDbControl(":mRNA registration");
2161 09 Dec 13 nicklas 649
2161 09 Dec 13 nicklas 650         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 651
3752 17 Feb 16 nicklas 652         JSONObject jsonReq = JsonUtil.parseRequest(req);
1758 30 Nov 12 nicklas 653         
1758 30 Nov 12 nicklas 654         Number bioPlateId = (Number)jsonReq.get("bioplate");
2666 15 Sep 14 nicklas 655         String pdfPath = Values.getStringOrNull((String)jsonReq.get("pdf"));
1758 30 Nov 12 nicklas 656         Number mrnaProtocolId = (Number)jsonReq.get("mrnaProtocol");
1889 27 Feb 13 nicklas 657         boolean failed = Boolean.TRUE.equals(jsonReq.get("failed"));
1742 23 Nov 12 nicklas 658
1758 30 Nov 12 nicklas 659         // Load mRNA plate and the creation event
1758 30 Nov 12 nicklas 660         ReactionPlate mrnaReactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.MRNA);
1758 30 Nov 12 nicklas 661         BioPlate mrnaPlate = mrnaReactionPlate.getBioPlate();
1758 30 Nov 12 nicklas 662         
1758 30 Nov 12 nicklas 663         // Find the creation event for the mRNA plate
1762 03 Dec 12 nicklas 664         List<BioPlateEvent> events = mrnaReactionPlate.findEvents(dc, BioPlateEventType.CREATE_BIOMATERIAL, "destination");
1758 30 Nov 12 nicklas 665         if (events.size() > 1)
1758 30 Nov 12 nicklas 666         {
1758 30 Nov 12 nicklas 667           throw new InvalidDataException("Found > 1 creation event for mRNA plate: " + mrnaReactionPlate.getName());
1758 30 Nov 12 nicklas 668         }
1758 30 Nov 12 nicklas 669         // ...it may not exist if a plate has been created with a batch import...
1758 30 Nov 12 nicklas 670         BioPlateEvent mrnaPlateCreationEvent = events.size() == 0 ? null : events.get(0);
1758 30 Nov 12 nicklas 671         
1758 30 Nov 12 nicklas 672         // Set creation date and protocol
1998 29 May 13 nicklas 673         Date mrnaDate = new Date();
1998 29 May 13 nicklas 674         
1758 30 Nov 12 nicklas 675         mrnaPlate.setEventDate(mrnaDate);
1758 30 Nov 12 nicklas 676         Protocol mrnaProtocol = mrnaProtocolId == null ? null : Protocol.getById(dc, mrnaProtocolId.intValue());
1758 30 Nov 12 nicklas 677         if (mrnaPlateCreationEvent != null)
1758 30 Nov 12 nicklas 678         {
1758 30 Nov 12 nicklas 679           // This automatically propagated to all mRNA items on the plate
1758 30 Nov 12 nicklas 680           mrnaPlateCreationEvent.setEventDate(mrnaDate);
1758 30 Nov 12 nicklas 681           mrnaPlateCreationEvent.setProtocol(mrnaProtocol);
1758 30 Nov 12 nicklas 682         }
1758 30 Nov 12 nicklas 683
1758 30 Nov 12 nicklas 684         // Link PDF file with the mRNA plate
2666 15 Sep 14 nicklas 685         if (pdfPath != null)
1889 27 Feb 13 nicklas 686         {
2666 15 Sep 14 nicklas 687           File pdf = File.getByPath(dc, new Path(pdfPath, Path.Type.FILE), false);
1889 27 Feb 13 nicklas 688           AnyToAny ata = AnyToAny.getNewOrExisting(dc, mrnaPlate, "PDF printout", pdf, true);
1889 27 Feb 13 nicklas 689           if (!ata.isInDatabase()) dc.saveItem(ata);
1988 23 May 13 nicklas 690           if (pdf.getDescription() == null) pdf.setDescription("BioAnalyzer QC for " + mrnaPlate.getName());
1889 27 Feb 13 nicklas 691           jsonMessages.add("Attached file '" + pdf.getName() + "' to plate " + mrnaPlate.getName());
1889 27 Feb 13 nicklas 692         }
1758 30 Nov 12 nicklas 693         
1998 29 May 13 nicklas 694         // Dates and operators 
1998 29 May 13 nicklas 695         Annotationtype.PURIFICATION_DATE.setAnnotationValue(dc, mrnaPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("purificationDate")));
1998 29 May 13 nicklas 696         Annotationtype.PURIFICATION_OPERATOR.setAnnotationValue(dc, mrnaPlate, Values.getStringOrNull((String)jsonReq.get("purificationOperator")));
1998 29 May 13 nicklas 697         Annotationtype.FRAGMENTATION_DATE.setAnnotationValue(dc, mrnaPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("fragmentationDate")));
1998 29 May 13 nicklas 698         Annotationtype.FRAGMENTATION_OPERATOR.setAnnotationValue(dc, mrnaPlate, Values.getStringOrNull((String)jsonReq.get("fragmentationOperator")));
1998 29 May 13 nicklas 699         Annotationtype.CLEANUP_DATE.setAnnotationValue(dc, mrnaPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("cleanupDate")));
1998 29 May 13 nicklas 700         Annotationtype.CLEANUP_OPERATOR.setAnnotationValue(dc, mrnaPlate, Values.getStringOrNull((String)jsonReq.get("cleanupOperator")));
1998 29 May 13 nicklas 701     
1758 30 Nov 12 nicklas 702         // Comment about the plate
1792 17 Dec 12 nicklas 703         mrnaPlate.setDescription((String)jsonReq.get("comments"));
1758 30 Nov 12 nicklas 704
1890 01 Mar 13 nicklas 705         // Flags to indicate that this plate has been processed, successfully or not
1758 30 Nov 12 nicklas 706         mrnaPlate.setDestroyed(true);
1890 01 Mar 13 nicklas 707         Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, mrnaPlate, failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL);
1758 30 Nov 12 nicklas 708       
1889 27 Feb 13 nicklas 709         BioPlate cdnaPlate = null;
1889 27 Feb 13 nicklas 710         BioPlateEventParticipant cdnaCreation = null;
3247 14 Apr 15 nicklas 711         ItemList flaggedList = null;
1889 27 Feb 13 nicklas 712         if (!failed)
1889 27 Feb 13 nicklas 713         {
1889 27 Feb 13 nicklas 714           // Prepare cDNA child plate
4888 05 Jul 18 nicklas 715           String cdnaPlateName = mrnaReactionPlate.getChildPlateName(dc, BioplateType.CDNA);
1889 27 Feb 13 nicklas 716           cdnaPlate = BioPlate.getNew(dc, BioplateType.CDNA.getPlateGeometry(dc), BioplateType.CDNA.load(dc));
4888 05 Jul 18 nicklas 717           cdnaPlate.setName(cdnaPlateName);
1889 27 Feb 13 nicklas 718           Annotationtype.POOL_SCHEMA.copyAnnotationValues(dc, mrnaPlate, cdnaPlate, true);
1889 27 Feb 13 nicklas 719   
1889 27 Feb 13 nicklas 720           dc.saveItem(cdnaPlate);
1889 27 Feb 13 nicklas 721           
1889 27 Feb 13 nicklas 722           // Create plate event...
1889 27 Feb 13 nicklas 723           BioPlateEventType eventType = BioPlateEventType.getById(dc, BioPlateEventType.CREATE_BIOMATERIAL);
1889 27 Feb 13 nicklas 724           BioPlateEvent event = BioPlateEvent.getNew(dc, eventType);
1889 27 Feb 13 nicklas 725           event.setName("Create cDNA child plate");
1889 27 Feb 13 nicklas 726           dc.saveItem(event);
1889 27 Feb 13 nicklas 727           
1889 27 Feb 13 nicklas 728           //... with parent (mRNA) and child (cDNA) plate
1889 27 Feb 13 nicklas 729           cdnaCreation = event.addParticipant(cdnaPlate, "child", 1);
1889 27 Feb 13 nicklas 730           event.addParticipant(mrnaPlate, "parent", 1);
1889 27 Feb 13 nicklas 731           dc.saveItem(cdnaCreation);
1889 27 Feb 13 nicklas 732         }
1889 27 Feb 13 nicklas 733         else
1889 27 Feb 13 nicklas 734         {
1889 27 Feb 13 nicklas 735           flaggedList = BiomaterialList.FLAGGED_RNA.load(dc);
1889 27 Feb 13 nicklas 736         }
1807 23 Jan 13 nicklas 737
1758 30 Nov 12 nicklas 738         List<BioWell> wells = mrnaPlate.getBioWells().list(dc);
1758 30 Nov 12 nicklas 739         int numCdna = 0;
1889 27 Feb 13 nicklas 740         int numFlagged = 0;
1758 30 Nov 12 nicklas 741         ItemSubtype cdnaType = Subtype.CDNA.load(dc);
1758 30 Nov 12 nicklas 742         for (BioWell well : wells)
1758 30 Nov 12 nicklas 743         {
1758 30 Nov 12 nicklas 744           if (!well.isEmpty())
1758 30 Nov 12 nicklas 745           {
1758 30 Nov 12 nicklas 746             Extract mrna = (Extract)well.getBioMaterial();
2906 07 Nov 14 nicklas 747             MRna m = MRna.get(mrna);
2906 07 Nov 14 nicklas 748             
1758 30 Nov 12 nicklas 749             if (mrnaPlateCreationEvent == null)
1758 30 Nov 12 nicklas 750             {
1758 30 Nov 12 nicklas 751               // If no plate event existed, we must set protocol and date on all mRNA items
1758 30 Nov 12 nicklas 752               BioMaterialEvent evt = mrna.getCreationEvent();
1758 30 Nov 12 nicklas 753               evt.setEventDate(mrnaDate);
1758 30 Nov 12 nicklas 754               evt.setProtocol(mrnaProtocol);
1758 30 Nov 12 nicklas 755             }
1889 27 Feb 13 nicklas 756             if (!failed)
1889 27 Feb 13 nicklas 757             {
1889 27 Feb 13 nicklas 758               Extract cdna = Extract.getNew(dc, cdnaCreation);
4888 05 Jul 18 nicklas 759               String cdnaName = m.getNextCdnaName(dc);
4888 05 Jul 18 nicklas 760               cdna.setName(cdnaName);
1889 27 Feb 13 nicklas 761               cdna.setItemSubtype(cdnaType);
5436 17 May 19 nicklas 762               Pipeline.RNA_SEQ.setAnnotation(dc, cdna);
1889 27 Feb 13 nicklas 763               cdna.setBioWell(cdnaPlate.getBioWell(well.getPlateCoordinate()));
1889 27 Feb 13 nicklas 764               cdna.getCreationEvent().setSource(mrna);
5791 16 Dec 19 nicklas 765               DoNotUse.copyDoNotUseAnnotations(dc, mrna, cdna, false);
1889 27 Feb 13 nicklas 766               dc.saveItem(cdna);
1889 27 Feb 13 nicklas 767               numCdna++;
1889 27 Feb 13 nicklas 768             }
1889 27 Feb 13 nicklas 769             else
1889 27 Feb 13 nicklas 770             {
5553 12 Aug 19 nicklas 771               if (!Reggie.isExternalItem(mrna.getName()))
1890 01 Mar 13 nicklas 772               {
2906 07 Nov 14 nicklas 773                 Extract rna = m.getRna().getRnaIfPrenormalized(dc).getItem();
1890 01 Mar 13 nicklas 774                 Annotationtype.FLAG.setAnnotationValue(dc, rna, Rna.FLAG_MRNA_PLATE_FAILED);
1890 01 Mar 13 nicklas 775                 flaggedList.add(rna);
1890 01 Mar 13 nicklas 776                 numFlagged++;
1890 01 Mar 13 nicklas 777               }
1889 27 Feb 13 nicklas 778             }
1758 30 Nov 12 nicklas 779           }
1758 30 Nov 12 nicklas 780         }
1758 30 Nov 12 nicklas 781         
1889 27 Feb 13 nicklas 782         if (!failed)
1889 27 Feb 13 nicklas 783         {
1889 27 Feb 13 nicklas 784           jsonMessages.add(numCdna + " cDNA extracts created");
1889 27 Feb 13 nicklas 785           jsonMessages.add("Prepared child cDNA plate '" + cdnaPlate.getName() + "' for future processing");
1889 27 Feb 13 nicklas 786         }
1889 27 Feb 13 nicklas 787         else
1889 27 Feb 13 nicklas 788         {
1889 27 Feb 13 nicklas 789           jsonMessages.add(numFlagged + " RNA items flagged for re-processing");
1889 27 Feb 13 nicklas 790           jsonMessages.add("No child cDNA plate created!");
1889 27 Feb 13 nicklas 791         }
1890 01 Mar 13 nicklas 792         jsonMessages.add("mRNA plate '" + mrnaPlate.getName() + "' registered with status: " + (failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL));
1758 30 Nov 12 nicklas 793         
1758 30 Nov 12 nicklas 794         dc.commit();
1758 30 Nov 12 nicklas 795       }
1762 03 Dec 12 nicklas 796       else if ("ImportCDnaResults".equals(cmd))
1762 03 Dec 12 nicklas 797       {
6333 15 Jun 21 nicklas 798         dc = sc.newDbControl(":cDNA registration");
2161 09 Dec 13 nicklas 799
2161 09 Dec 13 nicklas 800         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 801
3752 17 Feb 16 nicklas 802         JSONObject jsonReq = JsonUtil.parseRequest(req);
1762 03 Dec 12 nicklas 803         
1762 03 Dec 12 nicklas 804         Number bioPlateId = (Number)jsonReq.get("bioplate");
1762 03 Dec 12 nicklas 805         Number cdnaProtocolId = (Number)jsonReq.get("cdnaProtocol");
1890 01 Mar 13 nicklas 806         boolean failed = Boolean.TRUE.equals(jsonReq.get("failed"));
1758 30 Nov 12 nicklas 807
1762 03 Dec 12 nicklas 808         // Load cDNA plate and the creation event
1762 03 Dec 12 nicklas 809         ReactionPlate cdnaReactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.CDNA);
1762 03 Dec 12 nicklas 810         BioPlate cdnaPlate = cdnaReactionPlate.getBioPlate();
1762 03 Dec 12 nicklas 811         
1762 03 Dec 12 nicklas 812         // Find the creation event for the cDNA plate
1762 03 Dec 12 nicklas 813         List<BioPlateEvent> events = cdnaReactionPlate.findEvents(dc, BioPlateEventType.CREATE_BIOMATERIAL, "child");
1762 03 Dec 12 nicklas 814         if (events.size() > 1)
1762 03 Dec 12 nicklas 815         {
1762 03 Dec 12 nicklas 816           throw new InvalidDataException("Found > 1 creation event for cDNA plate: " + cdnaReactionPlate.getName());
1762 03 Dec 12 nicklas 817         }
1762 03 Dec 12 nicklas 818         // ...it may not exist if a plate has been created with a batch import...
1762 03 Dec 12 nicklas 819         BioPlateEvent cdnaPlateCreationEvent = events.size() == 0 ? null : events.get(0);
1762 03 Dec 12 nicklas 820         
1762 03 Dec 12 nicklas 821         // Set creation date and protocol
1999 29 May 13 nicklas 822         Date cdnaDate = new Date();
1762 03 Dec 12 nicklas 823         cdnaPlate.setEventDate(cdnaDate);
1762 03 Dec 12 nicklas 824         Protocol cdnaProtocol = cdnaProtocolId == null ? null : Protocol.getById(dc, cdnaProtocolId.intValue());
1762 03 Dec 12 nicklas 825         if (cdnaPlateCreationEvent != null)
1762 03 Dec 12 nicklas 826         {
1890 01 Mar 13 nicklas 827           // This automatically propagated to all cDNA items on the plate
1762 03 Dec 12 nicklas 828           cdnaPlateCreationEvent.setEventDate(cdnaDate);
1762 03 Dec 12 nicklas 829           cdnaPlateCreationEvent.setProtocol(cdnaProtocol);
1762 03 Dec 12 nicklas 830         }
1762 03 Dec 12 nicklas 831   
1999 29 May 13 nicklas 832         // Dates and operators
1999 29 May 13 nicklas 833         Annotationtype.SYNTHESIS_DATE.setAnnotationValue(dc, cdnaPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("synthesisDate")));
1999 29 May 13 nicklas 834         Annotationtype.SYNTHESIS_OPERATOR.setAnnotationValue(dc, cdnaPlate, Values.getStringOrNull((String)jsonReq.get("synthesisOperator")));
1762 03 Dec 12 nicklas 835         
1762 03 Dec 12 nicklas 836         // Comment about the plate
1890 01 Mar 13 nicklas 837         cdnaPlate.setDescription((String)jsonReq.get("comments"));
1762 03 Dec 12 nicklas 838         
1890 01 Mar 13 nicklas 839         // Register the success or failure of the plate
1890 01 Mar 13 nicklas 840         Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, cdnaPlate, failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL);
1890 01 Mar 13 nicklas 841         cdnaPlate.setDestroyed(failed);
1890 01 Mar 13 nicklas 842
1890 01 Mar 13 nicklas 843         List<BioWell> wells = cdnaPlate.getBioWells().list(dc);
1762 03 Dec 12 nicklas 844         
1890 01 Mar 13 nicklas 845         // Flag the parent RNA items
3247 14 Apr 15 nicklas 846         ItemList flaggedList = BiomaterialList.FLAGGED_RNA.load(dc);
1890 01 Mar 13 nicklas 847         int numFlagged = 0;
1890 01 Mar 13 nicklas 848         
1890 01 Mar 13 nicklas 849         for (BioWell well : wells)
1890 01 Mar 13 nicklas 850         {
1890 01 Mar 13 nicklas 851           if (!well.isEmpty())
1890 01 Mar 13 nicklas 852           {
1890 01 Mar 13 nicklas 853             Extract cdna = (Extract)well.getBioMaterial();
2906 07 Nov 14 nicklas 854             CDna c = CDna.get(cdna);
2906 07 Nov 14 nicklas 855             
1890 01 Mar 13 nicklas 856             if (cdnaPlateCreationEvent == null)
1890 01 Mar 13 nicklas 857             {
1890 01 Mar 13 nicklas 858               // If no plate event existed, we must set protocol and date on all CDNA items
1890 01 Mar 13 nicklas 859               BioMaterialEvent evt = cdna.getCreationEvent();
1890 01 Mar 13 nicklas 860               evt.setEventDate(cdnaDate);
1890 01 Mar 13 nicklas 861               evt.setProtocol(cdnaProtocol);
1890 01 Mar 13 nicklas 862             }
1890 01 Mar 13 nicklas 863             if (failed)
1890 01 Mar 13 nicklas 864             {
5553 12 Aug 19 nicklas 865               if (!Reggie.isExternalItem(cdna.getName()))
1890 01 Mar 13 nicklas 866               {
2906 07 Nov 14 nicklas 867                 MRna m =  c.getMRna();
2906 07 Nov 14 nicklas 868                 Rna r = m.getRna().getRnaIfPrenormalized(dc);
2906 07 Nov 14 nicklas 869                 Extract rna = r.getItem();
1890 01 Mar 13 nicklas 870                 Annotationtype.FLAG.setAnnotationValue(dc, rna, Rna.FLAG_CDNA_PLATE_FAILED);
1890 01 Mar 13 nicklas 871                 flaggedList.add(rna);
1890 01 Mar 13 nicklas 872                 numFlagged++;
1890 01 Mar 13 nicklas 873               }
1890 01 Mar 13 nicklas 874             }
1890 01 Mar 13 nicklas 875           }
1890 01 Mar 13 nicklas 876         }
1890 01 Mar 13 nicklas 877         
1890 01 Mar 13 nicklas 878         if (numFlagged > 0)
1890 01 Mar 13 nicklas 879         {
1890 01 Mar 13 nicklas 880           jsonMessages.add(numFlagged + " RNA items flagged for re-processing");
1890 01 Mar 13 nicklas 881         }
5382 24 Apr 19 nicklas 882
1890 01 Mar 13 nicklas 883         jsonMessages.add("cDNA plate '" + cdnaPlate.getName() + "' registered with status: " + (failed ? ReactionPlate.PROCESS_FAILED : ReactionPlate.PROCESS_SUCCESSFUL));
1762 03 Dec 12 nicklas 884         dc.commit();
1762 03 Dec 12 nicklas 885       }
1762 03 Dec 12 nicklas 886
1742 23 Nov 12 nicklas 887       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 888       CounterService.getInstance().setForceCount();
1742 23 Nov 12 nicklas 889     }
1742 23 Nov 12 nicklas 890     catch (Throwable t)
1742 23 Nov 12 nicklas 891     {
1742 23 Nov 12 nicklas 892       t.printStackTrace();
1742 23 Nov 12 nicklas 893       json.clear();
1742 23 Nov 12 nicklas 894       json.put("status", "error");
1742 23 Nov 12 nicklas 895       json.put("message", t.getMessage());
1742 23 Nov 12 nicklas 896       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1742 23 Nov 12 nicklas 897     }
1742 23 Nov 12 nicklas 898     finally
1742 23 Nov 12 nicklas 899     {
1742 23 Nov 12 nicklas 900       if (dc != null) dc.close();
1742 23 Nov 12 nicklas 901       json.writeJSONString(resp.getWriter());
1742 23 Nov 12 nicklas 902     }
1742 23 Nov 12 nicklas 903     
1742 23 Nov 12 nicklas 904   }
1742 23 Nov 12 nicklas 905   
3651 07 Dec 15 nicklas 906   /**
3651 07 Dec 15 nicklas 907     Find a project default RNA_NORMALIZATION_PROTOCOL that has RNA_TARGET_AMOUNT
3651 07 Dec 15 nicklas 908     close (witin 5%) to the specified amount. If there are more than
3651 07 Dec 15 nicklas 909     one protocol the latest (the one with the highest ID) is selected.
3651 07 Dec 15 nicklas 910     Return the protocol or null if no protocol can be found
3651 07 Dec 15 nicklas 911   */
3651 07 Dec 15 nicklas 912   private Protocol findNormalizationProtocol(DbControl dc, float targetRnaAmount)
3651 07 Dec 15 nicklas 913   {
3651 07 Dec 15 nicklas 914     // Find the pre-normalization protocol to use 
3651 07 Dec 15 nicklas 915     // We use the latest project default protocol matching the target RNA amount
3651 07 Dec 15 nicklas 916     Project project = Project.getById(dc, dc.getSessionControl().getActiveProjectId());
5363 16 Apr 19 nicklas 917     List<Protocol> protocols = project.findDefaultItems(dc, Subtype.RNA_NORMALIZATION_PROTOCOL.get(dc), true);
3651 07 Dec 15 nicklas 918     
3651 07 Dec 15 nicklas 919     // We only accept protocols that are within 5% of the requested target amount
3651 07 Dec 15 nicklas 920     float acceptableDelta = targetRnaAmount * 0.05f; 
3651 07 Dec 15 nicklas 921
3651 07 Dec 15 nicklas 922     Protocol protocol = null;
3651 07 Dec 15 nicklas 923     for (Protocol p : protocols)
3651 07 Dec 15 nicklas 924     {
3651 07 Dec 15 nicklas 925       Float amount = (Float)Annotationtype.RNA_TARGET_AMOUNT.getAnnotationValue(dc, p);
3651 07 Dec 15 nicklas 926       if (amount == null) continue;
3651 07 Dec 15 nicklas 927       
3651 07 Dec 15 nicklas 928       if (Math.abs(targetRnaAmount-amount) < acceptableDelta)
3651 07 Dec 15 nicklas 929       {
3651 07 Dec 15 nicklas 930         // Use the latest protocol
3651 07 Dec 15 nicklas 931         if (protocol == null || p.getId() > protocol.getId())
3651 07 Dec 15 nicklas 932         {
3651 07 Dec 15 nicklas 933           protocol = p;
3651 07 Dec 15 nicklas 934         }
3651 07 Dec 15 nicklas 935       }
3651 07 Dec 15 nicklas 936     }
3651 07 Dec 15 nicklas 937     
3651 07 Dec 15 nicklas 938     return protocol;
3651 07 Dec 15 nicklas 939   }
1742 23 Nov 12 nicklas 940   
1742 23 Nov 12 nicklas 941 }