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

Code
Comments
Other
Rev Date Author Line
2020 13 Sep 13 nicklas 1 package net.sf.basedb.reggie.servlet;
2020 13 Sep 13 nicklas 2
2020 13 Sep 13 nicklas 3 import java.io.IOException;
3108 27 Jan 15 nicklas 4 import java.util.ArrayList;
3108 27 Jan 15 nicklas 5 import java.util.Arrays;
2020 13 Sep 13 nicklas 6 import java.util.HashMap;
2020 13 Sep 13 nicklas 7 import java.util.List;
2020 13 Sep 13 nicklas 8 import java.util.Map;
2020 13 Sep 13 nicklas 9
2020 13 Sep 13 nicklas 10 import javax.servlet.ServletException;
2020 13 Sep 13 nicklas 11 import javax.servlet.http.HttpServlet;
2020 13 Sep 13 nicklas 12 import javax.servlet.http.HttpServletRequest;
2020 13 Sep 13 nicklas 13 import javax.servlet.http.HttpServletResponse;
2020 13 Sep 13 nicklas 14
2020 13 Sep 13 nicklas 15 import org.json.simple.JSONArray;
2020 13 Sep 13 nicklas 16 import org.json.simple.JSONObject;
2020 13 Sep 13 nicklas 17
2020 13 Sep 13 nicklas 18 import net.sf.basedb.core.BioMaterialEvent;
2020 13 Sep 13 nicklas 19 import net.sf.basedb.core.BioMaterialEventSource;
2020 13 Sep 13 nicklas 20 import net.sf.basedb.core.DbControl;
2020 13 Sep 13 nicklas 21 import net.sf.basedb.core.Extract;
2020 13 Sep 13 nicklas 22 import net.sf.basedb.core.ItemQuery;
2020 13 Sep 13 nicklas 23 import net.sf.basedb.core.ItemSubtype;
2020 13 Sep 13 nicklas 24 import net.sf.basedb.core.PhysicalBioAssay;
2020 13 Sep 13 nicklas 25 import net.sf.basedb.core.SessionControl;
2031 01 Oct 13 nicklas 26 import net.sf.basedb.core.query.Annotations;
2020 13 Sep 13 nicklas 27 import net.sf.basedb.core.query.Hql;
2031 01 Oct 13 nicklas 28 import net.sf.basedb.core.query.Orders;
2020 13 Sep 13 nicklas 29 import net.sf.basedb.core.query.Restrictions;
2598 22 Aug 14 nicklas 30 import net.sf.basedb.reggie.JsonUtil;
2020 13 Sep 13 nicklas 31 import net.sf.basedb.reggie.Reggie;
3059 19 Dec 14 nicklas 32 import net.sf.basedb.reggie.counter.CounterService;
2020 13 Sep 13 nicklas 33 import net.sf.basedb.reggie.dao.Annotationtype;
2020 13 Sep 13 nicklas 34 import net.sf.basedb.reggie.dao.FlowCell;
5434 17 May 19 nicklas 35 import net.sf.basedb.reggie.dao.Pipeline;
2020 13 Sep 13 nicklas 36 import net.sf.basedb.reggie.dao.PooledLibrary;
4897 10 Jul 18 nicklas 37 import net.sf.basedb.reggie.dao.ReggieItem;
2161 09 Dec 13 nicklas 38 import net.sf.basedb.reggie.dao.ReggieRole;
2020 13 Sep 13 nicklas 39 import net.sf.basedb.reggie.dao.Subtype;
2020 13 Sep 13 nicklas 40 import net.sf.basedb.util.Values;
2020 13 Sep 13 nicklas 41 import net.sf.basedb.util.error.ThrowableUtil;
2020 13 Sep 13 nicklas 42
2020 13 Sep 13 nicklas 43
2020 13 Sep 13 nicklas 44 public class FlowCellServlet 
2020 13 Sep 13 nicklas 45   extends HttpServlet 
2020 13 Sep 13 nicklas 46 {
2020 13 Sep 13 nicklas 47
2020 13 Sep 13 nicklas 48   private static final long serialVersionUID = -1284885184715864330L;
2020 13 Sep 13 nicklas 49
2020 13 Sep 13 nicklas 50   public FlowCellServlet()
2020 13 Sep 13 nicklas 51   {}
2020 13 Sep 13 nicklas 52
2020 13 Sep 13 nicklas 53   @Override
2020 13 Sep 13 nicklas 54   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
2020 13 Sep 13 nicklas 55     throws ServletException, IOException 
2020 13 Sep 13 nicklas 56   {
2020 13 Sep 13 nicklas 57     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 58     JsonUtil.setJsonResponseHeaders(resp);
2020 13 Sep 13 nicklas 59     
2020 13 Sep 13 nicklas 60     JSONObject json = new JSONObject();
2020 13 Sep 13 nicklas 61     json.put("status", "ok");
2020 13 Sep 13 nicklas 62     
3975 26 May 16 nicklas 63     final SessionControl sc = Reggie.getSessionControl(req);
2020 13 Sep 13 nicklas 64     DbControl dc = null;
2020 13 Sep 13 nicklas 65     try
2020 13 Sep 13 nicklas 66     {
3108 27 Jan 15 nicklas 67       if ("GetUnusedPools".equals(cmd) || "GetPoolInfo".equals(cmd))
2020 13 Sep 13 nicklas 68       {
2020 13 Sep 13 nicklas 69         /*
2020 13 Sep 13 nicklas 70           Find all pools which has not yet been used on a flow cell.
2020 13 Sep 13 nicklas 71         */
6334 15 Jun 21 nicklas 72         dc = sc.newDbControl(":Create flow cells");
3108 27 Jan 15 nicklas 73         List<PooledLibrary> pools = null;
3108 27 Jan 15 nicklas 74         if ("GetPoolInfo".equals(cmd))
3108 27 Jan 15 nicklas 75         {
3108 27 Jan 15 nicklas 76           pools = new ArrayList<PooledLibrary>();
3108 27 Jan 15 nicklas 77           for (String poolId : req.getParameter("pools").split(","))
3108 27 Jan 15 nicklas 78           {
3108 27 Jan 15 nicklas 79             pools.add(PooledLibrary.getById(dc, Values.getInt(poolId)));
3108 27 Jan 15 nicklas 80           }
3108 27 Jan 15 nicklas 81         }
3108 27 Jan 15 nicklas 82         else
3108 27 Jan 15 nicklas 83         {
5434 17 May 19 nicklas 84           pools = PooledLibrary.findUnusedPools(dc, Pipeline.RNA_SEQ);
3108 27 Jan 15 nicklas 85         }
3108 27 Jan 15 nicklas 86         
2020 13 Sep 13 nicklas 87         JSONArray jsonPools = new JSONArray();
2020 13 Sep 13 nicklas 88         for (PooledLibrary pool : pools)
2020 13 Sep 13 nicklas 89         {
2090 21 Oct 13 nicklas 90           pool.loadLibPlates(dc);
2020 13 Sep 13 nicklas 91           pool.loadAnnotations(dc, "molarity", Annotationtype.POOL_MOLARITY, null);
2020 13 Sep 13 nicklas 92           pool.loadAnnotations(dc, "conc", Annotationtype.POOL_CONC, null);
2020 13 Sep 13 nicklas 93           pool.setAnnotation("remainingQuantity", pool.getExtract().getRemainingQuantity());
5435 17 May 19 nicklas 94           pool.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
2020 13 Sep 13 nicklas 95           jsonPools.add(pool.asJSONObject());
2020 13 Sep 13 nicklas 96         }
2020 13 Sep 13 nicklas 97         json.put("pools", jsonPools);
2020 13 Sep 13 nicklas 98       }
2020 13 Sep 13 nicklas 99       else if ("GetNextAutoGeneratedFlowCellNames".equals(cmd))
2020 13 Sep 13 nicklas 100       {
4890 06 Jul 18 nicklas 101         // Get the flow cell names for the next number of flow cells to create
6334 15 Jun 21 nicklas 102         dc = sc.newDbControl(":Create flow cells");
2020 13 Sep 13 nicklas 103         int numNames = Values.getInt(req.getParameter("numNames"));
4890 06 Jul 18 nicklas 104         json.put("names", FlowCell.getNextNames(dc, numNames));
2020 13 Sep 13 nicklas 105       }
2020 13 Sep 13 nicklas 106       else if ("GetUnprocessedFlowCells".equals(cmd))
2020 13 Sep 13 nicklas 107       {
5470 05 Jun 19 nicklas 108         Pipeline pipeline = Pipeline.getByCName(req.getParameter("pipeline"));
5470 05 Jun 19 nicklas 109         
6334 15 Jun 21 nicklas 110         dc = sc.newDbControl(":Register sequencing started");
2020 13 Sep 13 nicklas 111         ItemQuery<PhysicalBioAssay> query = PhysicalBioAssay.getQuery();
2020 13 Sep 13 nicklas 112         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2020 13 Sep 13 nicklas 113         Subtype.FLOW_CELL.addFilter(dc, query);
5470 05 Jun 19 nicklas 114         pipeline.addFilter(dc, query);
2020 13 Sep 13 nicklas 115         query.join(Hql.innerJoin("creationEvent", "ce"));
2020 13 Sep 13 nicklas 116         query.restrict(Restrictions.eq(Hql.property("ce", "eventDate"), null));
2031 01 Oct 13 nicklas 117         // Must NOT have a PLATE_PROCESS_RESULT annotation
2031 01 Oct 13 nicklas 118         query.join(Annotations.leftJoin(null, Annotationtype.PLATE_PROCESS_RESULT.load(dc), "ppr"));
2031 01 Oct 13 nicklas 119         query.restrict(Restrictions.eq(Hql.alias("ppr"), null));
2031 01 Oct 13 nicklas 120         query.order(Orders.asc(Hql.property("name")));
2020 13 Sep 13 nicklas 121         
2020 13 Sep 13 nicklas 122         JSONArray jsonFlowCells = new JSONArray();
2020 13 Sep 13 nicklas 123         List<FlowCell> flowCells = FlowCell.toList(query.list(dc));
2020 13 Sep 13 nicklas 124         for (FlowCell fc : flowCells)
2020 13 Sep 13 nicklas 125         {
2089 21 Oct 13 nicklas 126           fc.loadAnnotations(dc, "SequencingCycles", Annotationtype.SEQUENCING_CYCLES, null);
2412 09 May 14 nicklas 127           fc.loadAnnotations(dc, "FlowCellType", Annotationtype.FLOWCELL_TYPE, null);
2062 14 Oct 13 nicklas 128           fc.loadAnnotations(dc, "externalId", Annotationtype.FLOWCELL_ID, null);
5470 05 Jun 19 nicklas 129           fc.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
2059 11 Oct 13 nicklas 130           fc.setAnnotation("comments", fc.getPhysicalBioAssay().getDescription());
2094 21 Oct 13 nicklas 131           
2094 21 Oct 13 nicklas 132           JSONArray jsonPools = new JSONArray();
2094 21 Oct 13 nicklas 133           List<PooledLibrary> pools = PooledLibrary.getByFlowCell(dc, fc);
2094 21 Oct 13 nicklas 134           for (PooledLibrary pool : pools)
2094 21 Oct 13 nicklas 135           {
2094 21 Oct 13 nicklas 136             jsonPools.add(pool.asJSONObject());
2094 21 Oct 13 nicklas 137           }
2094 21 Oct 13 nicklas 138           fc.setAnnotation("pools", jsonPools);
2020 13 Sep 13 nicklas 139           jsonFlowCells.add(fc.asJSONObject());
2020 13 Sep 13 nicklas 140         }
2020 13 Sep 13 nicklas 141         
2020 13 Sep 13 nicklas 142         json.put("flowCells", jsonFlowCells);
2020 13 Sep 13 nicklas 143       }
2033 02 Oct 13 olle 144       else if ("GetFlowCellInfo".equals(cmd))
2033 02 Oct 13 olle 145       {
6334 15 Jun 21 nicklas 146         dc = sc.newDbControl(":Lab protocol for sequencing");
2653 11 Sep 14 nicklas 147         JSONArray jsonFlowCells = new JSONArray();
2653 11 Sep 14 nicklas 148         
2653 11 Sep 14 nicklas 149         Integer[] fcIds = Values.getInt(req.getParameter("idlist").split(","));
2653 11 Sep 14 nicklas 150         for (int fcId : fcIds)
2033 02 Oct 13 olle 151         {
2653 11 Sep 14 nicklas 152           FlowCell fc = FlowCell.getById(dc, fcId);
2033 02 Oct 13 olle 153           PhysicalBioAssay physBA = fc.getPhysicalBioAssay();
2062 14 Oct 13 nicklas 154           fc.setAnnotation("description", physBA.getDescription());
2062 14 Oct 13 nicklas 155           fc.setAnnotation("numLanes", physBA.getSize());
2087 18 Oct 13 nicklas 156           fc.loadAnnotations(dc, "SequencingCycles", Annotationtype.SEQUENCING_CYCLES, null);
2412 09 May 14 nicklas 157           fc.loadAnnotations(dc, "FlowCellType", Annotationtype.FLOWCELL_TYPE, null);
2062 14 Oct 13 nicklas 158           fc.loadAnnotations(dc, "externalId", Annotationtype.FLOWCELL_ID, null);
5470 05 Jun 19 nicklas 159           fc.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
2062 14 Oct 13 nicklas 160           
2033 02 Oct 13 olle 161           // Get flow cell lane info
3108 27 Jan 15 nicklas 162           JSONObject[] jsonLanes = new JSONObject[physBA.getSize()];
2062 14 Oct 13 nicklas 163           for (BioMaterialEventSource eventSource : physBA.getCreationEvent().getEventSources().list(dc))
2033 02 Oct 13 olle 164           {
2112 30 Oct 13 nicklas 165             Extract poolAliquot = (Extract)eventSource.getBioMaterial();
2112 30 Oct 13 nicklas 166             Extract pool = (Extract)poolAliquot.getParent();
3108 27 Jan 15 nicklas 167             JSONObject jsonPool = new JSONObject();
3108 27 Jan 15 nicklas 168             jsonPool.put("id", pool.getId());
3108 27 Jan 15 nicklas 169             jsonPool.put("name", pool.getName());
3108 27 Jan 15 nicklas 170             jsonPool.put("molarity", Annotationtype.POOL_MOLARITY.getAnnotationValue(dc, pool));
3108 27 Jan 15 nicklas 171             jsonLanes[eventSource.getPosition()-1] = jsonPool;
2033 02 Oct 13 olle 172           }
3108 27 Jan 15 nicklas 173           fc.setAnnotation("lanes", Arrays.asList(jsonLanes));
2653 11 Sep 14 nicklas 174           jsonFlowCells.add(fc.asJSONObject());
2033 02 Oct 13 olle 175         }
2653 11 Sep 14 nicklas 176         
2653 11 Sep 14 nicklas 177         json.put("flowCells", jsonFlowCells);
2653 11 Sep 14 nicklas 178         
2033 02 Oct 13 olle 179       }
2222 11 Feb 14 nicklas 180       else if ("GetFlowCellByBarcode".equals(cmd))
2222 11 Feb 14 nicklas 181       {
6334 15 Jun 21 nicklas 182         dc = sc.newDbControl(":Register sequencing started");
2222 11 Feb 14 nicklas 183         String barcode = req.getParameter("barcode");
2222 11 Feb 14 nicklas 184         
2222 11 Feb 14 nicklas 185         FlowCell fc = FlowCell.findByBarcode(dc, barcode);
2222 11 Feb 14 nicklas 186         if (fc != null)
2222 11 Feb 14 nicklas 187         {
2222 11 Feb 14 nicklas 188           PhysicalBioAssay physBA = fc.getPhysicalBioAssay();
2222 11 Feb 14 nicklas 189           fc.setAnnotation("description", physBA.getDescription());
2222 11 Feb 14 nicklas 190           fc.setAnnotation("numLanes", physBA.getSize());
2222 11 Feb 14 nicklas 191           fc.loadAnnotations(dc, "SequencingCycles", Annotationtype.SEQUENCING_CYCLES, null);
2412 09 May 14 nicklas 192           fc.loadAnnotations(dc, "FlowCellType", Annotationtype.FLOWCELL_TYPE, null);
5470 05 Jun 19 nicklas 193           fc.loadAnnotations(dc, "externalId", Annotationtype.FLOWCELL_ID, null);          
2222 11 Feb 14 nicklas 194         }
2222 11 Feb 14 nicklas 195         
2222 11 Feb 14 nicklas 196         json.put("flowCell", fc == null ? null : fc.asJSONObject());
2222 11 Feb 14 nicklas 197       }
2020 13 Sep 13 nicklas 198     }
2020 13 Sep 13 nicklas 199     catch (Throwable t)
2020 13 Sep 13 nicklas 200     {
2020 13 Sep 13 nicklas 201       t.printStackTrace();
2020 13 Sep 13 nicklas 202       json.clear();
2020 13 Sep 13 nicklas 203       json.put("status", "error");
2020 13 Sep 13 nicklas 204       json.put("message", t.getMessage());
2020 13 Sep 13 nicklas 205       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2020 13 Sep 13 nicklas 206     }
2020 13 Sep 13 nicklas 207     finally
2020 13 Sep 13 nicklas 208     {
2020 13 Sep 13 nicklas 209       if (dc != null) dc.close();
2020 13 Sep 13 nicklas 210       json.writeJSONString(resp.getWriter());
2020 13 Sep 13 nicklas 211     }
2020 13 Sep 13 nicklas 212     
2020 13 Sep 13 nicklas 213   }
2020 13 Sep 13 nicklas 214
2020 13 Sep 13 nicklas 215   @Override
2020 13 Sep 13 nicklas 216   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
2020 13 Sep 13 nicklas 217     throws ServletException, IOException 
2020 13 Sep 13 nicklas 218   {
2020 13 Sep 13 nicklas 219     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 220     JsonUtil.setJsonResponseHeaders(resp);
2020 13 Sep 13 nicklas 221     
2020 13 Sep 13 nicklas 222     JSONObject json = new JSONObject();
2020 13 Sep 13 nicklas 223     json.put("status", "ok");
2020 13 Sep 13 nicklas 224     
2020 13 Sep 13 nicklas 225     JSONArray jsonMessages = new JSONArray();
2020 13 Sep 13 nicklas 226   
3975 26 May 16 nicklas 227     final SessionControl sc = Reggie.getSessionControl(req);
2020 13 Sep 13 nicklas 228     DbControl dc = null;
2020 13 Sep 13 nicklas 229     try
2020 13 Sep 13 nicklas 230     {
2020 13 Sep 13 nicklas 231       if ("CreateFlowCells".equals(cmd))
2020 13 Sep 13 nicklas 232       {
6334 15 Jun 21 nicklas 233         dc = sc.newDbControl(":Create flow cells");
2161 09 Dec 13 nicklas 234
2161 09 Dec 13 nicklas 235         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 236
2020 13 Sep 13 nicklas 237         // Create FlowCells and add pools to them
3752 17 Feb 16 nicklas 238         JSONObject jsonReq = JsonUtil.parseRequest(req);
2020 13 Sep 13 nicklas 239         JSONArray jsonFlowCells = (JSONArray)jsonReq.get("flowCells");
2020 13 Sep 13 nicklas 240         
2085 18 Oct 13 nicklas 241         Number read1 = (Number)jsonReq.get("read1");
2085 18 Oct 13 nicklas 242         Number indexRead = (Number)jsonReq.get("indexRead");
2085 18 Oct 13 nicklas 243         Number read2 = (Number)jsonReq.get("read2");
2085 18 Oct 13 nicklas 244         // Sequence string is concatenated: read1-index-read2
2087 18 Oct 13 nicklas 245         String sequencingCycles = read1 + "-" + indexRead + "-" + read2;
2161 09 Dec 13 nicklas 246         
2652 11 Sep 14 nicklas 247         String fcType = Values.getStringOrNull((String)jsonReq.get("flowCellType"));
2412 09 May 14 nicklas 248         
2020 13 Sep 13 nicklas 249         ItemSubtype flowCellType = Subtype.FLOW_CELL.load(dc);
2109 29 Oct 13 nicklas 250         ItemSubtype poolAliquotType = Subtype.POOLED_LIBRARY_ALIQUOT.load(dc);
2020 13 Sep 13 nicklas 251         
2020 13 Sep 13 nicklas 252         for (int fcNo = 0; fcNo < jsonFlowCells.size(); fcNo++)
2020 13 Sep 13 nicklas 253         {
2020 13 Sep 13 nicklas 254           JSONObject jsonFlowCell = (JSONObject)jsonFlowCells.get(fcNo);
2020 13 Sep 13 nicklas 255           
2028 30 Sep 13 nicklas 256           JSONArray jsonLanes = (JSONArray)jsonFlowCell.get("lanes");
2020 13 Sep 13 nicklas 257           PhysicalBioAssay flowCell = PhysicalBioAssay.getNew(dc);
2020 13 Sep 13 nicklas 258           flowCell.setItemSubtype(flowCellType);
5439 20 May 19 nicklas 259           Pipeline.RNA_SEQ.setAnnotation(dc, flowCell);
4897 10 Jul 18 nicklas 260           flowCell.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.FLOW_CELL, (String)jsonFlowCell.get("name")));
2045 07 Oct 13 nicklas 261           flowCell.setDescription((String)jsonFlowCell.get("comment"));
2028 30 Sep 13 nicklas 262           flowCell.setSize(jsonLanes.size());
2020 13 Sep 13 nicklas 263           dc.saveItem(flowCell);
2087 18 Oct 13 nicklas 264           Annotationtype.SEQUENCING_CYCLES.setAnnotationValue(dc, flowCell, sequencingCycles);
2652 11 Sep 14 nicklas 265           if (fcType != null)
2652 11 Sep 14 nicklas 266           {
2652 11 Sep 14 nicklas 267             Annotationtype.FLOWCELL_TYPE.setAnnotationValue(dc, flowCell, fcType);
2652 11 Sep 14 nicklas 268           }
2020 13 Sep 13 nicklas 269           
4890 06 Jul 18 nicklas 270           Map<Number, PooledLibrary> poolCache = new HashMap<Number, PooledLibrary>();
2020 13 Sep 13 nicklas 271           BioMaterialEvent clusterEvent = flowCell.getCreationEvent();
2020 13 Sep 13 nicklas 272           int emptyLanes = 0;
2028 30 Sep 13 nicklas 273           for (int laneNo = 0; laneNo < jsonLanes.size(); laneNo++)
2020 13 Sep 13 nicklas 274           {
2028 30 Sep 13 nicklas 275             JSONObject jsonLane = (JSONObject)jsonLanes.get(laneNo);
2028 30 Sep 13 nicklas 276             if (jsonLane == null) 
2020 13 Sep 13 nicklas 277             {
2020 13 Sep 13 nicklas 278               emptyLanes++;
2020 13 Sep 13 nicklas 279               continue;
2020 13 Sep 13 nicklas 280             }
2020 13 Sep 13 nicklas 281             
2028 30 Sep 13 nicklas 282             JSONObject jsonPool = (JSONObject)jsonLane.get("pool");
2111 30 Oct 13 olle 283             // JSONArray jsonLane may have JSONObject elements with a "defaultPool" key, but no "pool" key
2111 30 Oct 13 olle 284             if (jsonPool == null) 
2111 30 Oct 13 olle 285             {
2111 30 Oct 13 olle 286               emptyLanes++;
2111 30 Oct 13 olle 287               continue;
2111 30 Oct 13 olle 288             }
2020 13 Sep 13 nicklas 289             Number poolId = (Number)jsonPool.get("id");
2020 13 Sep 13 nicklas 290             float usedVolume = ((Number)jsonPool.get("usedVolume")).floatValue();
4890 06 Jul 18 nicklas 291             PooledLibrary pool = poolCache.get(poolId);
2020 13 Sep 13 nicklas 292             if (pool == null)
2020 13 Sep 13 nicklas 293             {
4890 06 Jul 18 nicklas 294               pool = PooledLibrary.getById(dc, poolId.intValue());
2020 13 Sep 13 nicklas 295               poolCache.put(poolId, pool);
2020 13 Sep 13 nicklas 296             }
2020 13 Sep 13 nicklas 297             
4890 06 Jul 18 nicklas 298             Float poolConc = (Float)Annotationtype.POOL_CONC.getAnnotationValue(dc, pool.getItem());
2020 13 Sep 13 nicklas 299             
2020 13 Sep 13 nicklas 300             // Create 'in-between' aliquot to make it possible to use 
2020 13 Sep 13 nicklas 301             // the same pool on multiple lanes
2020 13 Sep 13 nicklas 302             Extract poolA = Extract.getNew(dc);
4890 06 Jul 18 nicklas 303             String aliquotName = pool.getNextAliquotName(dc);
4890 06 Jul 18 nicklas 304             poolA.setName(aliquotName);
2109 29 Oct 13 nicklas 305             poolA.setItemSubtype(poolAliquotType);
5434 17 May 19 nicklas 306             Pipeline.RNA_SEQ.setAnnotation(dc, poolA);
4890 06 Jul 18 nicklas 307             BioMaterialEventSource srcA = poolA.getCreationEvent().setSource(pool.getItem());
2020 13 Sep 13 nicklas 308
2020 13 Sep 13 nicklas 309             Float usedQuantity = usedVolume * poolConc / 1000;
2020 13 Sep 13 nicklas 310             srcA.setUsedQuantity(usedQuantity); // Use 10µl and convert to µg from ng/µl
2020 13 Sep 13 nicklas 311             poolA.setOriginalQuantity(usedQuantity);
2020 13 Sep 13 nicklas 312             dc.saveItem(poolA);
2020 13 Sep 13 nicklas 313             
2020 13 Sep 13 nicklas 314             // Add the aliquot to the flow cell on the correct lane
2020 13 Sep 13 nicklas 315             BioMaterialEventSource src = clusterEvent.addSource(poolA);
2028 30 Sep 13 nicklas 316             src.setPosition(laneNo+1);
2020 13 Sep 13 nicklas 317             src.setUsedQuantity(usedQuantity);
2020 13 Sep 13 nicklas 318           }
5382 24 Apr 19 nicklas 319           int numPools = poolCache.size();
5382 24 Apr 19 nicklas 320           String nPools = numPools == 1 ? "1 pool" : numPools + " pools";
5382 24 Apr 19 nicklas 321           String msg = "Flow cell '" + flowCell.getName() + "' registered with " + nPools + 
5382 24 Apr 19 nicklas 322               " on "+flowCell.getSize() + " lanes";
2020 13 Sep 13 nicklas 323           if (emptyLanes > 0) msg += "; " + emptyLanes + " lanes are empty";
2020 13 Sep 13 nicklas 324           jsonMessages.add(msg);
2020 13 Sep 13 nicklas 325         }
2020 13 Sep 13 nicklas 326         
2029 30 Sep 13 nicklas 327         dc.commit();
2020 13 Sep 13 nicklas 328       }
2020 13 Sep 13 nicklas 329       
2020 13 Sep 13 nicklas 330       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 331       CounterService.getInstance().setForceCount();
2020 13 Sep 13 nicklas 332     }
2020 13 Sep 13 nicklas 333     catch (Throwable t)
2020 13 Sep 13 nicklas 334     {
2020 13 Sep 13 nicklas 335       t.printStackTrace();
2020 13 Sep 13 nicklas 336       json.clear();
2020 13 Sep 13 nicklas 337       json.put("status", "error");
2020 13 Sep 13 nicklas 338       json.put("message", t.getMessage());
2020 13 Sep 13 nicklas 339       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2020 13 Sep 13 nicklas 340     }
2020 13 Sep 13 nicklas 341     finally
2020 13 Sep 13 nicklas 342     {
2020 13 Sep 13 nicklas 343       if (dc != null) dc.close();
2020 13 Sep 13 nicklas 344       json.writeJSONString(resp.getWriter());
2020 13 Sep 13 nicklas 345     }
2020 13 Sep 13 nicklas 346     
2020 13 Sep 13 nicklas 347   }
2020 13 Sep 13 nicklas 348   
2020 13 Sep 13 nicklas 349   
2020 13 Sep 13 nicklas 350
2020 13 Sep 13 nicklas 351 }