extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/dao/SequencingRun.java

Code
Comments
Other
Rev Date Author Line
2049 07 Oct 13 nicklas 1 package net.sf.basedb.reggie.dao;
2049 07 Oct 13 nicklas 2
2049 07 Oct 13 nicklas 3 import java.util.ArrayList;
2049 07 Oct 13 nicklas 4 import java.util.Collection;
2049 07 Oct 13 nicklas 5 import java.util.List;
2049 07 Oct 13 nicklas 6
2049 07 Oct 13 nicklas 7 import org.json.simple.JSONObject;
2049 07 Oct 13 nicklas 8
2049 07 Oct 13 nicklas 9 import net.sf.basedb.core.DbControl;
2049 07 Oct 13 nicklas 10 import net.sf.basedb.core.DerivedBioAssay;
2232 17 Feb 14 nicklas 11 import net.sf.basedb.core.Extract;
2267 05 Mar 14 nicklas 12 import net.sf.basedb.core.Include;
2364 15 Apr 14 nicklas 13 import net.sf.basedb.core.InvalidDataException;
2049 07 Oct 13 nicklas 14 import net.sf.basedb.core.ItemQuery;
3699 18 Jan 16 nicklas 15 import net.sf.basedb.core.Job;
2232 17 Feb 14 nicklas 16 import net.sf.basedb.core.Type;
2049 07 Oct 13 nicklas 17 import net.sf.basedb.core.query.Expressions;
2049 07 Oct 13 nicklas 18 import net.sf.basedb.core.query.Hql;
2049 07 Oct 13 nicklas 19 import net.sf.basedb.core.query.Orders;
2049 07 Oct 13 nicklas 20 import net.sf.basedb.core.query.Restrictions;
2049 07 Oct 13 nicklas 21 import net.sf.basedb.reggie.Reggie;
2049 07 Oct 13 nicklas 22
2049 07 Oct 13 nicklas 23 /**
2049 07 Oct 13 nicklas 24   Class for loading information that is related to a sequencing run.
2049 07 Oct 13 nicklas 25   
2049 07 Oct 13 nicklas 26   @author nicklas
2049 07 Oct 13 nicklas 27   @since 2.13
2049 07 Oct 13 nicklas 28 */
2049 07 Oct 13 nicklas 29 public class SequencingRun 
2049 07 Oct 13 nicklas 30   extends ReggieItem<DerivedBioAssay>
2049 07 Oct 13 nicklas 31 {
2049 07 Oct 13 nicklas 32
2049 07 Oct 13 nicklas 33   /**
2225 13 Feb 14 nicklas 34     Value for the {@link Annotationtype#SEQUENCING_RESULT} annotation 
2225 13 Feb 14 nicklas 35     when the sequencing was successful.
2225 13 Feb 14 nicklas 36     @since 2.15
2225 13 Feb 14 nicklas 37   */
2225 13 Feb 14 nicklas 38   public static final String SEQUENCING_SUCCESSFUL = "Successful";
2225 13 Feb 14 nicklas 39   
2225 13 Feb 14 nicklas 40   /**
2225 13 Feb 14 nicklas 41     Value for the {@link Annotationtype#SEQUENCING_RESULT} annotation when
2225 13 Feb 14 nicklas 42     the sequencing failed and a new flow cell must be used for re-sequencing.
2225 13 Feb 14 nicklas 43     @since 2.15
2225 13 Feb 14 nicklas 44   */
2225 13 Feb 14 nicklas 45   public static final String SEQUENCING_FAILED = "Failed";
2225 13 Feb 14 nicklas 46   
2225 13 Feb 14 nicklas 47   /**
2225 13 Feb 14 nicklas 48     Value for the {@link Annotationtype#SEQUENCING_RESULT} annotation when
2225 13 Feb 14 nicklas 49     the first base report failed and the same flow cell can be used 
2225 13 Feb 14 nicklas 50     for re-sequencing.
2225 13 Feb 14 nicklas 51     @since 2.15
2225 13 Feb 14 nicklas 52   */
2225 13 Feb 14 nicklas 53   public static final String SEQUENCING_FIRST_BASE_REPORT_FAILED = "FirstBaseReportFailed";
2225 13 Feb 14 nicklas 54   
2225 13 Feb 14 nicklas 55   /**
2049 07 Oct 13 nicklas 56     Get a sequencing run when the id is known.
2049 07 Oct 13 nicklas 57   */
2049 07 Oct 13 nicklas 58   public static SequencingRun getById(DbControl dc, int id)
2049 07 Oct 13 nicklas 59   {
2049 07 Oct 13 nicklas 60     return new SequencingRun(DerivedBioAssay.getById(dc, id));
2049 07 Oct 13 nicklas 61   }
2049 07 Oct 13 nicklas 62   
6193 30 Mar 21 nicklas 63   public static SequencingRun get(DerivedBioAssay dba)
6193 30 Mar 21 nicklas 64   {
6193 30 Mar 21 nicklas 65     return new SequencingRun(dba);
6193 30 Mar 21 nicklas 66   }
6193 30 Mar 21 nicklas 67   
2364 15 Apr 14 nicklas 68   /**
3699 18 Jan 16 nicklas 69     Load the sequencing run represented by the given job.
3699 18 Jan 16 nicklas 70     @since 4.1
3699 18 Jan 16 nicklas 71   */
3699 18 Jan 16 nicklas 72   public static SequencingRun getByJob(DbControl dc, Job job)
3699 18 Jan 16 nicklas 73   {
3699 18 Jan 16 nicklas 74     ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
3699 18 Jan 16 nicklas 75     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
3699 18 Jan 16 nicklas 76     Subtype.SEQUENCING_RUN.addFilter(dc, query);
3699 18 Jan 16 nicklas 77     query.restrict(Restrictions.eq(Hql.property("job"), Hql.entity(job)));
3699 18 Jan 16 nicklas 78
3699 18 Jan 16 nicklas 79     List<DerivedBioAssay> list = query.list(dc);
3699 18 Jan 16 nicklas 80     if (list.size() > 1)
3699 18 Jan 16 nicklas 81     {
3699 18 Jan 16 nicklas 82       throw new InvalidDataException(
3699 18 Jan 16 nicklas 83           "More than one item with job " + job.getName() + " was found. " +
3699 18 Jan 16 nicklas 84           "This wizard can't be used until that is corrected.");
3699 18 Jan 16 nicklas 85     }
3699 18 Jan 16 nicklas 86     SequencingRun seqRun = null;
3699 18 Jan 16 nicklas 87     if (list.size() == 1)
3699 18 Jan 16 nicklas 88     {
3699 18 Jan 16 nicklas 89       seqRun = new SequencingRun(list.get(0));
3699 18 Jan 16 nicklas 90     }
3699 18 Jan 16 nicklas 91     return seqRun;
3699 18 Jan 16 nicklas 92   }
3699 18 Jan 16 nicklas 93   
3699 18 Jan 16 nicklas 94   /**
2364 15 Apr 14 nicklas 95     Load the sequencing run for a demuxed sequences item.
2364 15 Apr 14 nicklas 96     @since 2.16
2364 15 Apr 14 nicklas 97   */
2364 15 Apr 14 nicklas 98   public static SequencingRun getByDemuxedSequences(DbControl dc, DemuxedSequences demux)
2364 15 Apr 14 nicklas 99   {
2364 15 Apr 14 nicklas 100     ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
2364 15 Apr 14 nicklas 101     Subtype.SEQUENCING_RUN.addFilter(dc, query);
2364 15 Apr 14 nicklas 102     query.include(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2364 15 Apr 14 nicklas 103     
2364 15 Apr 14 nicklas 104     // Join demuxed sequences runs
2364 15 Apr 14 nicklas 105     query.join(Hql.innerJoin("children", "dx"));
2364 15 Apr 14 nicklas 106     query.restrict(Restrictions.eq(Hql.alias("dx"), Hql.entity(demux.getItem())));
2364 15 Apr 14 nicklas 107     
2364 15 Apr 14 nicklas 108     query.order(Orders.asc(Hql.property("name")));
2364 15 Apr 14 nicklas 109     
2364 15 Apr 14 nicklas 110     List<DerivedBioAssay> tmp = query.list(dc);
2049 07 Oct 13 nicklas 111   
2364 15 Apr 14 nicklas 112     DerivedBioAssay seqRun = null;
2364 15 Apr 14 nicklas 113     if (tmp.size() > 1)
2364 15 Apr 14 nicklas 114     {
2364 15 Apr 14 nicklas 115       throw new InvalidDataException(
2364 15 Apr 14 nicklas 116           "More than one sequencing run was found for " + demux.getName() +
2364 15 Apr 14 nicklas 117           "This wizard can't be used until that is corrected.");
2364 15 Apr 14 nicklas 118     }
2364 15 Apr 14 nicklas 119     else if (tmp.size() == 1)
2364 15 Apr 14 nicklas 120     {
2364 15 Apr 14 nicklas 121       seqRun = tmp.get(0);
2364 15 Apr 14 nicklas 122     }    
2364 15 Apr 14 nicklas 123     return seqRun == null ? null : new SequencingRun(seqRun);
2364 15 Apr 14 nicklas 124   }
2364 15 Apr 14 nicklas 125
2364 15 Apr 14 nicklas 126   
2232 17 Feb 14 nicklas 127   /**
2232 17 Feb 14 nicklas 128     Find all sequencing runs that include the library with the given case name.
2232 17 Feb 14 nicklas 129     This method will check for {@link Subtype#SEQUENCING_RUN} derived bioassays
2232 17 Feb 14 nicklas 130     that have a parent flow cell that include pools that contains libraries
2232 17 Feb 14 nicklas 131     with the given name.
2232 17 Feb 14 nicklas 132     @since 2.15
2232 17 Feb 14 nicklas 133   */
2232 17 Feb 14 nicklas 134   public static List<SequencingRun> findByCaseName(DbControl dc, String name)
2232 17 Feb 14 nicklas 135   {
2232 17 Feb 14 nicklas 136     // Get rid of suffixes in the name (eg. 'C' which is used for pre-neoadjuvant forms)
2232 17 Feb 14 nicklas 137     if (name.length() > 7) name = name.substring(0, 7);    
2232 17 Feb 14 nicklas 138   
2232 17 Feb 14 nicklas 139     // Find all 'Pooled library aliquot':s where the lib we are looking for is present
2232 17 Feb 14 nicklas 140     ItemQuery<Extract> poolQuery = Extract.getQuery();
2232 17 Feb 14 nicklas 141     Subtype.POOLED_LIBRARY_ALIQUOT.addFilter(dc, poolQuery);
2232 17 Feb 14 nicklas 142     poolQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2232 17 Feb 14 nicklas 143     poolQuery.join(Hql.innerJoin("parent", "pool"));
2232 17 Feb 14 nicklas 144     poolQuery.join(Hql.innerJoin("pool", "creationEvent", "poolc"));
2232 17 Feb 14 nicklas 145     poolQuery.join(Hql.innerJoin("poolc", "sources", "poolsrc"));
2232 17 Feb 14 nicklas 146     poolQuery.join(Hql.innerJoin("poolsrc", "bioMaterial", "lib"));
2232 17 Feb 14 nicklas 147     poolQuery.restrict(Restrictions.like(Hql.property("lib", "name"), Expressions.parameter("name", name+".%", Type.STRING)));
2232 17 Feb 14 nicklas 148     
2232 17 Feb 14 nicklas 149     // Find all Sequencing run items for flow cells that include any of the pools.
2232 17 Feb 14 nicklas 150     ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
2232 17 Feb 14 nicklas 151     Subtype.SEQUENCING_RUN.addFilter(dc, query);
2232 17 Feb 14 nicklas 152     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2232 17 Feb 14 nicklas 153     query.join(Hql.innerJoin(null, "physicalBioAssays", "fc", true));
2232 17 Feb 14 nicklas 154     query.join(Hql.innerJoin("fc", "creationEvent", "fcc", true));
2232 17 Feb 14 nicklas 155     query.join(Hql.innerJoin("fcc", "sources", "src"));
2232 17 Feb 14 nicklas 156     query.join(Hql.innerJoin("src", "bioMaterial", "poolA"));
2232 17 Feb 14 nicklas 157     query.restrict(Restrictions.eq(Hql.alias("poolA"), Expressions.any(poolQuery)));
2917 11 Nov 14 nicklas 158     query.order(Orders.asc(Hql.property("name")));
2232 17 Feb 14 nicklas 159     
2232 17 Feb 14 nicklas 160     return toList(query.list(dc));
2232 17 Feb 14 nicklas 161   }
2232 17 Feb 14 nicklas 162
4112 19 Sep 16 nicklas 163   /**
4112 19 Sep 16 nicklas 164     Find all sequencing runs that include the given library.
4112 19 Sep 16 nicklas 165     This method will check for {@link Subtype#SEQUENCING_RUN} derived bioassays
4112 19 Sep 16 nicklas 166     that have a parent flow cell that include pools that contains the given library.
4112 19 Sep 16 nicklas 167     @since 4.7
4112 19 Sep 16 nicklas 168   */
4112 19 Sep 16 nicklas 169   public static List<SequencingRun> findByLibrary(DbControl dc, Library lib)
4112 19 Sep 16 nicklas 170   {
4112 19 Sep 16 nicklas 171     // Find all 'Pooled library aliquot':s where the lib we are looking for is present
4112 19 Sep 16 nicklas 172     ItemQuery<Extract> poolQuery = Extract.getQuery();
4112 19 Sep 16 nicklas 173     Subtype.POOLED_LIBRARY_ALIQUOT.addFilter(dc, poolQuery);
4112 19 Sep 16 nicklas 174     poolQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
4112 19 Sep 16 nicklas 175     poolQuery.join(Hql.innerJoin("parent", "pool"));
4112 19 Sep 16 nicklas 176     poolQuery.join(Hql.innerJoin("pool", "creationEvent", "poolc"));
4112 19 Sep 16 nicklas 177     poolQuery.join(Hql.innerJoin("poolc", "sources", "poolsrc"));
4112 19 Sep 16 nicklas 178     poolQuery.join(Hql.innerJoin("poolsrc", "bioMaterial", "lib"));
4112 19 Sep 16 nicklas 179     poolQuery.restrict(Restrictions.eq(Hql.alias("lib"), Hql.entity(lib.getExtract())));
4112 19 Sep 16 nicklas 180     
4112 19 Sep 16 nicklas 181     // Find all Sequencing run items for flow cells that include any of the pools.
4112 19 Sep 16 nicklas 182     ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
4112 19 Sep 16 nicklas 183     Subtype.SEQUENCING_RUN.addFilter(dc, query);
4112 19 Sep 16 nicklas 184     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
4112 19 Sep 16 nicklas 185     query.join(Hql.innerJoin(null, "physicalBioAssays", "fc", true));
4112 19 Sep 16 nicklas 186     query.join(Hql.innerJoin("fc", "creationEvent", "fcc", true));
4112 19 Sep 16 nicklas 187     query.join(Hql.innerJoin("fcc", "sources", "src"));
4112 19 Sep 16 nicklas 188     query.join(Hql.innerJoin("src", "bioMaterial", "poolA"));
4112 19 Sep 16 nicklas 189     query.restrict(Restrictions.eq(Hql.alias("poolA"), Expressions.any(poolQuery)));
4112 19 Sep 16 nicklas 190     query.order(Orders.asc(Hql.property("name")));
4112 19 Sep 16 nicklas 191     
4112 19 Sep 16 nicklas 192     return toList(query.list(dc));
4112 19 Sep 16 nicklas 193   }
2232 17 Feb 14 nicklas 194   
6193 30 Mar 21 nicklas 195   public static SequencingRun findByFlowCell(DbControl dc, FlowCell fc)
2224 12 Feb 14 nicklas 196   {
2224 12 Feb 14 nicklas 197     ItemQuery<DerivedBioAssay> query = fc.getItem().getRootDerivedBioAssays();
2224 12 Feb 14 nicklas 198     Subtype.SEQUENCING_RUN.addFilter(dc, query);
2224 12 Feb 14 nicklas 199     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
2224 12 Feb 14 nicklas 200     query.order(Orders.asc(Hql.property("name")));
6193 30 Mar 21 nicklas 201     
6193 30 Mar 21 nicklas 202     List<DerivedBioAssay> tmp = query.list(dc);
6193 30 Mar 21 nicklas 203     
6193 30 Mar 21 nicklas 204     DerivedBioAssay seqRun = null;
6193 30 Mar 21 nicklas 205     if (tmp.size() > 1)
6193 30 Mar 21 nicklas 206     {
6193 30 Mar 21 nicklas 207       throw new InvalidDataException(
6193 30 Mar 21 nicklas 208           "More than one sequencing run was found for " + fc.getName() +
6193 30 Mar 21 nicklas 209           "This wizard can't be used until that is corrected.");
6193 30 Mar 21 nicklas 210     }
6193 30 Mar 21 nicklas 211     else if (tmp.size() == 1)
6193 30 Mar 21 nicklas 212     {
6193 30 Mar 21 nicklas 213       seqRun = tmp.get(0);
6193 30 Mar 21 nicklas 214     }
6193 30 Mar 21 nicklas 215     
6193 30 Mar 21 nicklas 216     return seqRun == null ? null : new SequencingRun(seqRun);
2224 12 Feb 14 nicklas 217   }
2224 12 Feb 14 nicklas 218   
2049 07 Oct 13 nicklas 219   public static List<SequencingRun> toList(Collection<DerivedBioAssay> bioassays)
2049 07 Oct 13 nicklas 220   {
2049 07 Oct 13 nicklas 221     List<SequencingRun> lib = new ArrayList<SequencingRun>(bioassays.size());
2049 07 Oct 13 nicklas 222     for (DerivedBioAssay ba : bioassays)
2049 07 Oct 13 nicklas 223     {
2049 07 Oct 13 nicklas 224       lib.add(new SequencingRun(ba));
2049 07 Oct 13 nicklas 225     }
2049 07 Oct 13 nicklas 226     return lib;
2049 07 Oct 13 nicklas 227   }
2049 07 Oct 13 nicklas 228   
2197 14 Jan 14 nicklas 229
2049 07 Oct 13 nicklas 230   /**
4883 04 Jul 18 nicklas 231     Generate a name for the new sequencing run. This method will search all 
4883 04 Jul 18 nicklas 232     sequencing runs and find the one with the highest numeric suffix which
4883 04 Jul 18 nicklas 233     is incremented to create the next name.
2049 07 Oct 13 nicklas 234   */
4883 04 Jul 18 nicklas 235   public static String getNextName(DbControl dc)
2049 07 Oct 13 nicklas 236   {
4895 09 Jul 18 nicklas 237     String prefix = Subtype.SEQUENCING_RUN.getItemPrefix();
2049 07 Oct 13 nicklas 238     int numDigitsInName = 4;
2049 07 Oct 13 nicklas 239     ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
2049 07 Oct 13 nicklas 240     Subtype.SEQUENCING_RUN.addFilter(dc, query);
4883 04 Jul 18 nicklas 241     query.include(Include.ALL);    
4883 04 Jul 18 nicklas 242     return ReggieItem.getNextItemName(dc, query, prefix, numDigitsInName, true);
2049 07 Oct 13 nicklas 243   }
2049 07 Oct 13 nicklas 244
2049 07 Oct 13 nicklas 245   
2049 07 Oct 13 nicklas 246   private SequencingRun(DerivedBioAssay bioAssay)
2049 07 Oct 13 nicklas 247   {
2049 07 Oct 13 nicklas 248     super(bioAssay);
2049 07 Oct 13 nicklas 249
2049 07 Oct 13 nicklas 250   }  
2049 07 Oct 13 nicklas 251   
2049 07 Oct 13 nicklas 252   /**
2049 07 Oct 13 nicklas 253     Get the derived bioassay that represents this sequencing run in BASE.
2049 07 Oct 13 nicklas 254   */
2049 07 Oct 13 nicklas 255   public DerivedBioAssay getDerivedBioAssay()
2049 07 Oct 13 nicklas 256   {
2049 07 Oct 13 nicklas 257     return getItem();
2049 07 Oct 13 nicklas 258   }
2049 07 Oct 13 nicklas 259
2049 07 Oct 13 nicklas 260   @Override
2049 07 Oct 13 nicklas 261   protected void initJSON(JSONObject json) 
2049 07 Oct 13 nicklas 262   {
2049 07 Oct 13 nicklas 263     super.initJSON(json);
2049 07 Oct 13 nicklas 264   }
2267 05 Mar 14 nicklas 265   
2267 05 Mar 14 nicklas 266   /**
2267 05 Mar 14 nicklas 267     Find the next name to give a "DemuxedSequences" child item. This assumes that
2376 24 Apr 14 nicklas 268     all child items are using the naming convention. foo.x, foo.x2, and
2267 05 Mar 14 nicklas 269     so on. NOTE! The first child item have no number!
2267 05 Mar 14 nicklas 270     @return The next unused name
2267 05 Mar 14 nicklas 271   */
2267 05 Mar 14 nicklas 272   public String getNextDemuxedSequencesName(DbControl dc)
2267 05 Mar 14 nicklas 273   {
2267 05 Mar 14 nicklas 274     DerivedBioAssay seqRun = getItem();
6193 30 Mar 21 nicklas 275     ItemQuery<DerivedBioAssay> query = null;
6193 30 Mar 21 nicklas 276     if (seqRun.isInDatabase())
6193 30 Mar 21 nicklas 277     {
6193 30 Mar 21 nicklas 278       query = seqRun.getChildren();
6193 30 Mar 21 nicklas 279       Subtype.DEMUXED_SEQUENCES.addFilter(dc, query);
6193 30 Mar 21 nicklas 280       query.setIncludes(Include.ALL);
6193 30 Mar 21 nicklas 281     }
4882 03 Jul 18 nicklas 282     return getNextChildItemName(dc, query, Subtype.DEMUXED_SEQUENCES.getItemSuffix(), true);  
2267 05 Mar 14 nicklas 283   }
2049 07 Oct 13 nicklas 284
2049 07 Oct 13 nicklas 285 }