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

Code
Comments
Other
Rev Date Author Line
3449 28 Jul 15 olle 1 package net.sf.basedb.meludi.dao;
3449 28 Jul 15 olle 2
3449 28 Jul 15 olle 3 import java.util.ArrayList;
3449 28 Jul 15 olle 4 import java.util.Collection;
3449 28 Jul 15 olle 5 import java.util.List;
3449 28 Jul 15 olle 6
3449 28 Jul 15 olle 7 import org.json.simple.JSONObject;
3449 28 Jul 15 olle 8
3449 28 Jul 15 olle 9 import net.sf.basedb.core.BioWell;
3449 28 Jul 15 olle 10 import net.sf.basedb.core.DbControl;
3449 28 Jul 15 olle 11 import net.sf.basedb.core.DerivedBioAssay;
3449 28 Jul 15 olle 12 import net.sf.basedb.core.Extract;
3449 28 Jul 15 olle 13 import net.sf.basedb.core.Include;
3449 28 Jul 15 olle 14 import net.sf.basedb.core.Item;
3449 28 Jul 15 olle 15 import net.sf.basedb.core.ItemQuery;
3449 28 Jul 15 olle 16 import net.sf.basedb.core.ItemSubtype;
3449 28 Jul 15 olle 17 import net.sf.basedb.core.Tag;
3449 28 Jul 15 olle 18 import net.sf.basedb.core.Type;
3449 28 Jul 15 olle 19 import net.sf.basedb.core.query.Expressions;
3449 28 Jul 15 olle 20 import net.sf.basedb.core.query.Hql;
3449 28 Jul 15 olle 21 import net.sf.basedb.core.query.Orders;
3449 28 Jul 15 olle 22 import net.sf.basedb.core.query.Restrictions;
3449 28 Jul 15 olle 23 import net.sf.basedb.meludi.JsonUtil;
3449 28 Jul 15 olle 24 import net.sf.basedb.meludi.Meludi;
3449 28 Jul 15 olle 25
3449 28 Jul 15 olle 26 /**
3449 28 Jul 15 olle 27   Class for loading information that is related to Library extracts.
3449 28 Jul 15 olle 28   
3449 28 Jul 15 olle 29   @author nicklas
3449 28 Jul 15 olle 30   @since 2.12
3449 28 Jul 15 olle 31 */
3449 28 Jul 15 olle 32 public class Library 
3449 28 Jul 15 olle 33   extends MeludiItem<Extract>
3449 28 Jul 15 olle 34 {
3449 28 Jul 15 olle 35   /**
3449 28 Jul 15 olle 36     Find all library items by case name. This method will check for {@link Subtype#LIBRARY}
3449 28 Jul 15 olle 37     extracts with a name matching the case name (eg. xxx.r.m.c.lib).
3449 28 Jul 15 olle 38     @since 2.13
3449 28 Jul 15 olle 39    */
3449 28 Jul 15 olle 40   public static List<Library> findByCaseName(DbControl dc, String name)
3449 28 Jul 15 olle 41   {
3449 28 Jul 15 olle 42     // Get rid of suffixes in the name (eg. 'C' which is used for pre-neoadjuvant forms)
4149 03 Oct 16 olle 43     name = Meludi.fetchRootItemName(name, dc.getSessionControl().getActiveProjectId());
3449 28 Jul 15 olle 44
3449 28 Jul 15 olle 45     // Look for a library with the given name 
3449 28 Jul 15 olle 46     ItemQuery<Extract> libQuery = Extract.getQuery();
3449 28 Jul 15 olle 47     Subtype.LIBRARY.addFilter(dc, libQuery);
3449 28 Jul 15 olle 48     libQuery.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 49     libQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", name+".%", Type.STRING)));
3449 28 Jul 15 olle 50     libQuery.order(Orders.asc(Hql.property("name")));
3449 28 Jul 15 olle 51
3449 28 Jul 15 olle 52     List<Extract> tmp = libQuery.list(dc);
3449 28 Jul 15 olle 53     List<Library> lib = new ArrayList<Library>(tmp.size());
3449 28 Jul 15 olle 54     for (Extract e : tmp)
3449 28 Jul 15 olle 55     {
3449 28 Jul 15 olle 56       lib.add(new Library(e));
3449 28 Jul 15 olle 57     }
3449 28 Jul 15 olle 58     return lib;
3449 28 Jul 15 olle 59   }
3449 28 Jul 15 olle 60
3449 28 Jul 15 olle 61
3449 28 Jul 15 olle 62   /**
3449 28 Jul 15 olle 63     Get a Library extract when the id is known.
3449 28 Jul 15 olle 64   */
3449 28 Jul 15 olle 65   public static Library getById(DbControl dc, int id)
3449 28 Jul 15 olle 66   {
3449 28 Jul 15 olle 67     return new Library(Extract.getById(dc, id));
3449 28 Jul 15 olle 68   }
3449 28 Jul 15 olle 69   
3449 28 Jul 15 olle 70   /**
3449 28 Jul 15 olle 71     @since 2.18
3449 28 Jul 15 olle 72   */
3449 28 Jul 15 olle 73   public static Library get(Extract extract)
3449 28 Jul 15 olle 74   {
3449 28 Jul 15 olle 75     return new Library(extract);
3449 28 Jul 15 olle 76   }
3449 28 Jul 15 olle 77
3449 28 Jul 15 olle 78   
3449 28 Jul 15 olle 79   @SuppressWarnings("unchecked")
3449 28 Jul 15 olle 80   public static List<Library> getByPool(DbControl dc, PooledLibrary pool)
3449 28 Jul 15 olle 81   {
5468 04 Jun 19 olle 82     //ItemQuery<Extract> libQuery = (ItemQuery<Extract>)pool.getExtract().getCreationEvent().getSources();
5468 04 Jun 19 olle 83     ItemQuery<Extract> libQuery = pool.getExtract().getCreationEvent().getSources();
3449 28 Jul 15 olle 84     Subtype.LIBRARY.addFilter(dc, libQuery);
3449 28 Jul 15 olle 85     libQuery.include(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 86     
3449 28 Jul 15 olle 87     libQuery.join(Hql.innerJoin(null, "bioWell", "bw", true));
3449 28 Jul 15 olle 88     libQuery.join(Hql.innerJoin("bw", "bioPlate", "bp", true));
3449 28 Jul 15 olle 89     
3449 28 Jul 15 olle 90     libQuery.order(Orders.asc(Hql.property("bp", "name")));
3449 28 Jul 15 olle 91     libQuery.order(Orders.asc(Hql.property("bw", "column")));
3449 28 Jul 15 olle 92     libQuery.order(Orders.asc(Hql.property("bw", "row")));
3449 28 Jul 15 olle 93     
3449 28 Jul 15 olle 94     List<Extract> tmp = libQuery.list(dc);
3449 28 Jul 15 olle 95
3449 28 Jul 15 olle 96     return toList(tmp);
3449 28 Jul 15 olle 97   }
3449 28 Jul 15 olle 98   
3449 28 Jul 15 olle 99   public static List<Library> toList(Collection<Extract> extracts)
3449 28 Jul 15 olle 100   {
3449 28 Jul 15 olle 101     List<Library> lib = new ArrayList<Library>(extracts.size());
3449 28 Jul 15 olle 102     for (Extract e : extracts)
3449 28 Jul 15 olle 103     {
3449 28 Jul 15 olle 104       lib.add(new Library(e));
3449 28 Jul 15 olle 105     }
3449 28 Jul 15 olle 106     return lib;
3449 28 Jul 15 olle 107   }
3449 28 Jul 15 olle 108   
3449 28 Jul 15 olle 109   private JSONObject jsonWell;
3449 28 Jul 15 olle 110   private JSONObject jsonBarcode;
3449 28 Jul 15 olle 111   
3449 28 Jul 15 olle 112   private Library(Extract extract)
3449 28 Jul 15 olle 113   {
3449 28 Jul 15 olle 114     super(extract);
3449 28 Jul 15 olle 115
3449 28 Jul 15 olle 116   }  
3449 28 Jul 15 olle 117   
3449 28 Jul 15 olle 118   /**
3449 28 Jul 15 olle 119     Get the real extract that represents this Library in BASE.
3449 28 Jul 15 olle 120   */
3449 28 Jul 15 olle 121   public Extract getExtract()
3449 28 Jul 15 olle 122   {
3449 28 Jul 15 olle 123     return getItem();
3449 28 Jul 15 olle 124   }
3449 28 Jul 15 olle 125
3449 28 Jul 15 olle 126   /**
3449 28 Jul 15 olle 127     Get the parent DNA item. If the parent item is not
3449 28 Jul 15 olle 128     a DNA null is returned.
3449 28 Jul 15 olle 129     @since 3.4
3449 28 Jul 15 olle 130   */
3449 28 Jul 15 olle 131   public Dna getDna(DbControl dc)
3449 28 Jul 15 olle 132   {
3449 28 Jul 15 olle 133     Extract lib = getItem();    
3449 28 Jul 15 olle 134     Extract dna = (Extract)lib.getParent();
3449 28 Jul 15 olle 135     ItemSubtype d = Subtype.DNA.load(dc);
3449 28 Jul 15 olle 136     return d.equals(dna.getItemSubtype()) ? Dna.get(dna) : null;
3449 28 Jul 15 olle 137   }
3449 28 Jul 15 olle 138
3449 28 Jul 15 olle 139   /**
3449 28 Jul 15 olle 140     Get the parent cDNA item. If the parent item is not
3449 28 Jul 15 olle 141     a cDNA null is returned.
3449 28 Jul 15 olle 142     @since 3.4
3449 28 Jul 15 olle 143   */
3449 28 Jul 15 olle 144 /*
3449 28 Jul 15 olle 145   public CDna getCDna(DbControl dc)
3449 28 Jul 15 olle 146   {
3449 28 Jul 15 olle 147     Extract lib = getItem();    
3449 28 Jul 15 olle 148     Extract cdna = (Extract)lib.getParent();
3449 28 Jul 15 olle 149     ItemSubtype c = Subtype.CDNA.load(dc);
3449 28 Jul 15 olle 150     return c.equals(cdna.getItemSubtype()) ? CDna.get(cdna) : null;
3449 28 Jul 15 olle 151   }
3449 28 Jul 15 olle 152 */
3449 28 Jul 15 olle 153
3449 28 Jul 15 olle 154   /**
3449 28 Jul 15 olle 155     Get the parent RNA or RNANormalizedAliquot item.
3449 28 Jul 15 olle 156     @param acceptPrenormalized If TRUE, the RNA item may be a prenormalized
3449 28 Jul 15 olle 157       aliquot
3449 28 Jul 15 olle 158     @since 3.4
3449 28 Jul 15 olle 159   */
3449 28 Jul 15 olle 160 /*
3449 28 Jul 15 olle 161   public Rna getRna(DbControl dc, boolean acceptPrenormalized)
3449 28 Jul 15 olle 162   {
3449 28 Jul 15 olle 163     Extract e = getItem();
3449 28 Jul 15 olle 164     ItemSubtype rnaType = Subtype.RNA.load(dc);
3449 28 Jul 15 olle 165     ItemSubtype normalizedType = Subtype.RNA_NORMALIZED_ALIQUOT.load(dc);
3449 28 Jul 15 olle 166     
3449 28 Jul 15 olle 167     while (e.getParentType() == Item.EXTRACT)
3449 28 Jul 15 olle 168     {
3449 28 Jul 15 olle 169       Extract parent = (Extract)e.getParent();
3449 28 Jul 15 olle 170       ItemSubtype parentType = parent.getItemSubtype();
3449 28 Jul 15 olle 171       if (rnaType.equals(parentType) || acceptPrenormalized && normalizedType.equals(parentType))
3449 28 Jul 15 olle 172       {
3449 28 Jul 15 olle 173         return Rna.get(parent);
3449 28 Jul 15 olle 174       }
3449 28 Jul 15 olle 175       e = parent;
3449 28 Jul 15 olle 176     }
3449 28 Jul 15 olle 177     return null;
3449 28 Jul 15 olle 178   }
3449 28 Jul 15 olle 179 */
3449 28 Jul 15 olle 180   
3449 28 Jul 15 olle 181   @SuppressWarnings("unchecked")
3449 28 Jul 15 olle 182   @Override
3449 28 Jul 15 olle 183   protected void initJSON(JSONObject json) 
3449 28 Jul 15 olle 184   {
3449 28 Jul 15 olle 185     super.initJSON(json);
3449 28 Jul 15 olle 186     if (jsonBarcode == null)
3449 28 Jul 15 olle 187     {
3449 28 Jul 15 olle 188       Tag barcode = getExtract().getTag();
3449 28 Jul 15 olle 189       jsonBarcode = new JSONObject();
3449 28 Jul 15 olle 190       if (barcode != null)
3449 28 Jul 15 olle 191       {
3449 28 Jul 15 olle 192         jsonBarcode.put("id", barcode.getId());
3449 28 Jul 15 olle 193         jsonBarcode.put("name", barcode.getName());
3449 28 Jul 15 olle 194       }
3449 28 Jul 15 olle 195     }
3449 28 Jul 15 olle 196     json.put("barcode", jsonBarcode);
3449 28 Jul 15 olle 197     if (jsonWell != null) json.put("bioWell", jsonWell);
3449 28 Jul 15 olle 198   }
3449 28 Jul 15 olle 199
3449 28 Jul 15 olle 200   /**
3449 28 Jul 15 olle 201     Load information about the plate and location the current Library
3449 28 Jul 15 olle 202     is located on.
3449 28 Jul 15 olle 203   */
3449 28 Jul 15 olle 204   public JSONObject loadBioPlateLocation()
3449 28 Jul 15 olle 205   {
3449 28 Jul 15 olle 206     if (jsonWell != null) return jsonWell;
3449 28 Jul 15 olle 207
3449 28 Jul 15 olle 208     Extract lib = getItem();
3449 28 Jul 15 olle 209     BioWell well = lib.getBioWell();
3449 28 Jul 15 olle 210
3449 28 Jul 15 olle 211     if (well != null)
3449 28 Jul 15 olle 212     {
3449 28 Jul 15 olle 213       jsonWell = JsonUtil.getBioWellAsJSON(well, true);
3449 28 Jul 15 olle 214     }
3449 28 Jul 15 olle 215
3449 28 Jul 15 olle 216     return jsonWell;
3449 28 Jul 15 olle 217   }
3449 28 Jul 15 olle 218   
3449 28 Jul 15 olle 219   /**
3449 28 Jul 15 olle 220     Get all pools with this library.
3449 28 Jul 15 olle 221     @since 2.16.2
3449 28 Jul 15 olle 222   */
3449 28 Jul 15 olle 223   public List<PooledLibrary> getPools(DbControl dc)
3449 28 Jul 15 olle 224   {
3449 28 Jul 15 olle 225     ItemQuery<Extract> query = Extract.getQuery();
3449 28 Jul 15 olle 226     Subtype.POOLED_LIBRARY.addFilter(dc, query);
3449 28 Jul 15 olle 227     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 228     
3449 28 Jul 15 olle 229     // Join libraries
3449 28 Jul 15 olle 230     query.join(Hql.innerJoin("creationEvent", "ce"));
3449 28 Jul 15 olle 231     query.join(Hql.innerJoin("ce", "sources", "src"));
3449 28 Jul 15 olle 232     query.restrict(Restrictions.eq(Hql.property("src", "bioMaterial"), Hql.entity(getItem())));
3449 28 Jul 15 olle 233     
3449 28 Jul 15 olle 234     query.order(Orders.asc(Hql.property("name")));
3449 28 Jul 15 olle 235     
3449 28 Jul 15 olle 236     return PooledLibrary.toList(query.list(dc));
3449 28 Jul 15 olle 237   }
3449 28 Jul 15 olle 238   
3449 28 Jul 15 olle 239   /**
3449 28 Jul 15 olle 240     Find the next name to give a "MergedSequences" child item. This assumes that
3449 28 Jul 15 olle 241     all child items are using the naming convention. foo.g, foo.g2, and
3449 28 Jul 15 olle 242     so on. NOTE! The first child item have no number!
3449 28 Jul 15 olle 243     @return The next unused name
3449 28 Jul 15 olle 244   */
3449 28 Jul 15 olle 245   public String getNextMergedSequencesName(DbControl dc)
3449 28 Jul 15 olle 246   {
3449 28 Jul 15 olle 247     Extract lib = getItem();
3449 28 Jul 15 olle 248     
3449 28 Jul 15 olle 249     ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
3449 28 Jul 15 olle 250 /*
3449 28 Jul 15 olle 251     Subtype.MERGED_SEQUENCES.addFilter(dc, query);
3449 28 Jul 15 olle 252 */
3449 28 Jul 15 olle 253     query.setIncludes(Include.ALL);
3449 28 Jul 15 olle 254     query.restrict(Restrictions.eq(Hql.property("extract"), Hql.entity(lib)));
3449 28 Jul 15 olle 255     long count = query.count(dc);
3449 28 Jul 15 olle 256     return lib.getName() + ".g" + (count == 0 ? "" : count+1);
3449 28 Jul 15 olle 257   }
3449 28 Jul 15 olle 258
3449 28 Jul 15 olle 259 }