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

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