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

Code
Comments
Other
Rev Date Author Line
5390 29 Apr 19 nicklas 1 package net.sf.basedb.reggie.servlet;
5390 29 Apr 19 nicklas 2
5390 29 Apr 19 nicklas 3 import java.io.IOException;
5397 03 May 19 nicklas 4 import java.io.InputStream;
5439 20 May 19 nicklas 5 import java.util.ArrayList;
5458 29 May 19 nicklas 6 import java.util.Arrays;
5425 14 May 19 nicklas 7 import java.util.Date;
5430 16 May 19 nicklas 8 import java.util.HashMap;
5392 02 May 19 nicklas 9 import java.util.List;
5430 16 May 19 nicklas 10 import java.util.Map;
5390 29 Apr 19 nicklas 11
5390 29 Apr 19 nicklas 12 import javax.servlet.ServletException;
5390 29 Apr 19 nicklas 13 import javax.servlet.http.HttpServlet;
5390 29 Apr 19 nicklas 14 import javax.servlet.http.HttpServletRequest;
5390 29 Apr 19 nicklas 15 import javax.servlet.http.HttpServletResponse;
5390 29 Apr 19 nicklas 16
5390 29 Apr 19 nicklas 17 import org.json.simple.JSONArray;
5390 29 Apr 19 nicklas 18 import org.json.simple.JSONObject;
5390 29 Apr 19 nicklas 19
5429 16 May 19 nicklas 20 import net.sf.basedb.core.AnyToAny;
5845 26 Feb 20 nicklas 21 import net.sf.basedb.core.BasicItem;
5425 14 May 19 nicklas 22 import net.sf.basedb.core.BioMaterialEvent;
5417 10 May 19 nicklas 23 import net.sf.basedb.core.BioMaterialEventSource;
5392 02 May 19 nicklas 24 import net.sf.basedb.core.BioPlate;
5417 10 May 19 nicklas 25 import net.sf.basedb.core.BioPlateEvent;
5417 10 May 19 nicklas 26 import net.sf.basedb.core.BioPlateEventParticipant;
5417 10 May 19 nicklas 27 import net.sf.basedb.core.BioPlateEventType;
5417 10 May 19 nicklas 28 import net.sf.basedb.core.BioWell;
5390 29 Apr 19 nicklas 29 import net.sf.basedb.core.DbControl;
5826 18 Feb 20 nicklas 30 import net.sf.basedb.core.DerivedBioAssay;
5392 02 May 19 nicklas 31 import net.sf.basedb.core.Extract;
5397 03 May 19 nicklas 32 import net.sf.basedb.core.File;
5425 14 May 19 nicklas 33 import net.sf.basedb.core.InvalidDataException;
5417 10 May 19 nicklas 34 import net.sf.basedb.core.Item;
5425 14 May 19 nicklas 35 import net.sf.basedb.core.ItemList;
5826 18 Feb 20 nicklas 36 import net.sf.basedb.core.ItemNotFoundException;
5392 02 May 19 nicklas 37 import net.sf.basedb.core.ItemQuery;
5417 10 May 19 nicklas 38 import net.sf.basedb.core.ItemSubtype;
5826 18 Feb 20 nicklas 39 import net.sf.basedb.core.Job;
5397 03 May 19 nicklas 40 import net.sf.basedb.core.Path;
5439 20 May 19 nicklas 41 import net.sf.basedb.core.PhysicalBioAssay;
5417 10 May 19 nicklas 42 import net.sf.basedb.core.PlateGeometry;
5425 14 May 19 nicklas 43 import net.sf.basedb.core.Protocol;
5390 29 Apr 19 nicklas 44 import net.sf.basedb.core.SessionControl;
5826 18 Feb 20 nicklas 45 import net.sf.basedb.core.Software;
5429 16 May 19 nicklas 46 import net.sf.basedb.core.Tag;
5845 26 Feb 20 nicklas 47 import net.sf.basedb.core.Trashcan;
5458 29 May 19 nicklas 48 import net.sf.basedb.core.Type;
5427 14 May 19 nicklas 49 import net.sf.basedb.core.query.Annotations;
5427 14 May 19 nicklas 50 import net.sf.basedb.core.query.Expressions;
5392 02 May 19 nicklas 51 import net.sf.basedb.core.query.Hql;
5392 02 May 19 nicklas 52 import net.sf.basedb.core.query.Orders;
5392 02 May 19 nicklas 53 import net.sf.basedb.core.query.Restrictions;
5826 18 Feb 20 nicklas 54 import net.sf.basedb.core.snapshot.SnapshotManager;
5826 18 Feb 20 nicklas 55 import net.sf.basedb.opengrid.JobDefinition;
5826 18 Feb 20 nicklas 56 import net.sf.basedb.opengrid.OpenGridCluster;
5826 18 Feb 20 nicklas 57 import net.sf.basedb.opengrid.service.OpenGridService;
5390 29 Apr 19 nicklas 58 import net.sf.basedb.reggie.JsonUtil;
5390 29 Apr 19 nicklas 59 import net.sf.basedb.reggie.Reggie;
5400 06 May 19 nicklas 60 import net.sf.basedb.reggie.activity.ActivityDef;
5390 29 Apr 19 nicklas 61 import net.sf.basedb.reggie.counter.CounterService;
5845 26 Feb 20 nicklas 62 import net.sf.basedb.reggie.dao.AlignedSequences;
5413 09 May 19 nicklas 63 import net.sf.basedb.reggie.dao.Annotationtype;
5425 14 May 19 nicklas 64 import net.sf.basedb.reggie.dao.BiomaterialList;
5390 29 Apr 19 nicklas 65 import net.sf.basedb.reggie.dao.BioplateType;
5392 02 May 19 nicklas 66 import net.sf.basedb.reggie.dao.Dna;
5791 16 Dec 19 nicklas 67 import net.sf.basedb.reggie.dao.DoNotUse;
5439 20 May 19 nicklas 68 import net.sf.basedb.reggie.dao.FlowCell;
5424 13 May 19 nicklas 69 import net.sf.basedb.reggie.dao.Library;
5826 18 Feb 20 nicklas 70 import net.sf.basedb.reggie.dao.MergedSequences;
5434 17 May 19 nicklas 71 import net.sf.basedb.reggie.dao.Pipeline;
5427 14 May 19 nicklas 72 import net.sf.basedb.reggie.dao.PooledLibrary;
5425 14 May 19 nicklas 73 import net.sf.basedb.reggie.dao.ReactionPlate;
5417 10 May 19 nicklas 74 import net.sf.basedb.reggie.dao.ReggieItem;
5405 08 May 19 nicklas 75 import net.sf.basedb.reggie.dao.ReggieRole;
5425 14 May 19 nicklas 76 import net.sf.basedb.reggie.dao.Rna;
5417 10 May 19 nicklas 77 import net.sf.basedb.reggie.dao.Subtype;
5826 18 Feb 20 nicklas 78 import net.sf.basedb.reggie.grid.MipsAlignJobCreator;
5826 18 Feb 20 nicklas 79 import net.sf.basedb.reggie.grid.ScriptUtil;
5397 03 May 19 nicklas 80 import net.sf.basedb.reggie.plugins.AliquotImporter;
5397 03 May 19 nicklas 81 import net.sf.basedb.reggie.plugins.AliquotImporter.AliquotPlate;
5760 26 Nov 19 nicklas 82 import net.sf.basedb.reggie.query.AnyToAnyRestriction;
5417 10 May 19 nicklas 83 import net.sf.basedb.util.EqualsHelper;
5397 03 May 19 nicklas 84 import net.sf.basedb.util.FileUtil;
5392 02 May 19 nicklas 85 import net.sf.basedb.util.Values;
5390 29 Apr 19 nicklas 86 import net.sf.basedb.util.error.ThrowableUtil;
5390 29 Apr 19 nicklas 87
5390 29 Apr 19 nicklas 88
5390 29 Apr 19 nicklas 89 public class MipsServlet 
5390 29 Apr 19 nicklas 90   extends HttpServlet 
5390 29 Apr 19 nicklas 91 {
5390 29 Apr 19 nicklas 92
5390 29 Apr 19 nicklas 93   private static final long serialVersionUID = 772229562222976136L;
5390 29 Apr 19 nicklas 94
5390 29 Apr 19 nicklas 95   public MipsServlet()
5390 29 Apr 19 nicklas 96   {}
5390 29 Apr 19 nicklas 97
5826 18 Feb 20 nicklas 98   @SuppressWarnings("unchecked")
5390 29 Apr 19 nicklas 99   @Override
5390 29 Apr 19 nicklas 100   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
5390 29 Apr 19 nicklas 101     throws ServletException, IOException 
5390 29 Apr 19 nicklas 102   {
5390 29 Apr 19 nicklas 103     String cmd = req.getParameter("cmd");
5390 29 Apr 19 nicklas 104     JsonUtil.setJsonResponseHeaders(resp);
5390 29 Apr 19 nicklas 105     
5390 29 Apr 19 nicklas 106     JSONObject json = new JSONObject();
5390 29 Apr 19 nicklas 107     json.put("status", "ok");
5390 29 Apr 19 nicklas 108     
5390 29 Apr 19 nicklas 109     final SessionControl sc = Reggie.getSessionControl(req);
5390 29 Apr 19 nicklas 110     DbControl dc = null;
5390 29 Apr 19 nicklas 111     try
5390 29 Apr 19 nicklas 112     {
5390 29 Apr 19 nicklas 113       if ("GetNextAutoGeneratedPlateName".equals(cmd))
5390 29 Apr 19 nicklas 114       {
5390 29 Apr 19 nicklas 115         /*
5390 29 Apr 19 nicklas 116           Eg. MIPsPlate0001, MIPsPlate0002 and so on
5390 29 Apr 19 nicklas 117         */
5390 29 Apr 19 nicklas 118         BioplateType bioPlateType = BioplateType.getByCName(req.getParameter("bioPlateType"));
6337 16 Jun 21 nicklas 119         dc = sc.newDbControl(":Desing MIPs plate");
5390 29 Apr 19 nicklas 120         json.put("name", bioPlateType.getNextPlateName(dc, false));
5390 29 Apr 19 nicklas 121       }
5458 29 May 19 nicklas 122       else if ("GetAliquotPlatesForMips".equals(cmd))
5458 29 May 19 nicklas 123       {
6337 16 Jun 21 nicklas 124         dc = sc.newDbControl(":Design MIPs plate");
5458 29 May 19 nicklas 125         
5458 29 May 19 nicklas 126         String ignore = Values.getStringOrNull(req.getParameter("ignore"));
5458 29 May 19 nicklas 127         
5458 29 May 19 nicklas 128         ItemQuery<BioPlate> query = BioPlate.getQuery();
5458 29 May 19 nicklas 129         BioplateType.PRE_NORMALIZED_DNA.addFilter(dc, query, true);
5458 29 May 19 nicklas 130         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5458 29 May 19 nicklas 131         
5458 29 May 19 nicklas 132         // Must have PlateProcess=DesignMIPs|AddAliquots
5458 29 May 19 nicklas 133         query.join(Annotations.innerJoin(Annotationtype.PLATE_PROCESSING.load(dc), "pp"));
5458 29 May 19 nicklas 134         query.restrict(Restrictions.or(
5458 29 May 19 nicklas 135             Restrictions.eq(Hql.alias("pp"), Expressions.string("DesignMIPs")),
5458 29 May 19 nicklas 136             Restrictions.eq(Hql.alias("pp"), Expressions.string("AddAliquots"))
5458 29 May 19 nicklas 137           ));
5458 29 May 19 nicklas 138         if (ignore != null)
5458 29 May 19 nicklas 139         {
5458 29 May 19 nicklas 140           List<Integer> idList = Arrays.asList(Values.getInt(ignore.split(",")));
5458 29 May 19 nicklas 141           query.restrict(Restrictions.not(Restrictions.in(Hql.property("id"), Expressions.parameter("idList", Type.INT))));
5458 29 May 19 nicklas 142           query.setParameter("idList", idList, Type.INT);
5458 29 May 19 nicklas 143         }
5458 29 May 19 nicklas 144         
5458 29 May 19 nicklas 145         query.order(Orders.asc(Hql.property("name")));
5458 29 May 19 nicklas 146         
5458 29 May 19 nicklas 147         // Count number of aliquots that have been used before (=they have a child item)
5458 29 May 19 nicklas 148         ItemQuery<Extract> aliquotQuery = Extract.getQuery();
5458 29 May 19 nicklas 149         aliquotQuery.join(Hql.innerJoin(null, "bioWell", "bw", true));
5458 29 May 19 nicklas 150         aliquotQuery.join(Hql.innerJoin("bw", "bioPlate", "bp"));
5458 29 May 19 nicklas 151         aliquotQuery.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entityParameter("plate", Item.BIOPLATE)));
5458 29 May 19 nicklas 152         aliquotQuery.restrict(Restrictions.eq(Hql.size(null, "childCreationEvents"), Expressions.integer(0)));
5458 29 May 19 nicklas 153         
5458 29 May 19 nicklas 154         JSONArray jsonPlates = new JSONArray();
5458 29 May 19 nicklas 155         List<ReactionPlate> result = ReactionPlate.toList(BioplateType.PRE_NORMALIZED_DNA, query.list(dc));
5458 29 May 19 nicklas 156         for (ReactionPlate plate: result)
5458 29 May 19 nicklas 157         {
5458 29 May 19 nicklas 158           plate.loadAnnotations(dc, "PlateProcessing", Annotationtype.PLATE_PROCESSING, null);
5458 29 May 19 nicklas 159           plate.loadAnnotations(dc, "AutoProcessing", Annotationtype.AUTO_PROCESSING, null);
5458 29 May 19 nicklas 160           BioPlate bp = plate.getBioPlate();
5458 29 May 19 nicklas 161           plate.setAnnotation("registrationDate", Reggie.CONVERTER_DATE_TO_STRING.convert(bp.getEntryDate()));
5458 29 May 19 nicklas 162           plate.setAnnotation("comments", bp.getDescription());
5458 29 May 19 nicklas 163
5458 29 May 19 nicklas 164           aliquotQuery.setEntityParameter("plate", bp);
5458 29 May 19 nicklas 165           plate.setAnnotation("unusedAliquots", aliquotQuery.count(dc));
5458 29 May 19 nicklas 166           
5458 29 May 19 nicklas 167           jsonPlates.add(plate.asJSONObject());
5458 29 May 19 nicklas 168         }
5458 29 May 19 nicklas 169         json.put("plates", jsonPlates);
5458 29 May 19 nicklas 170       }
5392 02 May 19 nicklas 171       else if ("GetUnusedAliquotsFromPlates".equals(cmd))
5392 02 May 19 nicklas 172       {
6337 16 Jun 21 nicklas 173         dc = sc.newDbControl(":Design MIPs plate");
5392 02 May 19 nicklas 174         
5459 29 May 19 nicklas 175         boolean includeAll = Values.getBoolean(req.getParameter("includeall"));
5459 29 May 19 nicklas 176         
5392 02 May 19 nicklas 177         JSONArray jsonPlates = new JSONArray();
5392 02 May 19 nicklas 178         
5392 02 May 19 nicklas 179         for (String plateId : req.getParameter("plates").split(","))
5392 02 May 19 nicklas 180         {
5392 02 May 19 nicklas 181           BioPlate aliquotPlate = BioPlate.getById(dc, Values.getInt(plateId));
5392 02 May 19 nicklas 182           
5392 02 May 19 nicklas 183           JSONObject jsonPlate = new JSONObject();
5392 02 May 19 nicklas 184           jsonPlate.put("id", aliquotPlate.getId());
5392 02 May 19 nicklas 185           jsonPlate.put("name", aliquotPlate.getName());
5392 02 May 19 nicklas 186           jsonPlate.put("columns", aliquotPlate.getColumns());
5392 02 May 19 nicklas 187           jsonPlate.put("rows", aliquotPlate.getRows());
5392 02 May 19 nicklas 188           jsonPlate.put("comments", aliquotPlate.getDescription());
5392 02 May 19 nicklas 189           jsonPlates.add(jsonPlate);
5390 29 Apr 19 nicklas 190
5458 29 May 19 nicklas 191           boolean reProcess = "ReProcess".equals(Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, aliquotPlate));
5458 29 May 19 nicklas 192           
5392 02 May 19 nicklas 193           ItemQuery<Extract> query = Extract.getQuery();
5392 02 May 19 nicklas 194           query.join(Hql.innerJoin(null, "bioWell", "bw", true));
5392 02 May 19 nicklas 195           query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
5392 02 May 19 nicklas 196           query.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entity(aliquotPlate)));
5459 29 May 19 nicklas 197           if (!reProcess && !includeAll)
5458 29 May 19 nicklas 198           {
5458 29 May 19 nicklas 199             // Aliquots must have no child items
5458 29 May 19 nicklas 200             query.restrict(Restrictions.eq(Hql.size(null, "childCreationEvents"), Expressions.integer(0)));
5458 29 May 19 nicklas 201           }
5392 02 May 19 nicklas 202           query.order(Orders.asc(Hql.property("bw", "column")));
5392 02 May 19 nicklas 203           query.order(Orders.asc(Hql.property("bw", "row")));
5392 02 May 19 nicklas 204
5392 02 May 19 nicklas 205           List<Dna> dna = Dna.toDna(query.list(dc));
5392 02 May 19 nicklas 206           JSONArray jsonAliquots = new JSONArray();
5392 02 May 19 nicklas 207           for (Dna d : dna)
5392 02 May 19 nicklas 208           {
5392 02 May 19 nicklas 209             d.loadBioPlateLocation();
5413 09 May 19 nicklas 210             Extract e = d.getExtract();
5413 09 May 19 nicklas 211             d.setAnnotation("comment", e.getDescription());
5459 29 May 19 nicklas 212             if (includeAll && !reProcess)
5459 29 May 19 nicklas 213             {
5459 29 May 19 nicklas 214               d.setAnnotation("numChildren", e.countChildren(true));
5459 29 May 19 nicklas 215             }
5603 13 Sep 19 nicklas 216             d.loadAnnotations(dc, "MIPS_Panel", Annotationtype.MIPS_PANEL, null);
5448 27 May 19 nicklas 217             d.loadAnnotations(dc, "MIPS_PoolVolume", Annotationtype.MIPS_POOL_VOLUME, null);
5448 27 May 19 nicklas 218             d.loadAnnotations(dc, "MIPS_NormalTumor", Annotationtype.MIPS_NORMAL_TUMOR, null);
5448 27 May 19 nicklas 219             d.loadAnnotations(dc, "MIPS_Form", Annotationtype.MIPS_FORM, null);
5392 02 May 19 nicklas 220             jsonAliquots.add(d.asJSONObject());
5392 02 May 19 nicklas 221           }
5392 02 May 19 nicklas 222           jsonPlate.put("aliquots", jsonAliquots);
5392 02 May 19 nicklas 223         }
5392 02 May 19 nicklas 224         
5392 02 May 19 nicklas 225         json.put("plates", jsonPlates);
5392 02 May 19 nicklas 226       }
5424 13 May 19 nicklas 227       else if ("GetUnprocessedPlates".equals(cmd))
5424 13 May 19 nicklas 228       {
6337 16 Jun 21 nicklas 229         dc = sc.newDbControl(":MIPs - Library registration");
5424 13 May 19 nicklas 230         BioplateType plateType = BioplateType.getByCName(req.getParameter("plateType"));
5424 13 May 19 nicklas 231         
5424 13 May 19 nicklas 232         // Find plates of the given 'plate type' without 'creation date' and no 'destroyed' flag set
5424 13 May 19 nicklas 233         ItemQuery<BioPlate> query = BioPlate.getQuery();
5424 13 May 19 nicklas 234         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5424 13 May 19 nicklas 235         query.restrict(Restrictions.eq(Hql.property("eventDate"), null));
5424 13 May 19 nicklas 236         plateType.addFilter(dc, query, true);
5424 13 May 19 nicklas 237         
5424 13 May 19 nicklas 238         query.order(Orders.desc(Hql.property("id")));
5424 13 May 19 nicklas 239         
5424 13 May 19 nicklas 240         List<BioPlate> result = query.list(dc);
5424 13 May 19 nicklas 241         JSONArray jsonPlates = new JSONArray();
5424 13 May 19 nicklas 242         for (BioPlate plate: result)
5424 13 May 19 nicklas 243         {
5424 13 May 19 nicklas 244           JSONObject jsonPlate = new JSONObject();
5424 13 May 19 nicklas 245           jsonPlate.put("id", plate.getId());
5424 13 May 19 nicklas 246           jsonPlate.put("name", plate.getName());
5424 13 May 19 nicklas 247           jsonPlate.put("columns", plate.getColumns());
5424 13 May 19 nicklas 248           jsonPlate.put("rows", plate.getRows());
5424 13 May 19 nicklas 249           jsonPlate.put("comments", plate.getDescription());
5424 13 May 19 nicklas 250           jsonPlates.add(jsonPlate);
5424 13 May 19 nicklas 251         }
5424 13 May 19 nicklas 252         json.put("bioplates", jsonPlates);
5424 13 May 19 nicklas 253       }
5424 13 May 19 nicklas 254       else if ("GetLibraryInfoForPlate".equals(cmd))
5424 13 May 19 nicklas 255       {
5424 13 May 19 nicklas 256         int plateId = Values.getInt(req.getParameter("bioplate"));
6337 16 Jun 21 nicklas 257         dc = sc.newDbControl(":MIPs - Library protocols");
5424 13 May 19 nicklas 258
5424 13 May 19 nicklas 259         BioPlate mipsPlate = BioPlate.getById(dc, plateId);
5424 13 May 19 nicklas 260
5424 13 May 19 nicklas 261         JSONObject jsonPlate = new JSONObject();
5424 13 May 19 nicklas 262         jsonPlate.put("id", mipsPlate.getId());
5424 13 May 19 nicklas 263         jsonPlate.put("name", mipsPlate.getName());
5424 13 May 19 nicklas 264         jsonPlate.put("columns", mipsPlate.getColumns());
5424 13 May 19 nicklas 265         jsonPlate.put("rows", mipsPlate.getRows());
5424 13 May 19 nicklas 266         jsonPlate.put("comments", mipsPlate.getDescription());
5424 13 May 19 nicklas 267         
5424 13 May 19 nicklas 268         ItemQuery<Extract> query = Extract.getQuery();
5424 13 May 19 nicklas 269         query.join(Hql.innerJoin(null, "bioWell", "bw", true));
5424 13 May 19 nicklas 270         query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
5424 13 May 19 nicklas 271         query.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entity(mipsPlate)));
5424 13 May 19 nicklas 272         query.order(Orders.asc(Hql.property("bw", "column")));
5424 13 May 19 nicklas 273         query.order(Orders.asc(Hql.property("bw", "row")));
5424 13 May 19 nicklas 274
5424 13 May 19 nicklas 275         List<Library> libs = Library.toList(query.list(dc));
5424 13 May 19 nicklas 276         JSONArray jsonLibs = new JSONArray();
5424 13 May 19 nicklas 277         
5424 13 May 19 nicklas 278         for (Library lib : libs)
5424 13 May 19 nicklas 279         {
5424 13 May 19 nicklas 280           lib.loadBioPlateLocation();
5424 13 May 19 nicklas 281           Dna dna = lib.getDna(dc, true);
5424 13 May 19 nicklas 282           dna.loadBioPlateLocation();
5603 13 Sep 19 nicklas 283           dna.loadAnnotations(dc, "MIPS_Panel", Annotationtype.MIPS_PANEL, null);
5451 27 May 19 nicklas 284           dna.loadAnnotations(dc, "MIPS_PoolVolume", Annotationtype.MIPS_POOL_VOLUME, null);
5451 27 May 19 nicklas 285           dna.loadAnnotations(dc, "MIPS_NormalTumor", Annotationtype.MIPS_NORMAL_TUMOR, null);
5451 27 May 19 nicklas 286           dna.loadAnnotations(dc, "MIPS_Form", Annotationtype.MIPS_FORM, null);
5424 13 May 19 nicklas 287           lib.setAnnotation("dna", dna.asJSONObject());
5424 13 May 19 nicklas 288           lib.setAnnotation("comment", lib.getItem().getDescription());
5424 13 May 19 nicklas 289           jsonLibs.add(lib.asJSONObject());
5424 13 May 19 nicklas 290         }
5424 13 May 19 nicklas 291         
5424 13 May 19 nicklas 292         json.put("libraries", jsonLibs);
5424 13 May 19 nicklas 293         json.put("plate", jsonPlate);
5424 13 May 19 nicklas 294       }
5427 14 May 19 nicklas 295       else if ("GetLibraryPlatesForPooling".equals(cmd))
5427 14 May 19 nicklas 296       {
5427 14 May 19 nicklas 297         /*
5427 14 May 19 nicklas 298           Find all MIPs plates for pooling.
5427 14 May 19 nicklas 299         */
6337 16 Jun 21 nicklas 300         dc = sc.newDbControl(":MIPs - Register pooled libraries");
5427 14 May 19 nicklas 301         
5427 14 May 19 nicklas 302         ItemQuery<BioPlate> query = BioPlate.getQuery();
5427 14 May 19 nicklas 303         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5427 14 May 19 nicklas 304         // Only MIPs plates that are not destroyed
5427 14 May 19 nicklas 305         BioplateType.MIPS.addFilter(dc, query, true);
5427 14 May 19 nicklas 306         // Must have a PLATE_PROCESS_RESULT=Success annotation
5427 14 May 19 nicklas 307         query.join(Annotations.innerJoin(null, Annotationtype.PLATE_PROCESS_RESULT.load(dc), "ppr"));
5427 14 May 19 nicklas 308         query.restrict(Restrictions.eq(Hql.alias("ppr"), Expressions.string(ReactionPlate.PROCESS_SUCCESSFUL)));
5427 14 May 19 nicklas 309         // Must NOT have a PoolDate annotation
5427 14 May 19 nicklas 310         query.join(Annotations.leftJoin(null, Annotationtype.POOL_DATE.load(dc), "pd"));
5427 14 May 19 nicklas 311         query.restrict(Restrictions.eq(Hql.alias("pd"), null));
5427 14 May 19 nicklas 312         query.order(Orders.asc(Hql.property("name")));
5427 14 May 19 nicklas 313         
5430 16 May 19 nicklas 314         // Query for loading libraries on a plate
5430 16 May 19 nicklas 315         ItemQuery<Extract> libQuery = Extract.getQuery();
5430 16 May 19 nicklas 316         libQuery.join(Hql.innerJoin(null, "bioWell", "bw", true));
5430 16 May 19 nicklas 317         libQuery.join(Hql.innerJoin("bw", "bioPlate", "bp"));
5430 16 May 19 nicklas 318         libQuery.join(Hql.innerJoin(null, "tag", "bc", true));
5430 16 May 19 nicklas 319         libQuery.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entityParameter("mipsplate", Item.BIOPLATE)));
5430 16 May 19 nicklas 320         libQuery.order(Orders.asc(Hql.property("bw", "column")));
5430 16 May 19 nicklas 321         libQuery.order(Orders.asc(Hql.property("bw", "row")));
5430 16 May 19 nicklas 322         
5427 14 May 19 nicklas 323         List<BioPlate> plates = query.list(dc);
5427 14 May 19 nicklas 324         JSONArray jsonPlates = new JSONArray();
5427 14 May 19 nicklas 325         for (BioPlate plate : plates)
5427 14 May 19 nicklas 326         {
5427 14 May 19 nicklas 327           JSONObject jsonPlate = new JSONObject();
5427 14 May 19 nicklas 328           jsonPlate.put("id", plate.getId());
5427 14 May 19 nicklas 329           jsonPlate.put("name", plate.getName());
5427 14 May 19 nicklas 330           jsonPlate.put("comments", plate.getDescription());
5430 16 May 19 nicklas 331           
5430 16 May 19 nicklas 332           try
5430 16 May 19 nicklas 333           {
5430 16 May 19 nicklas 334             AnyToAny barcodeTemplate = AnyToAny.getByName(dc, plate, "BarcodeTemplate");
5430 16 May 19 nicklas 335             BioPlate barcodePlate = (BioPlate)barcodeTemplate.getTo();
5430 16 May 19 nicklas 336             jsonPlate.put("barcodeTemplate", barcodePlate.getName());
5430 16 May 19 nicklas 337           }
5430 16 May 19 nicklas 338           catch (RuntimeException ex)
5430 16 May 19 nicklas 339           {}
5430 16 May 19 nicklas 340           
5430 16 May 19 nicklas 341           // Load libraries and their barcodes
5430 16 May 19 nicklas 342           libQuery.setEntityParameter("mipsplate", plate);
5430 16 May 19 nicklas 343           List<Library> libs = Library.toList(libQuery.list(dc));
5430 16 May 19 nicklas 344           JSONArray jsonLibs = new JSONArray();
5430 16 May 19 nicklas 345           for (Library lib : libs)
5430 16 May 19 nicklas 346           {
5430 16 May 19 nicklas 347             lib.loadBioPlateLocation();
5430 16 May 19 nicklas 348             Tag barcode = lib.getExtract().getTag();
5430 16 May 19 nicklas 349             lib.setAnnotation("barcode", barcode.getId());
5430 16 May 19 nicklas 350             jsonLibs.add(lib.asJSONObject());
5430 16 May 19 nicklas 351           }
5430 16 May 19 nicklas 352           jsonPlate.put("libraries", jsonLibs);
5427 14 May 19 nicklas 353           jsonPlates.add(jsonPlate);
5427 14 May 19 nicklas 354         }
5427 14 May 19 nicklas 355         json.put("bioplates", jsonPlates);
5427 14 May 19 nicklas 356         json.put("nextPoolName", PooledLibrary.getNextNames(dc, 1).get(0));
5427 14 May 19 nicklas 357       }
5429 16 May 19 nicklas 358       else if ("GetBarcodeTemplates".equals(cmd))
5429 16 May 19 nicklas 359       {
6337 16 Jun 21 nicklas 360         dc = sc.newDbControl(":MIPs - Library registration");
5429 16 May 19 nicklas 361         ItemQuery<BioPlate> query = BioPlate.getQuery();
5429 16 May 19 nicklas 362         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5429 16 May 19 nicklas 363         BioplateType.BARCODE_TEMPLATE.addFilter(dc, query, true);
5438 17 May 19 nicklas 364         Pipeline.MIPS.addFilter(dc, query);
5429 16 May 19 nicklas 365         query.order(Orders.asc(Hql.property("name")));
5429 16 May 19 nicklas 366         
5760 26 Nov 19 nicklas 367         // Find other MIPs plates with a given template that has not yet been pooled
5760 26 Nov 19 nicklas 368         // We use this information to display a warning if registering more than one plate with
5760 26 Nov 19 nicklas 369         // the same barcode template since that is typically a mistake
5760 26 Nov 19 nicklas 370         ItemQuery<BioPlate> unpooledQuery = BioPlate.getQuery();
5760 26 Nov 19 nicklas 371         unpooledQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5760 26 Nov 19 nicklas 372         // Only MIPs plates that are not destroyed
5760 26 Nov 19 nicklas 373         BioplateType.MIPS.addFilter(dc, unpooledQuery, true);
5760 26 Nov 19 nicklas 374         // Must have a PLATE_PROCESS_RESULT=Success annotation
5760 26 Nov 19 nicklas 375         unpooledQuery.join(Annotations.innerJoin(null, Annotationtype.PLATE_PROCESS_RESULT.load(dc), "ppr"));
5760 26 Nov 19 nicklas 376         unpooledQuery.restrict(Restrictions.eq(Hql.alias("ppr"), Expressions.string(ReactionPlate.PROCESS_SUCCESSFUL)));
5760 26 Nov 19 nicklas 377         // Must NOT have a PoolDate annotation
5760 26 Nov 19 nicklas 378         unpooledQuery.join(Annotations.leftJoin(null, Annotationtype.POOL_DATE.load(dc), "pd"));
5760 26 Nov 19 nicklas 379         unpooledQuery.restrict(Restrictions.eq(Hql.alias("pd"), null));
5760 26 Nov 19 nicklas 380         // Filter on BarcodeTemplate link
5760 26 Nov 19 nicklas 381         unpooledQuery.restrict(AnyToAnyRestriction.exists("BarcodeTemplate", 
5760 26 Nov 19 nicklas 382           Restrictions.eq(Hql.property("at", "toId"), Hql.entityParameter("template", Item.BIOPLATE))));
5760 26 Nov 19 nicklas 383         unpooledQuery.order(Orders.asc(Hql.property("name")));
5760 26 Nov 19 nicklas 384         
5429 16 May 19 nicklas 385         List<BioPlate> result = query.list(dc);
5429 16 May 19 nicklas 386         JSONArray jsonPlates = new JSONArray();
5429 16 May 19 nicklas 387         for (BioPlate plate: result)
5429 16 May 19 nicklas 388         {
5429 16 May 19 nicklas 389           JSONObject jsonPlate = new JSONObject();
5429 16 May 19 nicklas 390           jsonPlate.put("id", plate.getId());
5429 16 May 19 nicklas 391           jsonPlate.put("name", plate.getName());
5429 16 May 19 nicklas 392           jsonPlate.put("columns", plate.getColumns());
5429 16 May 19 nicklas 393           jsonPlate.put("rows", plate.getRows());
5429 16 May 19 nicklas 394           jsonPlate.put("comments", plate.getDescription());
5429 16 May 19 nicklas 395           jsonPlates.add(jsonPlate);
5760 26 Nov 19 nicklas 396           
5760 26 Nov 19 nicklas 397           JSONArray jsonUnpooled = new JSONArray();
5760 26 Nov 19 nicklas 398           unpooledQuery.setEntityParameter("template", plate);
5760 26 Nov 19 nicklas 399           for (BioPlate unpooled : unpooledQuery.list(dc))
5760 26 Nov 19 nicklas 400           {
5760 26 Nov 19 nicklas 401             JSONObject jsonUP = new JSONObject();
5760 26 Nov 19 nicklas 402             jsonUP.put("id", unpooled.getId());
5760 26 Nov 19 nicklas 403             jsonUP.put("name", unpooled.getName());
5760 26 Nov 19 nicklas 404             jsonUnpooled.add(jsonUP);
5760 26 Nov 19 nicklas 405           }
5760 26 Nov 19 nicklas 406           jsonPlate.put("otherPlates", jsonUnpooled);
5760 26 Nov 19 nicklas 407           
5429 16 May 19 nicklas 408         }
5429 16 May 19 nicklas 409         json.put("templates", jsonPlates);
5429 16 May 19 nicklas 410       }
5439 20 May 19 nicklas 411       else if ("GetUnusedPools".equals(cmd) || "GetPoolInfo".equals(cmd))
5439 20 May 19 nicklas 412       {
5439 20 May 19 nicklas 413         /*
5439 20 May 19 nicklas 414           Find all pools which has not yet been used on a flow cell.
5439 20 May 19 nicklas 415         */
6337 16 Jun 21 nicklas 416         dc = sc.newDbControl(":MIPs - Create flow cells");
5439 20 May 19 nicklas 417         List<PooledLibrary> pools = null;
5439 20 May 19 nicklas 418         if ("GetPoolInfo".equals(cmd))
5439 20 May 19 nicklas 419         {
5439 20 May 19 nicklas 420           pools = new ArrayList<PooledLibrary>();
5439 20 May 19 nicklas 421           for (String poolId : req.getParameter("pools").split(","))
5439 20 May 19 nicklas 422           {
5439 20 May 19 nicklas 423             pools.add(PooledLibrary.getById(dc, Values.getInt(poolId)));
5439 20 May 19 nicklas 424           }
5439 20 May 19 nicklas 425         }
5439 20 May 19 nicklas 426         else
5439 20 May 19 nicklas 427         {
5439 20 May 19 nicklas 428           pools = PooledLibrary.findUnusedPools(dc, Pipeline.MIPS);
5439 20 May 19 nicklas 429         }
5439 20 May 19 nicklas 430         
5439 20 May 19 nicklas 431         JSONArray jsonPools = new JSONArray();
5439 20 May 19 nicklas 432         for (PooledLibrary pool : pools)
5439 20 May 19 nicklas 433         {
5439 20 May 19 nicklas 434           pool.loadLibPlates(dc);
5439 20 May 19 nicklas 435           pool.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
5439 20 May 19 nicklas 436           jsonPools.add(pool.asJSONObject());
5439 20 May 19 nicklas 437         }
5439 20 May 19 nicklas 438         json.put("pools", jsonPools);
5439 20 May 19 nicklas 439       }
5439 20 May 19 nicklas 440       else if ("GetNextAutoGeneratedFlowCellNames".equals(cmd))
5439 20 May 19 nicklas 441       {
5439 20 May 19 nicklas 442         // Get the flow cell names for the next number of flow cells to create
6337 16 Jun 21 nicklas 443         dc = sc.newDbControl(":MIPs - Create flow cells");
5439 20 May 19 nicklas 444         int numNames = Values.getInt(req.getParameter("numNames"));
5439 20 May 19 nicklas 445         json.put("names", FlowCell.getNextNames(dc, numNames));
5439 20 May 19 nicklas 446       }
5826 18 Feb 20 nicklas 447       else if ("GetMergedSequencesForAlignment".equals(cmd))
5826 18 Feb 20 nicklas 448       {
6337 16 Jun 21 nicklas 449         dc = sc.newDbControl(":MIPs - Start alignment");
5826 18 Feb 20 nicklas 450         
5826 18 Feb 20 nicklas 451         List<MergedSequences> list = null;
5826 18 Feb 20 nicklas 452         String items = Values.getStringOrNull(req.getParameter("items"));
5826 18 Feb 20 nicklas 453         if (items == null)
5826 18 Feb 20 nicklas 454         {
5826 18 Feb 20 nicklas 455           // Load items from the pipeline list
5826 18 Feb 20 nicklas 456           ItemList mipsPipeline = BiomaterialList.MIPS_ALIGN_PIPELINE.load(dc);
6183 26 Mar 21 nicklas 457           ItemQuery<DerivedBioAssay> query = mipsPipeline.getMembers();
5826 18 Feb 20 nicklas 458           query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5826 18 Feb 20 nicklas 459           Subtype.MERGED_SEQUENCES.addFilter(dc, query);
5826 18 Feb 20 nicklas 460           // Join LibPlate for sorting
5826 18 Feb 20 nicklas 461           query.join(Hql.innerJoin(null, "extract", "lib", true));
5826 18 Feb 20 nicklas 462           query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
5826 18 Feb 20 nicklas 463           query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
5826 18 Feb 20 nicklas 464           query.order(Orders.asc(Hql.property("libPlate", "id")));
5826 18 Feb 20 nicklas 465           query.order(Orders.asc(Hql.property("name")));
5826 18 Feb 20 nicklas 466           query.setMaxResults(250);
5826 18 Feb 20 nicklas 467           list = MergedSequences.toList(query.list(dc));
5826 18 Feb 20 nicklas 468         }
5826 18 Feb 20 nicklas 469         else
5826 18 Feb 20 nicklas 470         {
5826 18 Feb 20 nicklas 471           Integer[] ids = Values.getInt(items.split(","));
5826 18 Feb 20 nicklas 472           list = new ArrayList<MergedSequences>();
5826 18 Feb 20 nicklas 473           for (Integer id : ids)
5826 18 Feb 20 nicklas 474           {
5826 18 Feb 20 nicklas 475             list.add(MergedSequences.getById(dc, id));
5826 18 Feb 20 nicklas 476           }
5826 18 Feb 20 nicklas 477         }
5439 20 May 19 nicklas 478
5826 18 Feb 20 nicklas 479         SnapshotManager manager = new SnapshotManager();
5826 18 Feb 20 nicklas 480         JSONArray jsonMergedSequences = new JSONArray();
5826 18 Feb 20 nicklas 481         for (MergedSequences ms : list)
5826 18 Feb 20 nicklas 482         {
5826 18 Feb 20 nicklas 483           ms.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
5826 18 Feb 20 nicklas 484           Library lib = ms.getLibrary(dc);
5826 18 Feb 20 nicklas 485           lib.loadBioPlateLocation();
5826 18 Feb 20 nicklas 486           ms.setAnnotation("lib", lib.asJSONObject());
5826 18 Feb 20 nicklas 487           ms.loadAnnotations(dc, manager, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
5826 18 Feb 20 nicklas 488           ms.loadDoNotUseAnnotations(dc, manager);
5826 18 Feb 20 nicklas 489           jsonMergedSequences.add(ms.asJSONObject());
5826 18 Feb 20 nicklas 490         }
5826 18 Feb 20 nicklas 491         json.put("mergedSequences", jsonMergedSequences);        
5826 18 Feb 20 nicklas 492       }
5845 26 Feb 20 nicklas 493       else if ("GetUnconfirmedAlignedSequences".equals(cmd))
5845 26 Feb 20 nicklas 494       {
6337 16 Jun 21 nicklas 495         dc = sc.newDbControl(":MIPs - Confirm alignments");
5845 26 Feb 20 nicklas 496         ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
5845 26 Feb 20 nicklas 497         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5845 26 Feb 20 nicklas 498         Subtype.ALIGNED_SEQUENCES.addFilter(dc, query);
5845 26 Feb 20 nicklas 499         Pipeline.MIPS.addFilter(dc, query);
5845 26 Feb 20 nicklas 500         // The job must be a MIPs alignment job and it must be ended
5845 26 Feb 20 nicklas 501         query.join(Hql.innerJoin("job", "jb"));
5845 26 Feb 20 nicklas 502         Subtype.MIPS_ALIGN_JOB.addFilter(dc, query, "jb");
5845 26 Feb 20 nicklas 503         query.restrict(Restrictions.neq(Hql.property("jb", "ended"), null));
5845 26 Feb 20 nicklas 504         // Must NOT have a ANALYSIS_RESULT annotation
5845 26 Feb 20 nicklas 505         query.join(Annotations.leftJoin(null, Annotationtype.ANALYSIS_RESULT.load(dc), "ar"));
5845 26 Feb 20 nicklas 506         query.restrict(Restrictions.eq(Hql.alias("ar"), null));
5845 26 Feb 20 nicklas 507         // Ignore if AutoProcess==AutoConfirm
5845 26 Feb 20 nicklas 508         query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
5845 26 Feb 20 nicklas 509         query.restrict(
5845 26 Feb 20 nicklas 510             Restrictions.or(
5845 26 Feb 20 nicklas 511               Restrictions.neq(Hql.alias("ap"), Expressions.string("AutoConfirm")),
5845 26 Feb 20 nicklas 512               Restrictions.eq(Hql.alias("ap"), null)
5845 26 Feb 20 nicklas 513             ));
5845 26 Feb 20 nicklas 514         // Join LibPlate for sorting
5845 26 Feb 20 nicklas 515         query.join(Hql.innerJoin(null, "extract", "lib", true));
5845 26 Feb 20 nicklas 516         query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
5845 26 Feb 20 nicklas 517         query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
5845 26 Feb 20 nicklas 518         query.order(Orders.asc(Hql.property("libPlate", "id")));
5845 26 Feb 20 nicklas 519         query.order(Orders.asc(Hql.property("name")));
5845 26 Feb 20 nicklas 520         query.setMaxResults(250);
5845 26 Feb 20 nicklas 521         
5845 26 Feb 20 nicklas 522         SnapshotManager manager = new SnapshotManager();
5845 26 Feb 20 nicklas 523         JSONArray jsonAligned = new JSONArray();
5845 26 Feb 20 nicklas 524         List<AlignedSequences> list = AlignedSequences.toList(query.list(dc));
5845 26 Feb 20 nicklas 525         for (AlignedSequences aligned : list)
5845 26 Feb 20 nicklas 526         {
5845 26 Feb 20 nicklas 527           aligned.loadAnnotations(dc, manager, "ALIGNED_PAIRS", Annotationtype.ALIGNED_PAIRS, null);
5845 26 Feb 20 nicklas 528           aligned.loadAnnotations(dc, manager, "ALIGNED_BASES", Annotationtype.ALIGNED_BASES, null);
5845 26 Feb 20 nicklas 529           aligned.loadAnnotations(dc, manager, "READ_PAIRS_EXAMINED", Annotationtype.READ_PAIRS_EXAMINED, null);
5845 26 Feb 20 nicklas 530           aligned.loadAnnotations(dc, manager, "READ_PAIR_DUPLICATES", Annotationtype.READ_PAIR_DUPLICATES, null);
5845 26 Feb 20 nicklas 531           aligned.loadAnnotations(dc, manager, "FRACTION_DUPLICATION", Annotationtype.FRACTION_DUPLICATION, null);
5845 26 Feb 20 nicklas 532           aligned.loadAnnotations(dc, manager, "DataFilesFolder", Annotationtype.DATA_FILES_FOLDER, null);
5845 26 Feb 20 nicklas 533           
5845 26 Feb 20 nicklas 534           // Load job information
5845 26 Feb 20 nicklas 535           Job job = aligned.getItem().getJob();
5845 26 Feb 20 nicklas 536           JSONObject jsonJob = JsonUtil.getJobAsJSON(job);
5845 26 Feb 20 nicklas 537           aligned.setAnnotation("job", jsonJob);
5826 18 Feb 20 nicklas 538
5845 26 Feb 20 nicklas 539           boolean debug = Boolean.TRUE.equals(job.getParameterValue("debug"));
5845 26 Feb 20 nicklas 540           jsonJob.put("debug", debug);
5845 26 Feb 20 nicklas 541           
5845 26 Feb 20 nicklas 542           // Load library information
5845 26 Feb 20 nicklas 543           Library lib = aligned.getLibrary(dc);
5845 26 Feb 20 nicklas 544           Extract dna = (Extract)lib.findSingleParent(dc, Subtype.DNA_NORMALIZED_ALIQUOT);
5845 26 Feb 20 nicklas 545           lib.loadBioPlateLocation();
5845 26 Feb 20 nicklas 546           lib.setAnnotation("MIPSPanel", Annotationtype.MIPS_PANEL.getAnnotationValue(dc, dna));
5845 26 Feb 20 nicklas 547           aligned.setAnnotation("lib", lib.asJSONObject());
5845 26 Feb 20 nicklas 548           
5845 26 Feb 20 nicklas 549           // Load merged information
5845 26 Feb 20 nicklas 550           MergedSequences merged = aligned.getMergedSequences(dc);
5845 26 Feb 20 nicklas 551           merged.loadAnnotations(dc, manager, "PF_READS", Annotationtype.PF_READS, null);
5845 26 Feb 20 nicklas 552           aligned.setAnnotation("merged", merged.asJSONObject());
5845 26 Feb 20 nicklas 553           
5845 26 Feb 20 nicklas 554           jsonAligned.add(aligned.asJSONObject());
5845 26 Feb 20 nicklas 555         }
5845 26 Feb 20 nicklas 556         
5845 26 Feb 20 nicklas 557         json.put("alignedSequences", jsonAligned);
5845 26 Feb 20 nicklas 558         
5845 26 Feb 20 nicklas 559       }
5390 29 Apr 19 nicklas 560     }
5390 29 Apr 19 nicklas 561     catch (Throwable t)
5390 29 Apr 19 nicklas 562     {
5390 29 Apr 19 nicklas 563       t.printStackTrace();
5390 29 Apr 19 nicklas 564       json.clear();
5390 29 Apr 19 nicklas 565       json.put("status", "error");
5390 29 Apr 19 nicklas 566       json.put("message", t.getMessage());
5390 29 Apr 19 nicklas 567       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
5390 29 Apr 19 nicklas 568     }
5390 29 Apr 19 nicklas 569     finally
5390 29 Apr 19 nicklas 570     {
5390 29 Apr 19 nicklas 571       if (dc != null) dc.close();
5390 29 Apr 19 nicklas 572       json.writeJSONString(resp.getWriter());
5390 29 Apr 19 nicklas 573     }
5390 29 Apr 19 nicklas 574     
5390 29 Apr 19 nicklas 575   }
5390 29 Apr 19 nicklas 576
5390 29 Apr 19 nicklas 577   @Override
5390 29 Apr 19 nicklas 578   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
5390 29 Apr 19 nicklas 579     throws ServletException, IOException 
5390 29 Apr 19 nicklas 580   {
5390 29 Apr 19 nicklas 581     String cmd = req.getParameter("cmd");
5390 29 Apr 19 nicklas 582     JsonUtil.setJsonResponseHeaders(resp);
5390 29 Apr 19 nicklas 583     
5390 29 Apr 19 nicklas 584     JSONObject json = new JSONObject();
5390 29 Apr 19 nicklas 585     json.put("status", "ok");
5390 29 Apr 19 nicklas 586     
5390 29 Apr 19 nicklas 587     JSONArray jsonMessages = new JSONArray();
5390 29 Apr 19 nicklas 588   
5390 29 Apr 19 nicklas 589     final SessionControl sc = Reggie.getSessionControl(req);
5390 29 Apr 19 nicklas 590     DbControl dc = null;
5390 29 Apr 19 nicklas 591     try
5390 29 Apr 19 nicklas 592     {
5400 06 May 19 nicklas 593       if ("PreValidateImportFile".equals(cmd) || "ImportAliquotsFromFile".equals(cmd))
5397 03 May 19 nicklas 594       {
5397 03 May 19 nicklas 595         String path = Values.getStringOrNull(req.getParameter("path"));
5397 03 May 19 nicklas 596         String sheet = Values.getStringOrNull(req.getParameter("sheet"));
5397 03 May 19 nicklas 597         String prefix = Values.getStringOrNull(req.getParameter("prefix"));
5400 06 May 19 nicklas 598         boolean validateOnly = "PreValidateImportFile".equals(cmd);
5405 08 May 19 nicklas 599         
6337 16 Jun 21 nicklas 600         dc = sc.newDbControl(":MIPs - Import DNA aliquot plates");
5405 08 May 19 nicklas 601         if (!validateOnly)
5405 08 May 19 nicklas 602         {
5405 08 May 19 nicklas 603           ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.MIPS_PLATE_DESIGNER, ReggieRole.ADMINISTRATOR);
5405 08 May 19 nicklas 604         }
5405 08 May 19 nicklas 605         
5397 03 May 19 nicklas 606         File importFile = File.getByPath(dc, new Path(path, Path.Type.FILE), false);
5406 08 May 19 nicklas 607         sc.setUserClientSetting("reggie.mips.last-import-prefix", prefix);
5397 03 May 19 nicklas 608         
5397 03 May 19 nicklas 609         AliquotImporter importer = new AliquotImporter();
5397 03 May 19 nicklas 610         importer.setNamePrefix(prefix);
5397 03 May 19 nicklas 611         InputStream in = null;
5399 06 May 19 nicklas 612         boolean fileIsValidForImport = false;
5397 03 May 19 nicklas 613         List<String> sheetNames = null;
5403 08 May 19 nicklas 614         JSONObject jsonImportFile = new JSONObject();
5397 03 May 19 nicklas 615         try
5397 03 May 19 nicklas 616         {
5397 03 May 19 nicklas 617           String fileName = importFile.getName();
5420 13 May 19 nicklas 618           in = importFile.getDownloadStream(0);
5420 13 May 19 nicklas 619           fileIsValidForImport = importer.doImport(dc, in, sheet, validateOnly, fileName);
5420 13 May 19 nicklas 620           sheetNames = importer.getWorksheets();
5420 13 May 19 nicklas 621           if (sheetNames != null) fileName += "/" + importer.getParsedWorksheet();
5403 08 May 19 nicklas 622           jsonImportFile.put("filename", fileName);
5397 03 May 19 nicklas 623         }
5397 03 May 19 nicklas 624         finally
5397 03 May 19 nicklas 625         {
5397 03 May 19 nicklas 626           FileUtil.close(in);
5397 03 May 19 nicklas 627         }
5390 29 Apr 19 nicklas 628
5399 06 May 19 nicklas 629         jsonImportFile.put("valid", fileIsValidForImport);
5397 03 May 19 nicklas 630         jsonImportFile.put("warnings", importer.getWarningMessages().size());
5397 03 May 19 nicklas 631         jsonImportFile.put("errors", importer.getErrorMessages().size());
5403 08 May 19 nicklas 632         jsonImportFile.put("numAliquots", importer.getNumAliquots());
5397 03 May 19 nicklas 633         if (sheetNames != null) jsonImportFile.put("sheetNames", sheetNames);
5397 03 May 19 nicklas 634         json.put("importFile", jsonImportFile);
5399 06 May 19 nicklas 635
5399 06 May 19 nicklas 636         JSONArray jsonPlates = new JSONArray();
5399 06 May 19 nicklas 637         for (AliquotPlate ap : importer.getPlates())
5397 03 May 19 nicklas 638         {
5399 06 May 19 nicklas 639           jsonPlates.add(ap.toJSONObject());
5400 06 May 19 nicklas 640           if (!validateOnly && fileIsValidForImport)
5400 06 May 19 nicklas 641           {
5400 06 May 19 nicklas 642             jsonMessages.add("Created plate '" + ap.getName() + "' with " + ap.getNumAliquots() + " aliquots.");
5400 06 May 19 nicklas 643           }
5399 06 May 19 nicklas 644         }
5399 06 May 19 nicklas 645         jsonImportFile.put("plates", jsonPlates);
5403 08 May 19 nicklas 646         jsonImportFile.put("numPlates", jsonPlates.size());
5399 06 May 19 nicklas 647
5399 06 May 19 nicklas 648         if (fileIsValidForImport)
5399 06 May 19 nicklas 649         {
5397 03 May 19 nicklas 650           if (importer.hasWarning())
5397 03 May 19 nicklas 651           {
5397 03 May 19 nicklas 652             jsonMessages.addAll(importer.getWarningMessages());
5397 03 May 19 nicklas 653           }
5397 03 May 19 nicklas 654         }
5397 03 May 19 nicklas 655         else
5397 03 May 19 nicklas 656         {
5397 03 May 19 nicklas 657           jsonMessages.addAll(importer.getErrorMessages());
5397 03 May 19 nicklas 658           if (importer.hasWarning())
5397 03 May 19 nicklas 659           {
5397 03 May 19 nicklas 660             jsonMessages.addAll(importer.getWarningMessages());
5397 03 May 19 nicklas 661           }
5397 03 May 19 nicklas 662         }
5400 06 May 19 nicklas 663         if (!validateOnly && fileIsValidForImport)
5400 06 May 19 nicklas 664         {
5421 13 May 19 nicklas 665           ActivityDef.IMPORTED_MIPS_ALIQUOTS.merge(dc, importer.getNumImportedAliquots());
5400 06 May 19 nicklas 666           dc.commit();
5400 06 May 19 nicklas 667         }
5397 03 May 19 nicklas 668       }
5417 10 May 19 nicklas 669       else if ("CreateMIPsPlate".equals(cmd))
5417 10 May 19 nicklas 670       {
6337 16 Jun 21 nicklas 671         dc = sc.newDbControl(":Design MIPs plate");
5397 03 May 19 nicklas 672
5417 10 May 19 nicklas 673         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.MIPS_PLATE_DESIGNER, ReggieRole.ADMINISTRATOR);
5417 10 May 19 nicklas 674
5417 10 May 19 nicklas 675         JSONObject jsonReq = JsonUtil.parseRequest(req);
5417 10 May 19 nicklas 676         JSONObject jsonMipsPlate = (JSONObject)jsonReq.get("mipsPlate");
5417 10 May 19 nicklas 677         JSONArray jsonAliquotPlates = (JSONArray)jsonReq.get("aliquotPlates");
5417 10 May 19 nicklas 678         
5417 10 May 19 nicklas 679         String plateName = (String)jsonMipsPlate.get("name");
5417 10 May 19 nicklas 680         JSONArray jsonWells = (JSONArray)jsonMipsPlate.get("wells");
5417 10 May 19 nicklas 681         
5417 10 May 19 nicklas 682         BioplateType plateType = BioplateType.MIPS;
5417 10 May 19 nicklas 683         PlateGeometry geometry = plateType.getPlateGeometry(dc);
5417 10 May 19 nicklas 684         
5417 10 May 19 nicklas 685         BioPlate workPlate = BioPlate.getNew(dc, geometry, plateType.load(dc));
5417 10 May 19 nicklas 686         workPlate.setName(ReggieItem.ensureNonExistingItem(dc, Item.BIOPLATE, "MIPs plate", plateName));
5417 10 May 19 nicklas 687         workPlate.setDescription(Values.getStringOrNull((String)jsonMipsPlate.get("comments")));
5417 10 May 19 nicklas 688         dc.saveItem(workPlate);
5417 10 May 19 nicklas 689         
5417 10 May 19 nicklas 690         BioPlateEventType eventType = BioPlateEventType.getById(dc, BioPlateEventType.CREATE_BIOMATERIAL);
5417 10 May 19 nicklas 691         BioPlateEvent event = BioPlateEvent.getNew(dc, eventType);
5417 10 May 19 nicklas 692         event.setName("Create " + plateType.getName());
5417 10 May 19 nicklas 693         dc.saveItem(event);
5417 10 May 19 nicklas 694         BioPlateEventParticipant destPlate = event.addParticipant(workPlate, "destination", 1);
5417 10 May 19 nicklas 695         dc.saveItem(destPlate);
5417 10 May 19 nicklas 696         for (int plateNo = 0; plateNo < jsonAliquotPlates.size(); plateNo++)
5417 10 May 19 nicklas 697         {
5417 10 May 19 nicklas 698           JSONObject jsonAliquotPlate = (JSONObject)jsonAliquotPlates.get(plateNo);
5417 10 May 19 nicklas 699           Number aliquotPlateId = (Number)jsonAliquotPlate.get("id");
5417 10 May 19 nicklas 700           Number numAliquots = (Number)jsonAliquotPlate.get("numAliquots");
5417 10 May 19 nicklas 701           Number numPlacedAliquots = (Number)jsonAliquotPlate.get("numPlacedAliquots");
5417 10 May 19 nicklas 702           
5417 10 May 19 nicklas 703           BioPlate aliquotPlate = BioPlate.getById(dc, aliquotPlateId.intValue());
5417 10 May 19 nicklas 704           if (EqualsHelper.equals(numAliquots, numPlacedAliquots))
5417 10 May 19 nicklas 705           {
5417 10 May 19 nicklas 706             jsonMessages.add("Used all " + numPlacedAliquots + " aliquots from " + aliquotPlate.getName());
5417 10 May 19 nicklas 707             Annotationtype.PLATE_PROCESSING.setAnnotationValue(dc, aliquotPlate, "Disable");
5417 10 May 19 nicklas 708           }
5417 10 May 19 nicklas 709           else
5417 10 May 19 nicklas 710           {
5417 10 May 19 nicklas 711             jsonMessages.add("Used " + numPlacedAliquots + " of " + numAliquots + " aliquots from " + aliquotPlate.getName());
5457 29 May 19 nicklas 712             Annotationtype.PLATE_PROCESSING.setAnnotationValue(dc, aliquotPlate, "DesignMIPs");
5417 10 May 19 nicklas 713           }
5417 10 May 19 nicklas 714           BioPlateEventParticipant srcPlate = event.addParticipant(aliquotPlate, "source", plateNo+1);
5417 10 May 19 nicklas 715           dc.saveItem(srcPlate);
5417 10 May 19 nicklas 716         }
5417 10 May 19 nicklas 717         
5417 10 May 19 nicklas 718         // Library
5417 10 May 19 nicklas 719         Subtype childType = plateType.getSubtype();
5417 10 May 19 nicklas 720         ItemSubtype extractType = childType.load(dc);
5417 10 May 19 nicklas 721         
5417 10 May 19 nicklas 722         for (int i = 0; i < jsonWells.size(); i++)
5417 10 May 19 nicklas 723         {
5417 10 May 19 nicklas 724           JSONObject jsonWell = (JSONObject)jsonWells.get(i);
5418 10 May 19 nicklas 725           JSONObject jsonAliquot = (JSONObject)jsonWell.get("aliquot");
5417 10 May 19 nicklas 726           
5417 10 May 19 nicklas 727           Number row = (Number)jsonWell.get("row");
5417 10 May 19 nicklas 728           Number col = (Number)jsonWell.get("column");
5417 10 May 19 nicklas 729           
5418 10 May 19 nicklas 730           Number dnaId = (Number)jsonAliquot.get("id");
5417 10 May 19 nicklas 731           Dna dna = Dna.getById(dc, dnaId.intValue());
5417 10 May 19 nicklas 732           Extract d = dna.getItem();
5417 10 May 19 nicklas 733           
5418 10 May 19 nicklas 734           // Create "Library" item
5417 10 May 19 nicklas 735           Extract childExtract = Extract.getNew(dc, destPlate);
5418 10 May 19 nicklas 736           String childName = dna.getNextLibName(dc, childType);
5418 10 May 19 nicklas 737           childExtract.setName(childName);
5417 10 May 19 nicklas 738           childExtract.setItemSubtype(extractType);
5436 17 May 19 nicklas 739           Pipeline.MIPS.setAnnotation(dc, childExtract);
5417 10 May 19 nicklas 740
5418 10 May 19 nicklas 741           // Place on MIPs plate
5418 10 May 19 nicklas 742           BioWell well = workPlate.getBioWell(row.intValue(), col.intValue());
5418 10 May 19 nicklas 743           childExtract.setBioWell(well);
5418 10 May 19 nicklas 744           
5448 27 May 19 nicklas 745           // Link library with parent and if it has a quantity, assume that all is consumed
5418 10 May 19 nicklas 746           Float remainingQuantityBefore = d.getRemainingQuantity();
5417 10 May 19 nicklas 747           BioMaterialEventSource evtSrc = childExtract.getCreationEvent().setSource(d);
5791 16 Dec 19 nicklas 748           DoNotUse.copyDoNotUseAnnotations(dc, d, childExtract, false);
5448 27 May 19 nicklas 749           if (remainingQuantityBefore != null) 
5417 10 May 19 nicklas 750           {
5448 27 May 19 nicklas 751             evtSrc.setUsedQuantity(remainingQuantityBefore.floatValue());
5417 10 May 19 nicklas 752           }
5417 10 May 19 nicklas 753           dc.saveItem(childExtract);
5417 10 May 19 nicklas 754         }
5417 10 May 19 nicklas 755         
5417 10 May 19 nicklas 756         jsonMessages.add("Created " + workPlate.getName() + " with " + jsonWells.size() + " child items");
5421 13 May 19 nicklas 757         ActivityDef.DESIGNED_MIPSPLATE.create(dc, workPlate.getName(), jsonWells.size());
5417 10 May 19 nicklas 758         
5417 10 May 19 nicklas 759         dc.commit();
5417 10 May 19 nicklas 760       }
5425 14 May 19 nicklas 761       else if ("RegisterLibraryPrep".equals(cmd))
5425 14 May 19 nicklas 762       {
6337 16 Jun 21 nicklas 763         dc = sc.newDbControl(":MIPs - Library registration");
5417 10 May 19 nicklas 764
5425 14 May 19 nicklas 765         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.MIPS_LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
5425 14 May 19 nicklas 766
5425 14 May 19 nicklas 767         JSONObject jsonReq = JsonUtil.parseRequest(req);
5425 14 May 19 nicklas 768         Number bioPlateId = (Number)jsonReq.get("bioplate");
5425 14 May 19 nicklas 769         Number libProtocolId = (Number)jsonReq.get("libProtocol");
5429 16 May 19 nicklas 770         Number barcodeTemplateId = (Number)jsonReq.get("barcodeTemplate");
5425 14 May 19 nicklas 771         boolean failed = Boolean.TRUE.equals(jsonReq.get("failed"));
5425 14 May 19 nicklas 772         
5429 16 May 19 nicklas 773         BioPlate barcodePlate = BioPlate.getById(dc, barcodeTemplateId.intValue());
5425 14 May 19 nicklas 774         BioPlate libPlate = BioPlate.getById(dc, bioPlateId.intValue());
5425 14 May 19 nicklas 775         ReactionPlate reactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.MIPS);
5425 14 May 19 nicklas 776
5425 14 May 19 nicklas 777         // Set date, protocol and comments for the event
5425 14 May 19 nicklas 778         Date libDate = new Date();
5425 14 May 19 nicklas 779         Protocol libProtocol = libProtocolId == null ? null : Protocol.getById(dc, libProtocolId.intValue());
5425 14 May 19 nicklas 780         libPlate.setDescription((String)jsonReq.get("libComments"));
5425 14 May 19 nicklas 781
5425 14 May 19 nicklas 782         // Find the creation event for the plate
5425 14 May 19 nicklas 783         List<BioPlateEvent> events = reactionPlate.findEvents(dc, BioPlateEventType.CREATE_BIOMATERIAL, "destination");
5425 14 May 19 nicklas 784         if (events.size() > 1)
5425 14 May 19 nicklas 785         {
5425 14 May 19 nicklas 786           throw new InvalidDataException("Found > 1 creation event for plate: " + libPlate.getName());
5425 14 May 19 nicklas 787         }
5425 14 May 19 nicklas 788         // ...it may not exist if a plate has been created with a batch import...
5425 14 May 19 nicklas 789         BioPlateEvent plateCreationEvent = events.size() == 0 ? null : events.get(0);
5425 14 May 19 nicklas 790         if (plateCreationEvent != null)
5425 14 May 19 nicklas 791         {
5425 14 May 19 nicklas 792           // This automatically propagated to all Library items on the plate
5425 14 May 19 nicklas 793           plateCreationEvent.setEventDate(libDate);
5425 14 May 19 nicklas 794           plateCreationEvent.setProtocol(libProtocol);
5425 14 May 19 nicklas 795         }
5425 14 May 19 nicklas 796         libPlate.setEventDate(libDate);
5425 14 May 19 nicklas 797         
5425 14 May 19 nicklas 798         // Dates and operators
5450 27 May 19 nicklas 799         Annotationtype.HYBRIDIZATION_DATE.setAnnotationValue(dc, libPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("hybridizationDate")));
5450 27 May 19 nicklas 800         Annotationtype.HYBRIDIZATION_OPERATOR.setAnnotationValue(dc, libPlate, Values.getStringOrNull((String)jsonReq.get("hybridizationOperator")));
5450 27 May 19 nicklas 801         Annotationtype.PCR_DATE.setAnnotationValue(dc, libPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("pcrDate")));
5450 27 May 19 nicklas 802         Annotationtype.PCR_OPERATOR.setAnnotationValue(dc, libPlate, Values.getStringOrNull((String)jsonReq.get("pcrOperator")));
5425 14 May 19 nicklas 803         Annotationtype.CLEANUP_DATE.setAnnotationValue(dc, libPlate, Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("cleanupDate")));
5425 14 May 19 nicklas 804         Annotationtype.CLEANUP_OPERATOR.setAnnotationValue(dc, libPlate, Values.getStringOrNull((String)jsonReq.get("cleanupOperator")));
5425 14 May 19 nicklas 805         
5429 16 May 19 nicklas 806         // Link the barcode template to the MIPs plate using any-to-any
5429 16 May 19 nicklas 807         AnyToAny templateLink = AnyToAny.getNewOrExisting(dc, libPlate, "BarcodeTemplate", barcodePlate, false);
5429 16 May 19 nicklas 808         if (!templateLink.isInDatabase()) dc.saveItem(templateLink);
5429 16 May 19 nicklas 809         
5425 14 May 19 nicklas 810         // In case we need to flag parent DNA items
5425 14 May 19 nicklas 811         ItemList flaggedList = BiomaterialList.FLAGGED_DNA.load(dc);
5425 14 May 19 nicklas 812         int numFlagged = 0;
5425 14 May 19 nicklas 813         
5425 14 May 19 nicklas 814         // Check all library items
5425 14 May 19 nicklas 815         int numExtracts = 0;
5429 16 May 19 nicklas 816         int numMissingBarcodes = 0;
5429 16 May 19 nicklas 817         ItemQuery<BioWell> wellQuery = libPlate.getBioWells();
5430 16 May 19 nicklas 818         wellQuery.order(Orders.asc(Hql.property("column")));
5429 16 May 19 nicklas 819         wellQuery.order(Orders.asc(Hql.property("row")));
5429 16 May 19 nicklas 820         for (BioWell well : wellQuery.list(dc))
5425 14 May 19 nicklas 821         {
5425 14 May 19 nicklas 822           if (!well.isEmpty())
5425 14 May 19 nicklas 823           {
5425 14 May 19 nicklas 824             numExtracts++;
5425 14 May 19 nicklas 825             Extract lib = (Extract)well.getBioMaterial();
5429 16 May 19 nicklas 826             // Copy barcode from the template
5429 16 May 19 nicklas 827             Extract proxy = (Extract)barcodePlate.getBioWell(well.getPlateCoordinate()).getBioMaterial();
5429 16 May 19 nicklas 828             Tag barcode = proxy == null ? null : proxy.getTag();
5429 16 May 19 nicklas 829             if (barcode == null)
5429 16 May 19 nicklas 830             {
5429 16 May 19 nicklas 831               jsonMessages.add("[Error]No barcode defined on " + barcodePlate.getName()+" " + well.getCoordinate());
5429 16 May 19 nicklas 832               numMissingBarcodes++;
5429 16 May 19 nicklas 833             }
5429 16 May 19 nicklas 834             lib.setTag(barcode);
5425 14 May 19 nicklas 835             Library l = Library.get(lib);
5425 14 May 19 nicklas 836             if (plateCreationEvent == null)
5425 14 May 19 nicklas 837             {
5425 14 May 19 nicklas 838               // If no plate event existed, we must set protocol and date on all Library items
5425 14 May 19 nicklas 839               BioMaterialEvent evt = lib.getCreationEvent();
5425 14 May 19 nicklas 840               evt.setEventDate(libDate);
5425 14 May 19 nicklas 841               evt.setProtocol(libProtocol);
5425 14 May 19 nicklas 842             }
5425 14 May 19 nicklas 843             
5553 12 Aug 19 nicklas 844             if (failed && !Reggie.isExternalItem(lib.getName()))
5425 14 May 19 nicklas 845             {
5425 14 May 19 nicklas 846               Dna d = l.getDna(dc, false);
5425 14 May 19 nicklas 847               if (d != null)
5425 14 May 19 nicklas 848               {
5425 14 May 19 nicklas 849                 Extract dna = d.getExtract();
5425 14 May 19 nicklas 850                 Annotationtype.FLAG.setAnnotationValue(dc, dna, Rna.FLAG_LIB_PLATE_FAILED);
5425 14 May 19 nicklas 851                 flaggedList.add(dna);
5425 14 May 19 nicklas 852                 numFlagged++;
5425 14 May 19 nicklas 853               }
5425 14 May 19 nicklas 854             }
5425 14 May 19 nicklas 855           }
5425 14 May 19 nicklas 856         }
5425 14 May 19 nicklas 857           
5425 14 May 19 nicklas 858         if (numFlagged > 0)
5425 14 May 19 nicklas 859         {
5425 14 May 19 nicklas 860           jsonMessages.add(numFlagged + " DNA items flagged for re-processing");
5425 14 May 19 nicklas 861         }
5425 14 May 19 nicklas 862
5425 14 May 19 nicklas 863         // Register the plate as completely processed
5429 16 May 19 nicklas 864         if (numMissingBarcodes == 0)
5425 14 May 19 nicklas 865         {
5429 16 May 19 nicklas 866           if (failed)
5429 16 May 19 nicklas 867           {
5429 16 May 19 nicklas 868             libPlate.setDestroyed(true);
5429 16 May 19 nicklas 869             Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, libPlate, ReactionPlate.PROCESS_FAILED);
5429 16 May 19 nicklas 870             jsonMessages.add("Plate '" + libPlate.getName() + "' registered with status: " + (ReactionPlate.PROCESS_FAILED));
5429 16 May 19 nicklas 871           }
5429 16 May 19 nicklas 872           else
5429 16 May 19 nicklas 873           {
5429 16 May 19 nicklas 874             libPlate.setDestroyed(false);
5429 16 May 19 nicklas 875             Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, libPlate, ReactionPlate.PROCESS_SUCCESSFUL);
5429 16 May 19 nicklas 876             jsonMessages.add("Plate '" + libPlate.getName() + "' registered with status: " + (ReactionPlate.PROCESS_SUCCESSFUL));
5429 16 May 19 nicklas 877             ActivityDef.COMPLETED_LIBPLATE.create(dc, libPlate.getName(), numExtracts);
5429 16 May 19 nicklas 878           }
5429 16 May 19 nicklas 879           dc.commit();
5425 14 May 19 nicklas 880         }
5425 14 May 19 nicklas 881       }
5427 14 May 19 nicklas 882       else if ("RegisterPooledLibraries".equals(cmd))
5427 14 May 19 nicklas 883       {
6337 16 Jun 21 nicklas 884         dc = sc.newDbControl(":MIPs - Register pooled libraries");
5427 14 May 19 nicklas 885
5427 14 May 19 nicklas 886         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.MIPS_LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
5427 14 May 19 nicklas 887
5427 14 May 19 nicklas 888         // Create PooledLibrary items from a Library bioplate
5427 14 May 19 nicklas 889         JSONObject jsonReq = JsonUtil.parseRequest(req);
5427 14 May 19 nicklas 890         JSONArray jsonPlates = (JSONArray)jsonReq.get("plates");
5427 14 May 19 nicklas 891         
5427 14 May 19 nicklas 892         String poolName = (String)jsonReq.get("poolName");
5427 14 May 19 nicklas 893         Number poolProtocolId = (Number)jsonReq.get("poolProtocol");
5427 14 May 19 nicklas 894         Date poolDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("poolDate"));
5427 14 May 19 nicklas 895         String operator = Values.getStringOrNull((String)jsonReq.get("poolOperator"));
5427 14 May 19 nicklas 896         
5427 14 May 19 nicklas 897         Protocol poolProtocol = poolProtocolId == null ? null : Protocol.getById(dc, poolProtocolId.intValue());
5427 14 May 19 nicklas 898
5427 14 May 19 nicklas 899         ItemSubtype pooledLibraryType = Subtype.POOLED_LIBRARY.load(dc);
5427 14 May 19 nicklas 900         int numLibs = 0;
5427 14 May 19 nicklas 901         Extract pool = Extract.getNew(dc);
5427 14 May 19 nicklas 902         pool.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.POOLED_LIBRARY, poolName));
5427 14 May 19 nicklas 903         pool.setItemSubtype(pooledLibraryType);
5427 14 May 19 nicklas 904         dc.saveItem(pool);
5434 17 May 19 nicklas 905         Pipeline.MIPS.setAnnotation(dc, pool);
5427 14 May 19 nicklas 906         Annotationtype.POOL_OPERATOR.setAnnotationValue(dc, pool, operator);
5427 14 May 19 nicklas 907         Annotationtype.POOL_DATE.setAnnotationValue(dc, pool, poolDate);
5427 14 May 19 nicklas 908         BioMaterialEvent poolEvent = pool.getCreationEvent();
5434 17 May 19 nicklas 909         poolEvent.setEventDate(poolDate);
5430 16 May 19 nicklas 910         Map<Tag, BioWell> usedBarcodes = new HashMap<>();
5430 16 May 19 nicklas 911         boolean barcodeErrors = false;
5427 14 May 19 nicklas 912         for (int plateNo = 0; plateNo < jsonPlates.size(); plateNo++)
5427 14 May 19 nicklas 913         {
5427 14 May 19 nicklas 914           Number plateId = (Number)jsonPlates.get(plateNo);
5427 14 May 19 nicklas 915           BioPlate mipsPlate = BioPlate.getById(dc, plateId.intValue());
5427 14 May 19 nicklas 916           Annotationtype.POOL_OPERATOR.setAnnotationValue(dc, mipsPlate, operator);
5427 14 May 19 nicklas 917           Annotationtype.POOL_DATE.setAnnotationValue(dc, mipsPlate, poolDate);
5473 05 Jun 19 nicklas 918           
5473 05 Jun 19 nicklas 919           // Link pool with library plates
5473 05 Jun 19 nicklas 920           String linkSuffix = plateNo == 0 ? "" : "."+(plateNo+1);
5473 05 Jun 19 nicklas 921           AnyToAny libPlateLink = AnyToAny.getNew(dc, pool, mipsPlate, "LibPlate"+linkSuffix, true);
5473 05 Jun 19 nicklas 922           dc.saveItemIf(pool, libPlateLink, false);
5473 05 Jun 19 nicklas 923           
5473 05 Jun 19 nicklas 924           // Validate that all libs have a unique barcode
5430 16 May 19 nicklas 925           ItemQuery<BioWell> wellQuery = mipsPlate.getBioWells();
5430 16 May 19 nicklas 926           wellQuery.order(Orders.asc(Hql.property("column")));
5430 16 May 19 nicklas 927           wellQuery.order(Orders.asc(Hql.property("row")));
5430 16 May 19 nicklas 928           for (BioWell well : wellQuery.list(dc))
5427 14 May 19 nicklas 929           {
5427 14 May 19 nicklas 930             if (!well.isEmpty())
5427 14 May 19 nicklas 931             {
5427 14 May 19 nicklas 932               numLibs++;
5427 14 May 19 nicklas 933               Extract lib = (Extract)well.getBioMaterial();
5427 14 May 19 nicklas 934               poolEvent.addSource(lib);
5430 16 May 19 nicklas 935               Tag barcode = lib.getTag();
5430 16 May 19 nicklas 936               if (barcode == null)
5430 16 May 19 nicklas 937               {
5430 16 May 19 nicklas 938                 barcodeErrors = true;
5430 16 May 19 nicklas 939                 jsonMessages.add("[Error]No barcode on library '" + lib.getName() + "' on " + mipsPlate.getName() + " " + well.getCoordinate());
5430 16 May 19 nicklas 940               }
5452 28 May 19 nicklas 941               else
5430 16 May 19 nicklas 942               {
5452 28 May 19 nicklas 943                 BioWell duplicate = usedBarcodes.put(barcode, well);
5452 28 May 19 nicklas 944                 if (duplicate != null)
5452 28 May 19 nicklas 945                 {
5452 28 May 19 nicklas 946                   barcodeErrors = true;
5452 28 May 19 nicklas 947                   jsonMessages.add("[Error]Duplicate barcode on library '" + lib.getName() + "' on " + mipsPlate.getName() + " " + well.getCoordinate()+": " + 
5452 28 May 19 nicklas 948                       barcode.getName() + " (also used on " + duplicate.getPlate().getName() + " " + duplicate.getCoordinate() + ")");
5452 28 May 19 nicklas 949                 }
5430 16 May 19 nicklas 950               }
5427 14 May 19 nicklas 951             }
5427 14 May 19 nicklas 952           }
5427 14 May 19 nicklas 953         }
5427 14 May 19 nicklas 954         
5430 16 May 19 nicklas 955         if (!barcodeErrors)
5430 16 May 19 nicklas 956         {
5430 16 May 19 nicklas 957           jsonMessages.add("Created '" + pool.getName() + "' from " + numLibs + " libraries");
5434 17 May 19 nicklas 958           ActivityDef.POOLED_LIBRARIES.create(dc, "MIPs " + pool.getName(), numLibs);
5430 16 May 19 nicklas 959           dc.commit();
5430 16 May 19 nicklas 960         }
5427 14 May 19 nicklas 961       }
5439 20 May 19 nicklas 962       else if ("CreateFlowCells".equals(cmd))
5439 20 May 19 nicklas 963       {
6337 16 Jun 21 nicklas 964         dc = sc.newDbControl(":MIPs - Create flow cells");
5439 20 May 19 nicklas 965
5439 20 May 19 nicklas 966         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.MIPS_LIBRARY_PREP, ReggieRole.ADMINISTRATOR);
5439 20 May 19 nicklas 967
5439 20 May 19 nicklas 968         // Create FlowCells and add pools to them
5439 20 May 19 nicklas 969         JSONObject jsonReq = JsonUtil.parseRequest(req);
5439 20 May 19 nicklas 970         JSONArray jsonFlowCells = (JSONArray)jsonReq.get("flowCells");
5439 20 May 19 nicklas 971         
5439 20 May 19 nicklas 972         Number read1 = (Number)jsonReq.get("read1");
5453 28 May 19 nicklas 973         Number indexRead1 = (Number)jsonReq.get("indexRead1");
5453 28 May 19 nicklas 974         Number indexRead2 = (Number)jsonReq.get("indexRead2");
5439 20 May 19 nicklas 975         Number read2 = (Number)jsonReq.get("read2");
5453 28 May 19 nicklas 976         // Sequence string is concatenated: read1-index1-index2-read2
5453 28 May 19 nicklas 977         String sequencingCycles = read1 + "-" + indexRead1 + "-" + indexRead2 + "-" + read2;
5439 20 May 19 nicklas 978         
5439 20 May 19 nicklas 979         String fcType = Values.getStringOrNull((String)jsonReq.get("flowCellType"));
5439 20 May 19 nicklas 980         
5439 20 May 19 nicklas 981         ItemSubtype flowCellType = Subtype.FLOW_CELL.load(dc);
5439 20 May 19 nicklas 982         ItemSubtype poolAliquotType = Subtype.POOLED_LIBRARY_ALIQUOT.load(dc);
5439 20 May 19 nicklas 983         
5439 20 May 19 nicklas 984         for (int fcNo = 0; fcNo < jsonFlowCells.size(); fcNo++)
5439 20 May 19 nicklas 985         {
5439 20 May 19 nicklas 986           JSONObject jsonFlowCell = (JSONObject)jsonFlowCells.get(fcNo);
5439 20 May 19 nicklas 987           
5439 20 May 19 nicklas 988           JSONArray jsonLanes = (JSONArray)jsonFlowCell.get("lanes");
5439 20 May 19 nicklas 989           PhysicalBioAssay flowCell = PhysicalBioAssay.getNew(dc);
5439 20 May 19 nicklas 990           flowCell.setItemSubtype(flowCellType);
5439 20 May 19 nicklas 991           Pipeline.MIPS.setAnnotation(dc, flowCell);
5439 20 May 19 nicklas 992           flowCell.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.FLOW_CELL, (String)jsonFlowCell.get("name")));
5439 20 May 19 nicklas 993           flowCell.setDescription((String)jsonFlowCell.get("comment"));
5439 20 May 19 nicklas 994           flowCell.setSize(jsonLanes.size());
5439 20 May 19 nicklas 995           dc.saveItem(flowCell);
5439 20 May 19 nicklas 996           Annotationtype.SEQUENCING_CYCLES.setAnnotationValue(dc, flowCell, sequencingCycles);
5439 20 May 19 nicklas 997           if (fcType != null)
5439 20 May 19 nicklas 998           {
5439 20 May 19 nicklas 999             Annotationtype.FLOWCELL_TYPE.setAnnotationValue(dc, flowCell, fcType);
5439 20 May 19 nicklas 1000           }
5439 20 May 19 nicklas 1001           
5439 20 May 19 nicklas 1002           Map<Number, PooledLibrary> poolCache = new HashMap<Number, PooledLibrary>();
5439 20 May 19 nicklas 1003           BioMaterialEvent clusterEvent = flowCell.getCreationEvent();
5439 20 May 19 nicklas 1004           int emptyLanes = 0;
5439 20 May 19 nicklas 1005           for (int laneNo = 0; laneNo < jsonLanes.size(); laneNo++)
5439 20 May 19 nicklas 1006           {
5439 20 May 19 nicklas 1007             JSONObject jsonLane = (JSONObject)jsonLanes.get(laneNo);
5439 20 May 19 nicklas 1008             if (jsonLane == null) 
5439 20 May 19 nicklas 1009             {
5439 20 May 19 nicklas 1010               emptyLanes++;
5439 20 May 19 nicklas 1011               continue;
5439 20 May 19 nicklas 1012             }
5439 20 May 19 nicklas 1013             
5439 20 May 19 nicklas 1014             JSONObject jsonPool = (JSONObject)jsonLane.get("pool");
5439 20 May 19 nicklas 1015             // JSONArray jsonLane may have JSONObject elements with a "defaultPool" key, but no "pool" key
5439 20 May 19 nicklas 1016             if (jsonPool == null) 
5439 20 May 19 nicklas 1017             {
5439 20 May 19 nicklas 1018               emptyLanes++;
5439 20 May 19 nicklas 1019               continue;
5439 20 May 19 nicklas 1020             }
5439 20 May 19 nicklas 1021             Number poolId = (Number)jsonPool.get("id");
5439 20 May 19 nicklas 1022             PooledLibrary pool = poolCache.get(poolId);
5439 20 May 19 nicklas 1023             if (pool == null)
5439 20 May 19 nicklas 1024             {
5439 20 May 19 nicklas 1025               pool = PooledLibrary.getById(dc, poolId.intValue());
5439 20 May 19 nicklas 1026               poolCache.put(poolId, pool);
5439 20 May 19 nicklas 1027             }
5439 20 May 19 nicklas 1028             
5439 20 May 19 nicklas 1029             // Create 'in-between' aliquot to make it possible to use 
5439 20 May 19 nicklas 1030             // the same pool on multiple lanes
5439 20 May 19 nicklas 1031             Extract poolA = Extract.getNew(dc);
5439 20 May 19 nicklas 1032             String aliquotName = pool.getNextAliquotName(dc);
5439 20 May 19 nicklas 1033             poolA.setName(aliquotName);
5439 20 May 19 nicklas 1034             poolA.setItemSubtype(poolAliquotType);
5439 20 May 19 nicklas 1035             Pipeline.MIPS.setAnnotation(dc, poolA);
5439 20 May 19 nicklas 1036             BioMaterialEventSource srcA = poolA.getCreationEvent().setSource(pool.getItem());
5439 20 May 19 nicklas 1037
5439 20 May 19 nicklas 1038             dc.saveItem(poolA);
5439 20 May 19 nicklas 1039             
5439 20 May 19 nicklas 1040             // Add the aliquot to the flow cell on the correct lane
5439 20 May 19 nicklas 1041             BioMaterialEventSource src = clusterEvent.addSource(poolA);
5439 20 May 19 nicklas 1042             src.setPosition(laneNo+1);
5439 20 May 19 nicklas 1043           }
5439 20 May 19 nicklas 1044           int numPools = poolCache.size();
5439 20 May 19 nicklas 1045           String nPools = numPools == 1 ? "1 pool" : numPools + " pools";
5439 20 May 19 nicklas 1046           String msg = "Flow cell '" + flowCell.getName() + "' registered with " + nPools + 
5439 20 May 19 nicklas 1047               " on "+flowCell.getSize() + " lanes";
5439 20 May 19 nicklas 1048           if (emptyLanes > 0) msg += "; " + emptyLanes + " lanes are empty";
5439 20 May 19 nicklas 1049           jsonMessages.add(msg);
5439 20 May 19 nicklas 1050         }
5439 20 May 19 nicklas 1051         
5439 20 May 19 nicklas 1052         dc.commit();
5439 20 May 19 nicklas 1053       }
5826 18 Feb 20 nicklas 1054       else if ("StartMipsAlignment".equals(cmd))
5826 18 Feb 20 nicklas 1055       {
6337 16 Jun 21 nicklas 1056         dc = sc.newDbControl(":MIPs - Start alignment");
5826 18 Feb 20 nicklas 1057
5826 18 Feb 20 nicklas 1058         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.MIPS_SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
5826 18 Feb 20 nicklas 1059
5826 18 Feb 20 nicklas 1060         JSONObject jsonReq = JsonUtil.parseRequest(req);
5826 18 Feb 20 nicklas 1061         JSONArray jsonMerged = (JSONArray)jsonReq.get("mergedSequences");
5826 18 Feb 20 nicklas 1062         
5826 18 Feb 20 nicklas 1063         Number alignSoftwareId = (Number)jsonReq.get("alignSoftware");
5826 18 Feb 20 nicklas 1064         Number alignProtocolId = (Number)jsonReq.get("alignProtocol");
5826 18 Feb 20 nicklas 1065         
5826 18 Feb 20 nicklas 1066         String clusterId = (String)jsonReq.get("cluster");
5826 18 Feb 20 nicklas 1067         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
5826 18 Feb 20 nicklas 1068         boolean autoConfirm = Boolean.TRUE.equals(jsonReq.get("autoConfirm"));
5826 18 Feb 20 nicklas 1069         Number priority = (Number)jsonReq.get("priority");
7372 06 Oct 23 nicklas 1070         String submitOptions = Values.getStringOrNull((String)jsonReq.get("submitOptionsOverride"));
5826 18 Feb 20 nicklas 1071
5826 18 Feb 20 nicklas 1072         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
5826 18 Feb 20 nicklas 1073         if (cluster == null)
5826 18 Feb 20 nicklas 1074         {
5826 18 Feb 20 nicklas 1075           throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
5826 18 Feb 20 nicklas 1076         }
5826 18 Feb 20 nicklas 1077         
5826 18 Feb 20 nicklas 1078         // Load common items
5826 18 Feb 20 nicklas 1079         Software alignSoftware = alignSoftwareId != null ? Software.getById(dc, alignSoftwareId.intValue()) : null;
5826 18 Feb 20 nicklas 1080         Protocol alignProtocol = alignProtocolId != null ? Protocol.getById(dc, alignProtocolId.intValue()) : null;
5826 18 Feb 20 nicklas 1081
5826 18 Feb 20 nicklas 1082         // Align job creator implementation
5826 18 Feb 20 nicklas 1083         MipsAlignJobCreator jobCreator = new MipsAlignJobCreator();
5826 18 Feb 20 nicklas 1084         jobCreator.setAutoConfirm(autoConfirm);
5826 18 Feb 20 nicklas 1085         jobCreator.setDebug(debug);
5826 18 Feb 20 nicklas 1086         jobCreator.setPriority(priority == null ? null : priority.intValue());
5826 18 Feb 20 nicklas 1087         jobCreator.setAlignProtocol(alignProtocol);
5826 18 Feb 20 nicklas 1088         jobCreator.setAlignSoftware(alignSoftware);
7372 06 Oct 23 nicklas 1089         jobCreator.setSubmitOptionsOverride(submitOptions);
5826 18 Feb 20 nicklas 1090
5826 18 Feb 20 nicklas 1091         List<MergedSequences> mergedSequences = new ArrayList<MergedSequences>();
5826 18 Feb 20 nicklas 1092         for (int mgNo = 0; mgNo < jsonMerged.size(); mgNo++)
5826 18 Feb 20 nicklas 1093         {
5826 18 Feb 20 nicklas 1094           JSONObject jsonMg = (JSONObject)jsonMerged.get(mgNo);
5826 18 Feb 20 nicklas 1095           Number mergedId = (Number)jsonMg.get("id");
5826 18 Feb 20 nicklas 1096         
5826 18 Feb 20 nicklas 1097           MergedSequences ms = MergedSequences.getById(dc, mergedId.intValue());
5826 18 Feb 20 nicklas 1098           mergedSequences.add(ms);
5826 18 Feb 20 nicklas 1099
5826 18 Feb 20 nicklas 1100           // Reset AUTO_PROCESSING annotation
5826 18 Feb 20 nicklas 1101           DerivedBioAssay merged = ms.getDerivedBioAssay();
5826 18 Feb 20 nicklas 1102           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, merged, null);
5826 18 Feb 20 nicklas 1103         }
5826 18 Feb 20 nicklas 1104         
5826 18 Feb 20 nicklas 1105         List<JobDefinition> jobDefs = jobCreator.createMipsAlignJobs(dc, cluster, mergedSequences);
5826 18 Feb 20 nicklas 1106         List<Job> jobs = ScriptUtil.submitJobs(dc, cluster, jobDefs);
5826 18 Feb 20 nicklas 1107         for (Job job : jobs)
5826 18 Feb 20 nicklas 1108         {
5826 18 Feb 20 nicklas 1109           if (job.getStatus() == Job.Status.ERROR)
5826 18 Feb 20 nicklas 1110           {
5826 18 Feb 20 nicklas 1111             jsonMessages.add("[Error]Job submission for '" + job.getName() + "' failed: " + job.getStatusMessage());
5826 18 Feb 20 nicklas 1112           }
5826 18 Feb 20 nicklas 1113           else
5826 18 Feb 20 nicklas 1114           {
5826 18 Feb 20 nicklas 1115             jsonMessages.add("Submitted MIPs alignment job to " + cluster.getConnectionInfo().getName() + " with id " + job.getExternalId());
5826 18 Feb 20 nicklas 1116           }
5826 18 Feb 20 nicklas 1117         }
5826 18 Feb 20 nicklas 1118         dc.commit();
5826 18 Feb 20 nicklas 1119       }
5845 26 Feb 20 nicklas 1120       else if ("RegisterAlignment".equals(cmd))
5845 26 Feb 20 nicklas 1121       {
6337 16 Jun 21 nicklas 1122         dc = sc.newDbControl(":MIPs - Confirm alignment");
5845 26 Feb 20 nicklas 1123
5845 26 Feb 20 nicklas 1124         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.MIPS_SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
5845 26 Feb 20 nicklas 1125
5845 26 Feb 20 nicklas 1126         JSONObject jsonReq = JsonUtil.parseRequest(req);
5845 26 Feb 20 nicklas 1127         JSONArray jsonAlignedSequences = (JSONArray)jsonReq.get("alignedSequences");
5845 26 Feb 20 nicklas 1128
5845 26 Feb 20 nicklas 1129         int numConfirmed = 0;
5845 26 Feb 20 nicklas 1130         int numUnconfirmed = 0;
5845 26 Feb 20 nicklas 1131         int numRealign = 0;
5845 26 Feb 20 nicklas 1132         int numFlaggedDna = 0;
5845 26 Feb 20 nicklas 1133         int numDeleted = 0;
5845 26 Feb 20 nicklas 1134         
5845 26 Feb 20 nicklas 1135         ItemList flaggedDna = BiomaterialList.FLAGGED_DNA.load(dc);
5845 26 Feb 20 nicklas 1136         ItemList mipsAlignPipeline = BiomaterialList.MIPS_ALIGN_PIPELINE.load(dc);
5845 26 Feb 20 nicklas 1137 //        ItemList nextMipsPipeline = BiomaterialList.MIPS_NEXTSTEP_TODO.load(dc); // TODO
5845 26 Feb 20 nicklas 1138         
5845 26 Feb 20 nicklas 1139         boolean deleteItemsCreatedByFailedJobs = Boolean.TRUE.equals(jsonReq.get("deleteItemsCreatedByFailedJobs"));
5845 26 Feb 20 nicklas 1140         List<BasicItem> toDelete = new ArrayList<BasicItem>();
5845 26 Feb 20 nicklas 1141         
5845 26 Feb 20 nicklas 1142         for (int alignNo = 0; alignNo < jsonAlignedSequences.size(); ++alignNo)
5845 26 Feb 20 nicklas 1143         {
5845 26 Feb 20 nicklas 1144           JSONObject jsonAligned = (JSONObject)jsonAlignedSequences.get(alignNo);
5845 26 Feb 20 nicklas 1145           Number alignedId = (Number)jsonAligned.get("id");
5845 26 Feb 20 nicklas 1146           
5845 26 Feb 20 nicklas 1147           AlignedSequences aligned = AlignedSequences.getById(dc, alignedId.intValue());
5845 26 Feb 20 nicklas 1148           MergedSequences merged = aligned.getMergedSequences(dc);
5845 26 Feb 20 nicklas 1149           
5845 26 Feb 20 nicklas 1150           DerivedBioAssay alignedDBA = aligned.getDerivedBioAssay();
5845 26 Feb 20 nicklas 1151           Job alignJob = alignedDBA.getJob();
5845 26 Feb 20 nicklas 1152           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, alignedDBA, null);
5845 26 Feb 20 nicklas 1153           
5845 26 Feb 20 nicklas 1154           String flag = (String)(jsonAligned.get("flag"));
5845 26 Feb 20 nicklas 1155           if (flag != null && !Reggie.isExternalItem(aligned.getName()))
5845 26 Feb 20 nicklas 1156           {
5845 26 Feb 20 nicklas 1157             Library lib = Library.get(alignedDBA.getExtract());
5845 26 Feb 20 nicklas 1158             Dna dna = lib.getDna(dc, false);
5845 26 Feb 20 nicklas 1159             Extract d = dna.getItem();
5845 26 Feb 20 nicklas 1160             Annotationtype.FLAG.setAnnotationValue(dc, d, flag);
5845 26 Feb 20 nicklas 1161             flaggedDna.add(d);
5845 26 Feb 20 nicklas 1162             numFlaggedDna++;
5845 26 Feb 20 nicklas 1163           }
5845 26 Feb 20 nicklas 1164           
5845 26 Feb 20 nicklas 1165           String comment = Values.getStringOrNull((String)jsonAligned.get("comment"));
5845 26 Feb 20 nicklas 1166           alignedDBA.setDescription(comment);
5845 26 Feb 20 nicklas 1167           
5845 26 Feb 20 nicklas 1168           boolean reAlign = Boolean.TRUE.equals(jsonAligned.get("realign"));
5845 26 Feb 20 nicklas 1169           boolean startNextStepTODO = Boolean.TRUE.equals(jsonAligned.get("nextStepTODO"));
5845 26 Feb 20 nicklas 1170           boolean deleted = false;
5845 26 Feb 20 nicklas 1171
5845 26 Feb 20 nicklas 1172           if (reAlign)
5845 26 Feb 20 nicklas 1173           {
5845 26 Feb 20 nicklas 1174             // Set ReProcess annotation on merged sequences item
5845 26 Feb 20 nicklas 1175             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, merged.getItem(), "ReProcess");
5845 26 Feb 20 nicklas 1176             mipsAlignPipeline.add(merged.getItem());
5845 26 Feb 20 nicklas 1177             numRealign++;
5845 26 Feb 20 nicklas 1178             
5845 26 Feb 20 nicklas 1179             if (deleteItemsCreatedByFailedJobs && alignJob.getStatus() == Job.Status.ERROR)
5845 26 Feb 20 nicklas 1180             {
5845 26 Feb 20 nicklas 1181               // Delete aligned sequences items
5845 26 Feb 20 nicklas 1182               alignedDBA.setRemoved(true);
5845 26 Feb 20 nicklas 1183               toDelete.add(alignedDBA);
5845 26 Feb 20 nicklas 1184               toDelete.addAll(Reggie.removeAttachedFiles(dc, alignedDBA));
5845 26 Feb 20 nicklas 1185               deleted = true;
5845 26 Feb 20 nicklas 1186               numDeleted++;
5845 26 Feb 20 nicklas 1187               if (comment != null)
5845 26 Feb 20 nicklas 1188               {
5845 26 Feb 20 nicklas 1189                 merged.getItem().setDescription(comment);
5845 26 Feb 20 nicklas 1190               }
5845 26 Feb 20 nicklas 1191             }
5845 26 Feb 20 nicklas 1192           }
5845 26 Feb 20 nicklas 1193           
5845 26 Feb 20 nicklas 1194           if (!deleted)
5845 26 Feb 20 nicklas 1195           {
5845 26 Feb 20 nicklas 1196             if (startNextStepTODO)
5845 26 Feb 20 nicklas 1197             {
5845 26 Feb 20 nicklas 1198               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, alignedDBA, AlignedSequences.ALIGN_SUCCESSFUL);
5845 26 Feb 20 nicklas 1199               // nextMipsPipeline.add(alignedDBA);  // TODO
5845 26 Feb 20 nicklas 1200               numConfirmed++;
5845 26 Feb 20 nicklas 1201             }
5845 26 Feb 20 nicklas 1202             else if (reAlign || flag != null)
5845 26 Feb 20 nicklas 1203             {
5845 26 Feb 20 nicklas 1204               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, alignedDBA, AlignedSequences.ALIGN_FAILED);
5845 26 Feb 20 nicklas 1205             }
5845 26 Feb 20 nicklas 1206             else
5845 26 Feb 20 nicklas 1207             {
5845 26 Feb 20 nicklas 1208               numUnconfirmed++;
5845 26 Feb 20 nicklas 1209             }
5845 26 Feb 20 nicklas 1210           }
5845 26 Feb 20 nicklas 1211         }
5845 26 Feb 20 nicklas 1212         
5845 26 Feb 20 nicklas 1213         if (numConfirmed > 0)
5845 26 Feb 20 nicklas 1214         {
5845 26 Feb 20 nicklas 1215           jsonMessages.add(numConfirmed + " libraries confirmed for nextStepTODO"); // TODO
5845 26 Feb 20 nicklas 1216           ActivityDef.MIPS_ALIGNMENT_CONFIRMED.merge(dc, numConfirmed);
5845 26 Feb 20 nicklas 1217         }
5845 26 Feb 20 nicklas 1218         else
5845 26 Feb 20 nicklas 1219         {
5845 26 Feb 20 nicklas 1220           jsonMessages.add("No libraries confirmed as aligned");
5845 26 Feb 20 nicklas 1221         }
5845 26 Feb 20 nicklas 1222         
5845 26 Feb 20 nicklas 1223         if (numRealign > 0)
5845 26 Feb 20 nicklas 1224         {
5845 26 Feb 20 nicklas 1225           jsonMessages.add(numRealign + " libraries flagged for re-alignment");
5845 26 Feb 20 nicklas 1226         }
5845 26 Feb 20 nicklas 1227         
5845 26 Feb 20 nicklas 1228         if (numDeleted > 0)
5845 26 Feb 20 nicklas 1229         {
5845 26 Feb 20 nicklas 1230           jsonMessages.add(numDeleted + " libraries deleted due to failure");
5845 26 Feb 20 nicklas 1231           if (toDelete.size() > numDeleted)
5845 26 Feb 20 nicklas 1232           {
5845 26 Feb 20 nicklas 1233             jsonMessages.add((toDelete.size() - numDeleted) + " linked items (eg. files) deleted");
5845 26 Feb 20 nicklas 1234           }
5845 26 Feb 20 nicklas 1235         }
5845 26 Feb 20 nicklas 1236         
5845 26 Feb 20 nicklas 1237         if (numFlaggedDna > 0)
5845 26 Feb 20 nicklas 1238         {
5845 26 Feb 20 nicklas 1239           jsonMessages.add("Added " + numFlaggedDna + " DNA items to the '" + flaggedDna.getName() + "' list");
5845 26 Feb 20 nicklas 1240         }
5845 26 Feb 20 nicklas 1241         
5845 26 Feb 20 nicklas 1242         if (numUnconfirmed > 0)
5845 26 Feb 20 nicklas 1243         {
5845 26 Feb 20 nicklas 1244           jsonMessages.add(numUnconfirmed + " libraries remain unconfirmed");
5845 26 Feb 20 nicklas 1245         }
5845 26 Feb 20 nicklas 1246         
5845 26 Feb 20 nicklas 1247         dc.commit();
5845 26 Feb 20 nicklas 1248         
5845 26 Feb 20 nicklas 1249         if (toDelete.size() > 0)
5845 26 Feb 20 nicklas 1250         {
5845 26 Feb 20 nicklas 1251           try
5845 26 Feb 20 nicklas 1252           {
5845 26 Feb 20 nicklas 1253             Trashcan.delete(sc, toDelete, false, null);
5845 26 Feb 20 nicklas 1254           }
5845 26 Feb 20 nicklas 1255           catch (RuntimeException ex)
5845 26 Feb 20 nicklas 1256           {
5845 26 Feb 20 nicklas 1257             ex.printStackTrace();
5845 26 Feb 20 nicklas 1258             jsonMessages.add("[Error]Could not delete all items created by failed jobs: " + ex.getMessage());
5845 26 Feb 20 nicklas 1259           }
5845 26 Feb 20 nicklas 1260         }
5845 26 Feb 20 nicklas 1261       }
5390 29 Apr 19 nicklas 1262       json.put("messages", jsonMessages);
5390 29 Apr 19 nicklas 1263       CounterService.getInstance().setForceCount();
5390 29 Apr 19 nicklas 1264     }
5390 29 Apr 19 nicklas 1265     catch (Throwable t)
5390 29 Apr 19 nicklas 1266     {
5390 29 Apr 19 nicklas 1267       t.printStackTrace();
5390 29 Apr 19 nicklas 1268       json.clear();
5390 29 Apr 19 nicklas 1269       json.put("status", "error");
5390 29 Apr 19 nicklas 1270       json.put("message", t.getMessage());
5390 29 Apr 19 nicklas 1271       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
5390 29 Apr 19 nicklas 1272     }
5390 29 Apr 19 nicklas 1273     finally
5390 29 Apr 19 nicklas 1274     {
5390 29 Apr 19 nicklas 1275       if (dc != null) dc.close();
5390 29 Apr 19 nicklas 1276       json.writeJSONString(resp.getWriter());
5390 29 Apr 19 nicklas 1277     }
5390 29 Apr 19 nicklas 1278     
5390 29 Apr 19 nicklas 1279   }
5390 29 Apr 19 nicklas 1280   
5390 29 Apr 19 nicklas 1281 }