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

Code
Comments
Other
Rev Date Author Line
2721 01 Oct 14 nicklas 1 package net.sf.basedb.reggie.servlet;
2721 01 Oct 14 nicklas 2
2721 01 Oct 14 nicklas 3 import java.io.IOException;
2826 16 Oct 14 nicklas 4 import java.util.ArrayList;
2721 01 Oct 14 nicklas 5 import java.util.List;
2721 01 Oct 14 nicklas 6
2721 01 Oct 14 nicklas 7 import javax.servlet.ServletException;
2721 01 Oct 14 nicklas 8 import javax.servlet.http.HttpServlet;
2721 01 Oct 14 nicklas 9 import javax.servlet.http.HttpServletRequest;
2721 01 Oct 14 nicklas 10 import javax.servlet.http.HttpServletResponse;
2721 01 Oct 14 nicklas 11
2721 01 Oct 14 nicklas 12 import org.json.simple.JSONArray;
2721 01 Oct 14 nicklas 13 import org.json.simple.JSONObject;
2721 01 Oct 14 nicklas 14
6819 26 Aug 22 nicklas 15 import net.sf.basedb.core.Annotatable;
2722 02 Oct 14 nicklas 16 import net.sf.basedb.core.ArrayDesign;
2826 16 Oct 14 nicklas 17 import net.sf.basedb.core.BasicItem;
3247 14 Apr 15 nicklas 18 import net.sf.basedb.core.ItemList;
2721 01 Oct 14 nicklas 19 import net.sf.basedb.core.DbControl;
2721 01 Oct 14 nicklas 20 import net.sf.basedb.core.DerivedBioAssay;
2721 01 Oct 14 nicklas 21 import net.sf.basedb.core.Extract;
4540 22 Jun 17 nicklas 22 import net.sf.basedb.core.File;
2721 01 Oct 14 nicklas 23 import net.sf.basedb.core.ItemNotFoundException;
2721 01 Oct 14 nicklas 24 import net.sf.basedb.core.ItemQuery;
2721 01 Oct 14 nicklas 25 import net.sf.basedb.core.Job;
2722 02 Oct 14 nicklas 26 import net.sf.basedb.core.Project;
2721 01 Oct 14 nicklas 27 import net.sf.basedb.core.Protocol;
2721 01 Oct 14 nicklas 28 import net.sf.basedb.core.RawBioAssay;
2721 01 Oct 14 nicklas 29 import net.sf.basedb.core.SessionControl;
2721 01 Oct 14 nicklas 30 import net.sf.basedb.core.Software;
2826 16 Oct 14 nicklas 31 import net.sf.basedb.core.Trashcan;
2721 01 Oct 14 nicklas 32 import net.sf.basedb.core.query.Annotations;
2721 01 Oct 14 nicklas 33 import net.sf.basedb.core.query.Expressions;
2721 01 Oct 14 nicklas 34 import net.sf.basedb.core.query.Hql;
2721 01 Oct 14 nicklas 35 import net.sf.basedb.core.query.Orders;
2721 01 Oct 14 nicklas 36 import net.sf.basedb.core.query.Restrictions;
3768 24 Feb 16 nicklas 37 import net.sf.basedb.core.snapshot.SnapshotManager;
4599 28 Sep 17 nicklas 38 import net.sf.basedb.opengrid.JobDefinition;
4306 17 Jan 17 nicklas 39 import net.sf.basedb.opengrid.OpenGridCluster;
4306 17 Jan 17 nicklas 40 import net.sf.basedb.opengrid.service.OpenGridService;
2721 01 Oct 14 nicklas 41 import net.sf.basedb.reggie.JsonUtil;
2721 01 Oct 14 nicklas 42 import net.sf.basedb.reggie.Reggie;
5384 26 Apr 19 nicklas 43 import net.sf.basedb.reggie.activity.ActivityDef;
4547 29 Jun 17 nicklas 44 import net.sf.basedb.reggie.autoconfirm.CufflinksAutoConfirmer;
3059 19 Dec 14 nicklas 45 import net.sf.basedb.reggie.counter.CounterService;
2721 01 Oct 14 nicklas 46 import net.sf.basedb.reggie.dao.AlignedSequences;
2721 01 Oct 14 nicklas 47 import net.sf.basedb.reggie.dao.Annotationtype;
2722 02 Oct 14 nicklas 48 import net.sf.basedb.reggie.dao.Arraydesign;
2826 16 Oct 14 nicklas 49 import net.sf.basedb.reggie.dao.BiomaterialList;
4540 22 Jun 17 nicklas 50 import net.sf.basedb.reggie.dao.Datafiletype;
2721 01 Oct 14 nicklas 51 import net.sf.basedb.reggie.dao.Library;
4542 26 Jun 17 nicklas 52 import net.sf.basedb.reggie.dao.MaskedSequences;
4542 26 Jun 17 nicklas 53 import net.sf.basedb.reggie.dao.MergedSequences;
5543 06 Aug 19 nicklas 54 import net.sf.basedb.reggie.dao.Pipeline;
2826 16 Oct 14 nicklas 55 import net.sf.basedb.reggie.dao.Rawbioassay;
4665 31 Jan 18 nicklas 56 import net.sf.basedb.reggie.dao.Rawdatatype;
2721 01 Oct 14 nicklas 57 import net.sf.basedb.reggie.dao.ReggieRole;
2826 16 Oct 14 nicklas 58 import net.sf.basedb.reggie.dao.Rna;
2721 01 Oct 14 nicklas 59 import net.sf.basedb.reggie.dao.Subtype;
4306 17 Jan 17 nicklas 60 import net.sf.basedb.reggie.grid.CufflinksJobCreator;
4599 28 Sep 17 nicklas 61 import net.sf.basedb.reggie.grid.ScriptUtil;
2721 01 Oct 14 nicklas 62 import net.sf.basedb.util.Values;
2721 01 Oct 14 nicklas 63 import net.sf.basedb.util.error.ThrowableUtil;
6819 26 Aug 22 nicklas 64 import net.sf.basedb.util.filter.Filter;
2721 01 Oct 14 nicklas 65
2721 01 Oct 14 nicklas 66
2721 01 Oct 14 nicklas 67 public class CufflinksServlet 
2721 01 Oct 14 nicklas 68   extends HttpServlet 
2721 01 Oct 14 nicklas 69 {
2721 01 Oct 14 nicklas 70
2721 01 Oct 14 nicklas 71   private static final long serialVersionUID = -8731078921144280328L;
2721 01 Oct 14 nicklas 72   
2721 01 Oct 14 nicklas 73   public CufflinksServlet()
2721 01 Oct 14 nicklas 74   {}
2721 01 Oct 14 nicklas 75
2721 01 Oct 14 nicklas 76   @SuppressWarnings("unchecked")
2721 01 Oct 14 nicklas 77   @Override
2721 01 Oct 14 nicklas 78   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
2721 01 Oct 14 nicklas 79     throws ServletException, IOException 
2721 01 Oct 14 nicklas 80   {
2721 01 Oct 14 nicklas 81     String cmd = req.getParameter("cmd");
2721 01 Oct 14 nicklas 82     JsonUtil.setJsonResponseHeaders(resp);
2721 01 Oct 14 nicklas 83     
2721 01 Oct 14 nicklas 84     JSONObject json = new JSONObject();
2721 01 Oct 14 nicklas 85     json.put("status", "ok");
2721 01 Oct 14 nicklas 86     
3975 26 May 16 nicklas 87     final SessionControl sc = Reggie.getSessionControl(req);
2721 01 Oct 14 nicklas 88     DbControl dc = null;
2721 01 Oct 14 nicklas 89     try
2721 01 Oct 14 nicklas 90     {
4547 29 Jun 17 nicklas 91       if ("GetMergedSequencesForAlignment".equals(cmd))
2721 01 Oct 14 nicklas 92       {
6335 15 Jun 21 nicklas 93         dc = sc.newDbControl(":Start Tophat and Cufflinks");
4596 27 Sep 17 nicklas 94         
4990 01 Oct 18 nicklas 95         List<MergedSequences> list = null;
4990 01 Oct 18 nicklas 96         String items = Values.getStringOrNull(req.getParameter("items"));
4990 01 Oct 18 nicklas 97         if (items == null)
4990 01 Oct 18 nicklas 98         {
4990 01 Oct 18 nicklas 99           ItemList legacyPipeline = BiomaterialList.LEGACY_PIPELINE.load(dc);
6183 26 Mar 21 nicklas 100           ItemQuery<DerivedBioAssay> query = legacyPipeline.getMembers();
4990 01 Oct 18 nicklas 101           query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
4990 01 Oct 18 nicklas 102           Subtype.MERGED_SEQUENCES.addFilter(dc, query);
4990 01 Oct 18 nicklas 103           // Join LibPlate for sorting
4990 01 Oct 18 nicklas 104           query.join(Hql.innerJoin(null, "extract", "lib", true));
4990 01 Oct 18 nicklas 105           query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
4990 01 Oct 18 nicklas 106           query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
4990 01 Oct 18 nicklas 107           query.order(Orders.asc(Hql.property("libPlate", "id")));
4990 01 Oct 18 nicklas 108           query.order(Orders.asc(Hql.property("name")));
4990 01 Oct 18 nicklas 109           query.setMaxResults(250);
4990 01 Oct 18 nicklas 110           list = MergedSequences.toList(query.list(dc));
4990 01 Oct 18 nicklas 111         }
4990 01 Oct 18 nicklas 112         else
4990 01 Oct 18 nicklas 113         {
4990 01 Oct 18 nicklas 114           Integer[] ids = Values.getInt(items.split(","));
4990 01 Oct 18 nicklas 115           list = new ArrayList<MergedSequences>();
4990 01 Oct 18 nicklas 116           for (Integer id : ids)
4990 01 Oct 18 nicklas 117           {
4990 01 Oct 18 nicklas 118             list.add(MergedSequences.getById(dc, id));
4990 01 Oct 18 nicklas 119           }
4990 01 Oct 18 nicklas 120         }
4990 01 Oct 18 nicklas 121
3768 24 Feb 16 nicklas 122         SnapshotManager manager = new SnapshotManager();
4547 29 Jun 17 nicklas 123         JSONArray jsonMergedSequences = new JSONArray();
4547 29 Jun 17 nicklas 124         for (MergedSequences ms : list)
2721 01 Oct 14 nicklas 125         {
5542 05 Aug 19 nicklas 126           ms.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
4547 29 Jun 17 nicklas 127           Library lib = ms.getLibrary(dc);
4547 29 Jun 17 nicklas 128           lib.loadBioPlateLocation();
3768 24 Feb 16 nicklas 129           Rna r = lib.getRna(dc, true);
5542 05 Aug 19 nicklas 130           if (r != null) lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
4547 29 Jun 17 nicklas 131           ms.setAnnotation("lib", lib.asJSONObject());
4547 29 Jun 17 nicklas 132           ms.loadAnnotations(dc, manager, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
4990 01 Oct 18 nicklas 133           ms.loadDoNotUseAnnotations(dc, manager);
4547 29 Jun 17 nicklas 134           jsonMergedSequences.add(ms.asJSONObject());
2721 01 Oct 14 nicklas 135         }
2721 01 Oct 14 nicklas 136         
4547 29 Jun 17 nicklas 137         json.put("mergedSequences", jsonMergedSequences);
2721 01 Oct 14 nicklas 138       }
2722 02 Oct 14 nicklas 139       else if ("GetArrayDesigns".equals(cmd))
2722 02 Oct 14 nicklas 140       {
2722 02 Oct 14 nicklas 141         /*
2722 02 Oct 14 nicklas 142           Get information array designs.
2722 02 Oct 14 nicklas 143         */
6335 15 Jun 21 nicklas 144         dc = sc.newDbControl(":Start Tophat and Cufflinks");
2722 02 Oct 14 nicklas 145         
6819 26 Aug 22 nicklas 146         Filter<Annotatable> pipelineFilter = Annotationtype.PIPELINE.createFilter(Pipeline.RNASEQ_LEGACY.getName());
6819 26 Aug 22 nicklas 147         List<Arraydesign> designs = Arraydesign.findByPlatformVariant(dc, Rawdatatype.CUFFLINKS.getVariantId(), pipelineFilter);
2722 02 Oct 14 nicklas 148         Project project = sc.getActiveProjectId() == 0 ? null : Project.getById(dc, sc.getActiveProjectId());
2722 02 Oct 14 nicklas 149         
2722 02 Oct 14 nicklas 150         int defaultDesignId = -1;
2722 02 Oct 14 nicklas 151         JSONObject jsonDefault = null;
2722 02 Oct 14 nicklas 152         JSONArray jsonDesigns = new JSONArray();
2722 02 Oct 14 nicklas 153         for (Arraydesign design : designs)
2722 02 Oct 14 nicklas 154         {
2722 02 Oct 14 nicklas 155           ArrayDesign ad = design.getItem();
2722 02 Oct 14 nicklas 156           if (ad.getNumDbFeatures() == 0) continue; // Skip array designs with no features
2722 02 Oct 14 nicklas 157           
2722 02 Oct 14 nicklas 158           JSONObject jsonDesign = design.asJSONObject();
2722 02 Oct 14 nicklas 159           jsonDesigns.add(jsonDesign);
2722 02 Oct 14 nicklas 160           
2722 02 Oct 14 nicklas 161           // Check if the current design is the latest default design
2722 02 Oct 14 nicklas 162           if (project != null && project.isDefaultItem(ad) && ad.getId() > defaultDesignId)
2722 02 Oct 14 nicklas 163           {
2722 02 Oct 14 nicklas 164             defaultDesignId = ad.getId();
2722 02 Oct 14 nicklas 165             jsonDefault = jsonDesign;
2722 02 Oct 14 nicklas 166           }
2722 02 Oct 14 nicklas 167         }
2721 01 Oct 14 nicklas 168
2722 02 Oct 14 nicklas 169         if (jsonDefault != null)
2722 02 Oct 14 nicklas 170         {
2722 02 Oct 14 nicklas 171           jsonDefault.put("isDefault", true);
2722 02 Oct 14 nicklas 172         }
2722 02 Oct 14 nicklas 173         json.put("arrayDesigns", jsonDesigns);
2722 02 Oct 14 nicklas 174       }
2826 16 Oct 14 nicklas 175       else if ("GetUnconfirmed".equals(cmd))
2826 16 Oct 14 nicklas 176       {
6335 15 Jun 21 nicklas 177         dc = sc.newDbControl(":Confirm Cufflinks");
2826 16 Oct 14 nicklas 178         ItemQuery<RawBioAssay> query = RawBioAssay.getQuery();
2826 16 Oct 14 nicklas 179         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
4667 01 Feb 18 nicklas 180         Rawdatatype.CUFFLINKS.addFilter(dc, query);
5546 07 Aug 19 nicklas 181         Pipeline.RNASEQ_LEGACY.addFilter(dc, query);
2826 16 Oct 14 nicklas 182         
2826 16 Oct 14 nicklas 183         // The job must be ended
2826 16 Oct 14 nicklas 184         query.join(Hql.innerJoin("job", "jb"));
2826 16 Oct 14 nicklas 185         query.restrict(Restrictions.neq(Hql.property("jb", "ended"), null));
2826 16 Oct 14 nicklas 186         // Must NOT have a ANALYSIS_RESULT annotation
2826 16 Oct 14 nicklas 187         query.join(Annotations.leftJoin(null, Annotationtype.ANALYSIS_RESULT.load(dc), "ar"));
2826 16 Oct 14 nicklas 188         query.restrict(Restrictions.eq(Hql.alias("ar"), null));
3032 12 Dec 14 nicklas 189         // Ignore if AutoProcess==AutoConfirm
3032 12 Dec 14 nicklas 190         query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
3032 12 Dec 14 nicklas 191         query.restrict(
3032 12 Dec 14 nicklas 192             Restrictions.or(
3032 12 Dec 14 nicklas 193               Restrictions.neq(Hql.alias("ap"), Expressions.string("AutoConfirm")),
3032 12 Dec 14 nicklas 194               Restrictions.eq(Hql.alias("ap"), null)
3032 12 Dec 14 nicklas 195             ));
4677 08 Feb 18 nicklas 196         // Join LibPlate for sorting
4677 08 Feb 18 nicklas 197         query.join(Hql.innerJoin(null, "parentExtract", "lib", true));
4677 08 Feb 18 nicklas 198         query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
4677 08 Feb 18 nicklas 199         query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
4677 08 Feb 18 nicklas 200         query.order(Orders.asc(Hql.property("libPlate", "id")));
2826 16 Oct 14 nicklas 201         query.order(Orders.asc(Hql.property("name")));
4677 08 Feb 18 nicklas 202         query.setMaxResults(250);
4677 08 Feb 18 nicklas 203       
3768 24 Feb 16 nicklas 204         SnapshotManager manager = new SnapshotManager();
2826 16 Oct 14 nicklas 205         List<Rawbioassay> list = Rawbioassay.toList(query.list(dc));
2826 16 Oct 14 nicklas 206         JSONArray jsonRawBioAssays = new JSONArray();
2826 16 Oct 14 nicklas 207         for (Rawbioassay raw : list)
2826 16 Oct 14 nicklas 208         {
2826 16 Oct 14 nicklas 209           RawBioAssay rba = raw.getItem();
3768 24 Feb 16 nicklas 210           raw.loadAnnotations(dc, manager, "DataFilesFolder", Annotationtype.DATA_FILES_FOLDER, null);
4540 22 Jun 17 nicklas 211           File fpkm = raw.getFile(dc, Datafiletype.FPKM);
4540 22 Jun 17 nicklas 212           if (fpkm != null) raw.setAnnotation("fpkmFile", fpkm.getPath().toString());
2826 16 Oct 14 nicklas 213           
2826 16 Oct 14 nicklas 214           // Load job information
2826 16 Oct 14 nicklas 215           Job job = rba.getJob();
4542 26 Jun 17 nicklas 216           JSONObject jsonJob = JsonUtil.getJobAsJSON(job);
4542 26 Jun 17 nicklas 217           raw.setAnnotation("cufflinksJob", jsonJob);
4542 26 Jun 17 nicklas 218           boolean debug = Boolean.TRUE.equals(job.getParameterValue("debug"));
4542 26 Jun 17 nicklas 219           JSONObject jsonLimits = new JSONObject();
4547 29 Jun 17 nicklas 220           jsonLimits.put("minAlignedPairsFlag", debug ? CufflinksAutoConfirmer.MIN_ALIGNED_PAIRS_FLAG_DEBUG : CufflinksAutoConfirmer.MIN_ALIGNED_PAIRS_FLAG);
4547 29 Jun 17 nicklas 221           jsonLimits.put("minAlignedPairsStop", debug ? CufflinksAutoConfirmer.MIN_ALIGNED_PAIRS_STOP_DEBUG : CufflinksAutoConfirmer.MIN_ALIGNED_PAIRS_STOP);
4542 26 Jun 17 nicklas 222           jsonJob.put("limits", jsonLimits);
4542 26 Jun 17 nicklas 223
2826 16 Oct 14 nicklas 224           // Load library information
2826 16 Oct 14 nicklas 225           Library lib = raw.getLibrary(dc);
3768 24 Feb 16 nicklas 226           Rna r = lib.getRna(dc, true);
4540 22 Jun 17 nicklas 227           lib.loadBioPlateLocation();
3768 24 Feb 16 nicklas 228           lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
2826 16 Oct 14 nicklas 229           raw.setAnnotation("lib", lib.asJSONObject());
2826 16 Oct 14 nicklas 230         
4542 26 Jun 17 nicklas 231           // Load aligned information
4542 26 Jun 17 nicklas 232           AlignedSequences aligned = raw.getAlignedSequences(dc);
4542 26 Jun 17 nicklas 233           aligned.loadAnnotations(dc, manager, "ALIGNED_PAIRS", Annotationtype.ALIGNED_PAIRS, null);
4542 26 Jun 17 nicklas 234           aligned.loadAnnotations(dc, manager, "READ_PAIRS_EXAMINED", Annotationtype.READ_PAIRS_EXAMINED, null);
4542 26 Jun 17 nicklas 235           aligned.loadAnnotations(dc, manager, "READ_PAIR_DUPLICATES", Annotationtype.READ_PAIR_DUPLICATES, null);
4542 26 Jun 17 nicklas 236           aligned.loadAnnotations(dc, manager, "FRACTION_DUPLICATION", Annotationtype.FRACTION_DUPLICATION, null);
4542 26 Jun 17 nicklas 237           raw.setAnnotation("aligned", aligned.asJSONObject());
4542 26 Jun 17 nicklas 238
4542 26 Jun 17 nicklas 239           // Load filtered information
4542 26 Jun 17 nicklas 240           MaskedSequences masked = aligned.getMaskedSequences(dc);
4542 26 Jun 17 nicklas 241           masked.loadAnnotations(dc, manager, "PM_READS", Annotationtype.PM_READS, null);
4542 26 Jun 17 nicklas 242           raw.setAnnotation("masked", masked.asJSONObject());
4542 26 Jun 17 nicklas 243           
4542 26 Jun 17 nicklas 244           // Load merged information
4542 26 Jun 17 nicklas 245           MergedSequences merged = masked.getMergedSequences(dc);
4542 26 Jun 17 nicklas 246           merged.loadAnnotations(dc, manager, "PF_READS", Annotationtype.PF_READS, null);
4542 26 Jun 17 nicklas 247           merged.loadAnnotations(dc, manager, "PT_READS", Annotationtype.PT_READS, null);
4542 26 Jun 17 nicklas 248           raw.setAnnotation("merged", merged.asJSONObject());
4542 26 Jun 17 nicklas 249           
2826 16 Oct 14 nicklas 250           jsonRawBioAssays.add(raw.asJSONObject());
2826 16 Oct 14 nicklas 251         }
2826 16 Oct 14 nicklas 252         
2826 16 Oct 14 nicklas 253         json.put("rawBioAssays", jsonRawBioAssays);
2826 16 Oct 14 nicklas 254       }
2807 15 Oct 14 nicklas 255
2721 01 Oct 14 nicklas 256     }
2721 01 Oct 14 nicklas 257     catch (Throwable t)
2721 01 Oct 14 nicklas 258     {
2721 01 Oct 14 nicklas 259       t.printStackTrace();
2721 01 Oct 14 nicklas 260       json.clear();
2721 01 Oct 14 nicklas 261       json.put("status", "error");
2721 01 Oct 14 nicklas 262       json.put("message", t.getMessage());
2721 01 Oct 14 nicklas 263       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2721 01 Oct 14 nicklas 264     }
2721 01 Oct 14 nicklas 265     finally
2721 01 Oct 14 nicklas 266     {
2721 01 Oct 14 nicklas 267       if (dc != null) dc.close();
2721 01 Oct 14 nicklas 268       json.writeJSONString(resp.getWriter());
2721 01 Oct 14 nicklas 269     }
2721 01 Oct 14 nicklas 270     
2721 01 Oct 14 nicklas 271   }
2721 01 Oct 14 nicklas 272
2721 01 Oct 14 nicklas 273   @Override
2721 01 Oct 14 nicklas 274   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
2721 01 Oct 14 nicklas 275     throws ServletException, IOException 
2721 01 Oct 14 nicklas 276   {
2721 01 Oct 14 nicklas 277     String ID = req.getParameter("ID");
2721 01 Oct 14 nicklas 278     String cmd = req.getParameter("cmd");
2721 01 Oct 14 nicklas 279     JsonUtil.setJsonResponseHeaders(resp);
2721 01 Oct 14 nicklas 280     
2721 01 Oct 14 nicklas 281     JSONObject json = new JSONObject();
2721 01 Oct 14 nicklas 282     json.put("status", "ok");
2721 01 Oct 14 nicklas 283     JSONArray jsonMessages = new JSONArray();
2721 01 Oct 14 nicklas 284   
3975 26 May 16 nicklas 285     final SessionControl sc = Reggie.getSessionControl(req);
2721 01 Oct 14 nicklas 286     DbControl dc = null;
2721 01 Oct 14 nicklas 287     try
2721 01 Oct 14 nicklas 288     {
2721 01 Oct 14 nicklas 289
2721 01 Oct 14 nicklas 290       if ("StartCufflinks".equals(cmd))
2721 01 Oct 14 nicklas 291       {
6335 15 Jun 21 nicklas 292         dc = sc.newDbControl(":Start Tophat and Cufflinks");
2721 01 Oct 14 nicklas 293
2721 01 Oct 14 nicklas 294         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
2721 01 Oct 14 nicklas 295
3752 17 Feb 16 nicklas 296         JSONObject jsonReq = JsonUtil.parseRequest(req);
4543 27 Jun 17 nicklas 297         JSONArray jsonMerged = (JSONArray)jsonReq.get("mergedSequences");
2721 01 Oct 14 nicklas 298         
4543 27 Jun 17 nicklas 299         Number maskingSoftwareId = (Number)jsonReq.get("maskingSoftware");
4543 27 Jun 17 nicklas 300         Number maskingProtocolId = (Number)jsonReq.get("maskingProtocol");
4543 27 Jun 17 nicklas 301         Number alignSoftwareId = (Number)jsonReq.get("alignSoftware");
4543 27 Jun 17 nicklas 302         Number alignProtocolId = (Number)jsonReq.get("alignProtocol");
2722 02 Oct 14 nicklas 303         Number arrayDesignId = (Number)jsonReq.get("arrayDesign");
2721 01 Oct 14 nicklas 304         Number cufflinksSoftwareId = (Number)jsonReq.get("cufflinksSoftware");
2721 01 Oct 14 nicklas 305         Number cufflinksProtocolId = (Number)jsonReq.get("cufflinksProtocol");
2721 01 Oct 14 nicklas 306       
2721 01 Oct 14 nicklas 307         String clusterId = (String)jsonReq.get("cluster");
2721 01 Oct 14 nicklas 308         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
3029 11 Dec 14 nicklas 309         boolean autoConfirm = Boolean.TRUE.equals(jsonReq.get("autoConfirm"));
2721 01 Oct 14 nicklas 310         Number priority = (Number)jsonReq.get("priority");
6981 17 Jan 23 nicklas 311         String partition = Values.getStringOrNull((String)jsonReq.get("partition"));
7372 06 Oct 23 nicklas 312         String submitOptions = Values.getStringOrNull((String)jsonReq.get("submitOptionsOverride"));
6981 17 Jan 23 nicklas 313         
4306 17 Jan 17 nicklas 314         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
2721 01 Oct 14 nicklas 315         if (cluster == null)
2721 01 Oct 14 nicklas 316         {
4306 17 Jan 17 nicklas 317           throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
2721 01 Oct 14 nicklas 318         }
3069 09 Jan 15 nicklas 319
2721 01 Oct 14 nicklas 320         // Load common items
4543 27 Jun 17 nicklas 321         Software maskingSoftware = maskingSoftwareId != null ? Software.getById(dc, maskingSoftwareId.intValue()) : null;
4543 27 Jun 17 nicklas 322         Protocol maskingProtocol = maskingProtocolId != null ? Protocol.getById(dc, maskingProtocolId.intValue()) : null;
4543 27 Jun 17 nicklas 323         Software alignSoftware = alignSoftwareId != null ? Software.getById(dc, alignSoftwareId.intValue()) : null;
4543 27 Jun 17 nicklas 324         Protocol alignProtocol = alignProtocolId != null ? Protocol.getById(dc, alignProtocolId.intValue()) : null;
2890 03 Nov 14 nicklas 325         ArrayDesign design = ArrayDesign.getById(dc, arrayDesignId.intValue());
2890 03 Nov 14 nicklas 326         Software cufflinksSoftware = cufflinksSoftwareId != null ? Software.getById(dc, cufflinksSoftwareId.intValue()) : null;
2890 03 Nov 14 nicklas 327         Protocol cufflinksProtocol = cufflinksProtocolId != null ? Protocol.getById(dc, cufflinksProtocolId.intValue()) : null;
3069 09 Jan 15 nicklas 328
3069 09 Jan 15 nicklas 329         // Cufflinks job creator implementation
3069 09 Jan 15 nicklas 330         CufflinksJobCreator jobCreator = new CufflinksJobCreator();
3069 09 Jan 15 nicklas 331         jobCreator.setArrayDesign(design);
3069 09 Jan 15 nicklas 332         jobCreator.setAutoConfirm(autoConfirm);
3069 09 Jan 15 nicklas 333         jobCreator.setDebug(debug);
3069 09 Jan 15 nicklas 334         jobCreator.setPriority(priority == null ? null : priority.intValue());
6981 17 Jan 23 nicklas 335         jobCreator.setPartition(partition);
3069 09 Jan 15 nicklas 336         jobCreator.setProtocol(cufflinksProtocol);
3069 09 Jan 15 nicklas 337         jobCreator.setSoftware(cufflinksSoftware);
4543 27 Jun 17 nicklas 338         jobCreator.setMaskingProtocol(maskingProtocol);
4543 27 Jun 17 nicklas 339         jobCreator.setMaskingSoftware(maskingSoftware);
4543 27 Jun 17 nicklas 340         jobCreator.setAlignProtocol(alignProtocol);
4543 27 Jun 17 nicklas 341         jobCreator.setAlignSoftware(alignSoftware);
7372 06 Oct 23 nicklas 342         jobCreator.setSubmitOptionsOverride(submitOptions);
2721 01 Oct 14 nicklas 343         
4543 27 Jun 17 nicklas 344         List<MergedSequences> mergedSequences = new ArrayList<MergedSequences>();
4543 27 Jun 17 nicklas 345         for (int mgNo = 0; mgNo < jsonMerged.size(); mgNo++)
4543 27 Jun 17 nicklas 346         {
4543 27 Jun 17 nicklas 347           JSONObject jsonMg = (JSONObject)jsonMerged.get(mgNo);
4543 27 Jun 17 nicklas 348           Number mergedId = (Number)jsonMg.get("id");
4543 27 Jun 17 nicklas 349         
4543 27 Jun 17 nicklas 350           MergedSequences ms = MergedSequences.getById(dc, mergedId.intValue());
4543 27 Jun 17 nicklas 351           mergedSequences.add(ms);
4543 27 Jun 17 nicklas 352
4543 27 Jun 17 nicklas 353           // Reset AUTO_PROCESSING annotation
4543 27 Jun 17 nicklas 354           DerivedBioAssay merged = ms.getDerivedBioAssay();
4543 27 Jun 17 nicklas 355           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, merged, null);
4543 27 Jun 17 nicklas 356         }
4548 29 Jun 17 nicklas 357
4599 28 Sep 17 nicklas 358         List<JobDefinition> jobDefs = jobCreator.createTophatCufflinkJobs(dc, cluster, mergedSequences);
4599 28 Sep 17 nicklas 359         List<Job> jobs = ScriptUtil.submitJobs(dc, cluster, jobDefs);
4599 28 Sep 17 nicklas 360         
3069 09 Jan 15 nicklas 361         for (Job job : jobs)
2890 03 Nov 14 nicklas 362         {
3194 19 Mar 15 nicklas 363           if (job.getStatus() == Job.Status.ERROR)
3194 19 Mar 15 nicklas 364           {
3194 19 Mar 15 nicklas 365             jsonMessages.add("[Error]Job submission for '" + job.getName() + "' failed: " + job.getStatusMessage());
3194 19 Mar 15 nicklas 366           }
3194 19 Mar 15 nicklas 367           else
3194 19 Mar 15 nicklas 368           {
4543 27 Jun 17 nicklas 369             jsonMessages.add("Submitted tophat+cufflinks job to " + cluster.getConnectionInfo().getName() + " with id " + job.getExternalId());
3194 19 Mar 15 nicklas 370           }
2721 01 Oct 14 nicklas 371         }
2890 03 Nov 14 nicklas 372         dc.commit();
2721 01 Oct 14 nicklas 373       }
2826 16 Oct 14 nicklas 374       else if ("RegisterCufflinks".equals(cmd))
2826 16 Oct 14 nicklas 375       {
6335 15 Jun 21 nicklas 376         dc = sc.newDbControl(":Confirm Cufflinks");
2721 01 Oct 14 nicklas 377
2826 16 Oct 14 nicklas 378         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
2826 16 Oct 14 nicklas 379
3752 17 Feb 16 nicklas 380         JSONObject jsonReq = JsonUtil.parseRequest(req);
2826 16 Oct 14 nicklas 381         JSONArray jsonRawBioAssays = (JSONArray)jsonReq.get("rawBioAssays");
2826 16 Oct 14 nicklas 382
2826 16 Oct 14 nicklas 383         int numConfirmed = 0;
2826 16 Oct 14 nicklas 384         int numUnconfirmed = 0;
2826 16 Oct 14 nicklas 385         int numReRun = 0;
2826 16 Oct 14 nicklas 386         int numFlaggedRna = 0;
2826 16 Oct 14 nicklas 387         int numDeleted = 0;
2826 16 Oct 14 nicklas 388         
3247 14 Apr 15 nicklas 389         ItemList flaggedRna = BiomaterialList.FLAGGED_RNA.load(dc);
4598 27 Sep 17 nicklas 390         ItemList legacyPipeline = BiomaterialList.LEGACY_PIPELINE.load(dc);
6023 26 Oct 20 nicklas 391         ItemList geneReportList = BiomaterialList.GENE_REPORT_CREATE.load(dc);
6023 26 Oct 20 nicklas 392         ItemList pilotReportList = BiomaterialList.PILOT_REPORT_CREATE.load(dc);
2826 16 Oct 14 nicklas 393         
2826 16 Oct 14 nicklas 394         boolean deleteItemsCreatedByFailedJobs = Boolean.TRUE.equals(jsonReq.get("deleteItemsCreatedByFailedJobs"));
2826 16 Oct 14 nicklas 395         List<BasicItem> toDelete = new ArrayList<BasicItem>();
2826 16 Oct 14 nicklas 396         
2826 16 Oct 14 nicklas 397         for (int rawNo = 0; rawNo < jsonRawBioAssays.size(); ++rawNo)
2826 16 Oct 14 nicklas 398         {
2826 16 Oct 14 nicklas 399           JSONObject jsonRaw = (JSONObject)jsonRawBioAssays.get(rawNo);
2826 16 Oct 14 nicklas 400           Number rawdId = (Number)jsonRaw.get("id");
2826 16 Oct 14 nicklas 401           
2826 16 Oct 14 nicklas 402           Rawbioassay raw = Rawbioassay.getById(dc, rawdId.intValue());
2826 16 Oct 14 nicklas 403           AlignedSequences aligned = raw.getAlignedSequences(dc);
4548 29 Jun 17 nicklas 404           MaskedSequences masked = aligned.getMaskedSequences(dc);
4548 29 Jun 17 nicklas 405           MergedSequences merged = masked.getMergedSequences(dc);
2826 16 Oct 14 nicklas 406           
4548 29 Jun 17 nicklas 407           DerivedBioAssay alignedDBA = aligned.getDerivedBioAssay();
2826 16 Oct 14 nicklas 408           RawBioAssay rba = raw.getRawBioAssay();
2826 16 Oct 14 nicklas 409           Job job = rba.getJob();
3029 11 Dec 14 nicklas 410           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rba, null);
2826 16 Oct 14 nicklas 411           
2826 16 Oct 14 nicklas 412           boolean flag = Boolean.TRUE.equals(jsonRaw.get("flag"));
5553 12 Aug 19 nicklas 413           if (flag && !Reggie.isExternalItem(raw.getName()))
2826 16 Oct 14 nicklas 414           {
2908 07 Nov 14 nicklas 415             Library lib = Library.get(rba.getParentExtract());
3309 06 May 15 nicklas 416             Rna rna = lib.getRna(dc, false);
2908 07 Nov 14 nicklas 417             Extract r = rna.getItem();
2908 07 Nov 14 nicklas 418             Annotationtype.FLAG.setAnnotationValue(dc, r, Rna.FLAG_CUFFLINKS_FAILED);
2908 07 Nov 14 nicklas 419             flaggedRna.add(r);
2826 16 Oct 14 nicklas 420             numFlaggedRna++;
2826 16 Oct 14 nicklas 421           }
2826 16 Oct 14 nicklas 422           
2826 16 Oct 14 nicklas 423           String comment = Values.getStringOrNull((String)jsonRaw.get("comment"));
2826 16 Oct 14 nicklas 424           rba.setDescription(comment);
2826 16 Oct 14 nicklas 425           
2826 16 Oct 14 nicklas 426           boolean reRun = Boolean.TRUE.equals(jsonRaw.get("rerun"));
2878 30 Oct 14 nicklas 427           boolean geneReport = Boolean.TRUE.equals(jsonRaw.get("geneReport"));
2826 16 Oct 14 nicklas 428           boolean deleted = false;
2826 16 Oct 14 nicklas 429
2826 16 Oct 14 nicklas 430           if (reRun)
2826 16 Oct 14 nicklas 431           {
2826 16 Oct 14 nicklas 432             // Set ReProcess annotation on merged sequences item
4598 27 Sep 17 nicklas 433             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, merged.getItem(), "ReProcess");
4598 27 Sep 17 nicklas 434             legacyPipeline.add(merged.getItem());
2826 16 Oct 14 nicklas 435             numReRun++;
2826 16 Oct 14 nicklas 436             
2826 16 Oct 14 nicklas 437             if (deleteItemsCreatedByFailedJobs && job.getStatus() == Job.Status.ERROR)
2826 16 Oct 14 nicklas 438             {
4548 29 Jun 17 nicklas 439               // Delete rawbioassay, masked and aligned sequences items
2826 16 Oct 14 nicklas 440               rba.setRemoved(true);
2826 16 Oct 14 nicklas 441               toDelete.add(rba);
2826 16 Oct 14 nicklas 442               toDelete.addAll(Reggie.removeAttachedFiles(dc, rba));
4548 29 Jun 17 nicklas 443               alignedDBA.setRemoved(true);
4548 29 Jun 17 nicklas 444               toDelete.add(alignedDBA);
4548 29 Jun 17 nicklas 445               toDelete.addAll(Reggie.removeAttachedFiles(dc, alignedDBA));
4548 29 Jun 17 nicklas 446               DerivedBioAssay mask = masked.getItem();
4548 29 Jun 17 nicklas 447               mask.setRemoved(true);
4548 29 Jun 17 nicklas 448               toDelete.add(mask);
4548 29 Jun 17 nicklas 449               toDelete.addAll(Reggie.removeAttachedFiles(dc, mask));
2826 16 Oct 14 nicklas 450               deleted = true;
2826 16 Oct 14 nicklas 451               numDeleted++;
4548 29 Jun 17 nicklas 452               merged.getItem().setDescription(comment);
2826 16 Oct 14 nicklas 453             }
2826 16 Oct 14 nicklas 454           }
2826 16 Oct 14 nicklas 455           
2826 16 Oct 14 nicklas 456           if (!deleted)
2826 16 Oct 14 nicklas 457           {
2878 30 Oct 14 nicklas 458             if (geneReport)
2826 16 Oct 14 nicklas 459             {
4548 29 Jun 17 nicklas 460               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, alignedDBA, AlignedSequences.ALIGN_SUCCESSFUL);
2878 30 Oct 14 nicklas 461               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, rba, Rawbioassay.FEATURE_EXTRACTION_SUCCESSFUL);
6023 26 Oct 20 nicklas 462               geneReportList.add(rba);
6023 26 Oct 20 nicklas 463               pilotReportList.add(rba);
2826 16 Oct 14 nicklas 464               numConfirmed++;
2826 16 Oct 14 nicklas 465             }
2826 16 Oct 14 nicklas 466             else if (reRun || flag)
2826 16 Oct 14 nicklas 467             {
4548 29 Jun 17 nicklas 468               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, alignedDBA, AlignedSequences.ALIGN_FAILED);
2878 30 Oct 14 nicklas 469               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, rba, Rawbioassay.FEATURE_EXTRACTION_FAILED);
2826 16 Oct 14 nicklas 470             }
2826 16 Oct 14 nicklas 471             else
2826 16 Oct 14 nicklas 472             {
2826 16 Oct 14 nicklas 473               numUnconfirmed++;
2826 16 Oct 14 nicklas 474             }
2826 16 Oct 14 nicklas 475           }
2826 16 Oct 14 nicklas 476         }
2826 16 Oct 14 nicklas 477         
2826 16 Oct 14 nicklas 478         if (numConfirmed > 0)
2826 16 Oct 14 nicklas 479         {
2878 30 Oct 14 nicklas 480           jsonMessages.add(numConfirmed + " libraries confirmed for Gene report");
5421 13 May 19 nicklas 481           ActivityDef.LEGACY_PIPELINE_CONFIRMED.merge(dc, numConfirmed);        
2826 16 Oct 14 nicklas 482         }
2826 16 Oct 14 nicklas 483         else
2826 16 Oct 14 nicklas 484         {
2878 30 Oct 14 nicklas 485           jsonMessages.add("No libraries confirmed for Gene report");
2826 16 Oct 14 nicklas 486         }
2826 16 Oct 14 nicklas 487         
2826 16 Oct 14 nicklas 488         if (numReRun > 0)
2826 16 Oct 14 nicklas 489         {
2826 16 Oct 14 nicklas 490           jsonMessages.add(numReRun + " libraries flagged for re-running");
2826 16 Oct 14 nicklas 491         }
2826 16 Oct 14 nicklas 492         
2826 16 Oct 14 nicklas 493         if (numDeleted > 0)
2826 16 Oct 14 nicklas 494         {
2826 16 Oct 14 nicklas 495           jsonMessages.add(numDeleted + " libraries deleted due to failure");
2826 16 Oct 14 nicklas 496           if (toDelete.size() > numDeleted)
2826 16 Oct 14 nicklas 497           {
4548 29 Jun 17 nicklas 498             jsonMessages.add((toDelete.size() - numDeleted) + " linked items (eg. parent items and files) deleted");
2826 16 Oct 14 nicklas 499           }
2826 16 Oct 14 nicklas 500         }
2826 16 Oct 14 nicklas 501         
2826 16 Oct 14 nicklas 502         if (numFlaggedRna > 0)
2826 16 Oct 14 nicklas 503         {
2826 16 Oct 14 nicklas 504           jsonMessages.add("Added " + numFlaggedRna + " RNA items to the '" + flaggedRna.getName() + "' list");
2826 16 Oct 14 nicklas 505         }
2826 16 Oct 14 nicklas 506         
2826 16 Oct 14 nicklas 507         if (numUnconfirmed > 0)
2826 16 Oct 14 nicklas 508         {
2826 16 Oct 14 nicklas 509           jsonMessages.add(numUnconfirmed + " libraries remain unconfirmed");
2826 16 Oct 14 nicklas 510         }
2826 16 Oct 14 nicklas 511         dc.commit();
2826 16 Oct 14 nicklas 512         if (toDelete.size() > 0)
2826 16 Oct 14 nicklas 513         {
2826 16 Oct 14 nicklas 514           try
2826 16 Oct 14 nicklas 515           {
2826 16 Oct 14 nicklas 516             Trashcan.delete(sc, toDelete, false, null);
2826 16 Oct 14 nicklas 517           }
2826 16 Oct 14 nicklas 518           catch (RuntimeException ex)
2826 16 Oct 14 nicklas 519           {
2826 16 Oct 14 nicklas 520             ex.printStackTrace();
3482 09 Sep 15 nicklas 521             jsonMessages.add("[Error]Could not delete all items created by failed jobs: " + ex.getMessage());
2826 16 Oct 14 nicklas 522           }
2826 16 Oct 14 nicklas 523         }
2826 16 Oct 14 nicklas 524       }
2826 16 Oct 14 nicklas 525
2721 01 Oct 14 nicklas 526       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 527       CounterService.getInstance().setForceCount();
2721 01 Oct 14 nicklas 528     }
2721 01 Oct 14 nicklas 529     catch (Throwable t)
2721 01 Oct 14 nicklas 530     {
2721 01 Oct 14 nicklas 531       t.printStackTrace();
2721 01 Oct 14 nicklas 532       json.clear();
2721 01 Oct 14 nicklas 533       json.put("status", "error");
2721 01 Oct 14 nicklas 534       json.put("message", t.getMessage());
2721 01 Oct 14 nicklas 535       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2721 01 Oct 14 nicklas 536     }
2721 01 Oct 14 nicklas 537     finally
2721 01 Oct 14 nicklas 538     {
2721 01 Oct 14 nicklas 539       if (dc != null) dc.close();
2721 01 Oct 14 nicklas 540       json.writeJSONString(resp.getWriter());
2721 01 Oct 14 nicklas 541     }
2721 01 Oct 14 nicklas 542     
2721 01 Oct 14 nicklas 543   }
2721 01 Oct 14 nicklas 544   
2721 01 Oct 14 nicklas 545
2721 01 Oct 14 nicklas 546
2721 01 Oct 14 nicklas 547 }