extensions/net.sf.basedb.meludi/trunk/src/net/sf/basedb/meludi/dao/Rna.java

Code
Comments
Other
Rev Date Author Line
3028 11 Dec 14 olle 1 package net.sf.basedb.meludi.dao;
3028 11 Dec 14 olle 2
3028 11 Dec 14 olle 3 import java.util.ArrayList;
3028 11 Dec 14 olle 4 import java.util.Collection;
3028 11 Dec 14 olle 5 import java.util.Date;
3028 11 Dec 14 olle 6 import java.util.List;
3028 11 Dec 14 olle 7
3028 11 Dec 14 olle 8 import org.json.simple.JSONObject;
3028 11 Dec 14 olle 9
5115 20 Nov 18 olle 10 import net.sf.basedb.core.AnnotationSimpleRestriction;
5115 20 Nov 18 olle 11 import net.sf.basedb.core.AnnotationType;
3028 11 Dec 14 olle 12 import net.sf.basedb.core.BioPlate;
3028 11 Dec 14 olle 13 import net.sf.basedb.core.BioWell;
3028 11 Dec 14 olle 14 import net.sf.basedb.core.DbControl;
3028 11 Dec 14 olle 15 import net.sf.basedb.core.Extract;
3028 11 Dec 14 olle 16 import net.sf.basedb.core.Include;
3180 17 Mar 15 olle 17 import net.sf.basedb.core.InvalidDataException;
3028 11 Dec 14 olle 18 import net.sf.basedb.core.ItemQuery;
3176 06 Mar 15 olle 19 import net.sf.basedb.core.MeasuredBioMaterial;
5115 20 Nov 18 olle 20 import net.sf.basedb.core.Operator;
3176 06 Mar 15 olle 21 import net.sf.basedb.core.Sample;
3028 11 Dec 14 olle 22 import net.sf.basedb.core.Type;
3028 11 Dec 14 olle 23 import net.sf.basedb.core.query.Expressions;
3028 11 Dec 14 olle 24 import net.sf.basedb.core.query.Hql;
3028 11 Dec 14 olle 25 import net.sf.basedb.core.query.Orders;
3028 11 Dec 14 olle 26 import net.sf.basedb.core.query.Restrictions;
3028 11 Dec 14 olle 27 import net.sf.basedb.meludi.JsonUtil;
3028 11 Dec 14 olle 28 import net.sf.basedb.meludi.Meludi;
3028 11 Dec 14 olle 29
3028 11 Dec 14 olle 30 /**
3028 11 Dec 14 olle 31   Class for loading information that is related to RNA extracts.
3028 11 Dec 14 olle 32   
3028 11 Dec 14 olle 33   @author nicklas
3028 11 Dec 14 olle 34   @since 2.4
3028 11 Dec 14 olle 35 */
3028 11 Dec 14 olle 36 public class Rna 
3028 11 Dec 14 olle 37   extends MeludiItem<Extract>
3028 11 Dec 14 olle 38 {
3028 11 Dec 14 olle 39
3028 11 Dec 14 olle 40   /**
3028 11 Dec 14 olle 41     Flag value for the {@link Annotationtype#FLAG} annotation when a RNA doesn't have
3028 11 Dec 14 olle 42     enough remaining quantity to be auto-selected for library preparation. Additional
3028 11 Dec 14 olle 43     processing of backup lysate or speciman may be required.
3028 11 Dec 14 olle 44     @since 2.12
3028 11 Dec 14 olle 45   */
3028 11 Dec 14 olle 46   public static final String FLAG_NOT_ENOUGH_REMAINING_QUANTITY = "NotEnoughRemainingQuantity";
3028 11 Dec 14 olle 47
3028 11 Dec 14 olle 48   /**
3028 11 Dec 14 olle 49     Flag value for the {@link Annotationtype#FLAG} annotation when a RNA doesn't have
3028 11 Dec 14 olle 50     a high enough quality score (RQS or RIN) to be auto-selected for library preparation. 
3028 11 Dec 14 olle 51     Additional processing of backup lysate or speciman may be required.
3028 11 Dec 14 olle 52     @since 2.12
3028 11 Dec 14 olle 53   */
3028 11 Dec 14 olle 54   public static final String FLAG_LOW_QUALITY_SCORE = "LowQualityScore";
3028 11 Dec 14 olle 55
3028 11 Dec 14 olle 56   /**
3028 11 Dec 14 olle 57     Flag value for the {@link Annotationtype#FLAG} annotation when a RNA
3028 11 Dec 14 olle 58     has been manually flagged for some other reason. The desription of
3028 11 Dec 14 olle 59     the RNA item should contain more information.
3028 11 Dec 14 olle 60     @since 2.12
3028 11 Dec 14 olle 61   */
3028 11 Dec 14 olle 62   public static final String FLAG_MANUAL = "ManualFlag";
3028 11 Dec 14 olle 63   
3028 11 Dec 14 olle 64   /**
3028 11 Dec 14 olle 65     Flag value for the {@link Annotationtype#FLAG} annotation when the mRNA
3028 11 Dec 14 olle 66     processing failed. RNA on the failed plate need to be re-processed on a 
3028 11 Dec 14 olle 67     new plate.
3028 11 Dec 14 olle 68     @since 2.12
3028 11 Dec 14 olle 69   */
3028 11 Dec 14 olle 70   public static final String FLAG_MRNA_PLATE_FAILED = "mRNAPlateFailed";
3028 11 Dec 14 olle 71
3028 11 Dec 14 olle 72   /**
3028 11 Dec 14 olle 73     Flag value for the {@link Annotationtype#FLAG} annotation when the cDNA
3028 11 Dec 14 olle 74     processing failed. RNA on the failed plate need to be re-processed on a 
3028 11 Dec 14 olle 75     new plate.
3028 11 Dec 14 olle 76     @since 2.12
3028 11 Dec 14 olle 77   */
3028 11 Dec 14 olle 78   public static final String FLAG_CDNA_PLATE_FAILED = "cDNAPlateFailed";
3028 11 Dec 14 olle 79
3028 11 Dec 14 olle 80   /**
3028 11 Dec 14 olle 81     Flag value for the {@link Annotationtype#FLAG} annotation when the Library
3028 11 Dec 14 olle 82     processing failed (for an entire plate). RNA on the failed plate need to be 
3028 11 Dec 14 olle 83     re-processed on a new plate.
3028 11 Dec 14 olle 84     @since 2.12
3028 11 Dec 14 olle 85   */
3028 11 Dec 14 olle 86   public static final String FLAG_LIB_PLATE_FAILED = "LibPlateFailed";
3028 11 Dec 14 olle 87
3028 11 Dec 14 olle 88   /**
3028 11 Dec 14 olle 89     Flag value for the {@link Annotationtype#FLAG} annotation when a Library
3028 11 Dec 14 olle 90     item has been excluded from a pool. The parent RNA need to be
3028 11 Dec 14 olle 91     re-processed on a new plate.
3028 11 Dec 14 olle 92     @since 2.12
3028 11 Dec 14 olle 93   */
3028 11 Dec 14 olle 94   public static final String FLAG_EXCLUDED_FROM_POOL = "ExcludedFromPool";
3028 11 Dec 14 olle 95
3028 11 Dec 14 olle 96   /**
3028 11 Dec 14 olle 97     Flag value for the {@link Annotationtype#FLAG} annotation when
3028 11 Dec 14 olle 98     a library has been sequenced but the demux step failed. Eg.
3028 11 Dec 14 olle 99     not enough sequences for the library.
3028 11 Dec 14 olle 100     @since 2.16
3028 11 Dec 14 olle 101   */
3028 11 Dec 14 olle 102   public static final String FLAG_DEMUX_FAILED = "DemuxFailed";
3028 11 Dec 14 olle 103
3028 11 Dec 14 olle 104   /**
3028 11 Dec 14 olle 105     Flag value for the {@link Annotationtype#FLAG} annotation when
3028 11 Dec 14 olle 106     a library has been sequenced but the alignment step failed. Eg.
3028 11 Dec 14 olle 107     not enough sequences that matches the target genome.
3028 11 Dec 14 olle 108     @since 2.16
3028 11 Dec 14 olle 109   */
3028 11 Dec 14 olle 110   public static final String FLAG_ALIGN_FAILED = "AlignFailed";
3028 11 Dec 14 olle 111
3028 11 Dec 14 olle 112   /**
3028 11 Dec 14 olle 113     Flag value for the {@link Annotationtype#FLAG} annotation when
3028 11 Dec 14 olle 114     a library has been sequenced but the cufflinks step failed.
3028 11 Dec 14 olle 115     @since 2.17
3028 11 Dec 14 olle 116   */
3028 11 Dec 14 olle 117   public static final String FLAG_CUFFLINKS_FAILED = "CufflinksFailed";
3028 11 Dec 14 olle 118
3028 11 Dec 14 olle 119   /**
3028 11 Dec 14 olle 120     The name of the RNA item in BASE that represents Generic Stratagene.
3028 11 Dec 14 olle 121     Tracked Stratagene should use the pattern 'Stratagene.rNNN' where
3028 11 Dec 14 olle 122     'NNN' is a number.
3028 11 Dec 14 olle 123     @since 2.12
3028 11 Dec 14 olle 124   */
3028 11 Dec 14 olle 125   public static final String STRATAGENE = "Stratagene.r";
3028 11 Dec 14 olle 126
3028 11 Dec 14 olle 127   /**
3028 11 Dec 14 olle 128     The name of the RNA item in BASE that represents an external extract.
3028 11 Dec 14 olle 129     @since 2.12
3028 11 Dec 14 olle 130   */
3028 11 Dec 14 olle 131   public static final String EXTERNAL = "External.r";
3028 11 Dec 14 olle 132   
3028 11 Dec 14 olle 133   /**
3028 11 Dec 14 olle 134     Checks if an item with the given name is a Stratagene RNA or a 
3028 11 Dec 14 olle 135     derivative from a Stratagene RNA.
3028 11 Dec 14 olle 136     @since 2.12
3028 11 Dec 14 olle 137   */
3028 11 Dec 14 olle 138   public static boolean isStratagene(String name)
3028 11 Dec 14 olle 139   {
3028 11 Dec 14 olle 140     return name.startsWith(STRATAGENE);
3028 11 Dec 14 olle 141   }
3028 11 Dec 14 olle 142   
3028 11 Dec 14 olle 143   /**
3028 11 Dec 14 olle 144     Checks if an item with the given name is a "special" RNA or derivate from
3028 11 Dec 14 olle 145     a special RNA (eg. Stratagene or External RNA).
3028 11 Dec 14 olle 146     @since 2.12
3028 11 Dec 14 olle 147   */
3028 11 Dec 14 olle 148   public static boolean isSpecialRna(String name)
3028 11 Dec 14 olle 149   {
3028 11 Dec 14 olle 150     return name.startsWith(STRATAGENE) || name.startsWith(EXTERNAL);
3028 11 Dec 14 olle 151   }
3028 11 Dec 14 olle 152   
3028 11 Dec 14 olle 153   /**
3028 11 Dec 14 olle 154     Find RNA extracts that have not yet been quality controlled. The query will look for
3028 11 Dec 14 olle 155     extract with the {@link Subtype#RNA} subtype that has no child extract with
3028 11 Dec 14 olle 156     the {@link Subtype#RNAQC} subtype.
3028 11 Dec 14 olle 157   */
3044 16 Dec 14 olle 158 /*
3028 11 Dec 14 olle 159   public static List<Rna> findRnaWithoutQc(DbControl dc)
3028 11 Dec 14 olle 160   {
3028 11 Dec 14 olle 161     // Create a query that load all RNA extracts that has at least one RNAQC child extract
3028 11 Dec 14 olle 162     ItemQuery<Extract> subquery = Extract.getQuery();
3028 11 Dec 14 olle 163     subquery.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3028 11 Dec 14 olle 164     // Filter on RNA subtype
3028 11 Dec 14 olle 165     Subtype.RNA.addFilter(dc, subquery);
3028 11 Dec 14 olle 166     
3028 11 Dec 14 olle 167     // Join child items and filter on RNAQC subtype
3028 11 Dec 14 olle 168     subquery.join(Hql.innerJoin("childCreationEvents", "cce"));
3028 11 Dec 14 olle 169     subquery.join(Hql.innerJoin("cce", "event", "evt", null, false));
3028 11 Dec 14 olle 170     subquery.join(Hql.innerJoin("evt", "bioMaterial", "bm", null, false));
3028 11 Dec 14 olle 171     Subtype.RNAQC.addFilter(dc, subquery, "bm");
3028 11 Dec 14 olle 172     
3028 11 Dec 14 olle 173     ItemQuery<Extract> query = Extract.getQuery();
3028 11 Dec 14 olle 174     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3028 11 Dec 14 olle 175     // Filter on RNA subtype
3028 11 Dec 14 olle 176     Subtype.RNA.addFilter(dc, query);
3028 11 Dec 14 olle 177     // Filter on id not equal to any that has a RNAQC child
3028 11 Dec 14 olle 178     query.restrict(Restrictions.neq(Hql.property("id"), Expressions.all(subquery)));
3028 11 Dec 14 olle 179     // Filter on creation date==null and parent != null (eg. to get rid of 'Stratagene')
3028 11 Dec 14 olle 180     query.join(Hql.innerJoin("creationEvent", "ce"));
3028 11 Dec 14 olle 181     query.restrict(Restrictions.neq(Hql.property("ce", "eventDate"), null));
3028 11 Dec 14 olle 182     query.restrict(Restrictions.neq(Hql.property("parent"), null));
3028 11 Dec 14 olle 183     
3028 11 Dec 14 olle 184     query.order(Orders.asc(Hql.property("id")));
3028 11 Dec 14 olle 185     
3028 11 Dec 14 olle 186     return toRna(query.list(dc));
3028 11 Dec 14 olle 187   }
3044 16 Dec 14 olle 188 */
3028 11 Dec 14 olle 189   
3028 11 Dec 14 olle 190   /**
3028 11 Dec 14 olle 191     Find all Stratagene RNA that is available for selection in the
3028 11 Dec 14 olle 192     mRNA wizards. This should include all 'Stratagene.rNNN' where
3028 11 Dec 14 olle 193     'NNN' is a number that has a registered location on a bioplate, and
3028 11 Dec 14 olle 194     the generic 'Stratagene.r' extract.
3028 11 Dec 14 olle 195   */
3028 11 Dec 14 olle 196   public static List<Rna> findStratagene(DbControl dc)
3028 11 Dec 14 olle 197   {
3028 11 Dec 14 olle 198     ItemQuery<Extract> query = Extract.getQuery();
3028 11 Dec 14 olle 199     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3028 11 Dec 14 olle 200     // Join bioplate
3028 11 Dec 14 olle 201     query.join(Hql.leftJoin(null, "bioWell", "bw", null, true));
3028 11 Dec 14 olle 202     query.join(Hql.leftJoin("bw", "bioPlate", "bp", null, true));
3028 11 Dec 14 olle 203     
3028 11 Dec 14 olle 204     // Filter on RNA subtype
3028 11 Dec 14 olle 205     Subtype.RNA.addFilter(dc, query);
3028 11 Dec 14 olle 206     
3028 11 Dec 14 olle 207     // Filter on name='Stratagene.r' OR name ~ 'Stratagene.rNNN'
3028 11 Dec 14 olle 208     query.restrict(
3028 11 Dec 14 olle 209       Restrictions.or(
3028 11 Dec 14 olle 210         Restrictions.eq(Hql.property("name"), Expressions.string(Rna.STRATAGENE)),
3028 11 Dec 14 olle 211         Restrictions.and(
3028 11 Dec 14 olle 212           Restrictions.rlike(Hql.property("name"), Expressions.string("^"+ Rna.STRATAGENE + "[0-9]+$")),
3028 11 Dec 14 olle 213           Restrictions.neq(Hql.property("bioWell"), null)
3028 11 Dec 14 olle 214         )
3028 11 Dec 14 olle 215       )
3028 11 Dec 14 olle 216     );
3028 11 Dec 14 olle 217
3028 11 Dec 14 olle 218     // Sort by bioplate position -- those without plate are sorted last by name
3028 11 Dec 14 olle 219     query.order(Orders.asc(Hql.expression("coalesce(bp.name, 'zzzz')", null)));
3028 11 Dec 14 olle 220     query.order(Orders.asc(Hql.property("bw", "row")));
3028 11 Dec 14 olle 221     query.order(Orders.asc(Hql.property("bw", "column")));
3028 11 Dec 14 olle 222     query.order(Orders.asc(Hql.property("name")));
3028 11 Dec 14 olle 223     
3028 11 Dec 14 olle 224     return toRna(query.list(dc));
3028 11 Dec 14 olle 225   }
3028 11 Dec 14 olle 226   
3028 11 Dec 14 olle 227   /**
3028 11 Dec 14 olle 228     Find all RNA items by case name. This method will check for {@link Subtype#RNA}
3028 11 Dec 14 olle 229     extracts with a name matching the case name (eg. xxx.r).
3028 11 Dec 14 olle 230     @since 2.11
3028 11 Dec 14 olle 231   */
3028 11 Dec 14 olle 232   public static List<Rna> findByCaseName(DbControl dc, String name)
3028 11 Dec 14 olle 233   {
3423 24 Jun 15 olle 234     // Remove suffix starting with dot "."
3423 24 Jun 15 olle 235     int firstDotIndex = name.indexOf(".");
3423 24 Jun 15 olle 236     if (firstDotIndex >= 0)
3423 24 Jun 15 olle 237     {
3423 24 Jun 15 olle 238       name = name.substring(0, firstDotIndex);
3423 24 Jun 15 olle 239     }
3423 24 Jun 15 olle 240
3028 11 Dec 14 olle 241     // Look for a rna with the given name 
3028 11 Dec 14 olle 242     ItemQuery<Extract> rnaQuery = Extract.getQuery();
3028 11 Dec 14 olle 243     Subtype.RNA.addFilter(dc, rnaQuery);
3028 11 Dec 14 olle 244     rnaQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", name+".%", Type.STRING)));
3028 11 Dec 14 olle 245     
3028 11 Dec 14 olle 246     List<Extract> tmp = rnaQuery.list(dc);
3028 11 Dec 14 olle 247     List<Rna> rna = new ArrayList<Rna>(tmp.size());
3028 11 Dec 14 olle 248     for (Extract e : tmp)
3028 11 Dec 14 olle 249     {
3028 11 Dec 14 olle 250       rna.add(new Rna(e));
3028 11 Dec 14 olle 251     }
5115 20 Nov 18 olle 252
5115 20 Nov 18 olle 253     if (rna.size() == 0)
5115 20 Nov 18 olle 254     {
5115 20 Nov 18 olle 255       // Try to use specimen name as input
5115 20 Nov 18 olle 256       ItemQuery<Sample> specimenQuery = Sample.getQuery();
5115 20 Nov 18 olle 257       String caseId = name;
5115 20 Nov 18 olle 258       AnnotationType caseIdType = Annotationtype.CASE_ID.load(dc);
5115 20 Nov 18 olle 259       specimenQuery = Sample.getQuery();
5115 20 Nov 18 olle 260       Subtype.SPECIMEN.addFilter(dc, specimenQuery);
5115 20 Nov 18 olle 261       specimenQuery.restrict(new AnnotationSimpleRestriction(null, caseIdType, Operator.EQ, caseId, true, false));
5115 20 Nov 18 olle 262       specimenQuery.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
5115 20 Nov 18 olle 263       specimenQuery.order(Orders.asc(Hql.property("name")));
5115 20 Nov 18 olle 264
5115 20 Nov 18 olle 265       // Look for an rna with the given name 
5115 20 Nov 18 olle 266       rnaQuery = Extract.getQuery();
5115 20 Nov 18 olle 267
5115 20 Nov 18 olle 268       for (Sample s : specimenQuery.list(dc))
5115 20 Nov 18 olle 269       {    
5115 20 Nov 18 olle 270         String specName = s.getName();
5115 20 Nov 18 olle 271         rnaQuery.reset();
5115 20 Nov 18 olle 272         Subtype.RNA.addFilter(dc, rnaQuery);
5115 20 Nov 18 olle 273         rnaQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", specName+".%", Type.STRING)));
5115 20 Nov 18 olle 274         
5115 20 Nov 18 olle 275         tmp = rnaQuery.list(dc);
5115 20 Nov 18 olle 276         for (Extract e : tmp)
5115 20 Nov 18 olle 277         {
5115 20 Nov 18 olle 278           rna.add(new Rna(e));
5115 20 Nov 18 olle 279         }
5115 20 Nov 18 olle 280       }
5115 20 Nov 18 olle 281     }
3028 11 Dec 14 olle 282     return rna;
3028 11 Dec 14 olle 283   }
3028 11 Dec 14 olle 284
3028 11 Dec 14 olle 285   /**
3180 17 Mar 15 olle 286     Find an RNA item with the given name.
3180 17 Mar 15 olle 287     @return An RNA item, or null if not found.
3180 17 Mar 15 olle 288   */
3180 17 Mar 15 olle 289   public static Rna findByName(DbControl dc, String name)
3180 17 Mar 15 olle 290   {
3180 17 Mar 15 olle 291     Rna rna = null;
3180 17 Mar 15 olle 292
3180 17 Mar 15 olle 293     ItemQuery<Extract> rnaQuery = Extract.getQuery();
3180 17 Mar 15 olle 294     Subtype.RNA.addFilter(dc, rnaQuery);
3180 17 Mar 15 olle 295     rnaQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string(name)));
3180 17 Mar 15 olle 296     rnaQuery.order(Orders.desc(Hql.property("name")));        
3180 17 Mar 15 olle 297     rnaQuery.include(Include.ALL);
3180 17 Mar 15 olle 298
3180 17 Mar 15 olle 299     List<Extract> tmp = rnaQuery.list(dc);
3180 17 Mar 15 olle 300     if (tmp.size() > 1)
3180 17 Mar 15 olle 301     {
3344 19 May 15 olle 302       String extractIds = "";
3344 19 May 15 olle 303       for (int i = 0; i < tmp.size(); i++)
3344 19 May 15 olle 304       {
3344 19 May 15 olle 305         Rna tmpRna = new Rna(tmp.get(i));
3344 19 May 15 olle 306         int id = tmpRna.getItem().getId();
3344 19 May 15 olle 307         if (!extractIds.equals(""))
3344 19 May 15 olle 308         {
3344 19 May 15 olle 309           extractIds += ", ";
3344 19 May 15 olle 310         }
3344 19 May 15 olle 311         extractIds += id;
3344 19 May 15 olle 312       }
3180 17 Mar 15 olle 313       throw new InvalidDataException(
3180 17 Mar 15 olle 314         "More than one RNA item with the name " + name + " was found. " +
3344 19 May 15 olle 315         " The ID values are " + extractIds + ". " +
3180 17 Mar 15 olle 316         "This wizard can't be used until that is corrected.");
3180 17 Mar 15 olle 317     }
3180 17 Mar 15 olle 318     if (tmp.size() == 1)
3180 17 Mar 15 olle 319     {
3180 17 Mar 15 olle 320       rna = new Rna(tmp.get(0));
3180 17 Mar 15 olle 321     }
3180 17 Mar 15 olle 322     return rna;
3180 17 Mar 15 olle 323 }
3180 17 Mar 15 olle 324
3180 17 Mar 15 olle 325   /**
3176 06 Mar 15 olle 326     Find unquantified RNA. An RNA item that has not been quantified
3176 06 Mar 15 olle 327     has a 'null' {@link MeasuredBioMaterial#getOriginalQuantity()}
3176 06 Mar 15 olle 328     and has no extracts.
3176 06 Mar 15 olle 329     @since 1.2
3176 06 Mar 15 olle 330    */
3176 06 Mar 15 olle 331   public static List<Rna> findUnquantifiedRna(DbControl dc)
3176 06 Mar 15 olle 332   {
3176 06 Mar 15 olle 333     List<Rna> unQuantifiedRna = new ArrayList<Rna>();
3176 06 Mar 15 olle 334
3176 06 Mar 15 olle 335     ItemQuery<Extract> extractQuery = Extract.getQuery();
3176 06 Mar 15 olle 336     Subtype.RNA.addFilter(dc, extractQuery);
3176 06 Mar 15 olle 337     extractQuery.restrict(Restrictions.eq(Hql.property("originalQuantity"), null));
3176 06 Mar 15 olle 338     extractQuery.order(Orders.asc(Hql.property("id")));
3176 06 Mar 15 olle 339     extractQuery.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3176 06 Mar 15 olle 340     List<Extract> extracts = extractQuery.list(dc);
3176 06 Mar 15 olle 341     for (Extract extract : extracts)
3176 06 Mar 15 olle 342     {
3176 06 Mar 15 olle 343       if (extract.countExtracts() == 0)
3176 06 Mar 15 olle 344       {
3176 06 Mar 15 olle 345         unQuantifiedRna.add(new Rna(extract));
3176 06 Mar 15 olle 346       }
3176 06 Mar 15 olle 347     }
3176 06 Mar 15 olle 348     return unQuantifiedRna;
3176 06 Mar 15 olle 349   }
3176 06 Mar 15 olle 350
3176 06 Mar 15 olle 351   /**
3028 11 Dec 14 olle 352     Get a RNA extract when the id is known.
3028 11 Dec 14 olle 353   */
3028 11 Dec 14 olle 354   public static Rna getById(DbControl dc, int id)
3028 11 Dec 14 olle 355   {
3028 11 Dec 14 olle 356     return new Rna(Extract.getById(dc, id));
3028 11 Dec 14 olle 357   }
3028 11 Dec 14 olle 358   
3028 11 Dec 14 olle 359   public static Rna get(Extract extract)
3028 11 Dec 14 olle 360   {
3028 11 Dec 14 olle 361     return new Rna(extract);
3028 11 Dec 14 olle 362   }
3028 11 Dec 14 olle 363   
3028 11 Dec 14 olle 364   public static List<Rna> toRna(Collection<Extract> extracts)
3028 11 Dec 14 olle 365   {
3028 11 Dec 14 olle 366     List<Rna> rna = new ArrayList<Rna>(extracts.size());
3028 11 Dec 14 olle 367     for (Extract e : extracts)
3028 11 Dec 14 olle 368     {
3028 11 Dec 14 olle 369       rna.add(new Rna(e));
3028 11 Dec 14 olle 370     }
3028 11 Dec 14 olle 371     return rna;
3028 11 Dec 14 olle 372   }
3028 11 Dec 14 olle 373   
3028 11 Dec 14 olle 374   private JSONObject jsonWell;
3028 11 Dec 14 olle 375   
3028 11 Dec 14 olle 376   private Rna(Extract extract)
3028 11 Dec 14 olle 377   {
3028 11 Dec 14 olle 378     super(extract);
3028 11 Dec 14 olle 379
3028 11 Dec 14 olle 380   }  
3028 11 Dec 14 olle 381   
3028 11 Dec 14 olle 382   
3028 11 Dec 14 olle 383   /**
3028 11 Dec 14 olle 384     Get the real extract that represents this RNA in BASE.
3028 11 Dec 14 olle 385   */
3028 11 Dec 14 olle 386   public Extract getExtract()
3028 11 Dec 14 olle 387   {
3028 11 Dec 14 olle 388     return getItem();
3028 11 Dec 14 olle 389   }
3028 11 Dec 14 olle 390
3028 11 Dec 14 olle 391   @SuppressWarnings("unchecked")
3028 11 Dec 14 olle 392   @Override
3028 11 Dec 14 olle 393   protected void initJSON(JSONObject json) 
3028 11 Dec 14 olle 394   {
3028 11 Dec 14 olle 395     super.initJSON(json);
3028 11 Dec 14 olle 396     if (jsonWell != null) json.put("bioWell", jsonWell);
3028 11 Dec 14 olle 397     
3028 11 Dec 14 olle 398   }
3028 11 Dec 14 olle 399
3028 11 Dec 14 olle 400   /**
3028 11 Dec 14 olle 401     Find the next name to give a RNAQC child item. This assumes that
3028 11 Dec 14 olle 402     all child items are using the naming convention. foo.qc, foo.qc2, and
3028 11 Dec 14 olle 403     so on. NOTE! The first child item have no number!
3028 11 Dec 14 olle 404     @return The next unused name
3028 11 Dec 14 olle 405   */
3044 16 Dec 14 olle 406 /*
3028 11 Dec 14 olle 407   public String getNextRnaQcName(DbControl dc)
3028 11 Dec 14 olle 408   {
3028 11 Dec 14 olle 409     Extract rna = getItem();
3028 11 Dec 14 olle 410     
3028 11 Dec 14 olle 411     ItemQuery<Extract> query = rna.getChildExtracts();
3028 11 Dec 14 olle 412     Subtype.RNAQC.addFilter(dc, query);
3028 11 Dec 14 olle 413     
3028 11 Dec 14 olle 414     query.include(Include.ALL);
3028 11 Dec 14 olle 415     long count = query.count(dc);
3028 11 Dec 14 olle 416     return rna.getName() + ".qc" + (count == 0 ? "" : count+1);
3028 11 Dec 14 olle 417   }
3044 16 Dec 14 olle 418 */
3028 11 Dec 14 olle 419   
3028 11 Dec 14 olle 420   /**
3028 11 Dec 14 olle 421     Find the last RNAQC child item that contains either a BA_RIN or CA_RQS value.
3028 11 Dec 14 olle 422     @return The RNAQC item
3028 11 Dec 14 olle 423   */
3044 16 Dec 14 olle 424 /*
3028 11 Dec 14 olle 425   public Extract findLastRnaQc(DbControl dc)
3028 11 Dec 14 olle 426   {
3028 11 Dec 14 olle 427     Extract rna = getExtract();
3028 11 Dec 14 olle 428     Extract lastQc = null;
3028 11 Dec 14 olle 429     
3028 11 Dec 14 olle 430     // Fetch all child RNAQC items
3028 11 Dec 14 olle 431     ItemQuery<Extract> rnaqcQuery = rna.getChildExtracts();
3028 11 Dec 14 olle 432     Subtype.RNAQC.addFilter(dc, rnaqcQuery);
3028 11 Dec 14 olle 433     rnaqcQuery.include(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3028 11 Dec 14 olle 434     List<Extract> rnaqc = rnaqcQuery.list(dc);
3028 11 Dec 14 olle 435
3028 11 Dec 14 olle 436     if (rnaqc.size() > 0)
3028 11 Dec 14 olle 437     {
3028 11 Dec 14 olle 438       Date lastQcDate = null;
3028 11 Dec 14 olle 439       // Try to find the last QC run on either Caliper or BioAnalyzer
3028 11 Dec 14 olle 440       // This should be stored in QC_RUN_DATE annotation on the BioPlate
3028 11 Dec 14 olle 441       // If no bioplate is associated with the RNA QC item, use the
3028 11 Dec 14 olle 442       // creation date
3028 11 Dec 14 olle 443       
3028 11 Dec 14 olle 444       for (Extract qc : rnaqc)
3028 11 Dec 14 olle 445       {
3028 11 Dec 14 olle 446         Float rqs = (Float)Annotationtype.CA_RQS.getAnnotationValue(dc, qc);
3028 11 Dec 14 olle 447         Float rin = (Float)Annotationtype.BA_RIN.getAnnotationValue(dc, qc);
3028 11 Dec 14 olle 448         // Skip RNAQC items without any quality score since it may
3028 11 Dec 14 olle 449         // be a new NanoDrop measurement
3028 11 Dec 14 olle 450         if (rqs == null && rin == null) continue;
3028 11 Dec 14 olle 451         
3028 11 Dec 14 olle 452         BioPlate plate = null;
3028 11 Dec 14 olle 453         BioWell well = qc.getBioWell();
3028 11 Dec 14 olle 454         Date qcDate = null;
3028 11 Dec 14 olle 455         // Try to get QC date from RNAQC reaction plate
3028 11 Dec 14 olle 456         if (well != null)
3028 11 Dec 14 olle 457         {
3028 11 Dec 14 olle 458           plate = well.getPlate();
3028 11 Dec 14 olle 459           qcDate = (Date)Annotationtype.QC_RUN_DATE.getAnnotationValue(dc, plate);
3028 11 Dec 14 olle 460         }
3028 11 Dec 14 olle 461         // If not found, get creation date from extract
3028 11 Dec 14 olle 462         if (qcDate == null)
3028 11 Dec 14 olle 463         {
3028 11 Dec 14 olle 464           qcDate = qc.getCreationEvent().getEventDate();
3028 11 Dec 14 olle 465         }
3028 11 Dec 14 olle 466         
3028 11 Dec 14 olle 467         // Use the current RNAQC if this is the first found or if the date is after
3028 11 Dec 14 olle 468         if (lastQc == null || lastQcDate != null && qcDate != null && qcDate.after(lastQcDate))
3028 11 Dec 14 olle 469         {
3028 11 Dec 14 olle 470           lastQcDate = qcDate;
3028 11 Dec 14 olle 471           lastQc = qc;
3028 11 Dec 14 olle 472         }
3028 11 Dec 14 olle 473       }
3028 11 Dec 14 olle 474     }
3028 11 Dec 14 olle 475     return lastQc;
3028 11 Dec 14 olle 476   }
3044 16 Dec 14 olle 477 */  
3044 16 Dec 14 olle 478
3044 16 Dec 14 olle 479 /*
3028 11 Dec 14 olle 480   public Float loadRnaQc(DbControl dc)
3028 11 Dec 14 olle 481   {
3028 11 Dec 14 olle 482     Float rqs = null;
3028 11 Dec 14 olle 483     Float rin = null;
3028 11 Dec 14 olle 484     
3028 11 Dec 14 olle 485     Extract qc = findLastRnaQc(dc);
3028 11 Dec 14 olle 486     if (qc != null)
3028 11 Dec 14 olle 487     {
3028 11 Dec 14 olle 488       rqs = (Float)Annotationtype.CA_RQS.getAnnotationValue(dc, qc);
3028 11 Dec 14 olle 489       rin = (Float)Annotationtype.BA_RIN.getAnnotationValue(dc, qc);
3028 11 Dec 14 olle 490       if (rqs != null) setAnnotation("rqs", rqs);
3028 11 Dec 14 olle 491       if (rin != null) setAnnotation("rin", rin);
3028 11 Dec 14 olle 492     }
3028 11 Dec 14 olle 493     return rqs != null ? rqs : rin;
3028 11 Dec 14 olle 494   }
3044 16 Dec 14 olle 495 */
3028 11 Dec 14 olle 496   
3028 11 Dec 14 olle 497   /**
3028 11 Dec 14 olle 498     Load information about the plate and location the current RNA
3028 11 Dec 14 olle 499     is located on.
3028 11 Dec 14 olle 500   */
3028 11 Dec 14 olle 501   public JSONObject loadBioPlateLocation()
3028 11 Dec 14 olle 502   {
3028 11 Dec 14 olle 503     if (jsonWell == null)
3028 11 Dec 14 olle 504     {
3028 11 Dec 14 olle 505       jsonWell = JsonUtil.getBioWellAsJSON(getItem().getBioWell(), true);
3028 11 Dec 14 olle 506     }
3028 11 Dec 14 olle 507     return jsonWell;
3028 11 Dec 14 olle 508   }
3028 11 Dec 14 olle 509   
3028 11 Dec 14 olle 510 }