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

Code
Comments
Other
Rev Date Author Line
7084 28 Mar 23 nicklas 1 package net.sf.basedb.reggie.servlet;
7084 28 Mar 23 nicklas 2
7084 28 Mar 23 nicklas 3 import java.io.IOException;
7084 28 Mar 23 nicklas 4 import java.util.ArrayList;
7084 28 Mar 23 nicklas 5 import java.util.List;
7084 28 Mar 23 nicklas 6
7084 28 Mar 23 nicklas 7 import javax.servlet.ServletException;
7084 28 Mar 23 nicklas 8 import javax.servlet.http.HttpServlet;
7084 28 Mar 23 nicklas 9 import javax.servlet.http.HttpServletRequest;
7084 28 Mar 23 nicklas 10 import javax.servlet.http.HttpServletResponse;
7084 28 Mar 23 nicklas 11
7084 28 Mar 23 nicklas 12 import org.json.simple.JSONArray;
7084 28 Mar 23 nicklas 13 import org.json.simple.JSONObject;
7084 28 Mar 23 nicklas 14
7084 28 Mar 23 nicklas 15 import net.sf.basedb.core.ItemList;
7084 28 Mar 23 nicklas 16 import net.sf.basedb.core.ItemNotFoundException;
7092 04 Apr 23 nicklas 17 import net.sf.basedb.core.BasicItem;
7084 28 Mar 23 nicklas 18 import net.sf.basedb.core.DbControl;
7084 28 Mar 23 nicklas 19 import net.sf.basedb.core.DerivedBioAssay;
7092 04 Apr 23 nicklas 20 import net.sf.basedb.core.Item;
7084 28 Mar 23 nicklas 21 import net.sf.basedb.core.ItemQuery;
7084 28 Mar 23 nicklas 22 import net.sf.basedb.core.Job;
7084 28 Mar 23 nicklas 23 import net.sf.basedb.core.Protocol;
7084 28 Mar 23 nicklas 24 import net.sf.basedb.core.SessionControl;
7084 28 Mar 23 nicklas 25 import net.sf.basedb.core.Software;
7092 04 Apr 23 nicklas 26 import net.sf.basedb.core.Trashcan;
7092 04 Apr 23 nicklas 27 import net.sf.basedb.core.query.Annotations;
7092 04 Apr 23 nicklas 28 import net.sf.basedb.core.query.Expressions;
7084 28 Mar 23 nicklas 29 import net.sf.basedb.core.query.Hql;
7084 28 Mar 23 nicklas 30 import net.sf.basedb.core.query.Orders;
7092 04 Apr 23 nicklas 31 import net.sf.basedb.core.query.Restrictions;
7084 28 Mar 23 nicklas 32 import net.sf.basedb.core.snapshot.SnapshotManager;
7084 28 Mar 23 nicklas 33 import net.sf.basedb.opengrid.JobDefinition;
7084 28 Mar 23 nicklas 34 import net.sf.basedb.opengrid.OpenGridCluster;
7084 28 Mar 23 nicklas 35 import net.sf.basedb.opengrid.service.OpenGridService;
7084 28 Mar 23 nicklas 36 import net.sf.basedb.reggie.JsonUtil;
7084 28 Mar 23 nicklas 37 import net.sf.basedb.reggie.Reggie;
7092 04 Apr 23 nicklas 38 import net.sf.basedb.reggie.activity.ActivityDef;
7119 19 Apr 23 nicklas 39 import net.sf.basedb.reggie.autoconfirm.BwaMem2AutoConfirmer;
7084 28 Mar 23 nicklas 40 import net.sf.basedb.reggie.counter.CounterService;
7092 04 Apr 23 nicklas 41 import net.sf.basedb.reggie.dao.AlignedSequences;
7084 28 Mar 23 nicklas 42 import net.sf.basedb.reggie.dao.Annotationtype;
7084 28 Mar 23 nicklas 43 import net.sf.basedb.reggie.dao.BiomaterialList;
7084 28 Mar 23 nicklas 44 import net.sf.basedb.reggie.dao.MergedSequences;
7294 23 Aug 23 nicklas 45 import net.sf.basedb.reggie.dao.Pipeline;
7084 28 Mar 23 nicklas 46 import net.sf.basedb.reggie.dao.ReggieRole;
7084 28 Mar 23 nicklas 47 import net.sf.basedb.reggie.dao.Subtype;
7084 28 Mar 23 nicklas 48 import net.sf.basedb.reggie.grid.BwaMem2AlignJobCreator;
7084 28 Mar 23 nicklas 49 import net.sf.basedb.reggie.grid.ScriptUtil;
7084 28 Mar 23 nicklas 50 import net.sf.basedb.util.Values;
7084 28 Mar 23 nicklas 51 import net.sf.basedb.util.error.ThrowableUtil;
7084 28 Mar 23 nicklas 52
7084 28 Mar 23 nicklas 53
7084 28 Mar 23 nicklas 54 public class BwaMem2Servlet 
7084 28 Mar 23 nicklas 55   extends HttpServlet 
7084 28 Mar 23 nicklas 56 {
7084 28 Mar 23 nicklas 57   
7092 04 Apr 23 nicklas 58   private static final long serialVersionUID = -4743785040225824571L;
7092 04 Apr 23 nicklas 59
7084 28 Mar 23 nicklas 60   public BwaMem2Servlet()
7084 28 Mar 23 nicklas 61   {}
7084 28 Mar 23 nicklas 62
7084 28 Mar 23 nicklas 63   @Override
7084 28 Mar 23 nicklas 64   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
7084 28 Mar 23 nicklas 65     throws ServletException, IOException 
7084 28 Mar 23 nicklas 66   {
7084 28 Mar 23 nicklas 67     String cmd = req.getParameter("cmd");
7084 28 Mar 23 nicklas 68     JsonUtil.setJsonResponseHeaders(resp);
7084 28 Mar 23 nicklas 69     
7084 28 Mar 23 nicklas 70     JSONObject json = new JSONObject();
7084 28 Mar 23 nicklas 71     json.put("status", "ok");
7084 28 Mar 23 nicklas 72     
7084 28 Mar 23 nicklas 73     final SessionControl sc = Reggie.getSessionControl(req);
7084 28 Mar 23 nicklas 74     DbControl dc = null;
7084 28 Mar 23 nicklas 75     try
7084 28 Mar 23 nicklas 76     {
7084 28 Mar 23 nicklas 77       if ("GetMergedSequencesForBwaMem2".equals(cmd))
7084 28 Mar 23 nicklas 78       {
7084 28 Mar 23 nicklas 79         dc = sc.newDbControl(":Start Bwa-mem2 alignment");
7084 28 Mar 23 nicklas 80         
7084 28 Mar 23 nicklas 81         List<MergedSequences> list = null;
7084 28 Mar 23 nicklas 82         String items = Values.getStringOrNull(req.getParameter("items"));
7084 28 Mar 23 nicklas 83         if (items == null)
7084 28 Mar 23 nicklas 84         {
7084 28 Mar 23 nicklas 85           // Load items from the pipeline list
7084 28 Mar 23 nicklas 86           ItemList bwaMemPipeline = BiomaterialList.BWA_MEM2_PIPELINE.load(dc);
7084 28 Mar 23 nicklas 87           ItemQuery<DerivedBioAssay> query = bwaMemPipeline.getMembers();
7084 28 Mar 23 nicklas 88           query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
7084 28 Mar 23 nicklas 89           Subtype.MERGED_SEQUENCES.addFilter(dc, query);
7084 28 Mar 23 nicklas 90           query.order(Orders.asc(Hql.property("name")));
7084 28 Mar 23 nicklas 91           query.setMaxResults(250);
7084 28 Mar 23 nicklas 92           list = MergedSequences.toList(query.list(dc));
7084 28 Mar 23 nicklas 93         }
7084 28 Mar 23 nicklas 94         else
7084 28 Mar 23 nicklas 95         {
7084 28 Mar 23 nicklas 96           Integer[] ids = Values.getInt(items.split(","));
7084 28 Mar 23 nicklas 97           list = new ArrayList<MergedSequences>();
7084 28 Mar 23 nicklas 98           for (Integer id : ids)
7084 28 Mar 23 nicklas 99           {
7084 28 Mar 23 nicklas 100             list.add(MergedSequences.getById(dc, id));
7084 28 Mar 23 nicklas 101           }
7084 28 Mar 23 nicklas 102         }
7084 28 Mar 23 nicklas 103
7084 28 Mar 23 nicklas 104         SnapshotManager manager = new SnapshotManager();
7084 28 Mar 23 nicklas 105         JSONArray jsonMergedSequences = new JSONArray();
7084 28 Mar 23 nicklas 106         for (MergedSequences ms : list)
7084 28 Mar 23 nicklas 107         {
7084 28 Mar 23 nicklas 108           ms.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
7084 28 Mar 23 nicklas 109           ms.loadAnnotations(dc, manager, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
7084 28 Mar 23 nicklas 110           ms.loadDoNotUseAnnotations(dc, manager);
7084 28 Mar 23 nicklas 111           jsonMergedSequences.add(ms.asJSONObject());
7084 28 Mar 23 nicklas 112         }
7084 28 Mar 23 nicklas 113         json.put("mergedSequences", jsonMergedSequences);        
7084 28 Mar 23 nicklas 114       }
7092 04 Apr 23 nicklas 115       else if ("GetUnconfirmedAlignedSequences".equals(cmd))
7092 04 Apr 23 nicklas 116       {
7092 04 Apr 23 nicklas 117         dc = sc.newDbControl(":Confirm Bwa-mem2 alignment");
7092 04 Apr 23 nicklas 118         ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
7092 04 Apr 23 nicklas 119         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
7092 04 Apr 23 nicklas 120         Subtype.ALIGNED_SEQUENCES.addFilter(dc, query);
7092 04 Apr 23 nicklas 121         // The job must be a BwaMem2Align job and it must be ended
7092 04 Apr 23 nicklas 122         query.join(Hql.innerJoin("job", "jb"));
7092 04 Apr 23 nicklas 123         Subtype.BWA_MEM2_ALIGN_JOB.addFilter(dc, query, "jb");
7092 04 Apr 23 nicklas 124         query.restrict(Restrictions.neq(Hql.property("jb", "ended"), null));
7092 04 Apr 23 nicklas 125         // Must NOT have a ANALYSIS_RESULT annotation
7092 04 Apr 23 nicklas 126         query.join(Annotations.leftJoin(null, Annotationtype.ANALYSIS_RESULT.load(dc), "ar"));
7092 04 Apr 23 nicklas 127         query.restrict(Restrictions.eq(Hql.alias("ar"), null));
7092 04 Apr 23 nicklas 128         // Ignore if AutoProcess==AutoConfirm
7092 04 Apr 23 nicklas 129         query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
7092 04 Apr 23 nicklas 130         query.restrict(
7092 04 Apr 23 nicklas 131             Restrictions.or(
7092 04 Apr 23 nicklas 132               Restrictions.neq(Hql.alias("ap"), Expressions.string("AutoConfirm")),
7092 04 Apr 23 nicklas 133               Restrictions.eq(Hql.alias("ap"), null)
7092 04 Apr 23 nicklas 134             ));
7092 04 Apr 23 nicklas 135         query.order(Orders.asc(Hql.property("name")));
7092 04 Apr 23 nicklas 136         query.setMaxResults(250);
7092 04 Apr 23 nicklas 137         
7092 04 Apr 23 nicklas 138         SnapshotManager manager = new SnapshotManager();
7092 04 Apr 23 nicklas 139         JSONArray jsonAligned = new JSONArray();
7092 04 Apr 23 nicklas 140         List<AlignedSequences> list = AlignedSequences.toList(query.list(dc));
7092 04 Apr 23 nicklas 141         for (AlignedSequences aligned : list)
7092 04 Apr 23 nicklas 142         {
7092 04 Apr 23 nicklas 143           aligned.loadAnnotations(dc, manager, "PIPELINE", Annotationtype.PIPELINE, null);
7092 04 Apr 23 nicklas 144           aligned.loadAnnotations(dc, manager, "ALIGNED_PAIRS", Annotationtype.ALIGNED_PAIRS, null);
7114 14 Apr 23 nicklas 145           aligned.loadAnnotations(dc, manager, "PF_BASES", Annotationtype.PF_BASES, null);
7114 14 Apr 23 nicklas 146           aligned.loadAnnotations(dc, manager, "PF_Q30_BASES", Annotationtype.PF_Q30_BASES, null);
7114 14 Apr 23 nicklas 147           aligned.loadAnnotations(dc, manager, "ALIGNED_BASES", Annotationtype.ALIGNED_BASES, null);
7092 04 Apr 23 nicklas 148           aligned.loadAnnotations(dc, manager, "READ_PAIRS_EXAMINED", Annotationtype.READ_PAIRS_EXAMINED, null);
7092 04 Apr 23 nicklas 149           aligned.loadAnnotations(dc, manager, "READ_PAIR_DUPLICATES", Annotationtype.READ_PAIR_DUPLICATES, null);
7092 04 Apr 23 nicklas 150           aligned.loadAnnotations(dc, manager, "FRACTION_DUPLICATION", Annotationtype.FRACTION_DUPLICATION, null);
7114 14 Apr 23 nicklas 151           aligned.loadAnnotations(dc, manager, "FRACTION_OPTICAL_DUPLICATION", Annotationtype.FRACTION_OPTICAL_DUPLICATION, null);
7114 14 Apr 23 nicklas 152           aligned.loadAnnotations(dc, manager, "MEAN_COVERAGE", Annotationtype.MEAN_COVERAGE, null);
7092 04 Apr 23 nicklas 153           aligned.loadAnnotations(dc, manager, "QC_GENOTYPE_COUNT", Annotationtype.QC_GENOTYPE_COUNT, null);
7092 04 Apr 23 nicklas 154           aligned.loadAnnotations(dc, manager, "QC_GENOTYPE_HET_PCT", Annotationtype.QC_GENOTYPE_HET_PCT, null);
7092 04 Apr 23 nicklas 155           aligned.loadAnnotations(dc, manager, "DataFilesFolder", Annotationtype.DATA_FILES_FOLDER, null);
7092 04 Apr 23 nicklas 156           
7092 04 Apr 23 nicklas 157           aligned.setAnnotation("qcVcf", JsonUtil.loadLinkedItem(dc, aligned.getItem(), "qc_genotype.vcf", Item.FILE, null));
7092 04 Apr 23 nicklas 158           
7092 04 Apr 23 nicklas 159           // Load job information
7092 04 Apr 23 nicklas 160           Job job = aligned.getItem().getJob();
7092 04 Apr 23 nicklas 161           JSONObject jsonJob = JsonUtil.getJobAsJSON(job);
7092 04 Apr 23 nicklas 162           aligned.setAnnotation("job", jsonJob);
7092 04 Apr 23 nicklas 163
7092 04 Apr 23 nicklas 164           boolean debug = Boolean.TRUE.equals(job.getParameterValue("debug"));
7092 04 Apr 23 nicklas 165           jsonJob.put("debug", debug);
7092 04 Apr 23 nicklas 166           
7092 04 Apr 23 nicklas 167           // Load merged information
7092 04 Apr 23 nicklas 168           MergedSequences merged = aligned.getMergedSequences(dc);
7092 04 Apr 23 nicklas 169           merged.loadAnnotations(dc, manager, "READS", Annotationtype.READS, null);
7092 04 Apr 23 nicklas 170           aligned.setAnnotation("merged", merged.asJSONObject());
7092 04 Apr 23 nicklas 171           
7092 04 Apr 23 nicklas 172           jsonAligned.add(aligned.asJSONObject());
7092 04 Apr 23 nicklas 173         }
7092 04 Apr 23 nicklas 174         
7119 19 Apr 23 nicklas 175         JSONObject jsonLimits = new JSONObject();
7119 19 Apr 23 nicklas 176         jsonLimits.put("minAlignedPairs", BwaMem2AutoConfirmer.MIN_ALIGNED_PAIRS);
7119 19 Apr 23 nicklas 177         jsonLimits.put("minMeanCoverage", BwaMem2AutoConfirmer.MIN_MEAN_COVERAGE);
7119 19 Apr 23 nicklas 178         jsonLimits.put("maxFractionDuplication", BwaMem2AutoConfirmer.MAX_FRACTION_DUPLICATION);
7119 19 Apr 23 nicklas 179         jsonLimits.put("maxHetPct", BwaMem2AutoConfirmer.MAX_HET_PERCENTAGE);
7119 19 Apr 23 nicklas 180         json.put("limits", jsonLimits);
7119 19 Apr 23 nicklas 181
7092 04 Apr 23 nicklas 182         json.put("alignedSequences", jsonAligned);
7092 04 Apr 23 nicklas 183       }
7084 28 Mar 23 nicklas 184     }
7084 28 Mar 23 nicklas 185     catch (Throwable t)
7084 28 Mar 23 nicklas 186     {
7084 28 Mar 23 nicklas 187       t.printStackTrace();
7084 28 Mar 23 nicklas 188       json.clear();
7084 28 Mar 23 nicklas 189       json.put("status", "error");
7084 28 Mar 23 nicklas 190       json.put("message", t.getMessage());
7084 28 Mar 23 nicklas 191       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
7084 28 Mar 23 nicklas 192     }
7084 28 Mar 23 nicklas 193     finally
7084 28 Mar 23 nicklas 194     {
7084 28 Mar 23 nicklas 195       if (dc != null) dc.close();
7084 28 Mar 23 nicklas 196       json.writeJSONString(resp.getWriter());
7084 28 Mar 23 nicklas 197     }
7084 28 Mar 23 nicklas 198     
7084 28 Mar 23 nicklas 199   }
7084 28 Mar 23 nicklas 200
7084 28 Mar 23 nicklas 201   @Override
7084 28 Mar 23 nicklas 202   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
7084 28 Mar 23 nicklas 203     throws ServletException, IOException 
7084 28 Mar 23 nicklas 204   {
7084 28 Mar 23 nicklas 205     String cmd = req.getParameter("cmd");
7084 28 Mar 23 nicklas 206     JsonUtil.setJsonResponseHeaders(resp);
7084 28 Mar 23 nicklas 207     
7084 28 Mar 23 nicklas 208     JSONObject json = new JSONObject();
7084 28 Mar 23 nicklas 209     json.put("status", "ok");
7084 28 Mar 23 nicklas 210     JSONArray jsonMessages = new JSONArray();
7084 28 Mar 23 nicklas 211   
7084 28 Mar 23 nicklas 212     final SessionControl sc = Reggie.getSessionControl(req);
7084 28 Mar 23 nicklas 213     DbControl dc = null;
7084 28 Mar 23 nicklas 214     try
7084 28 Mar 23 nicklas 215     {
7084 28 Mar 23 nicklas 216       if ("StartBwaMem2Alignment".equals(cmd))
7084 28 Mar 23 nicklas 217       {
7084 28 Mar 23 nicklas 218         dc = sc.newDbControl(":Start Bwa-mem2 alignment");
7084 28 Mar 23 nicklas 219
7084 28 Mar 23 nicklas 220         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
7084 28 Mar 23 nicklas 221
7084 28 Mar 23 nicklas 222         JSONObject jsonReq = JsonUtil.parseRequest(req);
7084 28 Mar 23 nicklas 223         JSONArray jsonMerged = (JSONArray)jsonReq.get("mergedSequences");
7084 28 Mar 23 nicklas 224         
7084 28 Mar 23 nicklas 225         Number alignSoftwareId = (Number)jsonReq.get("alignSoftware");
7084 28 Mar 23 nicklas 226         Number alignProtocolId = (Number)jsonReq.get("alignProtocol");
7084 28 Mar 23 nicklas 227         
7084 28 Mar 23 nicklas 228         String clusterId = (String)jsonReq.get("cluster");
7084 28 Mar 23 nicklas 229         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
7084 28 Mar 23 nicklas 230         boolean autoConfirm = Boolean.TRUE.equals(jsonReq.get("autoConfirm"));
7084 28 Mar 23 nicklas 231         Number priority = (Number)jsonReq.get("priority");
7084 28 Mar 23 nicklas 232         String partition = Values.getStringOrNull((String)jsonReq.get("partition"));
7372 06 Oct 23 nicklas 233         String submitOptions = Values.getStringOrNull((String)jsonReq.get("submitOptionsOverride"));
7084 28 Mar 23 nicklas 234
7084 28 Mar 23 nicklas 235         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
7084 28 Mar 23 nicklas 236         if (cluster == null)
7084 28 Mar 23 nicklas 237         {
7084 28 Mar 23 nicklas 238           throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
7084 28 Mar 23 nicklas 239         }
7084 28 Mar 23 nicklas 240         
7084 28 Mar 23 nicklas 241         // Load common items
7084 28 Mar 23 nicklas 242         Software alignSoftware = alignSoftwareId != null ? Software.getById(dc, alignSoftwareId.intValue()) : null;
7084 28 Mar 23 nicklas 243         Protocol alignProtocol = alignProtocolId != null ? Protocol.getById(dc, alignProtocolId.intValue()) : null;
7084 28 Mar 23 nicklas 244
7084 28 Mar 23 nicklas 245         // Align job creator implementation
7084 28 Mar 23 nicklas 246         BwaMem2AlignJobCreator jobCreator = new BwaMem2AlignJobCreator();
7084 28 Mar 23 nicklas 247         jobCreator.setAutoConfirm(autoConfirm);
7084 28 Mar 23 nicklas 248         jobCreator.setDebug(debug);
7084 28 Mar 23 nicklas 249         jobCreator.setPriority(priority == null ? null : priority.intValue());
7084 28 Mar 23 nicklas 250         jobCreator.setPartition(partition);
7084 28 Mar 23 nicklas 251         jobCreator.setAlignProtocol(alignProtocol);
7084 28 Mar 23 nicklas 252         jobCreator.setAlignSoftware(alignSoftware);
7372 06 Oct 23 nicklas 253         jobCreator.setSubmitOptionsOverride(submitOptions);
7084 28 Mar 23 nicklas 254
7084 28 Mar 23 nicklas 255         List<MergedSequences> mergedSequences = new ArrayList<MergedSequences>();
7084 28 Mar 23 nicklas 256         for (int mgNo = 0; mgNo < jsonMerged.size(); mgNo++)
7084 28 Mar 23 nicklas 257         {
7084 28 Mar 23 nicklas 258           JSONObject jsonMg = (JSONObject)jsonMerged.get(mgNo);
7084 28 Mar 23 nicklas 259           Number mergedId = (Number)jsonMg.get("id");
7084 28 Mar 23 nicklas 260         
7084 28 Mar 23 nicklas 261           MergedSequences ms = MergedSequences.getById(dc, mergedId.intValue());
7084 28 Mar 23 nicklas 262           mergedSequences.add(ms);
7084 28 Mar 23 nicklas 263
7084 28 Mar 23 nicklas 264           // Reset AUTO_PROCESSING annotation
7084 28 Mar 23 nicklas 265           DerivedBioAssay merged = ms.getDerivedBioAssay();
7084 28 Mar 23 nicklas 266           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, merged, null);
7084 28 Mar 23 nicklas 267         }
7084 28 Mar 23 nicklas 268         
7084 28 Mar 23 nicklas 269         List<JobDefinition> jobDefs = jobCreator.createBwaMem2AlignJobs(dc, cluster, mergedSequences);
7084 28 Mar 23 nicklas 270         List<Job> jobs = ScriptUtil.submitJobs(dc, cluster, jobDefs);
7084 28 Mar 23 nicklas 271         for (Job job : jobs)
7084 28 Mar 23 nicklas 272         {
7084 28 Mar 23 nicklas 273           if (job.getStatus() == Job.Status.ERROR)
7084 28 Mar 23 nicklas 274           {
7084 28 Mar 23 nicklas 275             jsonMessages.add("[Error]Job submission for '" + job.getName() + "' failed: " + job.getStatusMessage());
7084 28 Mar 23 nicklas 276           }
7084 28 Mar 23 nicklas 277           else
7084 28 Mar 23 nicklas 278           {
7084 28 Mar 23 nicklas 279             jsonMessages.add("Submitted Bwa-mem2 alignment job to " + cluster.getConnectionInfo().getName() + " with id " + job.getExternalId());
7084 28 Mar 23 nicklas 280           }
7084 28 Mar 23 nicklas 281         }
7084 28 Mar 23 nicklas 282         dc.commit();
7084 28 Mar 23 nicklas 283       }
7092 04 Apr 23 nicklas 284       else if ("RegisterBwaMem2Alignment".equals(cmd))
7092 04 Apr 23 nicklas 285       {
7092 04 Apr 23 nicklas 286         dc = sc.newDbControl(":Confirm Bwa-mem2 alignment");
7084 28 Mar 23 nicklas 287
7092 04 Apr 23 nicklas 288         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
7092 04 Apr 23 nicklas 289
7092 04 Apr 23 nicklas 290         JSONObject jsonReq = JsonUtil.parseRequest(req);
7092 04 Apr 23 nicklas 291         JSONArray jsonAlignedSequences = (JSONArray)jsonReq.get("alignedSequences");
7092 04 Apr 23 nicklas 292
7092 04 Apr 23 nicklas 293         int numConfirmed = 0;
7092 04 Apr 23 nicklas 294         int numUnconfirmed = 0;
7092 04 Apr 23 nicklas 295         int numRealign = 0;
7092 04 Apr 23 nicklas 296         int numDeleted = 0;
7092 04 Apr 23 nicklas 297         
7092 04 Apr 23 nicklas 298         ItemList bwaMem2Pipeline = BiomaterialList.BWA_MEM2_PIPELINE.load(dc);
7294 23 Aug 23 nicklas 299         ItemList ascatPipeline = BiomaterialList.ASCAT_PIPELINE.load(dc);
7398 07 Nov 23 nicklas 300         ItemList wgsVariantCallingPipeline = BiomaterialList.WGS_VARIANT_CALLING_PIPELINE.load(dc);
7092 04 Apr 23 nicklas 301         boolean deleteItemsCreatedByFailedJobs = Boolean.TRUE.equals(jsonReq.get("deleteItemsCreatedByFailedJobs"));
7092 04 Apr 23 nicklas 302         List<BasicItem> toDelete = new ArrayList<BasicItem>();
7092 04 Apr 23 nicklas 303         
7092 04 Apr 23 nicklas 304         for (int alignNo = 0; alignNo < jsonAlignedSequences.size(); ++alignNo)
7092 04 Apr 23 nicklas 305         {
7092 04 Apr 23 nicklas 306           JSONObject jsonAligned = (JSONObject)jsonAlignedSequences.get(alignNo);
7092 04 Apr 23 nicklas 307           Number alignedId = (Number)jsonAligned.get("id");
7092 04 Apr 23 nicklas 308           
7092 04 Apr 23 nicklas 309           AlignedSequences aligned = AlignedSequences.getById(dc, alignedId.intValue());
7092 04 Apr 23 nicklas 310           MergedSequences merged = aligned.getMergedSequences(dc);
7092 04 Apr 23 nicklas 311           
7092 04 Apr 23 nicklas 312           DerivedBioAssay alignedDBA = aligned.getDerivedBioAssay();
7092 04 Apr 23 nicklas 313           Job alignJob = alignedDBA.getJob();
7092 04 Apr 23 nicklas 314           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, alignedDBA, null);
7092 04 Apr 23 nicklas 315           
7092 04 Apr 23 nicklas 316           String comment = Values.getStringOrNull((String)jsonAligned.get("comment"));
7092 04 Apr 23 nicklas 317           alignedDBA.setDescription(comment);
7092 04 Apr 23 nicklas 318           
7092 04 Apr 23 nicklas 319           boolean reAlign = Boolean.TRUE.equals(jsonAligned.get("realign"));
7119 19 Apr 23 nicklas 320           boolean confirm = Boolean.TRUE.equals(jsonAligned.get("confirm"));
7092 04 Apr 23 nicklas 321           boolean deleted = false;
7092 04 Apr 23 nicklas 322
7092 04 Apr 23 nicklas 323           if (reAlign)
7092 04 Apr 23 nicklas 324           {
7092 04 Apr 23 nicklas 325             // Set ReProcess annotation on merged sequences item
7092 04 Apr 23 nicklas 326             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, merged.getItem(), "ReProcess");
7092 04 Apr 23 nicklas 327             bwaMem2Pipeline.add(merged.getItem());
7092 04 Apr 23 nicklas 328             numRealign++;
7092 04 Apr 23 nicklas 329             
7092 04 Apr 23 nicklas 330             if (deleteItemsCreatedByFailedJobs && alignJob.getStatus() == Job.Status.ERROR)
7092 04 Apr 23 nicklas 331             {
7092 04 Apr 23 nicklas 332               // Delete aligned sequences items
7092 04 Apr 23 nicklas 333               alignedDBA.setRemoved(true);
7092 04 Apr 23 nicklas 334               toDelete.add(alignedDBA);
7092 04 Apr 23 nicklas 335               toDelete.addAll(Reggie.removeAttachedFiles(dc, alignedDBA));
7092 04 Apr 23 nicklas 336               
7092 04 Apr 23 nicklas 337               deleted = true;
7092 04 Apr 23 nicklas 338               numDeleted++;
7092 04 Apr 23 nicklas 339               if (comment != null)
7092 04 Apr 23 nicklas 340               {
7092 04 Apr 23 nicklas 341                 merged.getItem().setDescription(comment);
7092 04 Apr 23 nicklas 342               }
7092 04 Apr 23 nicklas 343             }
7092 04 Apr 23 nicklas 344           }
7092 04 Apr 23 nicklas 345           
7092 04 Apr 23 nicklas 346           if (!deleted)
7092 04 Apr 23 nicklas 347           {
7119 19 Apr 23 nicklas 348             if (confirm)
7092 04 Apr 23 nicklas 349             {
7092 04 Apr 23 nicklas 350               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, alignedDBA, AlignedSequences.ALIGN_SUCCESSFUL);
7398 07 Nov 23 nicklas 351               // If it is a tumor item add it to the ASCAT and WGS variant calling pipelines
7398 07 Nov 23 nicklas 352               // but note that they also requires a paired normal
7294 23 Aug 23 nicklas 353               Pipeline pipeline = Pipeline.getByName((String)Annotationtype.PIPELINE.getAnnotationValue(dc, alignedDBA));
7294 23 Aug 23 nicklas 354               if (pipeline == Pipeline.DNA_TUMOR_WGS)
7294 23 Aug 23 nicklas 355               {
7294 23 Aug 23 nicklas 356                 ascatPipeline.add(alignedDBA);
7398 07 Nov 23 nicklas 357                 wgsVariantCallingPipeline.add(alignedDBA);
7294 23 Aug 23 nicklas 358               }
7092 04 Apr 23 nicklas 359               numConfirmed++;
7092 04 Apr 23 nicklas 360             }
7092 04 Apr 23 nicklas 361             else if (reAlign)
7092 04 Apr 23 nicklas 362             {
7092 04 Apr 23 nicklas 363               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, alignedDBA, AlignedSequences.ALIGN_FAILED);
7092 04 Apr 23 nicklas 364             }
7092 04 Apr 23 nicklas 365             else
7092 04 Apr 23 nicklas 366             {
7092 04 Apr 23 nicklas 367               numUnconfirmed++;
7092 04 Apr 23 nicklas 368             }
7092 04 Apr 23 nicklas 369           }
7092 04 Apr 23 nicklas 370         }
7092 04 Apr 23 nicklas 371         
7092 04 Apr 23 nicklas 372         if (numConfirmed > 0)
7092 04 Apr 23 nicklas 373         {
7119 19 Apr 23 nicklas 374           jsonMessages.add(numConfirmed + " alignments confirmed");
7092 04 Apr 23 nicklas 375           ActivityDef.BWA_MEM2_CONFIRMED.merge(dc, numConfirmed);
7092 04 Apr 23 nicklas 376         }
7092 04 Apr 23 nicklas 377         else
7092 04 Apr 23 nicklas 378         {
7119 19 Apr 23 nicklas 379           jsonMessages.add("No alignments confirmed");
7092 04 Apr 23 nicklas 380         }
7092 04 Apr 23 nicklas 381         
7092 04 Apr 23 nicklas 382         if (numRealign > 0)
7092 04 Apr 23 nicklas 383         {
7119 19 Apr 23 nicklas 384           jsonMessages.add(numRealign + " items flagged for re-alignment");
7092 04 Apr 23 nicklas 385         }
7092 04 Apr 23 nicklas 386         
7092 04 Apr 23 nicklas 387         if (numDeleted > 0)
7092 04 Apr 23 nicklas 388         {
7119 19 Apr 23 nicklas 389           jsonMessages.add(numDeleted + " items deleted due to failure");
7092 04 Apr 23 nicklas 390           if (toDelete.size() > numDeleted)
7092 04 Apr 23 nicklas 391           {
7092 04 Apr 23 nicklas 392             jsonMessages.add((toDelete.size() - numDeleted) + " linked items (eg. files) deleted");
7092 04 Apr 23 nicklas 393           }
7092 04 Apr 23 nicklas 394         }
7092 04 Apr 23 nicklas 395         
7092 04 Apr 23 nicklas 396         if (numUnconfirmed > 0)
7092 04 Apr 23 nicklas 397         {
7119 19 Apr 23 nicklas 398           jsonMessages.add(numUnconfirmed + " items remain unconfirmed");
7092 04 Apr 23 nicklas 399         }
7092 04 Apr 23 nicklas 400         dc.commit();
7092 04 Apr 23 nicklas 401         
7092 04 Apr 23 nicklas 402         if (toDelete.size() > 0)
7092 04 Apr 23 nicklas 403         {
7092 04 Apr 23 nicklas 404           try
7092 04 Apr 23 nicklas 405           {
7092 04 Apr 23 nicklas 406             Trashcan.delete(sc, toDelete, false, null);
7092 04 Apr 23 nicklas 407           }
7092 04 Apr 23 nicklas 408           catch (RuntimeException ex)
7092 04 Apr 23 nicklas 409           {
7092 04 Apr 23 nicklas 410             ex.printStackTrace();
7092 04 Apr 23 nicklas 411             jsonMessages.add("[Error]Could not delete all items created by failed jobs: " + ex.getMessage());
7092 04 Apr 23 nicklas 412           }
7092 04 Apr 23 nicklas 413         }
7092 04 Apr 23 nicklas 414       }
7092 04 Apr 23 nicklas 415
7084 28 Mar 23 nicklas 416       json.put("messages", jsonMessages);
7084 28 Mar 23 nicklas 417       CounterService.getInstance().setForceCount();
7084 28 Mar 23 nicklas 418     }
7084 28 Mar 23 nicklas 419     catch (Throwable t)
7084 28 Mar 23 nicklas 420     {
7084 28 Mar 23 nicklas 421       t.printStackTrace();
7084 28 Mar 23 nicklas 422       json.clear();
7084 28 Mar 23 nicklas 423       json.put("status", "error");
7084 28 Mar 23 nicklas 424       json.put("message", t.getMessage());
7084 28 Mar 23 nicklas 425       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
7084 28 Mar 23 nicklas 426     }
7084 28 Mar 23 nicklas 427     finally
7084 28 Mar 23 nicklas 428     {
7084 28 Mar 23 nicklas 429       if (dc != null) dc.close();
7084 28 Mar 23 nicklas 430       json.writeJSONString(resp.getWriter());
7084 28 Mar 23 nicklas 431     }
7084 28 Mar 23 nicklas 432     
7084 28 Mar 23 nicklas 433   }
7084 28 Mar 23 nicklas 434   
7084 28 Mar 23 nicklas 435   
7084 28 Mar 23 nicklas 436
7084 28 Mar 23 nicklas 437 }