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

Code
Comments
Other
Rev Date Author Line
5685 25 Oct 19 nicklas 1 package net.sf.basedb.reggie.servlet;
5685 25 Oct 19 nicklas 2
5685 25 Oct 19 nicklas 3 import java.io.IOException;
5730 18 Nov 19 nicklas 4 import java.io.InputStream;
5685 25 Oct 19 nicklas 5 import java.util.ArrayList;
5730 18 Nov 19 nicklas 6 import java.util.Arrays;
5685 25 Oct 19 nicklas 7 import java.util.List;
5730 18 Nov 19 nicklas 8 import java.util.zip.GZIPInputStream;
5685 25 Oct 19 nicklas 9
5685 25 Oct 19 nicklas 10 import javax.servlet.ServletException;
5685 25 Oct 19 nicklas 11 import javax.servlet.http.HttpServlet;
5685 25 Oct 19 nicklas 12 import javax.servlet.http.HttpServletRequest;
5685 25 Oct 19 nicklas 13 import javax.servlet.http.HttpServletResponse;
5685 25 Oct 19 nicklas 14
5685 25 Oct 19 nicklas 15 import org.json.simple.JSONArray;
5685 25 Oct 19 nicklas 16 import org.json.simple.JSONObject;
5685 25 Oct 19 nicklas 17
5685 25 Oct 19 nicklas 18 import net.sf.basedb.core.ItemList;
5685 25 Oct 19 nicklas 19 import net.sf.basedb.core.ItemNotFoundException;
5728 15 Nov 19 nicklas 20 import net.sf.basedb.core.BasicItem;
5685 25 Oct 19 nicklas 21 import net.sf.basedb.core.DbControl;
5685 25 Oct 19 nicklas 22 import net.sf.basedb.core.DerivedBioAssay;
5728 15 Nov 19 nicklas 23 import net.sf.basedb.core.Extract;
5728 15 Nov 19 nicklas 24 import net.sf.basedb.core.File;
5764 29 Nov 19 nicklas 25 import net.sf.basedb.core.Include;
6387 15 Sep 21 nicklas 26 import net.sf.basedb.core.InvalidDataException;
5713 12 Nov 19 nicklas 27 import net.sf.basedb.core.Item;
5685 25 Oct 19 nicklas 28 import net.sf.basedb.core.ItemQuery;
5685 25 Oct 19 nicklas 29 import net.sf.basedb.core.Job;
5728 15 Nov 19 nicklas 30 import net.sf.basedb.core.RawBioAssay;
5685 25 Oct 19 nicklas 31 import net.sf.basedb.core.SessionControl;
5766 02 Dec 19 nicklas 32 import net.sf.basedb.core.SimpleProgressReporter;
5685 25 Oct 19 nicklas 33 import net.sf.basedb.core.Software;
5728 15 Nov 19 nicklas 34 import net.sf.basedb.core.Trashcan;
5728 15 Nov 19 nicklas 35 import net.sf.basedb.core.query.Annotations;
5728 15 Nov 19 nicklas 36 import net.sf.basedb.core.query.Expressions;
5685 25 Oct 19 nicklas 37 import net.sf.basedb.core.query.Hql;
5685 25 Oct 19 nicklas 38 import net.sf.basedb.core.query.Orders;
5728 15 Nov 19 nicklas 39 import net.sf.basedb.core.query.Restrictions;
5685 25 Oct 19 nicklas 40 import net.sf.basedb.core.snapshot.SnapshotManager;
5685 25 Oct 19 nicklas 41 import net.sf.basedb.opengrid.JobDefinition;
5685 25 Oct 19 nicklas 42 import net.sf.basedb.opengrid.OpenGridCluster;
5685 25 Oct 19 nicklas 43 import net.sf.basedb.opengrid.service.OpenGridService;
5685 25 Oct 19 nicklas 44 import net.sf.basedb.reggie.JsonUtil;
5685 25 Oct 19 nicklas 45 import net.sf.basedb.reggie.Reggie;
5728 15 Nov 19 nicklas 46 import net.sf.basedb.reggie.activity.ActivityDef;
5685 25 Oct 19 nicklas 47 import net.sf.basedb.reggie.counter.CounterService;
5685 25 Oct 19 nicklas 48 import net.sf.basedb.reggie.dao.AlignedSequences;
5685 25 Oct 19 nicklas 49 import net.sf.basedb.reggie.dao.Annotationtype;
5685 25 Oct 19 nicklas 50 import net.sf.basedb.reggie.dao.BiomaterialList;
5728 15 Nov 19 nicklas 51 import net.sf.basedb.reggie.dao.Datafiletype;
5685 25 Oct 19 nicklas 52 import net.sf.basedb.reggie.dao.Library;
6387 15 Sep 21 nicklas 53 import net.sf.basedb.reggie.dao.Pipeline;
5728 15 Nov 19 nicklas 54 import net.sf.basedb.reggie.dao.Rawbioassay;
5728 15 Nov 19 nicklas 55 import net.sf.basedb.reggie.dao.Rawdatatype;
5685 25 Oct 19 nicklas 56 import net.sf.basedb.reggie.dao.ReggieRole;
5685 25 Oct 19 nicklas 57 import net.sf.basedb.reggie.dao.Rna;
5685 25 Oct 19 nicklas 58 import net.sf.basedb.reggie.dao.Subtype;
5685 25 Oct 19 nicklas 59 import net.sf.basedb.reggie.grid.ScriptUtil;
6387 15 Sep 21 nicklas 60 import net.sf.basedb.reggie.grid.TargetedGenotypeJobCreator;
6387 15 Sep 21 nicklas 61 import net.sf.basedb.reggie.grid.TargetedGenotypeJobCreator.TargetedGenotypeDefinition;
5685 25 Oct 19 nicklas 62 import net.sf.basedb.reggie.grid.VariantCallingJobCreator;
5765 29 Nov 19 nicklas 63 import net.sf.basedb.reggie.grid.VariantStatisticsJobCreator;
5877 24 Mar 20 nicklas 64 import net.sf.basedb.reggie.grid.VariantCallingJobCreator.VariantCollector;
5764 29 Nov 19 nicklas 65 import net.sf.basedb.reggie.query.AnyToAnyRestriction;
6387 15 Sep 21 nicklas 66 import net.sf.basedb.reggie.vcf.GtData;
5730 18 Nov 19 nicklas 67 import net.sf.basedb.reggie.vcf.SnpData;
5730 18 Nov 19 nicklas 68 import net.sf.basedb.reggie.vcf.VariantCallingInfoFactory;
6387 15 Sep 21 nicklas 69 import net.sf.basedb.reggie.vcf.VcfData;
5730 18 Nov 19 nicklas 70 import net.sf.basedb.reggie.vcf.VcfParser;
5730 18 Nov 19 nicklas 71 import net.sf.basedb.util.FileUtil;
5685 25 Oct 19 nicklas 72 import net.sf.basedb.util.Values;
5685 25 Oct 19 nicklas 73 import net.sf.basedb.util.error.ThrowableUtil;
5685 25 Oct 19 nicklas 74
5685 25 Oct 19 nicklas 75
5685 25 Oct 19 nicklas 76 public class VariantCallingServlet 
5685 25 Oct 19 nicklas 77   extends HttpServlet 
5685 25 Oct 19 nicklas 78 {
5685 25 Oct 19 nicklas 79
5685 25 Oct 19 nicklas 80   private static final long serialVersionUID = 7125951272052280637L;
5685 25 Oct 19 nicklas 81
5685 25 Oct 19 nicklas 82   public VariantCallingServlet()
5685 25 Oct 19 nicklas 83   {}
5685 25 Oct 19 nicklas 84
5685 25 Oct 19 nicklas 85   @SuppressWarnings("unchecked")
5685 25 Oct 19 nicklas 86   @Override
5685 25 Oct 19 nicklas 87   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
5685 25 Oct 19 nicklas 88     throws ServletException, IOException 
5685 25 Oct 19 nicklas 89   {
5685 25 Oct 19 nicklas 90     String cmd = req.getParameter("cmd");
5685 25 Oct 19 nicklas 91     JsonUtil.setJsonResponseHeaders(resp);
5685 25 Oct 19 nicklas 92     
5685 25 Oct 19 nicklas 93     JSONObject json = new JSONObject();
5685 25 Oct 19 nicklas 94     json.put("status", "ok");
5685 25 Oct 19 nicklas 95     
5685 25 Oct 19 nicklas 96     final SessionControl sc = Reggie.getSessionControl(req);
5685 25 Oct 19 nicklas 97     DbControl dc = null;
5685 25 Oct 19 nicklas 98     try
5685 25 Oct 19 nicklas 99     {
5685 25 Oct 19 nicklas 100       if ("GetAlignedSequencesForVariantCalling".equals(cmd))
5685 25 Oct 19 nicklas 101       {
6335 15 Jun 21 nicklas 102         dc = sc.newDbControl(":Start variant calling");
5685 25 Oct 19 nicklas 103         
5685 25 Oct 19 nicklas 104         List<AlignedSequences> list = null;
5685 25 Oct 19 nicklas 105         String items = Values.getStringOrNull(req.getParameter("items"));
5685 25 Oct 19 nicklas 106         if (items == null)
5685 25 Oct 19 nicklas 107         {
5685 25 Oct 19 nicklas 108           ItemList vcallPipeline = BiomaterialList.VARIANT_CALLING_PIPELINE.load(dc);
6183 26 Mar 21 nicklas 109           ItemQuery<DerivedBioAssay> query = vcallPipeline.getMembers();
5685 25 Oct 19 nicklas 110           query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5685 25 Oct 19 nicklas 111           Subtype.ALIGNED_SEQUENCES.addFilter(dc, query);
5685 25 Oct 19 nicklas 112           // Join LibPlate for sorting
5685 25 Oct 19 nicklas 113           query.join(Hql.innerJoin(null, "extract", "lib", true));
5685 25 Oct 19 nicklas 114           query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
5685 25 Oct 19 nicklas 115           query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
5685 25 Oct 19 nicklas 116           query.order(Orders.asc(Hql.property("libPlate", "id")));
5685 25 Oct 19 nicklas 117           query.order(Orders.asc(Hql.property("name")));
5685 25 Oct 19 nicklas 118           query.setMaxResults(250);
5685 25 Oct 19 nicklas 119           list = AlignedSequences.toList(query.list(dc));
5685 25 Oct 19 nicklas 120         }
5685 25 Oct 19 nicklas 121         else
5685 25 Oct 19 nicklas 122         {
5685 25 Oct 19 nicklas 123           Integer[] ids = Values.getInt(items.split(","));
5685 25 Oct 19 nicklas 124           list = new ArrayList<AlignedSequences>();
5685 25 Oct 19 nicklas 125           for (Integer id : ids)
5685 25 Oct 19 nicklas 126           {
5685 25 Oct 19 nicklas 127             list.add(AlignedSequences.getById(dc, id));
5685 25 Oct 19 nicklas 128           }
5685 25 Oct 19 nicklas 129         }
5685 25 Oct 19 nicklas 130         SnapshotManager manager = new SnapshotManager();
5685 25 Oct 19 nicklas 131         JSONArray jsonAlignedSequences = new JSONArray();
5685 25 Oct 19 nicklas 132         for (AlignedSequences as : list)
5685 25 Oct 19 nicklas 133         {
5685 25 Oct 19 nicklas 134           as.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
5721 13 Nov 19 nicklas 135           as.setAnnotation("rawVariants", JsonUtil.loadLinkedItem(dc, as.getItem(), "variants-raw.vcf.gz", Item.FILE, null));
5685 25 Oct 19 nicklas 136           Library lib = as.getLibrary(dc);
5685 25 Oct 19 nicklas 137           lib.loadBioPlateLocation();
5685 25 Oct 19 nicklas 138           Rna r = lib.getRna(dc, true);
5685 25 Oct 19 nicklas 139           if (r != null) lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
5685 25 Oct 19 nicklas 140           as.setAnnotation("lib", lib.asJSONObject());
5685 25 Oct 19 nicklas 141           as.loadAnnotations(dc, manager, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
5685 25 Oct 19 nicklas 142           as.loadDoNotUseAnnotations(dc, manager);
5685 25 Oct 19 nicklas 143           jsonAlignedSequences.add(as.asJSONObject());
5685 25 Oct 19 nicklas 144         }
5685 25 Oct 19 nicklas 145         json.put("alignedSequences", jsonAlignedSequences);
5685 25 Oct 19 nicklas 146       }
5728 15 Nov 19 nicklas 147       else if ("GetUnconfirmed".equals(cmd))
5728 15 Nov 19 nicklas 148       {
6335 15 Jun 21 nicklas 149         dc = sc.newDbControl(":Confirm variant calling");
5728 15 Nov 19 nicklas 150         ItemQuery<RawBioAssay> query = RawBioAssay.getQuery();
5728 15 Nov 19 nicklas 151         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5728 15 Nov 19 nicklas 152         Rawdatatype.VARIANT_CALL.addFilter(dc, query);
7435 15 Nov 23 nicklas 153         Pipeline.RNASEQ_HISAT_VARIANTCALL.addFilter(dc, query);
5728 15 Nov 19 nicklas 154         
5728 15 Nov 19 nicklas 155         // The job must be ended
5728 15 Nov 19 nicklas 156         query.join(Hql.innerJoin("job", "jb"));
5728 15 Nov 19 nicklas 157         query.restrict(Restrictions.neq(Hql.property("jb", "ended"), null));
5728 15 Nov 19 nicklas 158         // Must NOT have a ANALYSIS_RESULT annotation
5728 15 Nov 19 nicklas 159         query.join(Annotations.leftJoin(null, Annotationtype.ANALYSIS_RESULT.load(dc), "ar"));
5728 15 Nov 19 nicklas 160         query.restrict(Restrictions.eq(Hql.alias("ar"), null));
5728 15 Nov 19 nicklas 161         // Ignore if AutoProcess==AutoConfirm
5728 15 Nov 19 nicklas 162         query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
5728 15 Nov 19 nicklas 163         query.restrict(
5728 15 Nov 19 nicklas 164             Restrictions.or(
5728 15 Nov 19 nicklas 165               Restrictions.neq(Hql.alias("ap"), Expressions.string("AutoConfirm")),
5728 15 Nov 19 nicklas 166               Restrictions.eq(Hql.alias("ap"), null)
5728 15 Nov 19 nicklas 167             ));
5728 15 Nov 19 nicklas 168         // Join LibPlate for sorting
5728 15 Nov 19 nicklas 169         query.join(Hql.innerJoin(null, "parentExtract", "lib", true));
5728 15 Nov 19 nicklas 170         query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
5728 15 Nov 19 nicklas 171         query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
5728 15 Nov 19 nicklas 172         query.order(Orders.asc(Hql.property("libPlate", "id")));
5728 15 Nov 19 nicklas 173         query.order(Orders.asc(Hql.property("name")));
5728 15 Nov 19 nicklas 174         query.setMaxResults(250);
5728 15 Nov 19 nicklas 175         
5728 15 Nov 19 nicklas 176         SnapshotManager manager = new SnapshotManager();
5728 15 Nov 19 nicklas 177         List<Rawbioassay> list = Rawbioassay.toList(query.list(dc));
5728 15 Nov 19 nicklas 178         JSONArray jsonRawBioAssays = new JSONArray();
5728 15 Nov 19 nicklas 179         for (Rawbioassay raw : list)
5728 15 Nov 19 nicklas 180         {
5728 15 Nov 19 nicklas 181           RawBioAssay rba = raw.getItem();
5728 15 Nov 19 nicklas 182           raw.loadAnnotations(dc, manager, "DataFilesFolder", Annotationtype.DATA_FILES_FOLDER, null);
5776 06 Dec 19 nicklas 183           raw.loadAnnotations(dc, manager, "VariantsPassedFilter", Annotationtype.VARIANTS_PASSED_FILTER, null);
5728 15 Nov 19 nicklas 184           
5728 15 Nov 19 nicklas 185           DerivedBioAssay alignment = rba.getParentBioAssay();
5776 06 Dec 19 nicklas 186           raw.setAnnotation("CallableBases", Annotationtype.CALLABLE_BASES.getAnnotationValue(dc, manager, alignment));
5728 15 Nov 19 nicklas 187           raw.setAnnotation("VariantsRaw", Annotationtype.VARIANTS_RAW.getAnnotationValue(dc, manager, alignment));
5728 15 Nov 19 nicklas 188           
5728 15 Nov 19 nicklas 189           File vcf = raw.getFile(dc, Datafiletype.VCF);
5730 18 Nov 19 nicklas 190           if (vcf != null) raw.setAnnotation("vcfFile", JsonUtil.getFileAsJSON(vcf));
5728 15 Nov 19 nicklas 191           
5728 15 Nov 19 nicklas 192           // Load job information
5728 15 Nov 19 nicklas 193           Job job = rba.getJob();
5728 15 Nov 19 nicklas 194           JSONObject jsonJob = JsonUtil.getJobAsJSON(job);
5728 15 Nov 19 nicklas 195           raw.setAnnotation("vcallJob", jsonJob);
5685 25 Oct 19 nicklas 196
5728 15 Nov 19 nicklas 197           // Load library information
5728 15 Nov 19 nicklas 198           Library lib = raw.getLibrary(dc);
5728 15 Nov 19 nicklas 199           Rna r = lib.getRna(dc, true);
5728 15 Nov 19 nicklas 200           lib.loadBioPlateLocation();
5728 15 Nov 19 nicklas 201           lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
5728 15 Nov 19 nicklas 202           raw.setAnnotation("lib", lib.asJSONObject());
5728 15 Nov 19 nicklas 203           
5728 15 Nov 19 nicklas 204           jsonRawBioAssays.add(raw.asJSONObject());
5728 15 Nov 19 nicklas 205         }
5728 15 Nov 19 nicklas 206         
5728 15 Nov 19 nicklas 207         json.put("rawBioAssays", jsonRawBioAssays);
5728 15 Nov 19 nicklas 208       }
5730 18 Nov 19 nicklas 209       else if ("GetVcfStatistics".equals(cmd))
5730 18 Nov 19 nicklas 210       {
5730 18 Nov 19 nicklas 211         int fileId = Values.getInt(req.getParameter("fileId"));
5730 18 Nov 19 nicklas 212         int itemId = Values.getInt(req.getParameter("itemId"));
6335 15 Jun 21 nicklas 213         dc = sc.newDbControl(":View variants");
5730 18 Nov 19 nicklas 214         
5730 18 Nov 19 nicklas 215         Rawbioassay vcall = Rawbioassay.getById(dc, itemId);
5730 18 Nov 19 nicklas 216         AlignedSequences alignment = vcall.getAlignedSequences(dc);
5730 18 Nov 19 nicklas 217         
5730 18 Nov 19 nicklas 218         vcall.setAnnotation("VariantsRaw", Annotationtype.VARIANTS_RAW.getAnnotationValue(dc, alignment.getItem()));
5730 18 Nov 19 nicklas 219
5730 18 Nov 19 nicklas 220         File vcf = File.getById(dc, fileId);
6387 15 Sep 21 nicklas 221         TargetedGenotypeDefinition target = TargetedGenotypeJobCreator.getTargetByName((String)Annotationtype.TARGETED_GENOTYPE.getAnnotationValue(dc, vcf));
5730 18 Nov 19 nicklas 222         InputStream vcfIn = null;
5730 18 Nov 19 nicklas 223         try
5730 18 Nov 19 nicklas 224         {
5730 18 Nov 19 nicklas 225           VcfParser parser = new VcfParser();
5730 18 Nov 19 nicklas 226           parser.setUseLineNoAsId(true);
5730 18 Nov 19 nicklas 227           parser.setInfoFactory(new VariantCallingInfoFactory(
6387 15 Sep 21 nicklas 228               Arrays.asList("TYPE", "ncbiRefSeq", "cosmic_AA", "cosmic_CDS", "cosmic_ID", "dbsnp_ID"),
6387 15 Sep 21 nicklas 229               Arrays.asList("HGVS.c", "HGVS.p")
5730 18 Nov 19 nicklas 230               ));
5730 18 Nov 19 nicklas 231           vcfIn = vcf.getDownloadStream(0);
5730 18 Nov 19 nicklas 232           if (vcf.getName().endsWith(".gz")) vcfIn = new GZIPInputStream(vcfIn);
6387 15 Sep 21 nicklas 233           VcfData vcfData = parser.parse(vcfIn, vcf.getName());
5730 18 Nov 19 nicklas 234           FileUtil.close(vcfIn);
5730 18 Nov 19 nicklas 235           
5730 18 Nov 19 nicklas 236           List<SnpData> snpList = parser.getSnpData();
5730 18 Nov 19 nicklas 237           JSONArray jsonSnp = new JSONArray();
5730 18 Nov 19 nicklas 238           for (SnpData snp : snpList)
5730 18 Nov 19 nicklas 239           {
6387 15 Sep 21 nicklas 240             JSONObject jsonGt = new JSONObject();
6387 15 Sep 21 nicklas 241             jsonGt.put("snp", snp.asJSONObject());
6387 15 Sep 21 nicklas 242             
6387 15 Sep 21 nicklas 243             GtData gt = vcfData.getGtData(snp.getId());
6513 07 Dec 21 nicklas 244             if (gt != null) jsonGt.put("gt", gt.asJSONObject(vcfData.getQualityModel())); 
6387 15 Sep 21 nicklas 245
6387 15 Sep 21 nicklas 246             jsonSnp.add(jsonGt);
5730 18 Nov 19 nicklas 247           }
5730 18 Nov 19 nicklas 248           
5730 18 Nov 19 nicklas 249           json.put("rawbioassay", vcall.asJSONObject());
6387 15 Sep 21 nicklas 250           if (target != null) json.put("TargetedGenotype", target.asJSONObject());
5730 18 Nov 19 nicklas 251           json.put("snpData", jsonSnp);
5730 18 Nov 19 nicklas 252         }
5730 18 Nov 19 nicklas 253         finally
5730 18 Nov 19 nicklas 254         {
5730 18 Nov 19 nicklas 255           FileUtil.close(vcfIn);
5730 18 Nov 19 nicklas 256         }
5730 18 Nov 19 nicklas 257       }
5764 29 Nov 19 nicklas 258       else if ("PreValidateItemListForStatistics".equals(cmd))
5764 29 Nov 19 nicklas 259       {
5764 29 Nov 19 nicklas 260         int listId = Values.getInt(req.getParameter("listId"));
6335 15 Jun 21 nicklas 261         dc = sc.newDbControl(":Build variant database");
5764 29 Nov 19 nicklas 262         
5764 29 Nov 19 nicklas 263         ItemList list = ItemList.getById(dc, listId);
5764 29 Nov 19 nicklas 264         if (list.getMemberType() != Item.DERIVEDBIOASSAY)
5764 29 Nov 19 nicklas 265         {
5764 29 Nov 19 nicklas 266           json.put("message", "A list with DERIVEDBIOASSAY items is required");
5764 29 Nov 19 nicklas 267         }
5764 29 Nov 19 nicklas 268         else if (list.getSize() == 0)
5764 29 Nov 19 nicklas 269         {
5764 29 Nov 19 nicklas 270           json.put("message", "The list is empty");
5764 29 Nov 19 nicklas 271         }
5764 29 Nov 19 nicklas 272         else
5764 29 Nov 19 nicklas 273         {
6183 26 Mar 21 nicklas 274           ItemQuery<DerivedBioAssay> query = list.getMembers();
5764 29 Nov 19 nicklas 275           query.include(Include.ALL);
5764 29 Nov 19 nicklas 276           query.restrict(AnyToAnyRestriction.exists("variants-raw.vcf.gz", Item.FILE));
5764 29 Nov 19 nicklas 277           
5764 29 Nov 19 nicklas 278           long count = query.count(dc);
5764 29 Nov 19 nicklas 279           if (count != list.getSize())
5764 29 Nov 19 nicklas 280           {
5764 29 Nov 19 nicklas 281             json.put("message", "Only " + count + " of " + list.getSize() + 
5764 29 Nov 19 nicklas 282               " members in the list have a 'variants-raw.vcf.gz' file");
5764 29 Nov 19 nicklas 283           }
5764 29 Nov 19 nicklas 284         }
5764 29 Nov 19 nicklas 285       }
5877 24 Mar 20 nicklas 286       else if ("GetVariantCallsNeedingImport".equals(cmd))
5877 24 Mar 20 nicklas 287       {
6335 15 Jun 21 nicklas 288         dc = sc.newDbControl(":Build variant database");
5877 24 Mar 20 nicklas 289         ItemList list = BiomaterialList.VARIANT_IMPORT_TMP.get(dc);
5877 24 Mar 20 nicklas 290         json.put("countRawBioAssays", list.getSize());
5878 24 Mar 20 nicklas 291         json.put("disabled", "Disable".equals(Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, list)));
5877 24 Mar 20 nicklas 292       }
6387 15 Sep 21 nicklas 293       else if ("GetRawBioAssaysForTargetedGenotyping".equals(cmd))
6387 15 Sep 21 nicklas 294       {
6387 15 Sep 21 nicklas 295         Rawdatatype rawDataType = Rawdatatype.VARIANT_CALL;
6387 15 Sep 21 nicklas 296         Pipeline pipeline = Pipeline.RNASEQ_HISAT_VARIANTCALL;
6387 15 Sep 21 nicklas 297
6387 15 Sep 21 nicklas 298         dc = sc.newDbControl(":Start Targeted genotype analysis");
6387 15 Sep 21 nicklas 299         List<Rawbioassay> list = null;
6387 15 Sep 21 nicklas 300         String items = Values.getStringOrNull(req.getParameter("items"));
6387 15 Sep 21 nicklas 301         if (items == null)
6387 15 Sep 21 nicklas 302         {
6387 15 Sep 21 nicklas 303           ItemList tgtPipeline = BiomaterialList.TARGETED_GENOTYPE_PIPELINE.load(dc);
6387 15 Sep 21 nicklas 304           ItemQuery<RawBioAssay> query = tgtPipeline.getMembers();
6387 15 Sep 21 nicklas 305           query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
6387 15 Sep 21 nicklas 306           Rawdatatype.VARIANT_CALL.addFilter(dc, query);
6387 15 Sep 21 nicklas 307           // Join LibPlate for sorting
6387 15 Sep 21 nicklas 308           query.join(Hql.innerJoin(null, "parentExtract", "lib", true));
6387 15 Sep 21 nicklas 309           query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
6387 15 Sep 21 nicklas 310           query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
6387 15 Sep 21 nicklas 311           query.order(Orders.asc(Hql.property("libPlate", "id")));
6387 15 Sep 21 nicklas 312           query.order(Orders.asc(Hql.property("name")));
6387 15 Sep 21 nicklas 313           query.setMaxResults(500);
6387 15 Sep 21 nicklas 314           list = Rawbioassay.toList(query.list(dc));
6387 15 Sep 21 nicklas 315         }
6387 15 Sep 21 nicklas 316         else
6387 15 Sep 21 nicklas 317         {
6387 15 Sep 21 nicklas 318           Integer[] ids = Values.getInt(items.split(","));
6387 15 Sep 21 nicklas 319           list = new ArrayList<Rawbioassay>();
6387 15 Sep 21 nicklas 320           for (Integer id : ids)
6387 15 Sep 21 nicklas 321           {
6387 15 Sep 21 nicklas 322             list.add(Rawbioassay.getById(dc, id));
6387 15 Sep 21 nicklas 323           }
6387 15 Sep 21 nicklas 324         }
6387 15 Sep 21 nicklas 325         
6387 15 Sep 21 nicklas 326         SnapshotManager manager = new SnapshotManager();
6387 15 Sep 21 nicklas 327         JSONArray jsonRawBioAssays = new JSONArray();
6387 15 Sep 21 nicklas 328         for (Rawbioassay raw : list)
6387 15 Sep 21 nicklas 329         {
6387 15 Sep 21 nicklas 330           RawBioAssay rba = raw.getItem();
6387 15 Sep 21 nicklas 331           raw.loadAnnotations(dc, manager, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
6387 15 Sep 21 nicklas 332           raw.loadAnnotations(dc, manager, "pipeline", Annotationtype.PIPELINE, null);
6387 15 Sep 21 nicklas 333           
6387 15 Sep 21 nicklas 334           // Load library information
6387 15 Sep 21 nicklas 335           Library lib = raw.getLibrary(dc);
6387 15 Sep 21 nicklas 336           Rna r = lib.getRna(dc, true);
6387 15 Sep 21 nicklas 337           lib.loadBioPlateLocation();
6387 15 Sep 21 nicklas 338           lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
6387 15 Sep 21 nicklas 339           raw.setAnnotation("lib", lib.asJSONObject());
6387 15 Sep 21 nicklas 340           raw.loadDoNotUseAnnotations(dc, manager);
6387 15 Sep 21 nicklas 341           jsonRawBioAssays.add(raw.asJSONObject());
6387 15 Sep 21 nicklas 342         }
6387 15 Sep 21 nicklas 343         
6387 15 Sep 21 nicklas 344         json.put("rawBioAssays", jsonRawBioAssays);
6387 15 Sep 21 nicklas 345         json.put("rawDataType", rawDataType != null ? rawDataType.getId() : null);
6387 15 Sep 21 nicklas 346         json.put("pipeline", pipeline != null ? pipeline.getName() : "");
6387 15 Sep 21 nicklas 347       }
6387 15 Sep 21 nicklas 348       else if ("GetTargetedGenotypeDefs".equals(cmd))
6387 15 Sep 21 nicklas 349       {
6387 15 Sep 21 nicklas 350         dc = sc.newDbControl(":Start Targeted genotype analysis");
6387 15 Sep 21 nicklas 351         JSONArray jsonTargets = new JSONArray();
6387 15 Sep 21 nicklas 352         for (TargetedGenotypeDefinition tgDef : TargetedGenotypeJobCreator.getConfiguredTargets())
6387 15 Sep 21 nicklas 353         {
6387 15 Sep 21 nicklas 354           jsonTargets.add(tgDef.asJSONObject());
6387 15 Sep 21 nicklas 355         }        
6387 15 Sep 21 nicklas 356         json.put("targets", jsonTargets);
6387 15 Sep 21 nicklas 357       }
5685 25 Oct 19 nicklas 358     
5685 25 Oct 19 nicklas 359     }
5685 25 Oct 19 nicklas 360     catch (Throwable t)
5685 25 Oct 19 nicklas 361     {
5685 25 Oct 19 nicklas 362       t.printStackTrace();
5685 25 Oct 19 nicklas 363       json.clear();
5685 25 Oct 19 nicklas 364       json.put("status", "error");
5685 25 Oct 19 nicklas 365       json.put("message", t.getMessage());
5685 25 Oct 19 nicklas 366       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
5685 25 Oct 19 nicklas 367     }
5685 25 Oct 19 nicklas 368     finally
5685 25 Oct 19 nicklas 369     {
5685 25 Oct 19 nicklas 370       if (dc != null) dc.close();
5685 25 Oct 19 nicklas 371       json.writeJSONString(resp.getWriter());
5685 25 Oct 19 nicklas 372     }
5685 25 Oct 19 nicklas 373     
5685 25 Oct 19 nicklas 374   }
5685 25 Oct 19 nicklas 375
5877 24 Mar 20 nicklas 376   @SuppressWarnings("unchecked")
5685 25 Oct 19 nicklas 377   @Override
5685 25 Oct 19 nicklas 378   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
5685 25 Oct 19 nicklas 379     throws ServletException, IOException 
5685 25 Oct 19 nicklas 380   {
5685 25 Oct 19 nicklas 381     String cmd = req.getParameter("cmd");
5685 25 Oct 19 nicklas 382     JsonUtil.setJsonResponseHeaders(resp);
5685 25 Oct 19 nicklas 383     
5685 25 Oct 19 nicklas 384     JSONObject json = new JSONObject();
5685 25 Oct 19 nicklas 385     json.put("status", "ok");
5685 25 Oct 19 nicklas 386     JSONArray jsonMessages = new JSONArray();
5685 25 Oct 19 nicklas 387   
5685 25 Oct 19 nicklas 388     final SessionControl sc = Reggie.getSessionControl(req);
5685 25 Oct 19 nicklas 389     DbControl dc = null;
5685 25 Oct 19 nicklas 390     try
5685 25 Oct 19 nicklas 391     {
5685 25 Oct 19 nicklas 392
5685 25 Oct 19 nicklas 393       if ("StartVariantCalling".equals(cmd))
5685 25 Oct 19 nicklas 394       {
6335 15 Jun 21 nicklas 395         dc = sc.newDbControl(":Start variant calling");
5685 25 Oct 19 nicklas 396
5685 25 Oct 19 nicklas 397         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
5685 25 Oct 19 nicklas 398
5685 25 Oct 19 nicklas 399         JSONObject jsonReq = JsonUtil.parseRequest(req);
5685 25 Oct 19 nicklas 400         JSONArray jsonAligned = (JSONArray)jsonReq.get("alignedSequences");
5685 25 Oct 19 nicklas 401       
5685 25 Oct 19 nicklas 402         Number vcallSoftwareId = (Number)jsonReq.get("vcallSoftware");
5685 25 Oct 19 nicklas 403       
5685 25 Oct 19 nicklas 404         String clusterId = (String)jsonReq.get("cluster");
5713 12 Nov 19 nicklas 405         boolean rawOnly = Boolean.TRUE.equals(jsonReq.get("rawOnly"));
5713 12 Nov 19 nicklas 406         boolean skipRaw = Boolean.TRUE.equals(jsonReq.get("skipRaw"));
5685 25 Oct 19 nicklas 407         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
5685 25 Oct 19 nicklas 408         boolean autoConfirm = Boolean.TRUE.equals(jsonReq.get("autoConfirm"));
5685 25 Oct 19 nicklas 409         Number priority = (Number)jsonReq.get("priority");
6981 17 Jan 23 nicklas 410         String partition = Values.getStringOrNull((String)jsonReq.get("partition"));
7372 06 Oct 23 nicklas 411         String submitOptions = Values.getStringOrNull((String)jsonReq.get("submitOptionsOverride"));
5685 25 Oct 19 nicklas 412
5685 25 Oct 19 nicklas 413         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
5685 25 Oct 19 nicklas 414         if (cluster == null)
5685 25 Oct 19 nicklas 415         {
5685 25 Oct 19 nicklas 416           throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
5685 25 Oct 19 nicklas 417         }
5685 25 Oct 19 nicklas 418
5685 25 Oct 19 nicklas 419         // Load common items
5685 25 Oct 19 nicklas 420         Software vcallSoftware = vcallSoftwareId != null ? Software.getById(dc, vcallSoftwareId.intValue()) : null;
5685 25 Oct 19 nicklas 421
5685 25 Oct 19 nicklas 422         VariantCallingJobCreator jobCreator = new VariantCallingJobCreator();
5713 12 Nov 19 nicklas 423         jobCreator.setRawOnly(rawOnly);
5713 12 Nov 19 nicklas 424         jobCreator.setSkipRaw(skipRaw);
5685 25 Oct 19 nicklas 425         jobCreator.setDebug(debug);
5685 25 Oct 19 nicklas 426         jobCreator.setAutoConfirm(autoConfirm);
5685 25 Oct 19 nicklas 427         jobCreator.setPriority(priority == null ? null : priority.intValue());
6981 17 Jan 23 nicklas 428         jobCreator.setPartition(partition);
5685 25 Oct 19 nicklas 429         jobCreator.setSoftware(vcallSoftware);
7372 06 Oct 23 nicklas 430         jobCreator.setSubmitOptionsOverride(submitOptions);
5685 25 Oct 19 nicklas 431         
5685 25 Oct 19 nicklas 432         List<AlignedSequences> alignedSequences = new ArrayList<AlignedSequences>();
5685 25 Oct 19 nicklas 433         for (int alNo = 0; alNo < jsonAligned.size(); alNo++)
5685 25 Oct 19 nicklas 434         {
5685 25 Oct 19 nicklas 435           JSONObject jsonAl = (JSONObject)jsonAligned.get(alNo);
5685 25 Oct 19 nicklas 436           Number alignedId = (Number)jsonAl.get("id");
5685 25 Oct 19 nicklas 437         
5685 25 Oct 19 nicklas 438           AlignedSequences as = AlignedSequences.getById(dc, alignedId.intValue());
5685 25 Oct 19 nicklas 439           alignedSequences.add(as);
5685 25 Oct 19 nicklas 440
5685 25 Oct 19 nicklas 441           // Reset AUTO_PROCESSING annotation
5685 25 Oct 19 nicklas 442           DerivedBioAssay aligned = as.getDerivedBioAssay();
5685 25 Oct 19 nicklas 443           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, aligned, null);
5685 25 Oct 19 nicklas 444         }
5685 25 Oct 19 nicklas 445
5685 25 Oct 19 nicklas 446         List<JobDefinition> jobDefs = jobCreator.createVariantCallingJobs(dc, cluster, alignedSequences);
5714 12 Nov 19 nicklas 447         int numSkipped = jobCreator.getNumSkipped();
5685 25 Oct 19 nicklas 448         List<Job> jobs = ScriptUtil.submitJobs(dc, cluster, jobDefs);
5714 12 Nov 19 nicklas 449         if (numSkipped > 0)
5714 12 Nov 19 nicklas 450         {
5714 12 Nov 19 nicklas 451           jsonMessages.add("Skipped " + numSkipped + " of " + jsonAligned.size() + " alignments that already have raw variants");
5714 12 Nov 19 nicklas 452         }
5714 12 Nov 19 nicklas 453         if (jobs.size() == 0)
5714 12 Nov 19 nicklas 454         {
5714 12 Nov 19 nicklas 455           jsonMessages.add("No variant calling jobs submitted");
5714 12 Nov 19 nicklas 456         }
5685 25 Oct 19 nicklas 457         for (Job job : jobs)
5685 25 Oct 19 nicklas 458         {
5685 25 Oct 19 nicklas 459           if (job.getStatus() == Job.Status.ERROR)
5685 25 Oct 19 nicklas 460           {
5685 25 Oct 19 nicklas 461             jsonMessages.add("[Error]Job submission for '" + job.getName() + "' failed: " + job.getStatusMessage());
5685 25 Oct 19 nicklas 462           }
5685 25 Oct 19 nicklas 463           else
5685 25 Oct 19 nicklas 464           {
5685 25 Oct 19 nicklas 465             jsonMessages.add("Submitted variant calling job to " + cluster.getConnectionInfo().getName() + " with id " + job.getExternalId());
5685 25 Oct 19 nicklas 466           }
5685 25 Oct 19 nicklas 467         }
5685 25 Oct 19 nicklas 468         
5685 25 Oct 19 nicklas 469         dc.commit();
5685 25 Oct 19 nicklas 470       }
5728 15 Nov 19 nicklas 471       else if ("ConfirmVariantCalling".equals(cmd))
5728 15 Nov 19 nicklas 472       {
6335 15 Jun 21 nicklas 473         dc = sc.newDbControl(":Confirm variant calling");
5685 25 Oct 19 nicklas 474
5728 15 Nov 19 nicklas 475         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
5728 15 Nov 19 nicklas 476
5728 15 Nov 19 nicklas 477         JSONObject jsonReq = JsonUtil.parseRequest(req);
5728 15 Nov 19 nicklas 478         JSONArray jsonRawBioAssays = (JSONArray)jsonReq.get("rawBioAssays");
5728 15 Nov 19 nicklas 479
5728 15 Nov 19 nicklas 480         int numConfirmed = 0;
5728 15 Nov 19 nicklas 481         int numUnconfirmed = 0;
5728 15 Nov 19 nicklas 482         int numReRun = 0;
5728 15 Nov 19 nicklas 483         int numFlaggedRna = 0;
5728 15 Nov 19 nicklas 484         int numDeleted = 0;
5728 15 Nov 19 nicklas 485         
5728 15 Nov 19 nicklas 486         ItemList flaggedRna = BiomaterialList.FLAGGED_RNA.load(dc);
5728 15 Nov 19 nicklas 487         ItemList vcallPipeline = BiomaterialList.VARIANT_CALLING_PIPELINE.load(dc);
6664 05 Apr 22 nicklas 488         ItemList tgtPipeline = BiomaterialList.TARGETED_GENOTYPE_PIPELINE.load(dc);
6387 15 Sep 21 nicklas 489         ItemList indexFiltered = BiomaterialList.VARIANT_INDEX_FILTERED.load(dc);
6387 15 Sep 21 nicklas 490         ItemList indexAll = BiomaterialList.VARIANT_INDEX_ALL.load(dc);
5728 15 Nov 19 nicklas 491         
5728 15 Nov 19 nicklas 492         boolean deleteItemsCreatedByFailedJobs = Boolean.TRUE.equals(jsonReq.get("deleteItemsCreatedByFailedJobs"));
5728 15 Nov 19 nicklas 493         List<BasicItem> toDelete = new ArrayList<BasicItem>();
5728 15 Nov 19 nicklas 494         
5728 15 Nov 19 nicklas 495         for (int rawNo = 0; rawNo < jsonRawBioAssays.size(); ++rawNo)
5728 15 Nov 19 nicklas 496         {
5728 15 Nov 19 nicklas 497           JSONObject jsonRaw = (JSONObject)jsonRawBioAssays.get(rawNo);
5728 15 Nov 19 nicklas 498           Number rawdId = (Number)jsonRaw.get("id");
5728 15 Nov 19 nicklas 499           
5728 15 Nov 19 nicklas 500           Rawbioassay raw = Rawbioassay.getById(dc, rawdId.intValue());
5728 15 Nov 19 nicklas 501           AlignedSequences aligned = raw.getAlignedSequences(dc);
5728 15 Nov 19 nicklas 502           
5728 15 Nov 19 nicklas 503           DerivedBioAssay alignedDBA = aligned.getDerivedBioAssay();
5728 15 Nov 19 nicklas 504           RawBioAssay rba = raw.getRawBioAssay();
5728 15 Nov 19 nicklas 505           Job job = rba.getJob();
5728 15 Nov 19 nicklas 506           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rba, null);
5728 15 Nov 19 nicklas 507           
5728 15 Nov 19 nicklas 508           boolean flag = Boolean.TRUE.equals(jsonRaw.get("flag"));
5728 15 Nov 19 nicklas 509           if (flag && !Reggie.isExternalItem(raw.getName()))
5728 15 Nov 19 nicklas 510           {
5728 15 Nov 19 nicklas 511             Library lib = Library.get(rba.getParentExtract());
5728 15 Nov 19 nicklas 512             Rna rna = lib.getRna(dc, false);
5728 15 Nov 19 nicklas 513             Extract r = rna.getItem();
5728 15 Nov 19 nicklas 514             Annotationtype.FLAG.setAnnotationValue(dc, r, Rna.FLAG_STRINGTIE_FAILED);
5728 15 Nov 19 nicklas 515             flaggedRna.add(r);
5728 15 Nov 19 nicklas 516             numFlaggedRna++;
5728 15 Nov 19 nicklas 517           }
5728 15 Nov 19 nicklas 518           
5728 15 Nov 19 nicklas 519           String comment = Values.getStringOrNull((String)jsonRaw.get("comment"));
5728 15 Nov 19 nicklas 520           rba.setDescription(comment);
5728 15 Nov 19 nicklas 521           
5728 15 Nov 19 nicklas 522           boolean reRun = Boolean.TRUE.equals(jsonRaw.get("rerun"));
5728 15 Nov 19 nicklas 523           boolean confirm = Boolean.TRUE.equals(jsonRaw.get("confirm"));
5728 15 Nov 19 nicklas 524           boolean deleted = false;
5728 15 Nov 19 nicklas 525
5728 15 Nov 19 nicklas 526           if (reRun)
5728 15 Nov 19 nicklas 527           {
5728 15 Nov 19 nicklas 528             // Set ReProcess annotation on aligned sequences item
5728 15 Nov 19 nicklas 529             Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, aligned.getItem(), "ReProcess");
5728 15 Nov 19 nicklas 530             vcallPipeline.add(aligned.getItem());
5728 15 Nov 19 nicklas 531             numReRun++;
5728 15 Nov 19 nicklas 532             
5728 15 Nov 19 nicklas 533             if (deleteItemsCreatedByFailedJobs && !confirm)
5728 15 Nov 19 nicklas 534             {
5728 15 Nov 19 nicklas 535               // Delete rawbioassay and related items
5728 15 Nov 19 nicklas 536               rba.setRemoved(true);
5728 15 Nov 19 nicklas 537               toDelete.add(rba);
5728 15 Nov 19 nicklas 538               toDelete.addAll(Reggie.removeAttachedFiles(dc, rba));
5728 15 Nov 19 nicklas 539               deleted = true;
5728 15 Nov 19 nicklas 540               numDeleted++;
5728 15 Nov 19 nicklas 541               aligned.getItem().setDescription(comment);
5728 15 Nov 19 nicklas 542             }
5728 15 Nov 19 nicklas 543           }
5728 15 Nov 19 nicklas 544           
5728 15 Nov 19 nicklas 545           if (!deleted)
5728 15 Nov 19 nicklas 546           {
5728 15 Nov 19 nicklas 547             if (confirm)
5728 15 Nov 19 nicklas 548             {
5728 15 Nov 19 nicklas 549               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, rba, Rawbioassay.FEATURE_EXTRACTION_SUCCESSFUL);
6664 05 Apr 22 nicklas 550               tgtPipeline.add(rba);
6387 15 Sep 21 nicklas 551               indexFiltered.add(rba);
6387 15 Sep 21 nicklas 552               indexAll.add(rba);
5728 15 Nov 19 nicklas 553               numConfirmed++;
5728 15 Nov 19 nicklas 554             }
5728 15 Nov 19 nicklas 555             else if (reRun || flag)
5728 15 Nov 19 nicklas 556             {
5728 15 Nov 19 nicklas 557               Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, rba, Rawbioassay.FEATURE_EXTRACTION_FAILED);
5728 15 Nov 19 nicklas 558             }
5728 15 Nov 19 nicklas 559             else
5728 15 Nov 19 nicklas 560             {
5728 15 Nov 19 nicklas 561               numUnconfirmed++;
5728 15 Nov 19 nicklas 562             }
5728 15 Nov 19 nicklas 563           }
5728 15 Nov 19 nicklas 564         }
5728 15 Nov 19 nicklas 565         
5728 15 Nov 19 nicklas 566         if (numConfirmed > 0)
5728 15 Nov 19 nicklas 567         {
5728 15 Nov 19 nicklas 568           jsonMessages.add("Variant calling confirmed for " + numConfirmed + " alignments");
5728 15 Nov 19 nicklas 569           ActivityDef.VARIANT_CALLING_CONFIRMED.merge(dc, numConfirmed);
5728 15 Nov 19 nicklas 570         }
5728 15 Nov 19 nicklas 571         else
5728 15 Nov 19 nicklas 572         {
5728 15 Nov 19 nicklas 573           jsonMessages.add("No variant callings confirmed");
5728 15 Nov 19 nicklas 574         }
5728 15 Nov 19 nicklas 575         
5728 15 Nov 19 nicklas 576         if (numReRun > 0)
5728 15 Nov 19 nicklas 577         {
5728 15 Nov 19 nicklas 578           jsonMessages.add(numReRun + " alignments flagged for new variant calling");
5728 15 Nov 19 nicklas 579         }
5728 15 Nov 19 nicklas 580         
5728 15 Nov 19 nicklas 581         if (numDeleted > 0)
5728 15 Nov 19 nicklas 582         {
5728 15 Nov 19 nicklas 583           jsonMessages.add(numDeleted + " variant callings deleted due to failure");
5728 15 Nov 19 nicklas 584           if (toDelete.size() > numDeleted)
5728 15 Nov 19 nicklas 585           {
5728 15 Nov 19 nicklas 586             jsonMessages.add((toDelete.size() - numDeleted) + " linked items (eg. parent items and files) deleted");
5728 15 Nov 19 nicklas 587           }
5728 15 Nov 19 nicklas 588         }
5728 15 Nov 19 nicklas 589         
5728 15 Nov 19 nicklas 590         if (numFlaggedRna > 0)
5728 15 Nov 19 nicklas 591         {
5728 15 Nov 19 nicklas 592           jsonMessages.add("Added " + numFlaggedRna + " RNA items to the '" + flaggedRna.getName() + "' list");
5728 15 Nov 19 nicklas 593         }
5728 15 Nov 19 nicklas 594         
5728 15 Nov 19 nicklas 595         if (numUnconfirmed > 0)
5728 15 Nov 19 nicklas 596         {
5728 15 Nov 19 nicklas 597           jsonMessages.add(numUnconfirmed + " variant callings remain unconfirmed");
5728 15 Nov 19 nicklas 598         }
5728 15 Nov 19 nicklas 599         dc.commit();
5728 15 Nov 19 nicklas 600         if (toDelete.size() > 0)
5728 15 Nov 19 nicklas 601         {
5728 15 Nov 19 nicklas 602           try
5728 15 Nov 19 nicklas 603           {
5728 15 Nov 19 nicklas 604             Trashcan.delete(sc, toDelete, false, null);
5728 15 Nov 19 nicklas 605           }
5728 15 Nov 19 nicklas 606           catch (RuntimeException ex)
5728 15 Nov 19 nicklas 607           {
5728 15 Nov 19 nicklas 608             ex.printStackTrace();
5728 15 Nov 19 nicklas 609             jsonMessages.add("[Error]Could not delete all items created by failed jobs: " + ex.getMessage());
5728 15 Nov 19 nicklas 610           }
5728 15 Nov 19 nicklas 611         }
5728 15 Nov 19 nicklas 612       }
5765 29 Nov 19 nicklas 613       else if ("BuildVariantStatistics".equals(cmd))
5765 29 Nov 19 nicklas 614       {
6335 15 Jun 21 nicklas 615         dc = sc.newDbControl(":Build variant database");
5728 15 Nov 19 nicklas 616
5765 29 Nov 19 nicklas 617         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.ADMINISTRATOR);
5765 29 Nov 19 nicklas 618
5765 29 Nov 19 nicklas 619         JSONObject jsonReq = JsonUtil.parseRequest(req);
5765 29 Nov 19 nicklas 620       
5765 29 Nov 19 nicklas 621         Number listId = (Number)jsonReq.get("itemList");
5815 24 Jan 20 nicklas 622         boolean isNormals = "normals".equals(jsonReq.get("sampleType"));
5765 29 Nov 19 nicklas 623         String clusterId = (String)jsonReq.get("cluster");
5765 29 Nov 19 nicklas 624         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
5765 29 Nov 19 nicklas 625         Number priority = (Number)jsonReq.get("priority");
6981 17 Jan 23 nicklas 626         String partition = Values.getStringOrNull((String)jsonReq.get("partition"));
5765 29 Nov 19 nicklas 627
5765 29 Nov 19 nicklas 628         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
5765 29 Nov 19 nicklas 629         if (cluster == null)
5765 29 Nov 19 nicklas 630         {
5765 29 Nov 19 nicklas 631           throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
5765 29 Nov 19 nicklas 632         }
5765 29 Nov 19 nicklas 633         
5766 02 Dec 19 nicklas 634         SimpleProgressReporter progress = new SimpleProgressReporter(null);
5766 02 Dec 19 nicklas 635         sc.setSessionSetting("variant-statistics-progress", progress);
5766 02 Dec 19 nicklas 636         
5765 29 Nov 19 nicklas 637         ItemList list = ItemList.getById(dc, listId.intValue());
5765 29 Nov 19 nicklas 638         
5765 29 Nov 19 nicklas 639         VariantStatisticsJobCreator jobCreator = new VariantStatisticsJobCreator();
5765 29 Nov 19 nicklas 640         jobCreator.setDebug(debug);
5765 29 Nov 19 nicklas 641         jobCreator.setPriority(priority == null ? null : priority.intValue());
6981 17 Jan 23 nicklas 642         jobCreator.setPartition(partition);
5815 24 Jan 20 nicklas 643         jobCreator.setOutputBaseName(isNormals ? "scanb-normals" : "scanb-tumors");
5765 29 Nov 19 nicklas 644
5766 02 Dec 19 nicklas 645         Job statJob = jobCreator.createVariantStatisticsJob(dc, cluster, list, progress);
5765 29 Nov 19 nicklas 646         if (statJob.getStatus() == Job.Status.ERROR)
5765 29 Nov 19 nicklas 647         {
5765 29 Nov 19 nicklas 648           jsonMessages.add("[Error]Job submission for '" + statJob.getName() + "' failed: " + statJob.getStatusMessage());
5765 29 Nov 19 nicklas 649         }
5765 29 Nov 19 nicklas 650         else
5765 29 Nov 19 nicklas 651         {
5765 29 Nov 19 nicklas 652           jsonMessages.add("Submitted variant statistics job to " + cluster.getConnectionInfo().getName() + " with id " + statJob.getExternalId());
5765 29 Nov 19 nicklas 653         }
5766 02 Dec 19 nicklas 654         progress.display(100, "Done");
5765 29 Nov 19 nicklas 655         dc.commit();
5765 29 Nov 19 nicklas 656       }
5877 24 Mar 20 nicklas 657       else if ("ImportToExistingVariantCalls".equals(cmd))
5877 24 Mar 20 nicklas 658       {
6335 15 Jun 21 nicklas 659         dc = sc.newDbControl(":Import variants");
5765 29 Nov 19 nicklas 660
5877 24 Mar 20 nicklas 661         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.ADMINISTRATOR);
5877 24 Mar 20 nicklas 662
5877 24 Mar 20 nicklas 663         SimpleProgressReporter progress = new SimpleProgressReporter(null);
5877 24 Mar 20 nicklas 664         sc.setSessionSetting("variant-import-progress", progress);
5877 24 Mar 20 nicklas 665         
5877 24 Mar 20 nicklas 666         ItemList variantCalls = BiomaterialList.VARIANT_IMPORT_TMP.get(dc);
6183 26 Mar 21 nicklas 667         ItemQuery<RawBioAssay> query = variantCalls.getMembers();
5877 24 Mar 20 nicklas 668         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5877 24 Mar 20 nicklas 669         List<Integer> all = query.idList(dc);
5877 24 Mar 20 nicklas 670         
5877 24 Mar 20 nicklas 671         int total = all.size();
5877 24 Mar 20 nicklas 672         int current = 0;
5877 24 Mar 20 nicklas 673         int totalVariants = 0;
5877 24 Mar 20 nicklas 674         for (Integer rawId : all)
5877 24 Mar 20 nicklas 675         {
5877 24 Mar 20 nicklas 676           current++;
5877 24 Mar 20 nicklas 677           RawBioAssay vcall = RawBioAssay.getById(dc, rawId);
5877 24 Mar 20 nicklas 678           variantCalls.removeItem(vcall);
5877 24 Mar 20 nicklas 679           File vcfFile = Datafiletype.VCF.getFile(dc, vcall);
5877 24 Mar 20 nicklas 680           
5877 24 Mar 20 nicklas 681           if (vcfFile != null)
5877 24 Mar 20 nicklas 682           {
5877 24 Mar 20 nicklas 683             InputStream fromFileServer = null;
5877 24 Mar 20 nicklas 684             try
5877 24 Mar 20 nicklas 685             {
5877 24 Mar 20 nicklas 686               fromFileServer = vcfFile.getDownloadStream(0);
5877 24 Mar 20 nicklas 687               if (vcfFile.getName().endsWith(".gz"))
5877 24 Mar 20 nicklas 688               {
5877 24 Mar 20 nicklas 689                 fromFileServer = new GZIPInputStream(fromFileServer);
5877 24 Mar 20 nicklas 690               }
5877 24 Mar 20 nicklas 691               VariantCollector variants = new VariantCollector();
5877 24 Mar 20 nicklas 692               VcfParser parser = new VcfParser();
5877 24 Mar 20 nicklas 693               parser.setInfoFactory(new VariantCallingInfoFactory(Arrays.asList("ncbiRefSeq"), null));
5877 24 Mar 20 nicklas 694               parser.setUseLineNoAsId(true);
5877 24 Mar 20 nicklas 695               parser.parse(fromFileServer, vcfFile.getName(), variants);
5877 24 Mar 20 nicklas 696               int numVariants = variants.updateVariantAnnotations(dc, vcall);
5879 25 Mar 20 nicklas 697               totalVariants += numVariants;
5877 24 Mar 20 nicklas 698             }
5877 24 Mar 20 nicklas 699             finally
5877 24 Mar 20 nicklas 700             {
5877 24 Mar 20 nicklas 701               FileUtil.close(fromFileServer);
5877 24 Mar 20 nicklas 702             }
5877 24 Mar 20 nicklas 703           }
5877 24 Mar 20 nicklas 704           
5877 24 Mar 20 nicklas 705           if (current % 100 == 0)
5877 24 Mar 20 nicklas 706           {
5877 24 Mar 20 nicklas 707             // Report progress and commit what has been done so far
5877 24 Mar 20 nicklas 708             progress.display((current*100)/total, "Importing " + current + " of " + total + ": " + totalVariants + " variants found so far...");
5879 25 Mar 20 nicklas 709
5879 25 Mar 20 nicklas 710             if (current % 500 == 0)
5879 25 Mar 20 nicklas 711             {
5879 25 Mar 20 nicklas 712               dc.commit();
5879 25 Mar 20 nicklas 713               // Start new transaction and re-load the item list
6335 15 Jun 21 nicklas 714               dc = sc.newDbControl(dc.getName());
5879 25 Mar 20 nicklas 715               variantCalls = BiomaterialList.VARIANT_IMPORT_TMP.get(dc);
5879 25 Mar 20 nicklas 716             }
5877 24 Mar 20 nicklas 717           }
5877 24 Mar 20 nicklas 718         }
5878 24 Mar 20 nicklas 719         progress.display(100, "Importing " + current + " of " + total + ": " + totalVariants + " variants found so far...");
5877 24 Mar 20 nicklas 720         
5878 24 Mar 20 nicklas 721         if (variantCalls.getSize() == 0)
5878 24 Mar 20 nicklas 722         {
5878 24 Mar 20 nicklas 723           // Disable this wizard once the list is empty
5878 24 Mar 20 nicklas 724           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, variantCalls, "Disable");
5878 24 Mar 20 nicklas 725         }
5878 24 Mar 20 nicklas 726         
5877 24 Mar 20 nicklas 727         dc.commit();
5877 24 Mar 20 nicklas 728         jsonMessages.add("Imported " + totalVariants + " variants to " + all.size() + " raw bioassays");
5877 24 Mar 20 nicklas 729       }
6387 15 Sep 21 nicklas 730       else if ("StartTargetedGenotypeAnalysis".equals(cmd))
6387 15 Sep 21 nicklas 731       {
6387 15 Sep 21 nicklas 732         dc = sc.newDbControl(":Start targeted genotype analysis");
5877 24 Mar 20 nicklas 733
6387 15 Sep 21 nicklas 734         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
6387 15 Sep 21 nicklas 735
6387 15 Sep 21 nicklas 736         JSONObject jsonReq = JsonUtil.parseRequest(req);
6387 15 Sep 21 nicklas 737         JSONArray jsonRawBioAssays = (JSONArray)jsonReq.get("rawBioAssays");
6387 15 Sep 21 nicklas 738         List<String> targetNames = (List<String>)jsonReq.get("targets");      
6387 15 Sep 21 nicklas 739         Number gtSoftwareId = (Number)jsonReq.get("gtSoftware");
6387 15 Sep 21 nicklas 740
6387 15 Sep 21 nicklas 741         String clusterId = (String)jsonReq.get("cluster");
6387 15 Sep 21 nicklas 742         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
6387 15 Sep 21 nicklas 743         boolean autoConfirm = Boolean.TRUE.equals(jsonReq.get("autoConfirm"));
6387 15 Sep 21 nicklas 744         Number priority = (Number)jsonReq.get("priority");
6981 17 Jan 23 nicklas 745         String partition = Values.getStringOrNull((String)jsonReq.get("partition"));
7372 06 Oct 23 nicklas 746         String submitOptions = Values.getStringOrNull((String)jsonReq.get("submitOptionsOverride"));
6387 15 Sep 21 nicklas 747
6387 15 Sep 21 nicklas 748         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
6387 15 Sep 21 nicklas 749         if (cluster == null)
6387 15 Sep 21 nicklas 750         {
6387 15 Sep 21 nicklas 751           throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
6387 15 Sep 21 nicklas 752         }
6387 15 Sep 21 nicklas 753         
6387 15 Sep 21 nicklas 754         if (targetNames.size() == 0)
6387 15 Sep 21 nicklas 755         {
6387 15 Sep 21 nicklas 756           throw new InvalidDataException("No genotyping targets have been selected");
6387 15 Sep 21 nicklas 757         }
6387 15 Sep 21 nicklas 758         List<TargetedGenotypeDefinition> targets = new ArrayList<>();
6387 15 Sep 21 nicklas 759         for (String name : targetNames)
6387 15 Sep 21 nicklas 760         {
6387 15 Sep 21 nicklas 761           TargetedGenotypeDefinition target = TargetedGenotypeJobCreator.getTargetByName(name);
6387 15 Sep 21 nicklas 762           if (target == null)
6387 15 Sep 21 nicklas 763           {
6387 15 Sep 21 nicklas 764             throw new InvalidDataException("Unknown genotyping target: " + name);
6387 15 Sep 21 nicklas 765           }
6387 15 Sep 21 nicklas 766           targets.add(target);
6387 15 Sep 21 nicklas 767         }
6387 15 Sep 21 nicklas 768
6387 15 Sep 21 nicklas 769         // Load common items
6387 15 Sep 21 nicklas 770         Software gtSoftware = gtSoftwareId != null ? Software.getById(dc, gtSoftwareId.intValue()) : null;
6387 15 Sep 21 nicklas 771         
6387 15 Sep 21 nicklas 772         List<Rawbioassay> rawBioAssays = new ArrayList<Rawbioassay>(jsonRawBioAssays.size());        
6387 15 Sep 21 nicklas 773         for (int rawNo = 0; rawNo < jsonRawBioAssays.size(); rawNo++)
6387 15 Sep 21 nicklas 774         {
6387 15 Sep 21 nicklas 775           Number rawId = (Number)jsonRawBioAssays.get(rawNo);
6387 15 Sep 21 nicklas 776           Rawbioassay raw = Rawbioassay.getById(dc, rawId.intValue());
6387 15 Sep 21 nicklas 777           rawBioAssays.add(raw);
6387 15 Sep 21 nicklas 778           
6387 15 Sep 21 nicklas 779           // Reset AUTO_PROCESSING annotation
6387 15 Sep 21 nicklas 780           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, raw.getItem(), null);
6387 15 Sep 21 nicklas 781         }
6387 15 Sep 21 nicklas 782         
6387 15 Sep 21 nicklas 783         TargetedGenotypeJobCreator jobCreator = new TargetedGenotypeJobCreator();
6387 15 Sep 21 nicklas 784         jobCreator.setAutoConfirm(autoConfirm);
6387 15 Sep 21 nicklas 785         jobCreator.setDebug(debug);
6387 15 Sep 21 nicklas 786         jobCreator.setPriority(priority == null ? null : priority.intValue());
6981 17 Jan 23 nicklas 787         jobCreator.setPartition(partition);
6387 15 Sep 21 nicklas 788         jobCreator.setSoftware(gtSoftware);
7372 06 Oct 23 nicklas 789         jobCreator.setSubmitOptionsOverride(submitOptions);
6387 15 Sep 21 nicklas 790
6387 15 Sep 21 nicklas 791         List<JobDefinition> jobDefs = jobCreator.createGenotypeJobs(dc, cluster, rawBioAssays, targets);
6387 15 Sep 21 nicklas 792         List<Job> jobs = ScriptUtil.submitJobs(dc, cluster, jobDefs);
6387 15 Sep 21 nicklas 793
6387 15 Sep 21 nicklas 794         for (Job job : jobs)
6387 15 Sep 21 nicklas 795         {
6387 15 Sep 21 nicklas 796           if (job.getStatus() == Job.Status.ERROR)
6387 15 Sep 21 nicklas 797           {
6387 15 Sep 21 nicklas 798             jsonMessages.add("[Error]Job submission for '" + job.getName() + "' failed: " + job.getStatusMessage());
6387 15 Sep 21 nicklas 799           }
6387 15 Sep 21 nicklas 800           else
6387 15 Sep 21 nicklas 801           {
6403 17 Sep 21 nicklas 802             jsonMessages.add("Submitted targeted genotyping job to " + cluster.getConnectionInfo().getName() + " with id " + job.getExternalId());
6387 15 Sep 21 nicklas 803           }
6387 15 Sep 21 nicklas 804         }
6387 15 Sep 21 nicklas 805         
6387 15 Sep 21 nicklas 806         dc.commit();
6387 15 Sep 21 nicklas 807       }
6387 15 Sep 21 nicklas 808
5685 25 Oct 19 nicklas 809       json.put("messages", jsonMessages);
5685 25 Oct 19 nicklas 810       CounterService.getInstance().setForceCount();
5685 25 Oct 19 nicklas 811     }
5685 25 Oct 19 nicklas 812     catch (Throwable t)
5685 25 Oct 19 nicklas 813     {
5685 25 Oct 19 nicklas 814       t.printStackTrace();
5685 25 Oct 19 nicklas 815       json.clear();
5685 25 Oct 19 nicklas 816       json.put("status", "error");
5685 25 Oct 19 nicklas 817       json.put("message", t.getMessage());
5685 25 Oct 19 nicklas 818       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
5685 25 Oct 19 nicklas 819     }
5685 25 Oct 19 nicklas 820     finally
5685 25 Oct 19 nicklas 821     {
5685 25 Oct 19 nicklas 822       if (dc != null) dc.close();
5685 25 Oct 19 nicklas 823       json.writeJSONString(resp.getWriter());
5766 02 Dec 19 nicklas 824       sc.setSessionSetting("variant-statistics-progress", null);
5879 25 Mar 20 nicklas 825       sc.setSessionSetting("variant-import-progress", null);
5685 25 Oct 19 nicklas 826     }
5685 25 Oct 19 nicklas 827     
5685 25 Oct 19 nicklas 828   }
5685 25 Oct 19 nicklas 829   
5685 25 Oct 19 nicklas 830   
5685 25 Oct 19 nicklas 831 }