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

Code
Comments
Other
Rev Date Author Line
1637 07 May 12 nicklas 1 package net.sf.basedb.reggie.dao;
1637 07 May 12 nicklas 2
1637 07 May 12 nicklas 3 import java.util.ArrayList;
4089 08 Sep 16 nicklas 4 import java.util.Collection;
1637 07 May 12 nicklas 5 import java.util.List;
1637 07 May 12 nicklas 6
1637 07 May 12 nicklas 7 import org.json.simple.JSONObject;
1637 07 May 12 nicklas 8
1637 07 May 12 nicklas 9 import net.sf.basedb.core.DbControl;
1637 07 May 12 nicklas 10 import net.sf.basedb.core.Extract;
4905 11 Jul 18 nicklas 11 import net.sf.basedb.core.Include;
1637 07 May 12 nicklas 12 import net.sf.basedb.core.ItemQuery;
3166 05 Mar 15 nicklas 13 import net.sf.basedb.core.Sample;
1831 08 Feb 13 nicklas 14 import net.sf.basedb.core.Type;
4093 12 Sep 16 nicklas 15 import net.sf.basedb.core.query.Annotations;
1831 08 Feb 13 nicklas 16 import net.sf.basedb.core.query.Expressions;
1637 07 May 12 nicklas 17 import net.sf.basedb.core.query.Hql;
1637 07 May 12 nicklas 18 import net.sf.basedb.core.query.Orders;
4093 12 Sep 16 nicklas 19 import net.sf.basedb.core.query.Restriction;
1637 07 May 12 nicklas 20 import net.sf.basedb.core.query.Restrictions;
1826 07 Feb 13 nicklas 21 import net.sf.basedb.reggie.JsonUtil;
1637 07 May 12 nicklas 22 import net.sf.basedb.reggie.Reggie;
1637 07 May 12 nicklas 23
1637 07 May 12 nicklas 24 /**
1637 07 May 12 nicklas 25   Class for loading information that is related to RNA extracts.
1637 07 May 12 nicklas 26   
1637 07 May 12 nicklas 27   @author nicklas
1637 07 May 12 nicklas 28   @since 2.4
1637 07 May 12 nicklas 29 */
1637 07 May 12 nicklas 30 public class Lysate 
1637 07 May 12 nicklas 31   extends ReggieItem<Extract>
1637 07 May 12 nicklas 32 {
1637 07 May 12 nicklas 33
1637 07 May 12 nicklas 34   
1637 07 May 12 nicklas 35   /**
1637 07 May 12 nicklas 36     Find Lysate extracts that have not yet been processed. The query will look for
1637 07 May 12 nicklas 37     extract with the {@link Subtype#LYSATE} subtype that has no 'created' date.
1637 07 May 12 nicklas 38     The list is sorted by bioplate and position.
1637 07 May 12 nicklas 39   */
4093 12 Sep 16 nicklas 40   public static List<Lysate> findUnprocessedLysates(DbControl dc, boolean includeReProcess)
1637 07 May 12 nicklas 41   {
1637 07 May 12 nicklas 42     List<Lysate> lysate = new ArrayList<Lysate>();
1637 07 May 12 nicklas 43     
1637 07 May 12 nicklas 44     // Create a query that load all Lysate extracts without created date
1637 07 May 12 nicklas 45     ItemQuery<Extract> query = Extract.getQuery();
1637 07 May 12 nicklas 46     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1637 07 May 12 nicklas 47     // Filter on Lysate subtype
1637 07 May 12 nicklas 48     Subtype.LYSATE.addFilter(dc, query);
1637 07 May 12 nicklas 49     
7170 15 May 23 nicklas 50     // Default criteria is to only include items with no created date and no AutoProcessing
7170 15 May 23 nicklas 51     query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
7170 15 May 23 nicklas 52     Restriction criteria = Restrictions.and(
7170 15 May 23 nicklas 53         Restrictions.eq(Hql.property("ce", "eventDate"), null),
7170 15 May 23 nicklas 54         Restrictions.eq(Hql.alias("ap"), null));
4093 12 Sep 16 nicklas 55     if (includeReProcess)
4093 12 Sep 16 nicklas 56     {
4093 12 Sep 16 nicklas 57       // Also include items with AutoProcessing=ReProcess AND stored in ExtraLys* box
4093 12 Sep 16 nicklas 58       criteria = Restrictions.or(
4093 12 Sep 16 nicklas 59         criteria,
4093 12 Sep 16 nicklas 60         Restrictions.and(
4093 12 Sep 16 nicklas 61             Restrictions.eq(Hql.alias("ap"), Expressions.string("ReProcess")),
4093 12 Sep 16 nicklas 62             Restrictions.like(Hql.property("bp", "name"), Expressions.string("ExtraLys%"))
4093 12 Sep 16 nicklas 63         ));
4093 12 Sep 16 nicklas 64       // Sort ReProcess lysates last
4093 12 Sep 16 nicklas 65       query.order(Orders.desc(Hql.alias("ap")));
4093 12 Sep 16 nicklas 66     }
4093 12 Sep 16 nicklas 67     query.restrict(criteria);
4093 12 Sep 16 nicklas 68     
1637 07 May 12 nicklas 69     // Join the creation event and bioplate
1637 07 May 12 nicklas 70     query.join(Hql.innerJoin("creationEvent", "ce"));
1637 07 May 12 nicklas 71     query.join(Hql.innerJoin(null, "bioWell", "bw", true));
1637 07 May 12 nicklas 72     query.join(Hql.innerJoin("bw", "bioPlate", "bp", true));
1637 07 May 12 nicklas 73     
1637 07 May 12 nicklas 74     // Sort by bioplate position
1637 07 May 12 nicklas 75     query.order(Orders.asc(Hql.property("bp", "name")));
1637 07 May 12 nicklas 76     query.order(Orders.asc(Hql.property("bw", "row")));
1637 07 May 12 nicklas 77     query.order(Orders.asc(Hql.property("bw", "column")));
1637 07 May 12 nicklas 78         
1637 07 May 12 nicklas 79     List<Extract> extracts = query.list(dc);
1637 07 May 12 nicklas 80     for (Extract e : extracts)
1637 07 May 12 nicklas 81     {
1637 07 May 12 nicklas 82       lysate.add(new Lysate(e));
1637 07 May 12 nicklas 83     }
1637 07 May 12 nicklas 84     return lysate;
1637 07 May 12 nicklas 85   }
1637 07 May 12 nicklas 86   
1637 07 May 12 nicklas 87   /**
1831 08 Feb 13 nicklas 88     Find all lysate items by case name. This method will check for {@link Subtype#LYSATE} extracts 
1831 08 Feb 13 nicklas 89     with a name matching the case name (eg. xxx.l).
1831 08 Feb 13 nicklas 90     @since 2.11
1831 08 Feb 13 nicklas 91   */
1831 08 Feb 13 nicklas 92   public static List<Lysate> findByCaseName(DbControl dc, String name)
1831 08 Feb 13 nicklas 93   {
1831 08 Feb 13 nicklas 94     // Get rid of suffixes in the name (eg. 'C' which is used for pre-neoadjuvant forms)
1831 08 Feb 13 nicklas 95     if (name.length() > 7) name = name.substring(0, 7);    
1831 08 Feb 13 nicklas 96     
1831 08 Feb 13 nicklas 97     // Look for a blood case with the given name 
1831 08 Feb 13 nicklas 98     ItemQuery<Extract> lysateQuery = Extract.getQuery();
1831 08 Feb 13 nicklas 99     Subtype.LYSATE.addFilter(dc, lysateQuery);
2917 11 Nov 14 nicklas 100     lysateQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1831 08 Feb 13 nicklas 101     lysateQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", name+".%", Type.STRING)));
2917 11 Nov 14 nicklas 102     lysateQuery.order(Orders.asc(Hql.property("name")));
2917 11 Nov 14 nicklas 103
1831 08 Feb 13 nicklas 104     List<Extract> tmp = lysateQuery.list(dc);
1831 08 Feb 13 nicklas 105     List<Lysate> lysate = new ArrayList<Lysate>(tmp.size());
1831 08 Feb 13 nicklas 106     for (Extract e : tmp)
1831 08 Feb 13 nicklas 107     {
1831 08 Feb 13 nicklas 108       lysate.add(new Lysate(e));
1831 08 Feb 13 nicklas 109     }
1831 08 Feb 13 nicklas 110     return lysate;
1831 08 Feb 13 nicklas 111   }
1831 08 Feb 13 nicklas 112
1831 08 Feb 13 nicklas 113   /**
1637 07 May 12 nicklas 114     Get a Lysate when the id is known.
1637 07 May 12 nicklas 115   */
1637 07 May 12 nicklas 116   public static Lysate getById(DbControl dc, int id)
1637 07 May 12 nicklas 117   {
1637 07 May 12 nicklas 118     return new Lysate(Extract.getById(dc, id));
1637 07 May 12 nicklas 119   }
1637 07 May 12 nicklas 120   
3166 05 Mar 15 nicklas 121   /**
3166 05 Mar 15 nicklas 122     @since 3.2
3166 05 Mar 15 nicklas 123   */
3166 05 Mar 15 nicklas 124   public static Lysate get(Extract extract)
3166 05 Mar 15 nicklas 125   {
7142 04 May 23 nicklas 126     return extract == null ? null : new Lysate(extract);
3166 05 Mar 15 nicklas 127   }
3166 05 Mar 15 nicklas 128
4089 08 Sep 16 nicklas 129   
4089 08 Sep 16 nicklas 130   public static List<Lysate> toLysate(Collection<Extract> extracts)
4089 08 Sep 16 nicklas 131   {
4089 08 Sep 16 nicklas 132     List<Lysate> lysates = new ArrayList<Lysate>(extracts.size());
4089 08 Sep 16 nicklas 133     for (Extract e : extracts)
4089 08 Sep 16 nicklas 134     {
4089 08 Sep 16 nicklas 135       lysates.add(new Lysate(e));
4089 08 Sep 16 nicklas 136     }
4089 08 Sep 16 nicklas 137     return lysates;
4089 08 Sep 16 nicklas 138   }
4089 08 Sep 16 nicklas 139
1637 07 May 12 nicklas 140   private JSONObject jsonWell;
1637 07 May 12 nicklas 141   
1637 07 May 12 nicklas 142   private Lysate(Extract extract)
1637 07 May 12 nicklas 143   {
1637 07 May 12 nicklas 144     super(extract);
1637 07 May 12 nicklas 145
1637 07 May 12 nicklas 146   }  
1637 07 May 12 nicklas 147   
1637 07 May 12 nicklas 148   
1637 07 May 12 nicklas 149   /**
1637 07 May 12 nicklas 150     Get the real extract that represents this RNA in BASE.
1637 07 May 12 nicklas 151   */
1637 07 May 12 nicklas 152   public Extract getExtract()
1637 07 May 12 nicklas 153   {
1637 07 May 12 nicklas 154     return getItem();
1637 07 May 12 nicklas 155   }
3166 05 Mar 15 nicklas 156   
3166 05 Mar 15 nicklas 157   /**
3166 05 Mar 15 nicklas 158     @since 3.2
3166 05 Mar 15 nicklas 159   */
3166 05 Mar 15 nicklas 160   public SpecimenTube getSpecimen()
3166 05 Mar 15 nicklas 161   {
3166 05 Mar 15 nicklas 162     Extract lysate = getItem();    
3166 05 Mar 15 nicklas 163     Sample specimen = (Sample)lysate.getParent();
3166 05 Mar 15 nicklas 164     return SpecimenTube.get(specimen);
3166 05 Mar 15 nicklas 165   }
1637 07 May 12 nicklas 166
1637 07 May 12 nicklas 167   @Override
1637 07 May 12 nicklas 168   protected void initJSON(JSONObject json) 
1637 07 May 12 nicklas 169   {
1637 07 May 12 nicklas 170     super.initJSON(json);
1637 07 May 12 nicklas 171     if (jsonWell != null) json.put("bioWell", jsonWell);
1637 07 May 12 nicklas 172     
1637 07 May 12 nicklas 173   }
1637 07 May 12 nicklas 174
1637 07 May 12 nicklas 175   /**
4095 13 Sep 16 nicklas 176     Find the next name to give a child item. This assumes that
4095 13 Sep 16 nicklas 177     all child items are using the naming convention. foo.x, foo.x2, and
4095 13 Sep 16 nicklas 178     so on. NOTE! The first child item have no number!
4095 13 Sep 16 nicklas 179     @return The next unused name
4905 11 Jul 18 nicklas 180     @since 4.19
4095 13 Sep 16 nicklas 181   */
4905 11 Jul 18 nicklas 182   public String getNextChildName(DbControl dc, Subtype childType)
4095 13 Sep 16 nicklas 183   {
4095 13 Sep 16 nicklas 184     Extract lysate = getItem();
6193 30 Mar 21 nicklas 185     ItemQuery<Extract> query = null;
6193 30 Mar 21 nicklas 186     if (lysate.isInDatabase())
6193 30 Mar 21 nicklas 187     {
6193 30 Mar 21 nicklas 188       query = lysate.getChildExtracts();
6193 30 Mar 21 nicklas 189       childType.addFilter(dc, query);
6193 30 Mar 21 nicklas 190       query.include(Include.ALL);
6193 30 Mar 21 nicklas 191     }
4905 11 Jul 18 nicklas 192     return getNextChildItemName(dc, query, childType.getItemSuffix(), true);
4095 13 Sep 16 nicklas 193   }
4095 13 Sep 16 nicklas 194
4095 13 Sep 16 nicklas 195   
4095 13 Sep 16 nicklas 196   /**
1637 07 May 12 nicklas 197     Load information about the plate and location the current RNA
1637 07 May 12 nicklas 198     is located on.
1637 07 May 12 nicklas 199   */
1637 07 May 12 nicklas 200   public JSONObject loadBioPlateLocation()
1637 07 May 12 nicklas 201   {
1826 07 Feb 13 nicklas 202     if (jsonWell == null)
1637 07 May 12 nicklas 203     {
2134 11 Nov 13 nicklas 204       jsonWell = JsonUtil.getBioWellAsJSON(getItem().getBioWell(), true);
1637 07 May 12 nicklas 205     }
1637 07 May 12 nicklas 206     return jsonWell;
1637 07 May 12 nicklas 207   }
1637 07 May 12 nicklas 208   
1637 07 May 12 nicklas 209 }