extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/plugins/release/LibraryWriter.java

Code
Comments
Other
Rev Date Author Line
3937 16 May 16 nicklas 1 package net.sf.basedb.reggie.plugins.release;
3937 16 May 16 nicklas 2
4470 26 Apr 17 nicklas 3 import java.util.Date;
4469 26 Apr 17 nicklas 4 import java.util.List;
4469 26 Apr 17 nicklas 5
4378 03 Mar 17 nicklas 6 import org.json.simple.JSONArray;
4371 02 Mar 17 nicklas 7 import org.json.simple.JSONObject;
4371 02 Mar 17 nicklas 8
3937 16 May 16 nicklas 9 import net.sf.basedb.core.BioMaterialEvent;
4378 03 Mar 17 nicklas 10 import net.sf.basedb.core.BioPlate;
4470 26 Apr 17 nicklas 11 import net.sf.basedb.core.BioPlateType;
3937 16 May 16 nicklas 12 import net.sf.basedb.core.BioWell;
3937 16 May 16 nicklas 13 import net.sf.basedb.core.DbControl;
3937 16 May 16 nicklas 14 import net.sf.basedb.core.Extract;
4378 03 Mar 17 nicklas 15 import net.sf.basedb.core.Item;
4470 26 Apr 17 nicklas 16 import net.sf.basedb.core.Quantity;
4378 03 Mar 17 nicklas 17 import net.sf.basedb.core.Type;
3937 16 May 16 nicklas 18 import net.sf.basedb.reggie.Reggie;
3937 16 May 16 nicklas 19 import net.sf.basedb.reggie.dao.Annotationtype;
4470 26 Apr 17 nicklas 20 import net.sf.basedb.reggie.dao.BioplateType;
5549 08 Aug 19 nicklas 21 import net.sf.basedb.reggie.dao.Dna;
5101 16 Nov 18 nicklas 22 import net.sf.basedb.reggie.dao.Library;
5549 08 Aug 19 nicklas 23 import net.sf.basedb.reggie.dao.Pipeline;
5101 16 Nov 18 nicklas 24 import net.sf.basedb.reggie.dao.Rna;
4378 03 Mar 17 nicklas 25 import net.sf.basedb.reggie.json.FilteredJSONArray;
4378 03 Mar 17 nicklas 26 import net.sf.basedb.util.filter.NotNullFilter;
3937 16 May 16 nicklas 27 import net.sf.basedb.util.formatter.WellCoordinateFormatter;
4470 26 Apr 17 nicklas 28 import net.sf.basedb.util.units.UnitUtil;
3937 16 May 16 nicklas 29
3937 16 May 16 nicklas 30 /**
3937 16 May 16 nicklas 31   Cohort writer implementation for the library data table.
3937 16 May 16 nicklas 32   @since 4.5
3937 16 May 16 nicklas 33 */
3937 16 May 16 nicklas 34 public class LibraryWriter 
3937 16 May 16 nicklas 35   extends CohortWriter 
3937 16 May 16 nicklas 36 {
3937 16 May 16 nicklas 37
3937 16 May 16 nicklas 38   private final WellCoordinateFormatter rowF;
3937 16 May 16 nicklas 39   private final WellCoordinateFormatter colF;
5101 16 Nov 18 nicklas 40   private final BioPlateType neoPrepType;
5871 20 Mar 20 nicklas 41   private final BioPlateType libType;
5871 20 Mar 20 nicklas 42   private final BioPlateType externalLibType;
3937 16 May 16 nicklas 43   
5090 14 Nov 18 nicklas 44   public LibraryWriter(DbControl dc, ReleaseWriterOptions options)
3937 16 May 16 nicklas 45   {
5090 14 Nov 18 nicklas 46     super(dc, options);
3937 16 May 16 nicklas 47     this.rowF = new WellCoordinateFormatter(true);
3937 16 May 16 nicklas 48     this.colF = new WellCoordinateFormatter(false);
5101 16 Nov 18 nicklas 49     this.neoPrepType = BioplateType.NEOPREP.load(dc);
5871 20 Mar 20 nicklas 50     this.libType = BioplateType.LIBRARY.load(dc);
5871 20 Mar 20 nicklas 51     this.externalLibType = BioplateType.EXTERNAL_LIBRARY.load(dc);
3937 16 May 16 nicklas 52   }
3937 16 May 16 nicklas 53   
4371 02 Mar 17 nicklas 54   
4371 02 Mar 17 nicklas 55   @Override
5091 14 Nov 18 nicklas 56   public JSONArray toJSONObjects(CohortItem item)
4371 02 Mar 17 nicklas 57   {
5549 08 Aug 19 nicklas 58     JSONArray json = new JSONArray();
5101 16 Nov 18 nicklas 59     
5549 08 Aug 19 nicklas 60     addRnaSeqLibs(json, item);
5549 08 Aug 19 nicklas 61     addMipsLibs(json, item);
5549 08 Aug 19 nicklas 62     
5549 08 Aug 19 nicklas 63     return json.size() == 0 ? null : json;
5549 08 Aug 19 nicklas 64     
5549 08 Aug 19 nicklas 65   }
5549 08 Aug 19 nicklas 66   
5549 08 Aug 19 nicklas 67   private void addRnaSeqLibs(JSONArray json, CohortItem item)
5549 08 Aug 19 nicklas 68   {
5549 08 Aug 19 nicklas 69     List<Library> libraries = item.getLibraries(Pipeline.RNA_SEQ);
5549 08 Aug 19 nicklas 70     if (libraries.size() == 0) return;
5549 08 Aug 19 nicklas 71     
4470 26 Apr 17 nicklas 72     DbControl dc = item.getDbControl();
5101 16 Nov 18 nicklas 73     for (Library library : libraries)
5101 16 Nov 18 nicklas 74     {
5101 16 Nov 18 nicklas 75       Extract lib = library.getItem();
5101 16 Nov 18 nicklas 76       BioMaterialEvent creationEvent = lib.getCreationEvent();
5101 16 Nov 18 nicklas 77       Rna rna = library.getRna(dc, false);
5101 16 Nov 18 nicklas 78       
5101 16 Nov 18 nicklas 79       JSONObject jsonLib = new JSONObject();
5101 16 Nov 18 nicklas 80       jsonLib.put("name", item.toReleaseId(lib.getName()));
5101 16 Nov 18 nicklas 81       jsonLib.put("type", lib.getType().name());
5101 16 Nov 18 nicklas 82       jsonLib.put("subtype", getName(lib.getItemSubtype()));
5101 16 Nov 18 nicklas 83       jsonLib.put("parent", item.toReleaseId(rna.getName()));
5101 16 Nov 18 nicklas 84   
5101 16 Nov 18 nicklas 85       JSONArray jsonAnnotations = new FilteredJSONArray(new NotNullFilter<>(false));
5101 16 Nov 18 nicklas 86       jsonLib.put("annotations", jsonAnnotations);
5549 08 Aug 19 nicklas 87       jsonAnnotations.add(item.getAnnotationJSON(Annotationtype.PIPELINE, lib, null));
5101 16 Nov 18 nicklas 88       jsonAnnotations.add(item.createAnnotationJSON("Protocol", getName(creationEvent.getProtocol())));
5101 16 Nov 18 nicklas 89       jsonAnnotations.add(item.createAnnotationJSON("Barcode", getName(lib.getTag())));
5101 16 Nov 18 nicklas 90   
5101 16 Nov 18 nicklas 91       BioWell well = lib.getBioWell();
5101 16 Nov 18 nicklas 92       BioPlate libPlate = well.getPlate();
5101 16 Nov 18 nicklas 93       BioPlateType libPlateType = libPlate.getBioPlateType();
5101 16 Nov 18 nicklas 94       
5871 20 Mar 20 nicklas 95       Date libDate = null;
5871 20 Mar 20 nicklas 96       if (neoPrepType.equals(libPlateType))
5871 20 Mar 20 nicklas 97       {
5871 20 Mar 20 nicklas 98         libDate = (Date)item.getAnnotationValue(Annotationtype.NEOPREP_DATE, libPlate);
5871 20 Mar 20 nicklas 99       }
5871 20 Mar 20 nicklas 100       else if (libType.equals(libPlateType))
5871 20 Mar 20 nicklas 101       {
5871 20 Mar 20 nicklas 102         libDate = (Date)item.getAnnotationValue(Annotationtype.CLEANUP_DATE, libPlate);
5871 20 Mar 20 nicklas 103       }
5871 20 Mar 20 nicklas 104       // The plate type can also be 'External library plate', 
5871 20 Mar 20 nicklas 105       // but in this case the date we pick the date from event or entry date
5871 20 Mar 20 nicklas 106       if (libDate == null) libDate = libPlate.getEventDate();
5871 20 Mar 20 nicklas 107       if (libDate == null) libDate = libPlate.getEntryDate();
5871 20 Mar 20 nicklas 108       String libBatch = Reggie.CONVERTER_DATE_TO_STRING.convert(libDate) + libPlate.getName();
5101 16 Nov 18 nicklas 109       
5101 16 Nov 18 nicklas 110       jsonAnnotations.add(item.createBatchIndexAnnotationJSON("LibBatchNo", libBatch));
5101 16 Nov 18 nicklas 111       jsonAnnotations.add(item.createAnnotationJSON("LibRow", rowF.format(well.getRow())));
5124 21 Nov 18 nicklas 112       jsonAnnotations.add(item.createAnnotationJSON("LibColumn", well.getColumn()+1)); // !! Cant' use colF.format() since it produces a String
5101 16 Nov 18 nicklas 113       jsonAnnotations.add(item.getAnnotationJSON(Annotationtype.QUBIT_CONC, lib, null));
7286 15 Aug 23 nicklas 114       jsonAnnotations.add(item.getAnnotationJSON(Annotationtype.QUANTIT_CONC, lib, null));
5101 16 Nov 18 nicklas 115       jsonAnnotations.add(item.getAnnotationJSON(Annotationtype.LIBRARY_SIZE_EST, lib, null));
5101 16 Nov 18 nicklas 116       jsonAnnotations.add(item.getAnnotationJSON(Annotationtype.LIBRARY_MOLARITY_EST, lib, null));
4378 03 Mar 17 nicklas 117
5101 16 Nov 18 nicklas 118       json.add(jsonLib);
5101 16 Nov 18 nicklas 119     }
5549 08 Aug 19 nicklas 120   }
5549 08 Aug 19 nicklas 121   
5549 08 Aug 19 nicklas 122   private void addMipsLibs(JSONArray json, CohortItem item)
5549 08 Aug 19 nicklas 123   {
5549 08 Aug 19 nicklas 124     List<Library> libraries = item.getLibraries(Pipeline.MIPS);
5549 08 Aug 19 nicklas 125     if (libraries.size() == 0) return;
4371 02 Mar 17 nicklas 126     
5549 08 Aug 19 nicklas 127     DbControl dc = item.getDbControl();
5549 08 Aug 19 nicklas 128     for (Library library : libraries)
5549 08 Aug 19 nicklas 129     {
5549 08 Aug 19 nicklas 130       Extract lib = library.getItem();
5549 08 Aug 19 nicklas 131       BioMaterialEvent creationEvent = lib.getCreationEvent();
5549 08 Aug 19 nicklas 132       Dna dna = library.getDna(dc, false);
5549 08 Aug 19 nicklas 133       
5549 08 Aug 19 nicklas 134       JSONObject jsonLib = new JSONObject();
5549 08 Aug 19 nicklas 135       jsonLib.put("name", item.toReleaseId(lib.getName()));
5549 08 Aug 19 nicklas 136       jsonLib.put("type", lib.getType().name());
5549 08 Aug 19 nicklas 137       jsonLib.put("subtype", getName(lib.getItemSubtype()));
5549 08 Aug 19 nicklas 138       jsonLib.put("parent", item.toReleaseId(dna.getName()));
5549 08 Aug 19 nicklas 139   
5549 08 Aug 19 nicklas 140       JSONArray jsonAnnotations = new FilteredJSONArray(new NotNullFilter<>(false));
5549 08 Aug 19 nicklas 141       jsonLib.put("annotations", jsonAnnotations);
5549 08 Aug 19 nicklas 142       jsonAnnotations.add(item.getAnnotationJSON(Annotationtype.PIPELINE, lib, null));
5549 08 Aug 19 nicklas 143       jsonAnnotations.add(item.createAnnotationJSON("Protocol", getName(creationEvent.getProtocol())));
5549 08 Aug 19 nicklas 144       jsonAnnotations.add(item.createAnnotationJSON("Barcode", getName(lib.getTag())));
5549 08 Aug 19 nicklas 145   
5549 08 Aug 19 nicklas 146       BioWell well = lib.getBioWell();
5549 08 Aug 19 nicklas 147       BioPlate libPlate = well.getPlate();
5549 08 Aug 19 nicklas 148       String libBatch = Reggie.CONVERTER_DATE_TO_STRING.convert((Date)item.getAnnotationValue(Annotationtype.CLEANUP_DATE, libPlate));
5549 08 Aug 19 nicklas 149       if (libBatch != null) libBatch += libPlate.getName();
5549 08 Aug 19 nicklas 150       
5549 08 Aug 19 nicklas 151       jsonAnnotations.add(item.createBatchIndexAnnotationJSON("LibBatchNo", libBatch));
5549 08 Aug 19 nicklas 152       jsonAnnotations.add(item.createAnnotationJSON("LibRow", rowF.format(well.getRow())));
5549 08 Aug 19 nicklas 153       jsonAnnotations.add(item.createAnnotationJSON("LibColumn", well.getColumn()+1)); // !! Cant' use colF.format() since it produces a String
5549 08 Aug 19 nicklas 154
5549 08 Aug 19 nicklas 155       json.add(jsonLib);
5549 08 Aug 19 nicklas 156     }
4371 02 Mar 17 nicklas 157   }
3937 16 May 16 nicklas 158
4378 03 Mar 17 nicklas 159   @Override
4469 26 Apr 17 nicklas 160   public List<CohortTypeDef> getTypeDefsInJSON()
4378 03 Mar 17 nicklas 161   {
4448 07 Apr 17 nicklas 162     DbControl dc = getDbControl();
4469 26 Apr 17 nicklas 163     
4469 26 Apr 17 nicklas 164     CohortTypeDefFactory libFactory = new CohortTypeDefFactory(dc, Item.EXTRACT, "Library");
4469 26 Apr 17 nicklas 165     
5549 08 Aug 19 nicklas 166     libFactory.createAnnotationType(Annotationtype.PIPELINE);
4469 26 Apr 17 nicklas 167     libFactory.createAnnotationType("Protocol", Type.STRING);
4469 26 Apr 17 nicklas 168     libFactory.createAnnotationType("Barcode", Type.STRING);
4469 26 Apr 17 nicklas 169     
4470 26 Apr 17 nicklas 170     libFactory.createAnnotationType("LibBatchNo", Type.INT).setProjectSpecificValues(true);
4470 26 Apr 17 nicklas 171     libFactory.createAnnotationType("LibRow", Type.STRING);
4470 26 Apr 17 nicklas 172     libFactory.createAnnotationType("LibColumn", Type.INT);
4470 26 Apr 17 nicklas 173     
4470 26 Apr 17 nicklas 174     libFactory.createAnnotationType(Annotationtype.QUBIT_CONC).setUnit(UnitUtil.getUnit(dc, Quantity.DENSITY, "ng/µl"));
7286 15 Aug 23 nicklas 175     libFactory.createAnnotationType(Annotationtype.QUANTIT_CONC).setUnit(UnitUtil.getUnit(dc, Quantity.DENSITY, "ng/µl"));
4470 26 Apr 17 nicklas 176     libFactory.createAnnotationType(Annotationtype.LIBRARY_SIZE_EST);
4471 27 Apr 17 nicklas 177     libFactory.createAnnotationType(Annotationtype.LIBRARY_MOLARITY_EST).setUnit(UnitUtil.getUnit(dc, Quantity.MOLAR_CONCENTRATION, "nM"));
4470 26 Apr 17 nicklas 178     
4469 26 Apr 17 nicklas 179     return libFactory.allCreated();
4378 03 Mar 17 nicklas 180   }
4378 03 Mar 17 nicklas 181
3937 16 May 16 nicklas 182 }