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

Code
Comments
Other
Rev Date Author Line
1543 28 Feb 12 nicklas 1 package net.sf.basedb.reggie.servlet;
1543 28 Feb 12 nicklas 2
1543 28 Feb 12 nicklas 3 import java.io.IOException;
1544 29 Feb 12 nicklas 4 import java.io.Writer;
1543 28 Feb 12 nicklas 5 import java.util.Date;
1543 28 Feb 12 nicklas 6 import java.util.List;
1543 28 Feb 12 nicklas 7
1543 28 Feb 12 nicklas 8 import javax.servlet.ServletException;
1543 28 Feb 12 nicklas 9 import javax.servlet.http.HttpServlet;
1543 28 Feb 12 nicklas 10 import javax.servlet.http.HttpServletRequest;
1543 28 Feb 12 nicklas 11 import javax.servlet.http.HttpServletResponse;
1543 28 Feb 12 nicklas 12
1543 28 Feb 12 nicklas 13 import org.json.simple.JSONArray;
1543 28 Feb 12 nicklas 14 import org.json.simple.JSONObject;
1543 28 Feb 12 nicklas 15
1546 02 Mar 12 nicklas 16 import net.sf.basedb.core.AnnotationType;
2922 12 Nov 14 nicklas 17 import net.sf.basedb.core.AnyToAny;
3555 21 Oct 15 nicklas 18 import net.sf.basedb.core.BioMaterialEvent;
1551 06 Mar 12 nicklas 19 import net.sf.basedb.core.BioMaterialEventSource;
1543 28 Feb 12 nicklas 20 import net.sf.basedb.core.BioPlate;
1543 28 Feb 12 nicklas 21 import net.sf.basedb.core.BioPlateEvent;
1543 28 Feb 12 nicklas 22 import net.sf.basedb.core.BioPlateEventParticipant;
1543 28 Feb 12 nicklas 23 import net.sf.basedb.core.BioPlateEventType;
1552 07 Mar 12 nicklas 24 import net.sf.basedb.core.BioPlateType;
1543 28 Feb 12 nicklas 25 import net.sf.basedb.core.BioWell;
1543 28 Feb 12 nicklas 26 import net.sf.basedb.core.DbControl;
1543 28 Feb 12 nicklas 27 import net.sf.basedb.core.Extract;
1546 02 Mar 12 nicklas 28 import net.sf.basedb.core.File;
4897 10 Jul 18 nicklas 29 import net.sf.basedb.core.Item;
1543 28 Feb 12 nicklas 30 import net.sf.basedb.core.ItemNotFoundException;
1543 28 Feb 12 nicklas 31 import net.sf.basedb.core.ItemQuery;
1544 29 Feb 12 nicklas 32 import net.sf.basedb.core.Path;
1552 07 Mar 12 nicklas 33 import net.sf.basedb.core.PlateGeometry;
1564 16 Mar 12 nicklas 34 import net.sf.basedb.core.PluginConfiguration;
1564 16 Mar 12 nicklas 35 import net.sf.basedb.core.PluginDefinition;
1543 28 Feb 12 nicklas 36 import net.sf.basedb.core.Protocol;
1543 28 Feb 12 nicklas 37 import net.sf.basedb.core.SessionControl;
1564 16 Mar 12 nicklas 38 import net.sf.basedb.core.plugin.ParameterValues;
1564 16 Mar 12 nicklas 39 import net.sf.basedb.core.query.Hql;
1564 16 Mar 12 nicklas 40 import net.sf.basedb.core.query.Orders;
1983 21 May 13 nicklas 41 import net.sf.basedb.core.query.Restrictions;
2598 22 Aug 14 nicklas 42 import net.sf.basedb.reggie.JsonUtil;
1543 28 Feb 12 nicklas 43 import net.sf.basedb.reggie.Reggie;
5384 26 Apr 19 nicklas 44 import net.sf.basedb.reggie.activity.ActivityDef;
1900 07 Mar 13 nicklas 45 import net.sf.basedb.reggie.converter.WellCoordinateConverter;
3059 19 Dec 14 nicklas 46 import net.sf.basedb.reggie.counter.CounterService;
1610 23 Apr 12 nicklas 47 import net.sf.basedb.reggie.dao.Annotationtype;
1544 29 Feb 12 nicklas 48 import net.sf.basedb.reggie.dao.BioplateType;
5791 16 Dec 19 nicklas 49 import net.sf.basedb.reggie.dao.DoNotUse;
3742 12 Feb 16 nicklas 50 import net.sf.basedb.reggie.dao.Lysate;
1543 28 Feb 12 nicklas 51 import net.sf.basedb.reggie.dao.ReactionPlate;
4897 10 Jul 18 nicklas 52 import net.sf.basedb.reggie.dao.ReggieItem;
2161 09 Dec 13 nicklas 53 import net.sf.basedb.reggie.dao.ReggieRole;
1543 28 Feb 12 nicklas 54 import net.sf.basedb.reggie.dao.Rna;
3742 12 Feb 16 nicklas 55 import net.sf.basedb.reggie.dao.SpecimenTube;
1543 28 Feb 12 nicklas 56 import net.sf.basedb.reggie.dao.Subtype;
1546 02 Mar 12 nicklas 57 import net.sf.basedb.reggie.plugins.CaliperPlateImporter;
1564 16 Mar 12 nicklas 58 import net.sf.basedb.reggie.plugins.CaliperRunParametersExporter;
1544 29 Feb 12 nicklas 59 import net.sf.basedb.reggie.plugins.CaliperSampleNameExporter;
1543 28 Feb 12 nicklas 60 import net.sf.basedb.util.Coordinate;
1543 28 Feb 12 nicklas 61 import net.sf.basedb.util.Values;
1543 28 Feb 12 nicklas 62 import net.sf.basedb.util.error.ThrowableUtil;
1543 28 Feb 12 nicklas 63
1543 28 Feb 12 nicklas 64
1543 28 Feb 12 nicklas 65 public class RnaQcServlet 
1543 28 Feb 12 nicklas 66   extends HttpServlet 
1543 28 Feb 12 nicklas 67 {
1543 28 Feb 12 nicklas 68
1543 28 Feb 12 nicklas 69   private static final long serialVersionUID = -8104884012462693394L;
1543 28 Feb 12 nicklas 70
1543 28 Feb 12 nicklas 71   public RnaQcServlet()
1543 28 Feb 12 nicklas 72   {}
1543 28 Feb 12 nicklas 73
1543 28 Feb 12 nicklas 74   @Override
1543 28 Feb 12 nicklas 75   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1543 28 Feb 12 nicklas 76     throws ServletException, IOException 
1543 28 Feb 12 nicklas 77   {
1543 28 Feb 12 nicklas 78     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 79     JsonUtil.setJsonResponseHeaders(resp);
1543 28 Feb 12 nicklas 80     
1543 28 Feb 12 nicklas 81     JSONObject json = new JSONObject();
1543 28 Feb 12 nicklas 82     json.put("status", "ok");
1543 28 Feb 12 nicklas 83   
3975 26 May 16 nicklas 84     final SessionControl sc = Reggie.getSessionControl(req);
1543 28 Feb 12 nicklas 85     DbControl dc = null;
1543 28 Feb 12 nicklas 86     try
1543 28 Feb 12 nicklas 87     {
3059 19 Dec 14 nicklas 88       if ("GetActiveRnaQcBioPlates".equals(cmd))
1983 21 May 13 nicklas 89       {
1543 28 Feb 12 nicklas 90         /*
1543 28 Feb 12 nicklas 91           Find caliper/bioanalyzer bioplates that is currently being filled
1543 28 Feb 12 nicklas 92           with aliquots. We look for plates that has a bioplate type with
1543 28 Feb 12 nicklas 93           a restriction to the RNAQC subtype that have at least one free well
1543 28 Feb 12 nicklas 94           and are not marked as destroyed and has no value for ther QCRunDate 
1543 28 Feb 12 nicklas 95           annotation.
1543 28 Feb 12 nicklas 96         */
1544 29 Feb 12 nicklas 97         int numFreeWells = Values.getInt(req.getParameter("numFreeWells"), 1);
1544 29 Feb 12 nicklas 98         BioplateType bioPlateType = BioplateType.getByCName(req.getParameter("bioPlateType"));
1544 29 Feb 12 nicklas 99         
6330 14 Jun 21 nicklas 100         dc = sc.newDbControl(":Create aliquots for RNAQC");
1544 29 Feb 12 nicklas 101         List<ReactionPlate> plates = ReactionPlate.findActiveByBioMaterialSubtype(dc, 
1610 23 Apr 12 nicklas 102             bioPlateType, Subtype.RNAQC, numFreeWells, Annotationtype.QC_RUN_DATE);
1543 28 Feb 12 nicklas 103         
1543 28 Feb 12 nicklas 104         JSONArray jsonPlates = new JSONArray();
1543 28 Feb 12 nicklas 105         for (ReactionPlate plate : plates)
1543 28 Feb 12 nicklas 106         {
1543 28 Feb 12 nicklas 107           jsonPlates.add(plate.asJSONObject());
1543 28 Feb 12 nicklas 108         }
1543 28 Feb 12 nicklas 109         json.put("plates", jsonPlates);
3742 12 Feb 16 nicklas 110         json.put("YellowLabel", Annotationtype.YELLOW_LABEL.get(dc).getId());
1543 28 Feb 12 nicklas 111       }
1543 28 Feb 12 nicklas 112       else if ("GetRnaExtractsWithoutQc".equals(cmd))
1543 28 Feb 12 nicklas 113       {
1543 28 Feb 12 nicklas 114         /*
1543 28 Feb 12 nicklas 115           Find RNA items that have not yet been checked.
1543 28 Feb 12 nicklas 116         */
6330 14 Jun 21 nicklas 117         dc = sc.newDbControl(":Create aliquots for RNAQC");
1543 28 Feb 12 nicklas 118         List<Rna> rna = Rna.findRnaWithoutQc(dc);
1543 28 Feb 12 nicklas 119         
1543 28 Feb 12 nicklas 120         JSONArray jsonRna = new JSONArray();
1543 28 Feb 12 nicklas 121         
1543 28 Feb 12 nicklas 122         for (Rna r : rna)
1543 28 Feb 12 nicklas 123         {
3742 12 Feb 16 nicklas 124           Lysate l = r.getLysate();
3742 12 Feb 16 nicklas 125           SpecimenTube sp = l != null ? l.getSpecimen() : null;
1573 19 Mar 12 nicklas 126           r.loadBioPlateLocation();
4891 09 Jul 18 nicklas 127           r.setAnnotation("nextRnaQcName", r.getName()+"." + Subtype.RNAQC.getItemSuffix()); // Safe, since we know that the RNA has no RNAQC yet
1610 23 Apr 12 nicklas 128           r.loadAnnotations(dc, "NDConc", Annotationtype.ND_CONC, null);
6219 20 Apr 21 nicklas 129           r.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
6219 20 Apr 21 nicklas 130           r.coalesceInto("conc", "NDConc", "QubitConc");
3742 12 Feb 16 nicklas 131           if (sp != null) 
3742 12 Feb 16 nicklas 132           {
3742 12 Feb 16 nicklas 133             sp.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
3742 12 Feb 16 nicklas 134             r.setAnnotation("specimen", sp.asJSONObject());
3742 12 Feb 16 nicklas 135           }
1543 28 Feb 12 nicklas 136           jsonRna.add(r.asJSONObject());
1543 28 Feb 12 nicklas 137         }
1543 28 Feb 12 nicklas 138         
1543 28 Feb 12 nicklas 139         json.put("rnaExtracts", jsonRna);
1543 28 Feb 12 nicklas 140       }
1543 28 Feb 12 nicklas 141       else if ("GetRnaInfo".equals(cmd))
1543 28 Feb 12 nicklas 142       {
1543 28 Feb 12 nicklas 143         /*
1543 28 Feb 12 nicklas 144           Get information about the requested RNA. We are
1543 28 Feb 12 nicklas 145           particularly interested in the next available 
1543 28 Feb 12 nicklas 146           RNAQC name
1543 28 Feb 12 nicklas 147         */
6330 14 Jun 21 nicklas 148         dc = sc.newDbControl(":Create aliquots for RNAQC");
2808 15 Oct 14 nicklas 149         Integer[] rnaIds = Values.getInt(req.getParameter("rnaIds").split(","));
1543 28 Feb 12 nicklas 150         
2808 15 Oct 14 nicklas 151         JSONArray jsonRna = new JSONArray();
2808 15 Oct 14 nicklas 152         for (int i = 0; i < rnaIds.length; i++)
2808 15 Oct 14 nicklas 153         {
2808 15 Oct 14 nicklas 154           Rna rna = Rna.getById(dc, rnaIds[i]);
2808 15 Oct 14 nicklas 155           rna.loadBioPlateLocation();
4891 09 Jul 18 nicklas 156           rna.setAnnotation("nextRnaQcName", rna.getNextRnaQcName(dc));
2808 15 Oct 14 nicklas 157           rna.loadAnnotations(dc, "NDConc", Annotationtype.ND_CONC, null);
6219 20 Apr 21 nicklas 158           rna.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
6219 20 Apr 21 nicklas 159           rna.coalesceInto("conc", "NDConc", "QubitConc");
2808 15 Oct 14 nicklas 160           jsonRna.add(rna.asJSONObject());
2808 15 Oct 14 nicklas 161         }
2808 15 Oct 14 nicklas 162         json.put("rna", jsonRna);
1543 28 Feb 12 nicklas 163       }
1552 07 Mar 12 nicklas 164       else if ("GetNextAutoGeneratedPlateName".equals(cmd))
1552 07 Mar 12 nicklas 165       {
1552 07 Mar 12 nicklas 166         /*
1552 07 Mar 12 nicklas 167           Eg. CaliperPlate0001, CaliperPlate0002 and so on
1552 07 Mar 12 nicklas 168          */
1552 07 Mar 12 nicklas 169         BioplateType bioPlateType = BioplateType.getByCName(req.getParameter("bioPlateType"));
6330 14 Jun 21 nicklas 170         dc = sc.newDbControl(":Create aliquots for RNAQC");
4891 09 Jul 18 nicklas 171         json.put("nextPlateName", bioPlateType.getNextPlateName(dc, false));
1552 07 Mar 12 nicklas 172       }
1543 28 Feb 12 nicklas 173       else if ("GetFreeWellsInfo".equals(cmd))
1543 28 Feb 12 nicklas 174       {
1543 28 Feb 12 nicklas 175         /*
1543 28 Feb 12 nicklas 176           Get information about free wells on the requested plate
1543 28 Feb 12 nicklas 177         */
1544 29 Feb 12 nicklas 178         int bioPlateId = Values.getInt(req.getParameter("bioPlateId"));
1552 07 Mar 12 nicklas 179
6330 14 Jun 21 nicklas 180         dc = sc.newDbControl(":Create aliquots for RNAQC");
1552 07 Mar 12 nicklas 181         ReactionPlate plate = null;
1552 07 Mar 12 nicklas 182         if (bioPlateId == 0)
1552 07 Mar 12 nicklas 183         {
1552 07 Mar 12 nicklas 184           BioplateType bioPlateType = BioplateType.getByCName(req.getParameter("bioPlateType"));
1552 07 Mar 12 nicklas 185           plate = ReactionPlate.getNew(dc, bioPlateType);
1552 07 Mar 12 nicklas 186         }
1552 07 Mar 12 nicklas 187         else
1552 07 Mar 12 nicklas 188         {
1668 25 May 12 nicklas 189           plate = ReactionPlate.getById(dc, bioPlateId, null);
1552 07 Mar 12 nicklas 190         }
1543 28 Feb 12 nicklas 191         
1668 25 May 12 nicklas 192         plate.loadBioWells(dc, false);
1543 28 Feb 12 nicklas 193         
1543 28 Feb 12 nicklas 194         json.put("bioPlate", plate.asJSONObject());
1543 28 Feb 12 nicklas 195       }
1564 16 Mar 12 nicklas 196       else if ("GetConfigurationsForRunParametersExporter".equals(cmd))
1564 16 Mar 12 nicklas 197       {
1564 16 Mar 12 nicklas 198         /*
1564 16 Mar 12 nicklas 199           Get all configurations for the CaliperRunParametersExporter plugin
1564 16 Mar 12 nicklas 200         */
6330 14 Jun 21 nicklas 201         dc = sc.newDbControl(":Export Caliper run parameters");
1564 16 Mar 12 nicklas 202         PluginDefinition plugin = PluginDefinition.getByClassName(dc, CaliperRunParametersExporter.class.getName());
1564 16 Mar 12 nicklas 203         ItemQuery<PluginConfiguration> query = plugin.getPluginConfigurations();
1564 16 Mar 12 nicklas 204         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1564 16 Mar 12 nicklas 205         query.order(Orders.asc(Hql.property("name")));
1564 16 Mar 12 nicklas 206         
1564 16 Mar 12 nicklas 207         JSONArray jsonConfigurations = new JSONArray();
1564 16 Mar 12 nicklas 208         for (PluginConfiguration p : query.list(dc))
1564 16 Mar 12 nicklas 209         {
1564 16 Mar 12 nicklas 210           JSONObject jsonConfiguration = new JSONObject();
1564 16 Mar 12 nicklas 211           jsonConfiguration.put("id", p.getId());
1564 16 Mar 12 nicklas 212           jsonConfiguration.put("name", p.getName());
1564 16 Mar 12 nicklas 213           jsonConfigurations.add(jsonConfiguration);
1564 16 Mar 12 nicklas 214         }
1564 16 Mar 12 nicklas 215         
1564 16 Mar 12 nicklas 216         json.put("configurations", jsonConfigurations);
1564 16 Mar 12 nicklas 217       }
1544 29 Feb 12 nicklas 218       else if ("ExportSampleNamesForCaliper".equals(cmd))
1544 29 Feb 12 nicklas 219       {
1544 29 Feb 12 nicklas 220         /*
1544 29 Feb 12 nicklas 221           Generate CSV file with sample names for the Caliper software
1544 29 Feb 12 nicklas 222         */
1544 29 Feb 12 nicklas 223         int bioPlateId = Values.getInt(req.getParameter("bioPlateId"));
6330 14 Jun 21 nicklas 224         dc = sc.newDbControl(":Export Caliper sample names");
1544 29 Feb 12 nicklas 225         BioPlate plate = BioPlate.getById(dc, bioPlateId);
1900 07 Mar 13 nicklas 226             
1544 29 Feb 12 nicklas 227         resp.setHeader("Content-Disposition", "attachment; filename=" + Path.makeSafeFilename(plate.getName(), "") + ".csv");
1544 29 Feb 12 nicklas 228         resp.setContentType("text/csv");
1544 29 Feb 12 nicklas 229         resp.setCharacterEncoding("UTF-8");
1544 29 Feb 12 nicklas 230         Writer out = resp.getWriter();
1544 29 Feb 12 nicklas 231         CaliperSampleNameExporter exporter = new CaliperSampleNameExporter();
1900 07 Mar 13 nicklas 232         exporter.exportSampleNames(dc, plate, new WellCoordinateConverter(), out);
1544 29 Feb 12 nicklas 233   
1544 29 Feb 12 nicklas 234         out.flush();
1544 29 Feb 12 nicklas 235         out.close();
1544 29 Feb 12 nicklas 236       }
1564 16 Mar 12 nicklas 237       else if ("ExportRunParameters".equals(cmd))
1564 16 Mar 12 nicklas 238       {
1564 16 Mar 12 nicklas 239         /*
1564 16 Mar 12 nicklas 240           Generate run parameter XML file for the Caliper software
1564 16 Mar 12 nicklas 241         */
1564 16 Mar 12 nicklas 242         int bioPlateId = Values.getInt(req.getParameter("bioPlateId"));
1564 16 Mar 12 nicklas 243         int configurationId = Values.getInt(req.getParameter("configurationId"));
1564 16 Mar 12 nicklas 244         
6330 14 Jun 21 nicklas 245         dc = sc.newDbControl(":Export Caliper run parameters");
1564 16 Mar 12 nicklas 246         BioPlate plate = BioPlate.getById(dc, bioPlateId);
1564 16 Mar 12 nicklas 247         PluginConfiguration config = PluginConfiguration.getById(dc, configurationId);
1564 16 Mar 12 nicklas 248         ParameterValues parameters = config.getParameterValues(config.getParameterVersion());
1564 16 Mar 12 nicklas 249         boolean hiSense = "High".equals(parameters.getValue("sensitivity"));
1564 16 Mar 12 nicklas 250         
1564 16 Mar 12 nicklas 251         resp.setHeader("Content-Disposition", "attachment; filename=" + Path.makeSafeFilename(plate.getName(), "") + (hiSense ? "_hi" : "_std") + ".xml");
1564 16 Mar 12 nicklas 252         resp.setContentType("application/xml");
1564 16 Mar 12 nicklas 253         resp.setCharacterEncoding("UTF-8");
1564 16 Mar 12 nicklas 254         Writer out = resp.getWriter();
1564 16 Mar 12 nicklas 255         
1564 16 Mar 12 nicklas 256         CaliperRunParametersExporter exporter = new CaliperRunParametersExporter();
1564 16 Mar 12 nicklas 257         exporter.exportRunParameters(dc, plate, hiSense, parameters, out);
1564 16 Mar 12 nicklas 258         
1564 16 Mar 12 nicklas 259         out.flush();
1564 16 Mar 12 nicklas 260         out.close();
1564 16 Mar 12 nicklas 261       }
2922 12 Nov 14 nicklas 262       else if ("GetRnaQcItemsOnBioPlate".equals(cmd))
2922 12 Nov 14 nicklas 263       {
2922 12 Nov 14 nicklas 264         int bioPlateId = Values.getInt(req.getParameter("bioPlateId"));
2922 12 Nov 14 nicklas 265         
6330 14 Jun 21 nicklas 266         dc = sc.newDbControl(":RNAQC wizard");
2922 12 Nov 14 nicklas 267         BioPlate plate = BioPlate.getById(dc, bioPlateId);
2922 12 Nov 14 nicklas 268         
2922 12 Nov 14 nicklas 269         ItemQuery<Extract> query = Extract.getQuery();
2922 12 Nov 14 nicklas 270         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2922 12 Nov 14 nicklas 271         Subtype.RNAQC.addFilter(dc, query);
2922 12 Nov 14 nicklas 272         query.join(Hql.innerJoin("bioWell", "bw"));
2922 12 Nov 14 nicklas 273         query.restrict(Restrictions.eq(Hql.property("bw", "bioPlate"), Hql.entity(plate)));
2922 12 Nov 14 nicklas 274         query.order(Orders.asc(Hql.property("bw", "column")));
2922 12 Nov 14 nicklas 275         
2922 12 Nov 14 nicklas 276         JSONArray jsonRnaQc = new JSONArray();
2922 12 Nov 14 nicklas 277         for (Extract r : query.list(dc))
2922 12 Nov 14 nicklas 278         {
3742 12 Feb 16 nicklas 279           Rna rna = Rna.get((Extract)r.getParent());
3742 12 Feb 16 nicklas 280           Lysate l = rna.getLysate();
3742 12 Feb 16 nicklas 281           SpecimenTube sp = l != null ? l.getSpecimen() : null;
3742 12 Feb 16 nicklas 282
2922 12 Nov 14 nicklas 283           JSONObject jsonQc = new JSONObject();
2922 12 Nov 14 nicklas 284           jsonQc.put("id", r.getId());
2922 12 Nov 14 nicklas 285           jsonQc.put("name", r.getName());
2922 12 Nov 14 nicklas 286           jsonQc.put("bioWell", JsonUtil.getBioWellAsJSON(r.getBioWell(), false));
3742 12 Feb 16 nicklas 287           
3742 12 Feb 16 nicklas 288           if (sp != null) 
3742 12 Feb 16 nicklas 289           {
3742 12 Feb 16 nicklas 290             sp.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
3742 12 Feb 16 nicklas 291             jsonQc.put("specimen", sp.asJSONObject());
3742 12 Feb 16 nicklas 292           }
3742 12 Feb 16 nicklas 293           
2922 12 Nov 14 nicklas 294           jsonRnaQc.add(jsonQc);
2922 12 Nov 14 nicklas 295         }
2922 12 Nov 14 nicklas 296         
2922 12 Nov 14 nicklas 297         json.put("rnaqc", jsonRnaQc);
2922 12 Nov 14 nicklas 298         
2922 12 Nov 14 nicklas 299       }
1543 28 Feb 12 nicklas 300       
1543 28 Feb 12 nicklas 301     }
1543 28 Feb 12 nicklas 302     catch (Throwable t)
1543 28 Feb 12 nicklas 303     {
1543 28 Feb 12 nicklas 304       t.printStackTrace();
1543 28 Feb 12 nicklas 305       json.clear();
1543 28 Feb 12 nicklas 306       json.put("status", "error");
1543 28 Feb 12 nicklas 307       json.put("message", t.getMessage());
1543 28 Feb 12 nicklas 308       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1543 28 Feb 12 nicklas 309     }
1543 28 Feb 12 nicklas 310     finally
1543 28 Feb 12 nicklas 311     {
1543 28 Feb 12 nicklas 312       if (dc != null) dc.close();
1543 28 Feb 12 nicklas 313       json.writeJSONString(resp.getWriter());
1543 28 Feb 12 nicklas 314     }
1543 28 Feb 12 nicklas 315     
1543 28 Feb 12 nicklas 316   }
1543 28 Feb 12 nicklas 317
1543 28 Feb 12 nicklas 318   @Override
1543 28 Feb 12 nicklas 319   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
1543 28 Feb 12 nicklas 320     throws ServletException, IOException 
1543 28 Feb 12 nicklas 321   {
1543 28 Feb 12 nicklas 322     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 323     JsonUtil.setJsonResponseHeaders(resp);
1543 28 Feb 12 nicklas 324     
1543 28 Feb 12 nicklas 325     JSONObject json = new JSONObject();
1543 28 Feb 12 nicklas 326     json.put("status", "ok");
1543 28 Feb 12 nicklas 327     
1543 28 Feb 12 nicklas 328     JSONArray jsonMessages = new JSONArray();
1543 28 Feb 12 nicklas 329   
3975 26 May 16 nicklas 330     final SessionControl sc = Reggie.getSessionControl(req);
1543 28 Feb 12 nicklas 331     DbControl dc = null;
1543 28 Feb 12 nicklas 332     try
1543 28 Feb 12 nicklas 333     {
1543 28 Feb 12 nicklas 334       if ("PlaceRnaQcAliquots".equals(cmd))
1543 28 Feb 12 nicklas 335       {
6330 14 Jun 21 nicklas 336         dc = sc.newDbControl(":Create aliquots for RNAQC");
2161 09 Dec 13 nicklas 337
2161 09 Dec 13 nicklas 338         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 339
3752 17 Feb 16 nicklas 340         JSONObject jsonReq = JsonUtil.parseRequest(req);
1543 28 Feb 12 nicklas 341         JSONArray jsonRnaQcExtracts = (JSONArray)jsonReq.get("rnaQcExtracts");
1543 28 Feb 12 nicklas 342         JSONObject jsonPlate = (JSONObject)jsonReq.get("bioplate");
2161 09 Dec 13 nicklas 343             
1610 23 Apr 12 nicklas 344         AnnotationType qcHiSenseType = Annotationtype.QC_HISENSE.load(dc);
1551 06 Mar 12 nicklas 345         
1552 07 Mar 12 nicklas 346         int plateId = ((Number)jsonPlate.get("id")).intValue();
2774 09 Oct 14 nicklas 347         Number protocolId = (Number)jsonReq.get("protocol");    
1551 06 Mar 12 nicklas 348         
1552 07 Mar 12 nicklas 349         BioPlate plate = null;
1552 07 Mar 12 nicklas 350         if (plateId == 0)
1552 07 Mar 12 nicklas 351         {
1552 07 Mar 12 nicklas 352           JSONObject jsonBioPlateType = (JSONObject)jsonPlate.get("bioPlateType");
1552 07 Mar 12 nicklas 353           JSONObject jsonGeometry = (JSONObject)jsonPlate.get("geometry");
1552 07 Mar 12 nicklas 354           
1552 07 Mar 12 nicklas 355           BioPlateType plateType = BioPlateType.getById(dc, ((Number)jsonBioPlateType.get("id")).intValue());
1552 07 Mar 12 nicklas 356           PlateGeometry geometry = PlateGeometry.getById(dc, ((Number)jsonGeometry.get("id")).intValue());
1552 07 Mar 12 nicklas 357           
1552 07 Mar 12 nicklas 358           plate = BioPlate.getNew(dc, geometry, plateType);
4897 10 Jul 18 nicklas 359           plate.setName(ReggieItem.ensureNonExistingItem(dc, Item.BIOPLATE, plateType.getName(), (String)jsonPlate.get("name")));
1552 07 Mar 12 nicklas 360           plate.setBarcode((String)jsonPlate.get("barcode"));
1552 07 Mar 12 nicklas 361           dc.saveItem(plate);
1552 07 Mar 12 nicklas 362           jsonMessages.add("Created bioplate '" + plate.getName() + "'");
1552 07 Mar 12 nicklas 363         }
1552 07 Mar 12 nicklas 364         else
1552 07 Mar 12 nicklas 365         {
1552 07 Mar 12 nicklas 366           plate = BioPlate.getById(dc, plateId);
1552 07 Mar 12 nicklas 367         }
1543 28 Feb 12 nicklas 368         BioPlateEventType eventType = BioPlateEventType.getById(dc, BioPlateEventType.CREATE_BIOMATERIAL);
1543 28 Feb 12 nicklas 369         BioPlateEvent event = BioPlateEvent.getNew(dc, eventType);
1543 28 Feb 12 nicklas 370         event.setEventDate(new Date());
1543 28 Feb 12 nicklas 371         event.setName("Create RNA QC aliquots");
1551 06 Mar 12 nicklas 372         if (protocolId != null)
1551 06 Mar 12 nicklas 373         {
1551 06 Mar 12 nicklas 374           event.setProtocol(Protocol.getById(dc, protocolId.intValue()));
1551 06 Mar 12 nicklas 375         }
1543 28 Feb 12 nicklas 376         dc.saveItem(event);
1543 28 Feb 12 nicklas 377         BioPlateEventParticipant destPlate = event.addParticipant(plate, "destination", 1);
1543 28 Feb 12 nicklas 378         dc.saveItem(destPlate);
1543 28 Feb 12 nicklas 379                 
1543 28 Feb 12 nicklas 380         for (int i = 0; i < jsonRnaQcExtracts.size(); ++i)
1543 28 Feb 12 nicklas 381         {
1543 28 Feb 12 nicklas 382           JSONObject jsonRnaQc = (JSONObject)jsonRnaQcExtracts.get(i);
1543 28 Feb 12 nicklas 383           
1543 28 Feb 12 nicklas 384           // Get the RNA extract which is the parent
1543 28 Feb 12 nicklas 385           Number parentRnaId = (Number)jsonRnaQc.get("parentId");
1543 28 Feb 12 nicklas 386           Extract parentRna = Extract.getById(dc, parentRnaId.intValue());
1543 28 Feb 12 nicklas 387           
1551 06 Mar 12 nicklas 388           Boolean hiSense = (Boolean)jsonRnaQc.get("QCHiSense");
3545 16 Oct 15 nicklas 389           Number volume = (Number)jsonRnaQc.get("volume");
1551 06 Mar 12 nicklas 390           
1543 28 Feb 12 nicklas 391           // Create a child RNAQC extract
1543 28 Feb 12 nicklas 392           Extract rnaQc = Extract.getNew(dc, destPlate);
1543 28 Feb 12 nicklas 393           rnaQc.setItemSubtype(Subtype.RNAQC.load(dc));
4897 10 Jul 18 nicklas 394           rnaQc.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.RNAQC, (String)jsonRnaQc.get("name")));
1551 06 Mar 12 nicklas 395           BioMaterialEventSource evtSrc = rnaQc.getCreationEvent().setSource(parentRna);
5791 16 Dec 19 nicklas 396           DoNotUse.copyDoNotUseAnnotations(dc, parentRna, rnaQc, false);
3545 16 Oct 15 nicklas 397           if (volume != null)
3545 16 Oct 15 nicklas 398           {
6219 20 Apr 21 nicklas 399             Float conc = (Float)Annotationtype.ND_CONC.getAnnotationValue(dc, parentRna);
6219 20 Apr 21 nicklas 400             if (conc == null) conc = (Float)Annotationtype.QUBIT_CONC.getAnnotationValue(dc, parentRna);
6219 20 Apr 21 nicklas 401             if (conc != null)
3545 16 Oct 15 nicklas 402             {
6219 20 Apr 21 nicklas 403               float usedQuantity = volume.floatValue() * conc / 1000;
3545 16 Oct 15 nicklas 404               evtSrc.setUsedQuantity(usedQuantity);
3545 16 Oct 15 nicklas 405             }
3545 16 Oct 15 nicklas 406           }
1543 28 Feb 12 nicklas 407           
1551 06 Mar 12 nicklas 408           if (Boolean.TRUE.equals(hiSense))
1551 06 Mar 12 nicklas 409           {
3545 16 Oct 15 nicklas 410             Annotationtype.QC_HISENSE.setAnnotationValue(dc, rnaQc, hiSense);
1551 06 Mar 12 nicklas 411           }
1551 06 Mar 12 nicklas 412           
1543 28 Feb 12 nicklas 413           String row = (String)jsonRnaQc.get("row");
1543 28 Feb 12 nicklas 414           String column = (String)jsonRnaQc.get("column");
1543 28 Feb 12 nicklas 415           int r = Coordinate.alphaToNumeric(row) - 1;
1543 28 Feb 12 nicklas 416           int c = Coordinate.alphaToNumeric(column) - 1;
1543 28 Feb 12 nicklas 417           
1543 28 Feb 12 nicklas 418           BioWell well = plate.getBioWell(r, c);
1543 28 Feb 12 nicklas 419           if (well == null)
1543 28 Feb 12 nicklas 420           {
1543 28 Feb 12 nicklas 421             throw new ItemNotFoundException("Well[" + row + column + "]");
1543 28 Feb 12 nicklas 422           }
1543 28 Feb 12 nicklas 423           
1543 28 Feb 12 nicklas 424           rnaQc.setBioWell(well);
1543 28 Feb 12 nicklas 425           
1543 28 Feb 12 nicklas 426           jsonMessages.add("Created '" + rnaQc.getName() + "' on position " + row + column);
1543 28 Feb 12 nicklas 427           dc.saveItem(rnaQc);
1543 28 Feb 12 nicklas 428         }
1543 28 Feb 12 nicklas 429         
5421 13 May 19 nicklas 430         ActivityDef.PLACED_RNAQC.merge(dc, plate.getName(), jsonRnaQcExtracts.size());
5379 23 Apr 19 nicklas 431         
1546 02 Mar 12 nicklas 432         dc.commit();
1543 28 Feb 12 nicklas 433       }
1546 02 Mar 12 nicklas 434       else if ("PreValidateCaliperResults".equals(cmd))
1546 02 Mar 12 nicklas 435       {
2698 25 Sep 14 nicklas 436         String csvPath = Values.getString(req.getParameter("csv"));
1546 02 Mar 12 nicklas 437         
6330 14 Jun 21 nicklas 438         dc = sc.newDbControl(":Import RQS from Caliper");
2698 25 Sep 14 nicklas 439         File csv = File.getByPath(dc, new Path(csvPath, Path.Type.FILE), false);
1966 08 May 13 nicklas 440         AnnotationType rqs = Annotationtype.CA_RQS.load(dc);
1546 02 Mar 12 nicklas 441         
1904 13 Mar 13 nicklas 442         CaliperPlateImporter cpi = new CaliperPlateImporter(BioplateType.CALIPER_RNAQC.getPlateNamePrefix());
2454 23 May 14 nicklas 443         cpi.setAnnotationMapping("RNA Quality Score", rqs, null);
1966 08 May 13 nicklas 444         cpi.setAllowMatchAgainstLocation(true); // Allow a few lines to have location coordinates instead of rnaqc name
1546 02 Mar 12 nicklas 445         
2698 25 Sep 14 nicklas 446         boolean valid = cpi.doImport(dc, csv.getDownloadStream(0), true, csv.getName());
2836 20 Oct 14 nicklas 447         for (Extract e : cpi.getMissingExtracts())
2836 20 Oct 14 nicklas 448         {
2836 20 Oct 14 nicklas 449           cpi.addWarningMessage(csv.getName() + ": No 'RNA Quality Score' value found for '" + e.getName() + "' (" + e.getBioWell().getCoordinate() + "); using -100 instead");
2836 20 Oct 14 nicklas 450         }
2836 20 Oct 14 nicklas 451         
2698 25 Sep 14 nicklas 452         json.put("valid", valid);
2698 25 Sep 14 nicklas 453         json.put("warnings", cpi.getWarningMessages().size());
2698 25 Sep 14 nicklas 454         json.put("errors", cpi.getErrorMessages().size());
2698 25 Sep 14 nicklas 455         
2698 25 Sep 14 nicklas 456         if (cpi.hasError()) jsonMessages.addAll(cpi.getErrorMessages());
2698 25 Sep 14 nicklas 457         if (cpi.hasWarning()) jsonMessages.addAll(cpi.getWarningMessages());
2698 25 Sep 14 nicklas 458         
2698 25 Sep 14 nicklas 459         if (valid)
1546 02 Mar 12 nicklas 460         {
1546 02 Mar 12 nicklas 461           // The validation was ok
1546 02 Mar 12 nicklas 462           BioPlate plate = cpi.getBioPlate();
1668 25 May 12 nicklas 463           ReactionPlate rnaQc = ReactionPlate.getById(dc, plate.getId(), BioplateType.CALIPER_RNAQC);
3555 21 Oct 15 nicklas 464           rnaQc.loadBioWells(dc, true);
1610 23 Apr 12 nicklas 465           rnaQc.loadAnnotations(dc, "QCRunDate", Annotationtype.QC_RUN_DATE, Reggie.CONVERTER_DATE_TO_STRING);
1610 23 Apr 12 nicklas 466           rnaQc.loadAnnotations(dc, "QCOperator", Annotationtype.QC_OPERATOR, null);
2836 20 Oct 14 nicklas 467           rnaQc.setAnnotation("comments", plate.getDescription());
3555 21 Oct 15 nicklas 468           
1546 02 Mar 12 nicklas 469           JSONObject jsonPlate = rnaQc.asJSONObject();
1546 02 Mar 12 nicklas 470           
1546 02 Mar 12 nicklas 471           json.put("plate", jsonPlate);
1546 02 Mar 12 nicklas 472         }
1546 02 Mar 12 nicklas 473         
1546 02 Mar 12 nicklas 474       }
1546 02 Mar 12 nicklas 475       else if ("ImportCaliperResults".equals(cmd))
1546 02 Mar 12 nicklas 476       {
6330 14 Jun 21 nicklas 477         dc = sc.newDbControl(":Import RQS from Caliper");
2161 09 Dec 13 nicklas 478
2161 09 Dec 13 nicklas 479         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 480
3752 17 Feb 16 nicklas 481         JSONObject jsonReq = JsonUtil.parseRequest(req);
3555 21 Oct 15 nicklas 482         JSONArray jsonExtra = (JSONArray)jsonReq.get("extraAliquots");
1546 02 Mar 12 nicklas 483
2698 25 Sep 14 nicklas 484         String csvPath = Values.getStringOrNull((String)jsonReq.get("csv"));
2698 25 Sep 14 nicklas 485         String gxdStdPath = Values.getStringOrNull((String)jsonReq.get("gxdStd"));
2698 25 Sep 14 nicklas 486         String gxdHiPath = Values.getStringOrNull((String)jsonReq.get("gxdHi"));
2698 25 Sep 14 nicklas 487         String pdfPath = Values.getStringOrNull((String)jsonReq.get("pdf"));
1546 02 Mar 12 nicklas 488         
3555 21 Oct 15 nicklas 489         Date runDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("QCRunDate"));
3555 21 Oct 15 nicklas 490         String operator = Values.getStringOrNull((String)jsonReq.get("QCOperator"));
3555 21 Oct 15 nicklas 491         
1610 23 Apr 12 nicklas 492         AnnotationType rqs = Annotationtype.CA_RQS.load(dc);
1546 02 Mar 12 nicklas 493         
2698 25 Sep 14 nicklas 494         File csv = File.getByPath(dc, new Path(csvPath, Path.Type.FILE), false);
1546 02 Mar 12 nicklas 495         
1904 13 Mar 13 nicklas 496         CaliperPlateImporter cpi = new CaliperPlateImporter(BioplateType.CALIPER_RNAQC.getPlateNamePrefix());
2454 23 May 14 nicklas 497         cpi.setAnnotationMapping("RNA Quality Score", rqs, null);
1966 08 May 13 nicklas 498         cpi.setAllowMatchAgainstLocation(true);
1546 02 Mar 12 nicklas 499         
2675 17 Sep 14 nicklas 500         if (cpi.doImport(dc, csv.getDownloadStream(0), false, csv.getName()))
1546 02 Mar 12 nicklas 501         {
1546 02 Mar 12 nicklas 502           // The import was successful
1546 02 Mar 12 nicklas 503           BioPlate plate = cpi.getBioPlate();
1546 02 Mar 12 nicklas 504           jsonMessages.add("Imported data to " + cpi.getNumImported() + " extracts on plate " + plate.getName());
1546 02 Mar 12 nicklas 505           
5421 13 May 19 nicklas 506           ActivityDef.COMPLETED_CALIPER_RNAQC.merge(dc, plate.getName(), cpi.getNumImported());
5384 26 Apr 19 nicklas 507       
3555 21 Oct 15 nicklas 508           // Extra aliquots
3555 21 Oct 15 nicklas 509           for (int extraNo = 0; extraNo < jsonExtra.size(); extraNo++)
3555 21 Oct 15 nicklas 510           {
3555 21 Oct 15 nicklas 511             JSONObject jsonAliquot = (JSONObject)jsonExtra.get(extraNo);
3555 21 Oct 15 nicklas 512             Number volume = (Number)jsonAliquot.get("volume"); // µl
3555 21 Oct 15 nicklas 513             Number rnaQcId = (Number)jsonAliquot.get("id");
3555 21 Oct 15 nicklas 514             String comment = Values.getStringOrNull((String)jsonAliquot.get("comment"));
3555 21 Oct 15 nicklas 515             Extract rnaQc = Extract.getById(dc, rnaQcId.intValue());
3555 21 Oct 15 nicklas 516             Extract rna = (Extract)rnaQc.getParent();
6219 20 Apr 21 nicklas 517             Float conc = (Float)Annotationtype.ND_CONC.getAnnotationValue(dc, rna); // ng/µl
6219 20 Apr 21 nicklas 518             if (conc == null) conc = (Float)Annotationtype.QUBIT_CONC.getAnnotationValue(dc, rna);
3555 21 Oct 15 nicklas 519             
3555 21 Oct 15 nicklas 520             BioMaterialEvent extraEvent = rna.newEvent();
3555 21 Oct 15 nicklas 521             extraEvent.setEventDate(runDate);
6219 20 Apr 21 nicklas 522             if (conc != null)
3555 21 Oct 15 nicklas 523             {
6219 20 Apr 21 nicklas 524               extraEvent.setUsedQuantity(conc * volume.floatValue() / 1000); // µg
3555 21 Oct 15 nicklas 525             }
3555 21 Oct 15 nicklas 526             extraEvent.setComment(volume + " µl extra aliquot for additional RNAQC" + (comment == null ? "" : ": " + comment));
3555 21 Oct 15 nicklas 527             extraEvent.setProtocol(rnaQc.getProtocol());
3555 21 Oct 15 nicklas 528             dc.saveItem(extraEvent);
3555 21 Oct 15 nicklas 529             
3555 21 Oct 15 nicklas 530             jsonMessages.add(volume + " µl extra aliquot taken from " + rna.getName());
3555 21 Oct 15 nicklas 531           }
3555 21 Oct 15 nicklas 532           
2836 20 Oct 14 nicklas 533           for (Extract e : cpi.getMissingExtracts())
2836 20 Oct 14 nicklas 534           {
2836 20 Oct 14 nicklas 535             cpi.addWarningMessage(csv.getName() + ": No 'RNA Quality Score' value found for '" + e.getName() + "' (" + e.getBioWell().getCoordinate() + "); using -100 instead");
2836 20 Oct 14 nicklas 536             Annotationtype.CA_RQS.setAnnotationValue(dc, e, -100);
2836 20 Oct 14 nicklas 537           }
2836 20 Oct 14 nicklas 538           
1564 16 Mar 12 nicklas 539           if (cpi.hasWarning())
1564 16 Mar 12 nicklas 540           {
1564 16 Mar 12 nicklas 541             jsonMessages.addAll(cpi.getWarningMessages());
1564 16 Mar 12 nicklas 542           }
1564 16 Mar 12 nicklas 543           
1546 02 Mar 12 nicklas 544           // Attach the files we used to the bioplate
1561 15 Mar 12 nicklas 545           cpi.attachFile(dc, csv, "Well table CSV");
1546 02 Mar 12 nicklas 546           jsonMessages.add("Attached file '" + csv.getName() + "' to plate " + plate.getName());
2698 25 Sep 14 nicklas 547           if (gxdStdPath != null)
1546 02 Mar 12 nicklas 548           {
2698 25 Sep 14 nicklas 549             File gxd = File.getByPath(dc, new Path(gxdStdPath, Path.Type.FILE), false);
1561 15 Mar 12 nicklas 550             cpi.attachFile(dc, gxd, "LabView GX raw data (std)");
1546 02 Mar 12 nicklas 551             jsonMessages.add("Attached file '" + gxd.getName() + "' to plate " + plate.getName());
1546 02 Mar 12 nicklas 552           }
2698 25 Sep 14 nicklas 553           if (gxdHiPath != null)
1561 15 Mar 12 nicklas 554           {
2698 25 Sep 14 nicklas 555             File gxd = File.getByPath(dc, new Path(gxdHiPath, Path.Type.FILE), false);
1561 15 Mar 12 nicklas 556             cpi.attachFile(dc, gxd, "LabView GX raw data (hi)");
1561 15 Mar 12 nicklas 557             jsonMessages.add("Attached file '" + gxd.getName() + "' to plate " + plate.getName());
1561 15 Mar 12 nicklas 558           }
2698 25 Sep 14 nicklas 559           if (pdfPath != null)
1546 02 Mar 12 nicklas 560           {
2698 25 Sep 14 nicklas 561             File pdf = File.getByPath(dc, new Path(pdfPath, Path.Type.FILE), false);
1546 02 Mar 12 nicklas 562             cpi.attachFile(dc, pdf, "PDF printout");
1546 02 Mar 12 nicklas 563             jsonMessages.add("Attached file '" + pdf.getName() + "' to plate " + plate.getName());
1546 02 Mar 12 nicklas 564           }
1619 25 Apr 12 nicklas 565                     
1619 25 Apr 12 nicklas 566           Annotationtype.QC_RUN_DATE.setAnnotationValue(dc, plate, runDate);
1619 25 Apr 12 nicklas 567           Annotationtype.QC_OPERATOR.setAnnotationValue(dc, plate, operator);
2836 20 Oct 14 nicklas 568           plate.setDescription(Values.getStringOrNull((String)jsonReq.get("comments")));
1546 02 Mar 12 nicklas 569           plate.setDestroyed(true);
3555 21 Oct 15 nicklas 570           
1546 02 Mar 12 nicklas 571           dc.commit();
1546 02 Mar 12 nicklas 572         }
1546 02 Mar 12 nicklas 573         else
1546 02 Mar 12 nicklas 574         {
1546 02 Mar 12 nicklas 575           jsonMessages.addAll(cpi.getErrorMessages());
1707 14 Jun 12 nicklas 576           jsonMessages.addAll(cpi.getWarningMessages());
1546 02 Mar 12 nicklas 577           json.put("status", "error");
1546 02 Mar 12 nicklas 578           json.put("message", "Import failed from file: " + csv.getName());
1546 02 Mar 12 nicklas 579         }
1546 02 Mar 12 nicklas 580       }
2922 12 Nov 14 nicklas 581       else if ("RegisterRinValues".equals(cmd))
2922 12 Nov 14 nicklas 582       {
6330 14 Jun 21 nicklas 583         dc = sc.newDbControl(":Register RIN from BioAnalyzer");
1546 02 Mar 12 nicklas 584
2922 12 Nov 14 nicklas 585         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
2922 12 Nov 14 nicklas 586
3752 17 Feb 16 nicklas 587         JSONObject jsonReq = JsonUtil.parseRequest(req);
2922 12 Nov 14 nicklas 588         JSONArray jsonRnaQc = (JSONArray)jsonReq.get("rnaqc");
2922 12 Nov 14 nicklas 589         
2922 12 Nov 14 nicklas 590         Number bioplateId = (Number)jsonReq.get("bioPlate");
2922 12 Nov 14 nicklas 591         BioPlate plate = BioPlate.getById(dc, bioplateId.intValue());
2922 12 Nov 14 nicklas 592         
2922 12 Nov 14 nicklas 593         for (int rnaNo = 0; rnaNo < jsonRnaQc.size(); rnaNo++)
2922 12 Nov 14 nicklas 594         {
2922 12 Nov 14 nicklas 595           JSONObject jsonQc = (JSONObject)jsonRnaQc.get(rnaNo);
2922 12 Nov 14 nicklas 596           
2922 12 Nov 14 nicklas 597           Number rnaQcId = (Number)jsonQc.get("id");
2922 12 Nov 14 nicklas 598           Extract rnaQc = Extract.getById(dc, rnaQcId.intValue());
2922 12 Nov 14 nicklas 599           
2922 12 Nov 14 nicklas 600           Annotationtype.BA_RIN.setAnnotationValue(dc, rnaQc, jsonQc.get("rin"));
2922 12 Nov 14 nicklas 601           rnaQc.setDescription(Values.getStringOrNull((String)jsonQc.get("comment")));
2922 12 Nov 14 nicklas 602         }
2922 12 Nov 14 nicklas 603         jsonMessages.add("Registered RIN valies for " + jsonRnaQc.size() + " extracts on plate " + plate.getName());
2922 12 Nov 14 nicklas 604         
5421 13 May 19 nicklas 605         ActivityDef.COMPLETED_BA_RNAQC.merge(dc, plate.getName(), jsonRnaQc.size());
5379 23 Apr 19 nicklas 606
2922 12 Nov 14 nicklas 607         String pdfPath = Values.getStringOrNull((String)jsonReq.get("pdf"));
2922 12 Nov 14 nicklas 608         if (pdfPath != null)
2922 12 Nov 14 nicklas 609         {
2922 12 Nov 14 nicklas 610           File pdf = File.getByPath(dc, new Path(pdfPath, Path.Type.FILE), false);
2922 12 Nov 14 nicklas 611           AnyToAny ata = AnyToAny.getNewOrExisting(dc, plate, "PDF printout", pdf, true);
2922 12 Nov 14 nicklas 612           if (!ata.isInDatabase())
2922 12 Nov 14 nicklas 613           {
2922 12 Nov 14 nicklas 614             dc.saveItem(ata);
2922 12 Nov 14 nicklas 615           }
2922 12 Nov 14 nicklas 616           if (pdf.getDescription() == null)
2922 12 Nov 14 nicklas 617           {
2922 12 Nov 14 nicklas 618             pdf.setDescription("PDF printout for " + plate.getName());
2922 12 Nov 14 nicklas 619           }
2922 12 Nov 14 nicklas 620           jsonMessages.add("Attached file '" + pdf.getName() + "' to plate " + plate.getName());
2922 12 Nov 14 nicklas 621         }
2922 12 Nov 14 nicklas 622         
2922 12 Nov 14 nicklas 623         Date runDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("QCRunDate"));
2922 12 Nov 14 nicklas 624         String operator = Values.getStringOrNull((String)jsonReq.get("QCOperator"));
2922 12 Nov 14 nicklas 625         Annotationtype.QC_RUN_DATE.setAnnotationValue(dc, plate, runDate);
2922 12 Nov 14 nicklas 626         Annotationtype.QC_OPERATOR.setAnnotationValue(dc, plate, operator);
2922 12 Nov 14 nicklas 627         plate.setDescription(Values.getStringOrNull((String)jsonReq.get("comments")));
2922 12 Nov 14 nicklas 628         plate.setDestroyed(true);
2922 12 Nov 14 nicklas 629         
2922 12 Nov 14 nicklas 630         dc.commit();
2922 12 Nov 14 nicklas 631       }
2922 12 Nov 14 nicklas 632
1543 28 Feb 12 nicklas 633       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 634       CounterService.getInstance().setForceCount();
1543 28 Feb 12 nicklas 635     }
1543 28 Feb 12 nicklas 636     catch (Throwable t)
1543 28 Feb 12 nicklas 637     {
1543 28 Feb 12 nicklas 638       t.printStackTrace();
1543 28 Feb 12 nicklas 639       json.clear();
1543 28 Feb 12 nicklas 640       json.put("status", "error");
1543 28 Feb 12 nicklas 641       json.put("message", t.getMessage());
1543 28 Feb 12 nicklas 642       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1543 28 Feb 12 nicklas 643     }
1543 28 Feb 12 nicklas 644     finally
1543 28 Feb 12 nicklas 645     {
1543 28 Feb 12 nicklas 646       if (dc != null) dc.close();
1543 28 Feb 12 nicklas 647       json.writeJSONString(resp.getWriter());
1543 28 Feb 12 nicklas 648     }
1543 28 Feb 12 nicklas 649     
1543 28 Feb 12 nicklas 650   }
1543 28 Feb 12 nicklas 651   
1543 28 Feb 12 nicklas 652   
1543 28 Feb 12 nicklas 653 }