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

Code
Comments
Other
Rev Date Author Line
2265 05 Mar 14 nicklas 1 package net.sf.basedb.reggie.servlet;
2265 05 Mar 14 nicklas 2
2265 05 Mar 14 nicklas 3 import java.io.IOException;
2333 08 Apr 14 nicklas 4 import java.util.ArrayList;
2265 05 Mar 14 nicklas 5 import java.util.HashSet;
2265 05 Mar 14 nicklas 6 import java.util.List;
2265 05 Mar 14 nicklas 7 import java.util.Set;
4985 28 Sep 18 nicklas 8 import java.util.TreeSet;
2265 05 Mar 14 nicklas 9
2265 05 Mar 14 nicklas 10 import javax.servlet.ServletException;
2265 05 Mar 14 nicklas 11 import javax.servlet.http.HttpServlet;
2265 05 Mar 14 nicklas 12 import javax.servlet.http.HttpServletRequest;
2265 05 Mar 14 nicklas 13 import javax.servlet.http.HttpServletResponse;
2265 05 Mar 14 nicklas 14
2265 05 Mar 14 nicklas 15 import org.json.simple.JSONArray;
2265 05 Mar 14 nicklas 16 import org.json.simple.JSONObject;
2265 05 Mar 14 nicklas 17
2683 18 Sep 14 nicklas 18 import net.sf.basedb.core.BasicItem;
3247 14 Apr 15 nicklas 19 import net.sf.basedb.core.ItemList;
2265 05 Mar 14 nicklas 20 import net.sf.basedb.core.DbControl;
2265 05 Mar 14 nicklas 21 import net.sf.basedb.core.DerivedBioAssay;
2265 05 Mar 14 nicklas 22 import net.sf.basedb.core.Extract;
5620 20 Sep 19 nicklas 23 import net.sf.basedb.core.Item;
2320 04 Apr 14 nicklas 24 import net.sf.basedb.core.ItemNotFoundException;
2265 05 Mar 14 nicklas 25 import net.sf.basedb.core.ItemQuery;
2265 05 Mar 14 nicklas 26 import net.sf.basedb.core.Job;
2265 05 Mar 14 nicklas 27 import net.sf.basedb.core.PhysicalBioAssay;
2265 05 Mar 14 nicklas 28 import net.sf.basedb.core.Protocol;
2265 05 Mar 14 nicklas 29 import net.sf.basedb.core.SessionControl;
2265 05 Mar 14 nicklas 30 import net.sf.basedb.core.Software;
2636 04 Sep 14 nicklas 31 import net.sf.basedb.core.Trashcan;
2265 05 Mar 14 nicklas 32 import net.sf.basedb.core.query.Annotations;
2265 05 Mar 14 nicklas 33 import net.sf.basedb.core.query.Expressions;
2265 05 Mar 14 nicklas 34 import net.sf.basedb.core.query.Hql;
2364 15 Apr 14 nicklas 35 import net.sf.basedb.core.query.Orders;
2265 05 Mar 14 nicklas 36 import net.sf.basedb.core.query.Restrictions;
3768 24 Feb 16 nicklas 37 import net.sf.basedb.core.snapshot.SnapshotManager;
4306 17 Jan 17 nicklas 38 import net.sf.basedb.opengrid.OpenGridCluster;
4306 17 Jan 17 nicklas 39 import net.sf.basedb.opengrid.service.OpenGridService;
2364 15 Apr 14 nicklas 40 import net.sf.basedb.reggie.JsonUtil;
2265 05 Mar 14 nicklas 41 import net.sf.basedb.reggie.Reggie;
5384 26 Apr 19 nicklas 42 import net.sf.basedb.reggie.activity.ActivityDef;
3049 17 Dec 14 nicklas 43 import net.sf.basedb.reggie.autoconfirm.DemuxAutoConfirmer;
3059 19 Dec 14 nicklas 44 import net.sf.basedb.reggie.counter.CounterService;
2265 05 Mar 14 nicklas 45 import net.sf.basedb.reggie.dao.Annotationtype;
2367 17 Apr 14 nicklas 46 import net.sf.basedb.reggie.dao.BiomaterialList;
2364 15 Apr 14 nicklas 47 import net.sf.basedb.reggie.dao.DemuxedSequences;
5551 12 Aug 19 nicklas 48 import net.sf.basedb.reggie.dao.Dna;
2265 05 Mar 14 nicklas 49 import net.sf.basedb.reggie.dao.FlowCell;
2265 05 Mar 14 nicklas 50 import net.sf.basedb.reggie.dao.Library;
2364 15 Apr 14 nicklas 51 import net.sf.basedb.reggie.dao.MergedSequences;
5479 10 Jun 19 nicklas 52 import net.sf.basedb.reggie.dao.Pipeline;
2265 05 Mar 14 nicklas 53 import net.sf.basedb.reggie.dao.PooledLibrary;
4985 28 Sep 18 nicklas 54 import net.sf.basedb.reggie.dao.ReggieItem;
2265 05 Mar 14 nicklas 55 import net.sf.basedb.reggie.dao.ReggieRole;
2367 17 Apr 14 nicklas 56 import net.sf.basedb.reggie.dao.Rna;
2265 05 Mar 14 nicklas 57 import net.sf.basedb.reggie.dao.SequencingRun;
2265 05 Mar 14 nicklas 58 import net.sf.basedb.reggie.dao.Subtype;
4306 17 Jan 17 nicklas 59 import net.sf.basedb.reggie.grid.DemuxJobCreator;
4306 17 Jan 17 nicklas 60 import net.sf.basedb.reggie.grid.DemuxJobCreator.DemuxDefinition;
5487 12 Jun 19 nicklas 61 import net.sf.basedb.reggie.grid.MipsDemuxJobCreator;
5486 12 Jun 19 nicklas 62 import net.sf.basedb.reggie.grid.RnaSeqDemuxJobCreator;
2265 05 Mar 14 nicklas 63 import net.sf.basedb.util.Values;
2265 05 Mar 14 nicklas 64 import net.sf.basedb.util.error.ThrowableUtil;
2265 05 Mar 14 nicklas 65
2265 05 Mar 14 nicklas 66
2265 05 Mar 14 nicklas 67 public class DemuxMergeServlet 
2265 05 Mar 14 nicklas 68   extends HttpServlet 
2265 05 Mar 14 nicklas 69 {
2265 05 Mar 14 nicklas 70
2265 05 Mar 14 nicklas 71   private static final long serialVersionUID = 5275254325858237519L;
2265 05 Mar 14 nicklas 72
2265 05 Mar 14 nicklas 73   public DemuxMergeServlet()
2265 05 Mar 14 nicklas 74   {}
2265 05 Mar 14 nicklas 75
2265 05 Mar 14 nicklas 76   @Override
2265 05 Mar 14 nicklas 77   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
2265 05 Mar 14 nicklas 78     throws ServletException, IOException 
2265 05 Mar 14 nicklas 79   {
2265 05 Mar 14 nicklas 80     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 81     JsonUtil.setJsonResponseHeaders(resp);
2265 05 Mar 14 nicklas 82     
2265 05 Mar 14 nicklas 83     JSONObject json = new JSONObject();
2265 05 Mar 14 nicklas 84     json.put("status", "ok");
2265 05 Mar 14 nicklas 85     
3975 26 May 16 nicklas 86     final SessionControl sc = Reggie.getSessionControl(req);
2265 05 Mar 14 nicklas 87     DbControl dc = null;
2265 05 Mar 14 nicklas 88     try
2265 05 Mar 14 nicklas 89     {
3059 19 Dec 14 nicklas 90       if ("GetConfirmedSequencingRuns".equals(cmd))
2265 05 Mar 14 nicklas 91       {
5479 10 Jun 19 nicklas 92         Pipeline pipeline = Pipeline.getByCName(req.getParameter("pipeline"));
6335 15 Jun 21 nicklas 93         dc = sc.newDbControl(":Start demux and merge");
2265 05 Mar 14 nicklas 94         ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
2265 05 Mar 14 nicklas 95         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2265 05 Mar 14 nicklas 96         Subtype.SEQUENCING_RUN.addFilter(dc, query);
5479 10 Jun 19 nicklas 97         pipeline.addFilter(dc, query);
2265 05 Mar 14 nicklas 98         // Must have a SEQUENCING_CONFIRMED=TRUE annotation
2265 05 Mar 14 nicklas 99         query.join(Annotations.leftJoin(null, Annotationtype.SEQUENCING_CONFIRMED.load(dc), "scf"));
2265 05 Mar 14 nicklas 100         query.restrict(Restrictions.eq(Hql.alias("scf"), Expressions.bool(true)));
2265 05 Mar 14 nicklas 101         // And SEQUENCING_RESULT=Successful
2265 05 Mar 14 nicklas 102         query.join(Annotations.leftJoin(null, Annotationtype.SEQUENCING_RESULT.load(dc), "ssr"));
2265 05 Mar 14 nicklas 103         query.restrict(Restrictions.eq(Hql.alias("ssr"), Expressions.string(SequencingRun.SEQUENCING_SUCCESSFUL)));
2640 08 Sep 14 nicklas 104
2640 08 Sep 14 nicklas 105         // And AUTO_PROCESSING==ReProcess OR AUTO_PROCESSING==null && <no child (DemuxedSequences) items>
2640 08 Sep 14 nicklas 106         query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
2640 08 Sep 14 nicklas 107         query.restrict(
2640 08 Sep 14 nicklas 108           Restrictions.or(
2640 08 Sep 14 nicklas 109             Restrictions.eq(Hql.alias("ap"), Expressions.string("ReProcess")),
2640 08 Sep 14 nicklas 110             Restrictions.and(
2640 08 Sep 14 nicklas 111               Restrictions.eq(Hql.alias("ap"), null),
5533 27 Jun 19 nicklas 112               Restrictions.eq(Hql.size(null, "children"), Expressions.integer(0))
2640 08 Sep 14 nicklas 113             )
2640 08 Sep 14 nicklas 114           ));
2265 05 Mar 14 nicklas 115       
2265 05 Mar 14 nicklas 116         JSONArray jsonRuns = new JSONArray();
2265 05 Mar 14 nicklas 117         List<SequencingRun> runs = SequencingRun.toList(query.list(dc));
2265 05 Mar 14 nicklas 118         for (SequencingRun run : runs)
2265 05 Mar 14 nicklas 119         {
2265 05 Mar 14 nicklas 120           loadSequencingRunInfoForDemux(dc, run);
2265 05 Mar 14 nicklas 121           jsonRuns.add(run.asJSONObject());
2265 05 Mar 14 nicklas 122         }
2265 05 Mar 14 nicklas 123         
2265 05 Mar 14 nicklas 124         json.put("sequencingRuns", jsonRuns);
5479 10 Jun 19 nicklas 125         json.put("pipeline", pipeline.asJSONObject());
2265 05 Mar 14 nicklas 126       }
2265 05 Mar 14 nicklas 127       else if ("GetSequencingRunInfo".equals(cmd))
2265 05 Mar 14 nicklas 128       {
6335 15 Jun 21 nicklas 129         dc = sc.newDbControl(":Start demux and merge");
2808 15 Oct 14 nicklas 130         Integer[] seqRunIds = Values.getInt(req.getParameter("ids").split(","));
2808 15 Oct 14 nicklas 131         JSONArray jsonSeqRuns = new JSONArray();
2808 15 Oct 14 nicklas 132         for (int i = 0; i < seqRunIds.length; i++)
2808 15 Oct 14 nicklas 133         {
2808 15 Oct 14 nicklas 134           SequencingRun run = SequencingRun.getById(dc, seqRunIds[i]);
2808 15 Oct 14 nicklas 135           loadSequencingRunInfoForDemux(dc, run);
2808 15 Oct 14 nicklas 136           jsonSeqRuns.add(run.asJSONObject());
2808 15 Oct 14 nicklas 137         }
2808 15 Oct 14 nicklas 138         json.put("sequencingRuns", jsonSeqRuns);
2265 05 Mar 14 nicklas 139       }
2364 15 Apr 14 nicklas 140       else if ("GetUnconfirmedDemuxJobs".equals(cmd))
2364 15 Apr 14 nicklas 141       {
5493 13 Jun 19 nicklas 142         Pipeline pipeline = Pipeline.getByCName(req.getParameter("pipeline"));
6335 15 Jun 21 nicklas 143         dc = sc.newDbControl(":Confirm demux and merge");
2367 17 Apr 14 nicklas 144         ItemQuery<Job> query = Job.getQuery();
2364 15 Apr 14 nicklas 145         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2372 23 Apr 14 nicklas 146         // The job must be ended and successful
2367 17 Apr 14 nicklas 147         query.restrict(Restrictions.neq(Hql.property("ended"), null));
2367 17 Apr 14 nicklas 148         // Join jobs for DemuxedSequences
2367 17 Apr 14 nicklas 149         query.join(Hql.innerJoin("derivedBioAssays", "dba"));
2367 17 Apr 14 nicklas 150         Subtype.DEMUXED_SEQUENCES.addFilter(dc, query, "dba");
5493 13 Jun 19 nicklas 151         pipeline.addFilter(dc, query, "dba");
3723 25 Jan 16 nicklas 152         // Must have a none or "Partial" ANALYSIS_RESULT annotation
2367 17 Apr 14 nicklas 153         query.join(Annotations.leftJoin("dba", Annotationtype.ANALYSIS_RESULT.load(dc), "ar"));
3723 25 Jan 16 nicklas 154         query.restrict(Restrictions.or(
3723 25 Jan 16 nicklas 155             Restrictions.eq(Hql.alias("ar"), Expressions.string(DemuxedSequences.DEMUX_PARTIAL)),
3723 25 Jan 16 nicklas 156             Restrictions.eq(Hql.alias("ar"), null)
3723 25 Jan 16 nicklas 157           ));
3032 12 Dec 14 nicklas 158         // Ignore if AutoProcess==AutoConfirm
3032 12 Dec 14 nicklas 159         query.join(Annotations.leftJoin("dba", Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
3032 12 Dec 14 nicklas 160         query.restrict(
3032 12 Dec 14 nicklas 161             Restrictions.or(
3032 12 Dec 14 nicklas 162               Restrictions.neq(Hql.alias("ap"), Expressions.string("AutoConfirm")),
3032 12 Dec 14 nicklas 163               Restrictions.eq(Hql.alias("ap"), null)
3032 12 Dec 14 nicklas 164             ));
2367 17 Apr 14 nicklas 165         query.setDistinct(true);
2372 23 Apr 14 nicklas 166         query.order(Orders.asc(Hql.property("ended")));
2372 23 Apr 14 nicklas 167
2364 15 Apr 14 nicklas 168         JSONArray jsonJobs = new JSONArray();
2367 17 Apr 14 nicklas 169         for (Job j : query.list(dc))
2364 15 Apr 14 nicklas 170         {
2367 17 Apr 14 nicklas 171           JSONObject jsonJob = JsonUtil.getJobAsJSON(j);
2367 17 Apr 14 nicklas 172           jsonJob.put("jobFolder", j.getParameterValue("jobFolder"));
3049 17 Dec 14 nicklas 173           boolean debug = Boolean.TRUE.equals(j.getParameterValue("debug"));
3049 17 Dec 14 nicklas 174           jsonJob.put("debug", debug);
2367 17 Apr 14 nicklas 175           
3049 17 Dec 14 nicklas 176           JSONObject jsonLimits = new JSONObject();
3049 17 Dec 14 nicklas 177           jsonLimits.put("minPtReads", debug ? DemuxAutoConfirmer.MIN_PT_READS_DEBUG : DemuxAutoConfirmer.MIN_PT_READS);
3049 17 Dec 14 nicklas 178           jsonJob.put("limits", jsonLimits);
3049 17 Dec 14 nicklas 179           
2367 17 Apr 14 nicklas 180           // Load the demuxed sequences items
2367 17 Apr 14 nicklas 181           ItemQuery<DerivedBioAssay> dxQuery = DerivedBioAssay.getQuery();
2367 17 Apr 14 nicklas 182           Subtype.DEMUXED_SEQUENCES.addFilter(dc, dxQuery);
2367 17 Apr 14 nicklas 183           dxQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2367 17 Apr 14 nicklas 184           dxQuery.restrict(Restrictions.eq(Hql.property("job"), Hql.entity(j)));
2434 19 May 14 nicklas 185           dxQuery.order(Orders.asc(Hql.property("name")));
2367 17 Apr 14 nicklas 186           
2367 17 Apr 14 nicklas 187           JSONArray jsonDxs = new JSONArray();
3723 25 Jan 16 nicklas 188           boolean partial = false;
2367 17 Apr 14 nicklas 189           for (DemuxedSequences demux : DemuxedSequences.toList(dxQuery.list(dc)))
2364 15 Apr 14 nicklas 190           {
2367 17 Apr 14 nicklas 191             SequencingRun seqRun = SequencingRun.getByDemuxedSequences(dc, demux);
2367 17 Apr 14 nicklas 192             loadSequencingRunInfoForDemux(dc, seqRun);
2437 19 May 14 nicklas 193             demux.loadAnnotations(dc, "PF_READS", Annotationtype.PF_READS, null);
2434 19 May 14 nicklas 194             demux.loadAnnotations(dc, "PF_NNNN_PCT", Annotationtype.PF_NNNN_PCT, null);
2434 19 May 14 nicklas 195             demux.loadAnnotations(dc, "PF_UNUSED_PCT", Annotationtype.PF_UNUSED_PCT, null);
2434 19 May 14 nicklas 196             demux.loadAnnotations(dc, "DEMUX_WARNING", Annotationtype.DEMUX_WARNINGS, null);
3140 24 Feb 15 nicklas 197             demux.loadAnnotations(dc, "SKIPPED_TILES", Annotationtype.SKIPPED_TILES, null);
5620 20 Sep 19 nicklas 198             demux.setAnnotation("DemultiplexMetrics", JsonUtil.loadLinkedItem(dc, demux.getItem(), "DemultiplexMetrics", Item.FILE, null));
3723 25 Jan 16 nicklas 199             if (DemuxedSequences.DEMUX_PARTIAL.equals(Annotationtype.ANALYSIS_RESULT.getAnnotationValue(dc, demux.getItem())))
3723 25 Jan 16 nicklas 200             {
3723 25 Jan 16 nicklas 201               partial = true;
3723 25 Jan 16 nicklas 202             }
2367 17 Apr 14 nicklas 203             demux.setAnnotation("seqRun", seqRun.asJSONObject());
2367 17 Apr 14 nicklas 204             jsonDxs.add(demux.asJSONObject());
2367 17 Apr 14 nicklas 205             
2364 15 Apr 14 nicklas 206           }
3723 25 Jan 16 nicklas 207           if (partial) jsonJob.put("partial", true);
2367 17 Apr 14 nicklas 208           jsonJob.put("demuxedSequences", jsonDxs);
2367 17 Apr 14 nicklas 209           jsonJobs.add(jsonJob);
2364 15 Apr 14 nicklas 210         }
2364 15 Apr 14 nicklas 211         
2364 15 Apr 14 nicklas 212         json.put("jobs", jsonJobs);
2364 15 Apr 14 nicklas 213       }
2364 15 Apr 14 nicklas 214       else if ("GetMergedSequences".equals(cmd))
2364 15 Apr 14 nicklas 215       {
2364 15 Apr 14 nicklas 216         int jobId = Values.getInt(req.getParameter("job"));
5797 18 Dec 19 nicklas 217         Pipeline pipeline = Pipeline.getByCName(req.getParameter("pipeline"));
6335 15 Jun 21 nicklas 218         dc = sc.newDbControl(":Confirm demux and merge");
2819 16 Oct 14 nicklas 219         
2819 16 Oct 14 nicklas 220         Job job = Job.getById(dc, jobId);
2819 16 Oct 14 nicklas 221         List<MergedSequences> merged = MergedSequences.findByJob(dc, job);
2819 16 Oct 14 nicklas 222         List<DemuxedSequences> demux = DemuxedSequences.findByJob(dc, job);
2819 16 Oct 14 nicklas 223         
2819 16 Oct 14 nicklas 224         // Get all pools taking omitted lanes into account
2819 16 Oct 14 nicklas 225         Set<PooledLibrary> allPools = new HashSet<PooledLibrary>();
2819 16 Oct 14 nicklas 226         for (DemuxedSequences dx : demux)
2819 16 Oct 14 nicklas 227         {
2819 16 Oct 14 nicklas 228           allPools.addAll(dx.getPools(dc));
2819 16 Oct 14 nicklas 229         }
2819 16 Oct 14 nicklas 230         
3768 24 Feb 16 nicklas 231         SnapshotManager manager = new SnapshotManager();
2364 15 Apr 14 nicklas 232         JSONArray jsonMerged = new JSONArray();
2364 15 Apr 14 nicklas 233         for (MergedSequences mg : merged)
2364 15 Apr 14 nicklas 234         {
2364 15 Apr 14 nicklas 235           Library lib = mg.getLibrary(dc);
5797 18 Dec 19 nicklas 236           lib.loadBioPlateLocation();
5797 18 Dec 19 nicklas 237           if (pipeline == Pipeline.MIPS)
5797 18 Dec 19 nicklas 238           {
5797 18 Dec 19 nicklas 239             Extract dna = (Extract)lib.findSingleParent(dc, Subtype.DNA_NORMALIZED_ALIQUOT);
5797 18 Dec 19 nicklas 240             lib.setAnnotation("MIPSPanel", Annotationtype.MIPS_PANEL.getAnnotationValue(dc, dna));
5797 18 Dec 19 nicklas 241           }
2819 16 Oct 14 nicklas 242           // All pools for the library, but we only keep those used in the demux
2819 16 Oct 14 nicklas 243           List<PooledLibrary> pools = lib.getPools(dc);
2819 16 Oct 14 nicklas 244           pools.retainAll(allPools);
3768 24 Feb 16 nicklas 245           Rna r = lib.getRna(dc, true);
5493 13 Jun 19 nicklas 246           if (r != null) lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
3768 24 Feb 16 nicklas 247           lib.loadAnnotations(dc, manager, "CA_Size", Annotationtype.CA_SIZE, null);
3768 24 Feb 16 nicklas 248           lib.loadAnnotations(dc, manager, "library_size_est", Annotationtype.LIBRARY_SIZE_EST, null);
2364 15 Apr 14 nicklas 249           mg.setAnnotation("lib", lib.asJSONObject());
3768 24 Feb 16 nicklas 250           mg.loadAnnotations(dc, manager, "READS", Annotationtype.READS, null);
3768 24 Feb 16 nicklas 251           mg.loadAnnotations(dc, manager, "PF_READS", Annotationtype.PF_READS, null);
3768 24 Feb 16 nicklas 252           mg.loadAnnotations(dc, manager, "ADAPTER_READS", Annotationtype.ADAPTER_READS, null);
3768 24 Feb 16 nicklas 253           mg.loadAnnotations(dc, manager, "PT_READS", Annotationtype.PT_READS, null);
3768 24 Feb 16 nicklas 254           mg.loadAnnotations(dc, manager, "FragmentSizeAvg", Annotationtype.FRAGMENT_SIZE_AVG, null);
3768 24 Feb 16 nicklas 255           mg.loadAnnotations(dc, manager, "FragmentSizeStdev", Annotationtype.FRAGMENT_SIZE_STDEV, null);
3768 24 Feb 16 nicklas 256           mg.loadAnnotations(dc, manager, "AnalysisResult", Annotationtype.ANALYSIS_RESULT, null);
2819 16 Oct 14 nicklas 257           JSONArray jsonPools = new JSONArray();
2819 16 Oct 14 nicklas 258           for (PooledLibrary pool : pools)
2819 16 Oct 14 nicklas 259           {
2819 16 Oct 14 nicklas 260             jsonPools.add(pool.asJSONObject());
2819 16 Oct 14 nicklas 261           }
2819 16 Oct 14 nicklas 262           mg.setAnnotation("pools", jsonPools);
2819 16 Oct 14 nicklas 263           
2364 15 Apr 14 nicklas 264           jsonMerged.add(mg.asJSONObject());
2364 15 Apr 14 nicklas 265         }
2364 15 Apr 14 nicklas 266         
2364 15 Apr 14 nicklas 267         json.put("mergedSequences", jsonMerged);
2364 15 Apr 14 nicklas 268       }
2265 05 Mar 14 nicklas 269
2265 05 Mar 14 nicklas 270     }
2265 05 Mar 14 nicklas 271     catch (Throwable t)
2265 05 Mar 14 nicklas 272     {
2265 05 Mar 14 nicklas 273       t.printStackTrace();
2265 05 Mar 14 nicklas 274       json.clear();
2265 05 Mar 14 nicklas 275       json.put("status", "error");
2265 05 Mar 14 nicklas 276       json.put("message", t.getMessage());
2265 05 Mar 14 nicklas 277       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2265 05 Mar 14 nicklas 278     }
2265 05 Mar 14 nicklas 279     finally
2265 05 Mar 14 nicklas 280     {
2265 05 Mar 14 nicklas 281       if (dc != null) dc.close();
2265 05 Mar 14 nicklas 282       json.writeJSONString(resp.getWriter());
2265 05 Mar 14 nicklas 283     }
2265 05 Mar 14 nicklas 284     
2265 05 Mar 14 nicklas 285   }
2265 05 Mar 14 nicklas 286
2265 05 Mar 14 nicklas 287   @SuppressWarnings("unchecked")
2265 05 Mar 14 nicklas 288   @Override
2265 05 Mar 14 nicklas 289   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
2265 05 Mar 14 nicklas 290     throws ServletException, IOException 
2265 05 Mar 14 nicklas 291   {
2265 05 Mar 14 nicklas 292     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 293     JsonUtil.setJsonResponseHeaders(resp);
2265 05 Mar 14 nicklas 294     
2265 05 Mar 14 nicklas 295     JSONObject json = new JSONObject();
2265 05 Mar 14 nicklas 296     json.put("status", "ok");
2265 05 Mar 14 nicklas 297     JSONArray jsonMessages = new JSONArray();
2265 05 Mar 14 nicklas 298   
3975 26 May 16 nicklas 299     final SessionControl sc = Reggie.getSessionControl(req);
2265 05 Mar 14 nicklas 300     DbControl dc = null;
2265 05 Mar 14 nicklas 301     try
2265 05 Mar 14 nicklas 302     {
2265 05 Mar 14 nicklas 303
2265 05 Mar 14 nicklas 304       if ("StartDemuxAndMerge".equals(cmd))
2265 05 Mar 14 nicklas 305       {
6335 15 Jun 21 nicklas 306         dc = sc.newDbControl(":Start demux and merge");
5487 12 Jun 19 nicklas 307         Pipeline pipeline = Pipeline.getByCName(req.getParameter("pipeline"));
5487 12 Jun 19 nicklas 308         
5487 12 Jun 19 nicklas 309         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", pipeline == Pipeline.RNA_SEQ ? ReggieRole.SECONDARY_ANALYSIS : ReggieRole.MIPS_SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
2265 05 Mar 14 nicklas 310
3752 17 Feb 16 nicklas 311         JSONObject jsonReq = JsonUtil.parseRequest(req);
2265 05 Mar 14 nicklas 312         JSONArray jsonSeqRuns = (JSONArray)jsonReq.get("sequencingRuns");
6280 09 Jun 21 nicklas 313         String whichLibs = (String)jsonReq.get("whichLibs");
2265 05 Mar 14 nicklas 314
2265 05 Mar 14 nicklas 315         Number demuxSoftwareId = (Number)jsonReq.get("demuxSoftware");
2265 05 Mar 14 nicklas 316         Number demuxProtocolId = (Number)jsonReq.get("demuxProtocol");
2265 05 Mar 14 nicklas 317         Number mergeSoftwareId = (Number)jsonReq.get("mergeSoftware");
2265 05 Mar 14 nicklas 318         Number mergeProtocolId = (Number)jsonReq.get("mergeProtocol");
2320 04 Apr 14 nicklas 319         String clusterId = (String)jsonReq.get("cluster");
2377 24 Apr 14 nicklas 320         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
3029 11 Dec 14 nicklas 321         boolean autoConfirm = Boolean.TRUE.equals(jsonReq.get("autoConfirm"));
2736 06 Oct 14 nicklas 322         // Add 'rm' in script to remove files that we no longer need (to preserve disk space)
2819 16 Oct 14 nicklas 323         boolean earlyCleanup = !debug;
2378 25 Apr 14 nicklas 324         Number priority = (Number)jsonReq.get("priority");
6981 17 Jan 23 nicklas 325         String partition = Values.getStringOrNull((String)jsonReq.get("partition"));
7372 06 Oct 23 nicklas 326         String submitOptions = Values.getStringOrNull((String)jsonReq.get("submitOptionsOverride"));
2386 25 Apr 14 nicklas 327         
4306 17 Jan 17 nicklas 328         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
2320 04 Apr 14 nicklas 329         if (cluster == null)
2320 04 Apr 14 nicklas 330         {
2320 04 Apr 14 nicklas 331           throw new ItemNotFoundException("OpenGridScheduler[" + clusterId + "]");
2320 04 Apr 14 nicklas 332         }
2333 08 Apr 14 nicklas 333         
3128 10 Feb 15 nicklas 334         // Load common items
3128 10 Feb 15 nicklas 335         Software demuxSoftware = demuxSoftwareId != null ? Software.getById(dc, demuxSoftwareId.intValue()) : null;
3128 10 Feb 15 nicklas 336         Protocol demuxProtocol = demuxProtocolId != null ? Protocol.getById(dc, demuxProtocolId.intValue()) : null;
3128 10 Feb 15 nicklas 337         
3128 10 Feb 15 nicklas 338         Software mergeSoftware = mergeSoftwareId != null ? Software.getById(dc, mergeSoftwareId.intValue()) : null;
3128 10 Feb 15 nicklas 339         Protocol mergeProtocol = mergeProtocolId != null ? Protocol.getById(dc, mergeProtocolId.intValue()) : null;
6280 09 Jun 21 nicklas 340   
6280 09 Jun 21 nicklas 341         // Library selection
6280 09 Jun 21 nicklas 342         boolean excludeDoNotUse = "allExceptDoNotUse".equals(whichLibs);
6280 09 Jun 21 nicklas 343         List<Extract> ignoreLibs = new ArrayList<>();
6280 09 Jun 21 nicklas 344         if ("selected".equals(whichLibs))
6280 09 Jun 21 nicklas 345         {
6280 09 Jun 21 nicklas 346           JSONArray jsonIgnore = (JSONArray)jsonReq.get("ignoreLibs");
6280 09 Jun 21 nicklas 347           for (int libNo = 0; libNo < jsonIgnore.size(); libNo++)
6280 09 Jun 21 nicklas 348           {
6280 09 Jun 21 nicklas 349             Number libId = (Number)jsonIgnore.get(libNo);
6280 09 Jun 21 nicklas 350             ignoreLibs.add(Extract.getById(dc, libId.intValue()));
6280 09 Jun 21 nicklas 351           }
6280 09 Jun 21 nicklas 352         }
6280 09 Jun 21 nicklas 353
5487 12 Jun 19 nicklas 354         DemuxJobCreator jobCreator = pipeline == Pipeline.RNA_SEQ ? new RnaSeqDemuxJobCreator() : new MipsDemuxJobCreator();
3714 20 Jan 16 nicklas 355         jobCreator.setAutoConfirm(autoConfirm);
3714 20 Jan 16 nicklas 356         jobCreator.setDebug(debug);
3714 20 Jan 16 nicklas 357         jobCreator.setPriority(priority == null ? null : priority.intValue());
6981 17 Jan 23 nicklas 358         jobCreator.setPartition(partition);
3714 20 Jan 16 nicklas 359         jobCreator.setDemuxProtocol(demuxProtocol);
3714 20 Jan 16 nicklas 360         jobCreator.setDemuxSoftware(demuxSoftware);
3714 20 Jan 16 nicklas 361         jobCreator.setMergeProtocol(mergeProtocol);
3714 20 Jan 16 nicklas 362         jobCreator.setMergeSoftware(mergeSoftware);
7372 06 Oct 23 nicklas 363         jobCreator.setSubmitOptionsOverride(submitOptions);
2555 05 Aug 14 nicklas 364         
3714 20 Jan 16 nicklas 365         List<DemuxDefinition> demuxDefs = new ArrayList<DemuxDefinition>();
2265 05 Mar 14 nicklas 366         for (int seqRunNo = 0; seqRunNo < jsonSeqRuns.size(); seqRunNo++)
2265 05 Mar 14 nicklas 367         {
2265 05 Mar 14 nicklas 368           JSONObject jsonSeqRun = (JSONObject)jsonSeqRuns.get(seqRunNo);
2265 05 Mar 14 nicklas 369           Number seqRunId = (Number)jsonSeqRun.get("id");
2320 04 Apr 14 nicklas 370           String readString = (String)jsonSeqRun.get("ReadString");
5319 28 Feb 19 nicklas 371           List<Number> jsonOmitLanes = (List<Number>)jsonSeqRun.get("OmitLanes");
2265 05 Mar 14 nicklas 372           
3714 20 Jan 16 nicklas 373           SequencingRun seqRun = SequencingRun.getById(dc, seqRunId.intValue());
3714 20 Jan 16 nicklas 374           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, seqRun.getItem(), null);
2265 05 Mar 14 nicklas 375           
6280 09 Jun 21 nicklas 376           demuxDefs.add(new DemuxDefinition(seqRun, readString, ignoreLibs, jsonOmitLanes, excludeDoNotUse));
2265 05 Mar 14 nicklas 377         }
3714 20 Jan 16 nicklas 378         
3714 20 Jan 16 nicklas 379         Job demuxJob = jobCreator.createDemuxJob(dc, cluster, demuxDefs, jsonMessages);
3714 20 Jan 16 nicklas 380         if (demuxJob.getStatus() == Job.Status.ERROR)
2356 11 Apr 14 nicklas 381         {
3714 20 Jan 16 nicklas 382           jsonMessages.add("[Error]Job submission for '" + demuxJob.getName() + "' failed: " + demuxJob.getStatusMessage());
2356 11 Apr 14 nicklas 383         }
2356 11 Apr 14 nicklas 384         else
2356 11 Apr 14 nicklas 385         {
4306 17 Jan 17 nicklas 386           jsonMessages.add("Submitted demux and merge job to " + cluster.getConnectionInfo().getName() + " with id " + demuxJob.getExternalId());
2356 11 Apr 14 nicklas 387         }
2267 05 Mar 14 nicklas 388         dc.commit();
2386 25 Apr 14 nicklas 389
2265 05 Mar 14 nicklas 390       }
2367 17 Apr 14 nicklas 391       else if ("RegisterDemuxAndMerge".equals(cmd))
2367 17 Apr 14 nicklas 392       {
6335 15 Jun 21 nicklas 393         dc = sc.newDbControl(":Confirm demux and merge");
2367 17 Apr 14 nicklas 394
2367 17 Apr 14 nicklas 395         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
2367 17 Apr 14 nicklas 396
5551 12 Aug 19 nicklas 397         Pipeline pipeline = Pipeline.getByCName(req.getParameter("pipeline"));
3752 17 Feb 16 nicklas 398         JSONObject jsonReq = JsonUtil.parseRequest(req);
2367 17 Apr 14 nicklas 399         
2367 17 Apr 14 nicklas 400         boolean failed = Boolean.TRUE.equals(jsonReq.get("failed"));
2367 17 Apr 14 nicklas 401         String analysisResult = failed ?  DemuxedSequences.DEMUX_FAILED : DemuxedSequences.DEMUX_SUCCESSFUL;
2367 17 Apr 14 nicklas 402         
2367 17 Apr 14 nicklas 403         boolean flagPools = failed && Boolean.TRUE.equals(jsonReq.get("flagPools"));
2636 04 Sep 14 nicklas 404         boolean deleteItemsCreatedByFailedJob = failed && Boolean.TRUE.equals(jsonReq.get("deleteItemsCreatedByFailedJob"));
2640 08 Sep 14 nicklas 405         boolean demuxAgain = failed && Boolean.TRUE.equals(jsonReq.get("demuxAgain"));
2640 08 Sep 14 nicklas 406         int numReProcess = 0;
2636 04 Sep 14 nicklas 407         
3247 14 Apr 15 nicklas 408         ItemList flaggedPools = BiomaterialList.FLAGGED_POOL.load(dc);
2367 17 Apr 14 nicklas 409         Set<Extract> pools = new HashSet<Extract>();
2367 17 Apr 14 nicklas 410         int numFlaggedPools = 0;
2367 17 Apr 14 nicklas 411         
2367 17 Apr 14 nicklas 412         JSONArray jsonDemuxedSequences = (JSONArray)jsonReq.get("demuxedSequences");
2367 17 Apr 14 nicklas 413         JSONArray jsonMergedSequences = (JSONArray)jsonReq.get("mergedSequences");
2636 04 Sep 14 nicklas 414
2683 18 Sep 14 nicklas 415         List<BasicItem> toDelete = new ArrayList<BasicItem>();
2367 17 Apr 14 nicklas 416         
2367 17 Apr 14 nicklas 417         for (int demuxNo = 0; demuxNo < jsonDemuxedSequences.size(); ++demuxNo)
2367 17 Apr 14 nicklas 418         {
2367 17 Apr 14 nicklas 419           JSONObject jsonDemux = (JSONObject)jsonDemuxedSequences.get(demuxNo);
2367 17 Apr 14 nicklas 420           Number demuxId = (Number)jsonDemux.get("id");
2367 17 Apr 14 nicklas 421           DemuxedSequences dx = DemuxedSequences.getById(dc, demuxId.intValue());
2367 17 Apr 14 nicklas 422           
2367 17 Apr 14 nicklas 423           DerivedBioAssay demux = dx.getDerivedBioAssay();
2367 17 Apr 14 nicklas 424           Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, demux, analysisResult);
3029 11 Dec 14 nicklas 425           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, demux, null);
2636 04 Sep 14 nicklas 426           if (deleteItemsCreatedByFailedJob) 
2636 04 Sep 14 nicklas 427           {
2636 04 Sep 14 nicklas 428             demux.setRemoved(true);
2636 04 Sep 14 nicklas 429             toDelete.add(demux);
2685 19 Sep 14 nicklas 430             toDelete.addAll(Reggie.removeAttachedFiles(dc, demux));
2636 04 Sep 14 nicklas 431           }
2367 17 Apr 14 nicklas 432           
2640 08 Sep 14 nicklas 433           if (demuxAgain)
2640 08 Sep 14 nicklas 434           {
2640 08 Sep 14 nicklas 435             SequencingRun seqRun = SequencingRun.getByDemuxedSequences(dc, dx);
2640 08 Sep 14 nicklas 436             // Set ReProcess annotation on sequencing run item
2640 08 Sep 14 nicklas 437             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, seqRun.getItem(), "ReProcess");
2640 08 Sep 14 nicklas 438             numReProcess++;            
2640 08 Sep 14 nicklas 439           }
2640 08 Sep 14 nicklas 440
2640 08 Sep 14 nicklas 441           
2367 17 Apr 14 nicklas 442           if (flagPools)
2367 17 Apr 14 nicklas 443           {
2367 17 Apr 14 nicklas 444             FlowCell fc = FlowCell.getByDemuxedSequences(dc, dx);
2367 17 Apr 14 nicklas 445             // Load pool aliquots and pools for all lanes 
2367 17 Apr 14 nicklas 446             ItemQuery<Extract> aliquotQuery = fc.getItem().getExtracts(0);
2367 17 Apr 14 nicklas 447             aliquotQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2367 17 Apr 14 nicklas 448             List<Extract> aliquots = aliquotQuery.list(dc);
2367 17 Apr 14 nicklas 449
2367 17 Apr 14 nicklas 450             // If the demuxing failed, we flag the pools and add them to the 'Flagged pools' list.
2367 17 Apr 14 nicklas 451             // Typically, most pools have enough material to try clustering again but that
2367 17 Apr 14 nicklas 452             // the responsibility of another wizard
2367 17 Apr 14 nicklas 453             for (Extract poolA : aliquots)
2367 17 Apr 14 nicklas 454             {
2367 17 Apr 14 nicklas 455               Extract pool = (Extract)poolA.getParent();
2367 17 Apr 14 nicklas 456               if (!pools.contains(pool))
2367 17 Apr 14 nicklas 457               {
2367 17 Apr 14 nicklas 458                 pools.add(pool);
2367 17 Apr 14 nicklas 459                 flaggedPools.add(pool);
2367 17 Apr 14 nicklas 460                 numFlaggedPools++;
2367 17 Apr 14 nicklas 461                 Annotationtype.FLAG.setAnnotationValue(dc, pool, PooledLibrary.FLAG_DEMUX_FAILED);
2367 17 Apr 14 nicklas 462                 Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, pool, "Disable");
2367 17 Apr 14 nicklas 463               }
2367 17 Apr 14 nicklas 464             }
2367 17 Apr 14 nicklas 465           }
2367 17 Apr 14 nicklas 466         }
2367 17 Apr 14 nicklas 467         
2367 17 Apr 14 nicklas 468         if (numFlaggedPools > 0)
2367 17 Apr 14 nicklas 469         {
2367 17 Apr 14 nicklas 470           jsonMessages.add("Added " + numFlaggedPools + " pools to the '" + flaggedPools.getName() + "' list.");
2367 17 Apr 14 nicklas 471         }
2367 17 Apr 14 nicklas 472         
4597 27 Sep 17 nicklas 473         int numHisatAlign = 0;
6809 24 Aug 22 nicklas 474         int numHisat2023Align = 0;
4597 27 Sep 17 nicklas 475         int numLegacyAlign = 0;
5788 13 Dec 19 nicklas 476         int numMipsAlign = 0;
5384 26 Apr 19 nicklas 477         int numConfirmed = 0;
2367 17 Apr 14 nicklas 478         int numFlaggedRna = 0;
5551 12 Aug 19 nicklas 479         int numFlaggedDna = 0;
2636 04 Sep 14 nicklas 480         int numDeleted = 0;
3247 14 Apr 15 nicklas 481         ItemList flaggedRna = BiomaterialList.FLAGGED_RNA.load(dc);
5551 12 Aug 19 nicklas 482         ItemList flaggedDna = BiomaterialList.FLAGGED_DNA.load(dc);
4597 27 Sep 17 nicklas 483         ItemList hisatPipeline = BiomaterialList.HISAT_PIPELINE.load(dc);
6811 25 Aug 22 nicklas 484         ItemList hisat2023Pipeline = BiomaterialList.HISAT_2023_PIPELINE.load(dc);
4597 27 Sep 17 nicklas 485         ItemList legacyPipeline = BiomaterialList.LEGACY_PIPELINE.load(dc);
5788 13 Dec 19 nicklas 486         ItemList mipsPipeline = BiomaterialList.MIPS_ALIGN_PIPELINE.load(dc);
2367 17 Apr 14 nicklas 487
2367 17 Apr 14 nicklas 488         for (int mergeNo = 0; mergeNo < jsonMergedSequences.size(); ++mergeNo)
2367 17 Apr 14 nicklas 489         {
2367 17 Apr 14 nicklas 490           JSONObject jsonMerge = (JSONObject)jsonMergedSequences.get(mergeNo);
2367 17 Apr 14 nicklas 491           Number mergeId = (Number)jsonMerge.get("id");
4597 27 Sep 17 nicklas 492           boolean legacyAlign = !failed && Boolean.TRUE.equals(jsonMerge.get("legacyAlign"));
4597 27 Sep 17 nicklas 493           boolean hisatAlign = !failed && Boolean.TRUE.equals(jsonMerge.get("hisatAlign"));
6809 24 Aug 22 nicklas 494           boolean hisat2023Align = !failed && Boolean.TRUE.equals(jsonMerge.get("hisat2023Align"));
5788 13 Dec 19 nicklas 495           boolean mipsAlign = !failed && Boolean.TRUE.equals(jsonMerge.get("mipsAlign"));
5551 12 Aug 19 nicklas 496               
5788 13 Dec 19 nicklas 497           boolean align = legacyAlign || hisatAlign || mipsAlign;
5384 26 Apr 19 nicklas 498           if (align) numConfirmed++;
2367 17 Apr 14 nicklas 499           
2367 17 Apr 14 nicklas 500           boolean flag = !failed && Boolean.TRUE.equals(jsonMerge.get("flag"));
2367 17 Apr 14 nicklas 501           
2367 17 Apr 14 nicklas 502           DerivedBioAssay merge = DerivedBioAssay.getById(dc, mergeId.intValue());
3049 17 Dec 14 nicklas 503           Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, merge, flag && !align ? DemuxedSequences.DEMUX_FAILED : analysisResult);
2367 17 Apr 14 nicklas 504           if (!align)
2367 17 Apr 14 nicklas 505           {
2367 17 Apr 14 nicklas 506             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, merge, "Disable");
2367 17 Apr 14 nicklas 507           }
2367 17 Apr 14 nicklas 508           merge.setDescription((String)jsonMerge.get("comment"));
2636 04 Sep 14 nicklas 509           if (deleteItemsCreatedByFailedJob)
2636 04 Sep 14 nicklas 510           {
2636 04 Sep 14 nicklas 511             merge.setRemoved(true);
2636 04 Sep 14 nicklas 512             toDelete.add(merge);
2685 19 Sep 14 nicklas 513             toDelete.addAll(Reggie.removeAttachedFiles(dc, merge));
2636 04 Sep 14 nicklas 514             numDeleted++;
2636 04 Sep 14 nicklas 515           }
2367 17 Apr 14 nicklas 516
5551 12 Aug 19 nicklas 517           if (pipeline == Pipeline.RNA_SEQ)
4597 27 Sep 17 nicklas 518           {
5551 12 Aug 19 nicklas 519             if (hisatAlign)
5551 12 Aug 19 nicklas 520             {
5551 12 Aug 19 nicklas 521               hisatPipeline.add(merge);
5551 12 Aug 19 nicklas 522               numHisatAlign++;
5551 12 Aug 19 nicklas 523             }
6809 24 Aug 22 nicklas 524             
6809 24 Aug 22 nicklas 525             if (hisat2023Align)
6809 24 Aug 22 nicklas 526             {
6809 24 Aug 22 nicklas 527               hisat2023Pipeline.add(merge);
6809 24 Aug 22 nicklas 528               numHisat2023Align++;
6809 24 Aug 22 nicklas 529             }
5551 12 Aug 19 nicklas 530   
5551 12 Aug 19 nicklas 531             if (legacyAlign)
5551 12 Aug 19 nicklas 532             {
5551 12 Aug 19 nicklas 533               legacyPipeline.add(merge);
5551 12 Aug 19 nicklas 534               numLegacyAlign++;
5551 12 Aug 19 nicklas 535             }
5551 12 Aug 19 nicklas 536             
5553 12 Aug 19 nicklas 537             if (flag && !Reggie.isExternalItem(merge.getName()))
5551 12 Aug 19 nicklas 538             {
5551 12 Aug 19 nicklas 539               Library lib = Library.get(merge.getExtract());
5551 12 Aug 19 nicklas 540               Rna rna = lib.getRna(dc, false);
5551 12 Aug 19 nicklas 541               if (rna != null)
5551 12 Aug 19 nicklas 542               {
5551 12 Aug 19 nicklas 543                 Extract r = rna.getItem();
5551 12 Aug 19 nicklas 544                 Annotationtype.FLAG.setAnnotationValue(dc, r, Rna.FLAG_DEMUX_FAILED);
5551 12 Aug 19 nicklas 545                 flaggedRna.add(r);
5551 12 Aug 19 nicklas 546                 numFlaggedRna++;
5551 12 Aug 19 nicklas 547               }
5551 12 Aug 19 nicklas 548             }
4597 27 Sep 17 nicklas 549           }
5551 12 Aug 19 nicklas 550           
5551 12 Aug 19 nicklas 551           else if (pipeline == Pipeline.MIPS)
4597 27 Sep 17 nicklas 552           {
5788 13 Dec 19 nicklas 553             if (mipsAlign)
5551 12 Aug 19 nicklas 554             {
5788 13 Dec 19 nicklas 555               mipsPipeline.add(merge);
5788 13 Dec 19 nicklas 556               numMipsAlign++;
5551 12 Aug 19 nicklas 557             }
5551 12 Aug 19 nicklas 558             
5553 12 Aug 19 nicklas 559             if (flag && !Reggie.isExternalItem(merge.getName()))
5551 12 Aug 19 nicklas 560             {
5551 12 Aug 19 nicklas 561               Library lib = Library.get(merge.getExtract());
5551 12 Aug 19 nicklas 562               Dna dna = lib.getDna(dc, false);
5551 12 Aug 19 nicklas 563               if (dna != null)
5551 12 Aug 19 nicklas 564               {
5551 12 Aug 19 nicklas 565                 Extract d = dna.getExtract();
5551 12 Aug 19 nicklas 566                 Annotationtype.FLAG.setAnnotationValue(dc, d, Rna.FLAG_DEMUX_FAILED);
5551 12 Aug 19 nicklas 567                 flaggedDna.add(d);
5551 12 Aug 19 nicklas 568                 numFlaggedDna++;
5551 12 Aug 19 nicklas 569               }
5551 12 Aug 19 nicklas 570             }
5551 12 Aug 19 nicklas 571           
4597 27 Sep 17 nicklas 572           }
2367 17 Apr 14 nicklas 573         }
2367 17 Apr 14 nicklas 574         
5551 12 Aug 19 nicklas 575         if (pipeline == Pipeline.RNA_SEQ)
2367 17 Apr 14 nicklas 576         {
5551 12 Aug 19 nicklas 577           if (numHisatAlign > 0)
5551 12 Aug 19 nicklas 578           {
5551 12 Aug 19 nicklas 579             jsonMessages.add(numHisatAlign + " libraries confirmed for Hisat alignment");
5551 12 Aug 19 nicklas 580           }
5551 12 Aug 19 nicklas 581           else
5551 12 Aug 19 nicklas 582           {
5551 12 Aug 19 nicklas 583             jsonMessages.add("No libraries confirmed for Hisat alignment");
5551 12 Aug 19 nicklas 584           }
6809 24 Aug 22 nicklas 585           if (numHisat2023Align > 0)
6809 24 Aug 22 nicklas 586           {
6809 24 Aug 22 nicklas 587             jsonMessages.add(numHisat2023Align + " libraries confirmed for Hisat/2023 alignment");
6809 24 Aug 22 nicklas 588           }
6809 24 Aug 22 nicklas 589           else
6809 24 Aug 22 nicklas 590           {
6809 24 Aug 22 nicklas 591             jsonMessages.add("No libraries confirmed for Hisat/2023 alignment");
6809 24 Aug 22 nicklas 592           }
5551 12 Aug 19 nicklas 593           if (numLegacyAlign > 0)
5551 12 Aug 19 nicklas 594           {
5551 12 Aug 19 nicklas 595             jsonMessages.add(numLegacyAlign + " libraries confirmed for legacy alignment");
5551 12 Aug 19 nicklas 596           }
5551 12 Aug 19 nicklas 597           else
5551 12 Aug 19 nicklas 598           {
5551 12 Aug 19 nicklas 599             jsonMessages.add("No libraries confirmed for legacy alignment");
5551 12 Aug 19 nicklas 600           }
2367 17 Apr 14 nicklas 601         }
5551 12 Aug 19 nicklas 602         else if (pipeline == Pipeline.MIPS)
2367 17 Apr 14 nicklas 603         {
5788 13 Dec 19 nicklas 604           if (numMipsAlign > 0)
5551 12 Aug 19 nicklas 605           {
5788 13 Dec 19 nicklas 606             jsonMessages.add(numMipsAlign + " libraries confirmed for MIPs alignment");
5551 12 Aug 19 nicklas 607           }
5551 12 Aug 19 nicklas 608           else
5551 12 Aug 19 nicklas 609           {
5788 13 Dec 19 nicklas 610             jsonMessages.add("No libraries confirmed for MIPs alignment");
5788 13 Dec 19 nicklas 611           }
2367 17 Apr 14 nicklas 612         }
5551 12 Aug 19 nicklas 613         
5384 26 Apr 19 nicklas 614         if (numConfirmed > 0)
5384 26 Apr 19 nicklas 615         {
5580 20 Aug 19 nicklas 616           ActivityDef.DEMUX_CONFIRMED.merge(dc, pipeline.getName(), numConfirmed);
5384 26 Apr 19 nicklas 617         }
2636 04 Sep 14 nicklas 618         if (numDeleted > 0)
2636 04 Sep 14 nicklas 619         {
2636 04 Sep 14 nicklas 620           jsonMessages.add(numDeleted + " libraries deleted due to failure");
2683 18 Sep 14 nicklas 621           if (toDelete.size() > numDeleted)
2683 18 Sep 14 nicklas 622           {
2683 18 Sep 14 nicklas 623             jsonMessages.add((toDelete.size() - numDeleted) + " linked items (eg. files) deleted");
2683 18 Sep 14 nicklas 624           }
2636 04 Sep 14 nicklas 625         }
2636 04 Sep 14 nicklas 626         
2367 17 Apr 14 nicklas 627         if (numFlaggedRna > 0)
2367 17 Apr 14 nicklas 628         {
2367 17 Apr 14 nicklas 629           jsonMessages.add("Added " + numFlaggedRna + " RNA items to the '" + flaggedRna.getName() + "' list");
2367 17 Apr 14 nicklas 630         }
5551 12 Aug 19 nicklas 631         if (numFlaggedDna > 0)
5551 12 Aug 19 nicklas 632         {
5551 12 Aug 19 nicklas 633           jsonMessages.add("Added " + numFlaggedDna + " DNA items to the '" + flaggedDna.getName() + "' list");
5551 12 Aug 19 nicklas 634         }
2640 08 Sep 14 nicklas 635         
2640 08 Sep 14 nicklas 636         if (numReProcess > 0)
2640 08 Sep 14 nicklas 637         {
2640 08 Sep 14 nicklas 638           jsonMessages.add(numReProcess + " sequencing runs flagged for re-demuxing");
2640 08 Sep 14 nicklas 639         }
2367 17 Apr 14 nicklas 640         dc.commit();
2636 04 Sep 14 nicklas 641         
2636 04 Sep 14 nicklas 642         if (toDelete.size() > 0)
2636 04 Sep 14 nicklas 643         {
2636 04 Sep 14 nicklas 644           try
2636 04 Sep 14 nicklas 645           {
2636 04 Sep 14 nicklas 646             Trashcan.delete(sc, toDelete, false, null);
2636 04 Sep 14 nicklas 647           }
2636 04 Sep 14 nicklas 648           catch (RuntimeException ex)
2636 04 Sep 14 nicklas 649           {
2636 04 Sep 14 nicklas 650             ex.printStackTrace();
3482 09 Sep 15 nicklas 651             jsonMessages.add("[Error]Could not delete all items created by failed jobs: " + ex.getMessage());
2636 04 Sep 14 nicklas 652           }
2636 04 Sep 14 nicklas 653         }
2367 17 Apr 14 nicklas 654       }
2265 05 Mar 14 nicklas 655       
2265 05 Mar 14 nicklas 656       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 657       CounterService.getInstance().setForceCount();
2265 05 Mar 14 nicklas 658     }
2265 05 Mar 14 nicklas 659     catch (Throwable t)
2265 05 Mar 14 nicklas 660     {
2265 05 Mar 14 nicklas 661       t.printStackTrace();
2265 05 Mar 14 nicklas 662       json.clear();
2265 05 Mar 14 nicklas 663       json.put("status", "error");
2265 05 Mar 14 nicklas 664       json.put("message", t.getMessage());
2265 05 Mar 14 nicklas 665       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2265 05 Mar 14 nicklas 666     }
2265 05 Mar 14 nicklas 667     finally
2265 05 Mar 14 nicklas 668     {
2265 05 Mar 14 nicklas 669       if (dc != null) dc.close();
2265 05 Mar 14 nicklas 670       json.writeJSONString(resp.getWriter());
2265 05 Mar 14 nicklas 671     }
2265 05 Mar 14 nicklas 672     
2265 05 Mar 14 nicklas 673   }
2265 05 Mar 14 nicklas 674   
2265 05 Mar 14 nicklas 675   
2265 05 Mar 14 nicklas 676   private void loadSequencingRunInfoForDemux(DbControl dc, SequencingRun run)
2265 05 Mar 14 nicklas 677   {
2436 19 May 14 nicklas 678     FlowCell fc = FlowCell.getBySequencingRun(dc, run);
2436 19 May 14 nicklas 679     PhysicalBioAssay pba = fc.getItem();
2436 19 May 14 nicklas 680     String flowCellType = (String)Annotationtype.FLOWCELL_TYPE.getAnnotationValue(dc, pba);
2436 19 May 14 nicklas 681     
5479 10 Jun 19 nicklas 682     run.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
2640 08 Sep 14 nicklas 683     run.loadAnnotations(dc, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
2265 05 Mar 14 nicklas 684     run.loadAnnotations(dc, "SequencingStart", Annotationtype.SEQUENCING_START, Reggie.CONVERTER_DATE_TO_STRING);
2265 05 Mar 14 nicklas 685     run.loadAnnotations(dc, "SequencingEnd", Annotationtype.SEQUENCING_END, Reggie.CONVERTER_DATETIME_TO_STRING);
2265 05 Mar 14 nicklas 686     run.loadAnnotations(dc, "HiSeqPosition", Annotationtype.HISEQ_POSITION, null);
2265 05 Mar 14 nicklas 687     run.loadAnnotations(dc, "SequencingResult", Annotationtype.SEQUENCING_RESULT, null);
6214 15 Apr 21 nicklas 688     run.loadAnnotations(dc, "DataFilesFolder", Annotationtype.DATA_FILES_FOLDER, null);
2265 05 Mar 14 nicklas 689     String seqCycles = (String)Annotationtype.SEQUENCING_CYCLES.getAnnotationValue(dc, run.getDerivedBioAssay());
5545 06 Aug 19 nicklas 690     Pipeline pipeline = Pipeline.getByName((String)Annotationtype.PIPELINE.getAnnotationValue(dc, run.getItem()));
2265 05 Mar 14 nicklas 691     run.setAnnotation("SequencingCycles", seqCycles);
5545 06 Aug 19 nicklas 692     run.setAnnotation("ReadString", sequencingCycles2ReadStringPicard(seqCycles, pipeline, flowCellType));
2265 05 Mar 14 nicklas 693     run.setAnnotation("comments", run.getDerivedBioAssay().getDescription());
2265 05 Mar 14 nicklas 694       
2265 05 Mar 14 nicklas 695     fc.loadAnnotations(dc, "FlowCellID", Annotationtype.FLOWCELL_ID, null);
2320 04 Apr 14 nicklas 696     fc.loadAnnotations(dc, "FailedLanes", Annotationtype.FAILED_LANES, null);
2648 10 Sep 14 nicklas 697     fc.loadAnnotations(dc, "FlowCellType", Annotationtype.FLOWCELL_TYPE, null);
2265 05 Mar 14 nicklas 698     fc.setAnnotation("numLanes", pba.getSize());
2265 05 Mar 14 nicklas 699     fc.setAnnotation("comments", pba.getDescription());
2265 05 Mar 14 nicklas 700     
2265 05 Mar 14 nicklas 701     // Load pools on flow cell
2265 05 Mar 14 nicklas 702     List<PooledLibrary> pools = PooledLibrary.getByFlowCell(dc, fc);
4985 28 Sep 18 nicklas 703     Set<Library> libs = new TreeSet<>(new ReggieItem.NameComparator());
4985 28 Sep 18 nicklas 704
2265 05 Mar 14 nicklas 705     JSONArray jsonPools = new JSONArray();
2265 05 Mar 14 nicklas 706     for (PooledLibrary pool : pools)
2265 05 Mar 14 nicklas 707     {
4985 28 Sep 18 nicklas 708       libs.addAll(Library.getByPool(dc, pool));
5526 25 Jun 19 nicklas 709       pool.setAnnotation("libPlates", SequencingRunServlet.loadLibPlates(dc, pool));
2265 05 Mar 14 nicklas 710       jsonPools.add(pool.asJSONObject());
2265 05 Mar 14 nicklas 711     }
2265 05 Mar 14 nicklas 712     fc.setAnnotation("pools", jsonPools);
4985 28 Sep 18 nicklas 713     
4985 28 Sep 18 nicklas 714     JSONArray jsonLibs = new JSONArray();
4985 28 Sep 18 nicklas 715     for (Library lib : libs)
4985 28 Sep 18 nicklas 716     {
4985 28 Sep 18 nicklas 717       lib.loadDoNotUseAnnotations(dc, null);
4985 28 Sep 18 nicklas 718       jsonLibs.add(lib.asJSONObject());
4985 28 Sep 18 nicklas 719     }
4985 28 Sep 18 nicklas 720     fc.setAnnotation("libraries", jsonLibs);
4985 28 Sep 18 nicklas 721     
2265 05 Mar 14 nicklas 722     run.setAnnotation("flowCell", fc.asJSONObject());
2265 05 Mar 14 nicklas 723   }
2333 08 Apr 14 nicklas 724   
2683 18 Sep 14 nicklas 725   
5545 06 Aug 19 nicklas 726   public static String sequencingCycles2ReadStringPicard(String sequencingCycles, Pipeline pipeline, String flowCellType)
2429 16 May 14 nicklas 727   {
2429 16 May 14 nicklas 728     StringBuilder readString = new StringBuilder();
2429 16 May 14 nicklas 729     try
2429 16 May 14 nicklas 730     {
2429 16 May 14 nicklas 731       Integer[] cycles = Values.getInt(sequencingCycles.split("-"));
5478 10 Jun 19 nicklas 732       if (cycles.length == 3 || cycles.length == 4)
2429 16 May 14 nicklas 733       {
5478 10 Jun 19 nicklas 734         int read1 = 0;
5478 10 Jun 19 nicklas 735         int read2 = cycles.length - 1;
5478 10 Jun 19 nicklas 736         int index1 = 1;
5478 10 Jun 19 nicklas 737         int index2 = cycles.length == 4 ? 2 : -1;
5478 10 Jun 19 nicklas 738         
5545 06 Aug 19 nicklas 739         if (pipeline == Pipeline.RNA_SEQ)
2429 16 May 14 nicklas 740         {
5862 11 Mar 20 nicklas 741           boolean skipLastInBarcode = FlowCell.FLOW_CELL_TYPE_HISEQ.equals(flowCellType);
5862 11 Mar 20 nicklas 742           
5545 06 Aug 19 nicklas 743           // First value converted to (x-1)T1S
5545 06 Aug 19 nicklas 744           if (cycles[read1] > 0) readString.append(cycles[read1]-1).append("T1S");
5545 06 Aug 19 nicklas 745
5862 11 Mar 20 nicklas 746           // Second value converted to (x-1)B1S (HiSeq) or (x)B (NextSeq,NovaSeq)
5545 06 Aug 19 nicklas 747           if (cycles[index1] > 0)
2436 19 May 14 nicklas 748           {
5862 11 Mar 20 nicklas 749             if (skipLastInBarcode)
5545 06 Aug 19 nicklas 750             {
5862 11 Mar 20 nicklas 751               readString.append(cycles[index1]-1).append("B1S");
5545 06 Aug 19 nicklas 752             }
5545 06 Aug 19 nicklas 753             else
5545 06 Aug 19 nicklas 754             {
5862 11 Mar 20 nicklas 755               readString.append(cycles[index1]).append("B");
5545 06 Aug 19 nicklas 756             }
2436 19 May 14 nicklas 757           }
5545 06 Aug 19 nicklas 758           if (index2 > 0 && cycles[index2] > 0)
2436 19 May 14 nicklas 759           {
5862 11 Mar 20 nicklas 760             if (skipLastInBarcode)
5545 06 Aug 19 nicklas 761             {
5862 11 Mar 20 nicklas 762               readString.append(cycles[index2]-1).append("B1S");
5545 06 Aug 19 nicklas 763             }
5545 06 Aug 19 nicklas 764             else
5545 06 Aug 19 nicklas 765             {
5862 11 Mar 20 nicklas 766               readString.append(cycles[index2]).append("B");
5545 06 Aug 19 nicklas 767             }
2436 19 May 14 nicklas 768           }
5545 06 Aug 19 nicklas 769           
5545 06 Aug 19 nicklas 770           // Last value converted to (x-1)T1S
5545 06 Aug 19 nicklas 771           if (cycles[read2] > 0) readString.append(cycles[read2]-1).append("T1S");
2429 16 May 14 nicklas 772         }
5545 06 Aug 19 nicklas 773         else if (pipeline == Pipeline.MIPS)
5478 10 Jun 19 nicklas 774         {
5545 06 Aug 19 nicklas 775           // First read converted to 4M(x-4)T
5545 06 Aug 19 nicklas 776           if (cycles[read1] > 0) readString.append("4M").append(cycles[read1]-4).append("T");
5545 06 Aug 19 nicklas 777           
5545 06 Aug 19 nicklas 778           // Index1 and Index2 are left as they are
5545 06 Aug 19 nicklas 779           if (cycles[index1] > 0) readString.append(cycles[index1]).append("B");
5545 06 Aug 19 nicklas 780           if (index2 > 0 && cycles[index2] > 0) readString.append(cycles[index2]).append("B");
5545 06 Aug 19 nicklas 781
5545 06 Aug 19 nicklas 782           // Last read converted to 4M(x-4)T
5545 06 Aug 19 nicklas 783           if (cycles[read2] > 0) readString.append("4M").append(cycles[read2]-4).append("T");
5478 10 Jun 19 nicklas 784         }
2429 16 May 14 nicklas 785         
2429 16 May 14 nicklas 786       }
2429 16 May 14 nicklas 787     }
2429 16 May 14 nicklas 788     catch (RuntimeException ex)
2429 16 May 14 nicklas 789     {
2429 16 May 14 nicklas 790       // Could not convert to read string 
2429 16 May 14 nicklas 791       readString.setLength(0);
2429 16 May 14 nicklas 792     }
2429 16 May 14 nicklas 793     return readString.length() == 0 ? null : readString.toString();
2429 16 May 14 nicklas 794   }
2365 16 Apr 14 nicklas 795   
2265 05 Mar 14 nicklas 796 }