Changeset 5728


Ignore:
Timestamp:
Nov 15, 2019, 11:32:32 AM (3 years ago)
Author:
Nicklas Nordborg
Message:

References #1199: Implement Variant calling pipeline

Implemented wizard for manual confirmation of variant calling.

Location:
extensions/net.sf.basedb.reggie/trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/activity/ActivityDef.java

    r5672 r5728  
    177177
    178178  /**
     179    Variant calling manually confirmed. Use {@link #merge(int)}.
     180    @since 4.24
     181  */
     182  public static final ActivityDef VARIANT_CALLING_CONFIRMED = new ActivityDef("variantcalling-confirmed", "Variant calling confirmed for 1 alignment", "Variant calling confirmed for {COUNT} alignments");
     183  /**
     184    Variant calling auto-confirmed. Use {@link #merge(int)}.
     185    @since 4.24
     186  */
     187  public static final ActivityDef VARIANT_CALLING_AUTOCONFIRMED = new ActivityDef("variantcalling-autoconfirmed", "Variant calling auto-confirmed for 1 alignment", "Variant calling auto-confirmed for {COUNT} alignments");
     188
     189  /**
    179190    Legacy pipeline manually confirmed. Use {@link #merge(int)}.
    180191  */
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/autoconfirm/VariantCallAutoConfirmer.java

    r5724 r5728  
    55import net.sf.basedb.core.RawBioAssay;
    66import net.sf.basedb.core.SessionControl;
     7import net.sf.basedb.reggie.activity.ActivityDef;
    78import net.sf.basedb.reggie.dao.Annotationtype;
    89import net.sf.basedb.reggie.dao.Datafiletype;
     
    5253    RawBioAssay raw = item(dc);
    5354    Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, raw, Rawbioassay.FEATURE_EXTRACTION_SUCCESSFUL);
     55    ActivityDef.VARIANT_CALLING_AUTOCONFIRMED.merge(dc, 1).setUser("Auto-confirm");
    5456    return false; // No next step
    5557  }
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/VariantCallingServlet.java

    r5721 r5728  
    1515import net.sf.basedb.core.ItemList;
    1616import net.sf.basedb.core.ItemNotFoundException;
     17import net.sf.basedb.core.BasicItem;
    1718import net.sf.basedb.core.DbControl;
    1819import net.sf.basedb.core.DerivedBioAssay;
     20import net.sf.basedb.core.Extract;
     21import net.sf.basedb.core.File;
    1922import net.sf.basedb.core.Item;
    2023import net.sf.basedb.core.ItemQuery;
    2124import net.sf.basedb.core.Job;
     25import net.sf.basedb.core.RawBioAssay;
    2226import net.sf.basedb.core.SessionControl;
    2327import net.sf.basedb.core.Software;
     28import net.sf.basedb.core.Trashcan;
     29import net.sf.basedb.core.query.Annotations;
     30import net.sf.basedb.core.query.Expressions;
    2431import net.sf.basedb.core.query.Hql;
    2532import net.sf.basedb.core.query.Orders;
     33import net.sf.basedb.core.query.Restrictions;
    2634import net.sf.basedb.core.snapshot.SnapshotManager;
    2735import net.sf.basedb.opengrid.JobDefinition;
     
    3038import net.sf.basedb.reggie.JsonUtil;
    3139import net.sf.basedb.reggie.Reggie;
     40import net.sf.basedb.reggie.activity.ActivityDef;
    3241import net.sf.basedb.reggie.counter.CounterService;
    3342import net.sf.basedb.reggie.dao.AlignedSequences;
    3443import net.sf.basedb.reggie.dao.Annotationtype;
    3544import net.sf.basedb.reggie.dao.BiomaterialList;
     45import net.sf.basedb.reggie.dao.Datafiletype;
    3646import net.sf.basedb.reggie.dao.Library;
     47import net.sf.basedb.reggie.dao.Rawbioassay;
     48import net.sf.basedb.reggie.dao.Rawdatatype;
    3749import net.sf.basedb.reggie.dao.ReggieRole;
    3850import net.sf.basedb.reggie.dao.Rna;
     
    115127        json.put("alignedSequences", jsonAlignedSequences);
    116128      }
    117 
     129      else if ("GetUnconfirmed".equals(cmd))
     130      {
     131        dc = sc.newDbControl();
     132        ItemQuery<RawBioAssay> query = RawBioAssay.getQuery();
     133        query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
     134        Rawdatatype.VARIANT_CALL.addFilter(dc, query);
     135       
     136        // The job must be ended
     137        query.join(Hql.innerJoin("job", "jb"));
     138        query.restrict(Restrictions.neq(Hql.property("jb", "ended"), null));
     139        // Must NOT have a ANALYSIS_RESULT annotation
     140        query.join(Annotations.leftJoin(null, Annotationtype.ANALYSIS_RESULT.load(dc), "ar"));
     141        query.restrict(Restrictions.eq(Hql.alias("ar"), null));
     142        // Ignore if AutoProcess==AutoConfirm
     143        query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
     144        query.restrict(
     145            Restrictions.or(
     146              Restrictions.neq(Hql.alias("ap"), Expressions.string("AutoConfirm")),
     147              Restrictions.eq(Hql.alias("ap"), null)
     148            ));
     149        // Join LibPlate for sorting
     150        query.join(Hql.innerJoin(null, "parentExtract", "lib", true));
     151        query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
     152        query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
     153        query.order(Orders.asc(Hql.property("libPlate", "id")));
     154        query.order(Orders.asc(Hql.property("name")));
     155        query.setMaxResults(250);
     156       
     157        SnapshotManager manager = new SnapshotManager();
     158        List<Rawbioassay> list = Rawbioassay.toList(query.list(dc));
     159        JSONArray jsonRawBioAssays = new JSONArray();
     160        for (Rawbioassay raw : list)
     161        {
     162          RawBioAssay rba = raw.getItem();
     163          raw.setAnnotation("spots", rba.getNumFileSpots());
     164          raw.loadAnnotations(dc, manager, "DataFilesFolder", Annotationtype.DATA_FILES_FOLDER, null);
     165         
     166          DerivedBioAssay alignment = rba.getParentBioAssay();
     167          raw.setAnnotation("VariantsRaw", Annotationtype.VARIANTS_RAW.getAnnotationValue(dc, manager, alignment));
     168          raw.setAnnotation("CallableBases", Annotationtype.CALLABLE_BASES.getAnnotationValue(dc, manager, alignment));
     169         
     170          File vcf = raw.getFile(dc, Datafiletype.VCF);
     171          if (vcf != null) raw.setAnnotation("vcfFile", vcf.getPath().toString());
     172         
     173          // Load job information
     174          Job job = rba.getJob();
     175          JSONObject jsonJob = JsonUtil.getJobAsJSON(job);
     176          raw.setAnnotation("vcallJob", jsonJob);
     177
     178          // Load library information
     179          Library lib = raw.getLibrary(dc);
     180          Rna r = lib.getRna(dc, true);
     181          lib.loadBioPlateLocation();
     182          lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
     183          raw.setAnnotation("lib", lib.asJSONObject());
     184         
     185          jsonRawBioAssays.add(raw.asJSONObject());
     186        }
     187       
     188        json.put("rawBioAssays", jsonRawBioAssays);
     189      }
    118190   
    119191    }
     
    224296        dc.commit();
    225297      }
     298      else if ("ConfirmVariantCalling".equals(cmd))
     299      {
     300        dc = sc.newDbControl();
     301
     302        ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
     303
     304        JSONObject jsonReq = JsonUtil.parseRequest(req);
     305        JSONArray jsonRawBioAssays = (JSONArray)jsonReq.get("rawBioAssays");
     306
     307        int numConfirmed = 0;
     308        int numUnconfirmed = 0;
     309        int numReRun = 0;
     310        int numFlaggedRna = 0;
     311        int numDeleted = 0;
     312       
     313        ItemList flaggedRna = BiomaterialList.FLAGGED_RNA.load(dc);
     314        ItemList vcallPipeline = BiomaterialList.VARIANT_CALLING_PIPELINE.load(dc);
     315       
     316        boolean deleteItemsCreatedByFailedJobs = Boolean.TRUE.equals(jsonReq.get("deleteItemsCreatedByFailedJobs"));
     317        List<BasicItem> toDelete = new ArrayList<BasicItem>();
     318       
     319        for (int rawNo = 0; rawNo < jsonRawBioAssays.size(); ++rawNo)
     320        {
     321          JSONObject jsonRaw = (JSONObject)jsonRawBioAssays.get(rawNo);
     322          Number rawdId = (Number)jsonRaw.get("id");
     323         
     324          Rawbioassay raw = Rawbioassay.getById(dc, rawdId.intValue());
     325          AlignedSequences aligned = raw.getAlignedSequences(dc);
     326         
     327          DerivedBioAssay alignedDBA = aligned.getDerivedBioAssay();
     328          RawBioAssay rba = raw.getRawBioAssay();
     329          Job job = rba.getJob();
     330          Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rba, null);
     331         
     332          boolean flag = Boolean.TRUE.equals(jsonRaw.get("flag"));
     333          if (flag && !Reggie.isExternalItem(raw.getName()))
     334          {
     335            Library lib = Library.get(rba.getParentExtract());
     336            Rna rna = lib.getRna(dc, false);
     337            Extract r = rna.getItem();
     338            Annotationtype.FLAG.setAnnotationValue(dc, r, Rna.FLAG_STRINGTIE_FAILED);
     339            flaggedRna.add(r);
     340            numFlaggedRna++;
     341          }
     342         
     343          String comment = Values.getStringOrNull((String)jsonRaw.get("comment"));
     344          rba.setDescription(comment);
     345         
     346          boolean reRun = Boolean.TRUE.equals(jsonRaw.get("rerun"));
     347          boolean confirm = Boolean.TRUE.equals(jsonRaw.get("confirm"));
     348          boolean deleted = false;
     349
     350          if (reRun)
     351          {
     352            // Set ReProcess annotation on aligned sequences item
     353            Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, aligned.getItem(), "ReProcess");
     354            vcallPipeline.add(aligned.getItem());
     355            numReRun++;
     356           
     357            if (deleteItemsCreatedByFailedJobs && !confirm)
     358            {
     359              // Delete rawbioassay and related items
     360              rba.setRemoved(true);
     361              toDelete.add(rba);
     362              toDelete.addAll(Reggie.removeAttachedFiles(dc, rba));
     363              deleted = true;
     364              numDeleted++;
     365              aligned.getItem().setDescription(comment);
     366            }
     367          }
     368         
     369          if (!deleted)
     370          {
     371            if (confirm)
     372            {
     373              Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, rba, Rawbioassay.FEATURE_EXTRACTION_SUCCESSFUL);
     374              numConfirmed++;
     375            }
     376            else if (reRun || flag)
     377            {
     378              Annotationtype.ANALYSIS_RESULT.setAnnotationValue(dc, rba, Rawbioassay.FEATURE_EXTRACTION_FAILED);
     379            }
     380            else
     381            {
     382              numUnconfirmed++;
     383            }
     384          }
     385        }
     386       
     387        if (numConfirmed > 0)
     388        {
     389          jsonMessages.add("Variant calling confirmed for " + numConfirmed + " alignments");
     390          ActivityDef.VARIANT_CALLING_CONFIRMED.merge(dc, numConfirmed);
     391        }
     392        else
     393        {
     394          jsonMessages.add("No variant callings confirmed");
     395        }
     396       
     397        if (numReRun > 0)
     398        {
     399          jsonMessages.add(numReRun + " alignments flagged for new variant calling");
     400        }
     401       
     402        if (numDeleted > 0)
     403        {
     404          jsonMessages.add(numDeleted + " variant callings deleted due to failure");
     405          if (toDelete.size() > numDeleted)
     406          {
     407            jsonMessages.add((toDelete.size() - numDeleted) + " linked items (eg. parent items and files) deleted");
     408          }
     409        }
     410       
     411        if (numFlaggedRna > 0)
     412        {
     413          jsonMessages.add("Added " + numFlaggedRna + " RNA items to the '" + flaggedRna.getName() + "' list");
     414        }
     415       
     416        if (numUnconfirmed > 0)
     417        {
     418          jsonMessages.add(numUnconfirmed + " variant callings remain unconfirmed");
     419        }
     420        dc.commit();
     421        if (toDelete.size() > 0)
     422        {
     423          try
     424          {
     425            Trashcan.delete(sc, toDelete, false, null);
     426          }
     427          catch (RuntimeException ex)
     428          {
     429            ex.printStackTrace();
     430            jsonMessages.add("[Error]Could not delete all items created by failed jobs: " + ex.getMessage());
     431          }
     432        }
     433      }
    226434
    227435      json.put("messages", jsonMessages);
Note: See TracChangeset for help on using the changeset viewer.