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

Code
Comments
Other
Rev Date Author Line
3939 16 May 16 nicklas 1 package net.sf.basedb.reggie.plugins.release;
3939 16 May 16 nicklas 2
3959 20 May 16 nicklas 3
5099 15 Nov 18 nicklas 4 import java.util.Date;
5099 15 Nov 18 nicklas 5 import java.util.List;
5099 15 Nov 18 nicklas 6
5091 14 Nov 18 nicklas 7 import org.json.simple.JSONArray;
5099 15 Nov 18 nicklas 8 import org.json.simple.JSONObject;
5090 14 Nov 18 nicklas 9
5099 15 Nov 18 nicklas 10 import net.sf.basedb.core.BioMaterialEvent;
3939 16 May 16 nicklas 11 import net.sf.basedb.core.DbControl;
5099 15 Nov 18 nicklas 12 import net.sf.basedb.core.Extract;
5099 15 Nov 18 nicklas 13 import net.sf.basedb.core.Item;
5099 15 Nov 18 nicklas 14 import net.sf.basedb.core.Quantity;
5099 15 Nov 18 nicklas 15 import net.sf.basedb.core.Sample;
5099 15 Nov 18 nicklas 16 import net.sf.basedb.core.Type;
5166 03 Dec 18 nicklas 17 import net.sf.basedb.core.Unit;
5099 15 Nov 18 nicklas 18 import net.sf.basedb.reggie.Reggie;
5099 15 Nov 18 nicklas 19 import net.sf.basedb.reggie.converter.BooleanToEnumConverter;
5099 15 Nov 18 nicklas 20 import net.sf.basedb.reggie.converter.MultiplyFloatConverter;
5099 15 Nov 18 nicklas 21 import net.sf.basedb.reggie.dao.Annotationtype;
5099 15 Nov 18 nicklas 22 import net.sf.basedb.reggie.dao.Lysate;
5099 15 Nov 18 nicklas 23 import net.sf.basedb.reggie.json.FilteredJSONArray;
5099 15 Nov 18 nicklas 24 import net.sf.basedb.util.filter.NotNullFilter;
5099 15 Nov 18 nicklas 25 import net.sf.basedb.util.units.UnitUtil;
3939 16 May 16 nicklas 26
3939 16 May 16 nicklas 27 /**
3939 16 May 16 nicklas 28   Cohort writer implementation for the Lysate data table.
3939 16 May 16 nicklas 29   @since 4.5
3939 16 May 16 nicklas 30 */
3939 16 May 16 nicklas 31 public class LysateWriter 
3939 16 May 16 nicklas 32   extends CohortWriter 
3939 16 May 16 nicklas 33 {
3939 16 May 16 nicklas 34
5099 15 Nov 18 nicklas 35   private final BooleanToEnumConverter<String> multPiecesConverter;
5099 15 Nov 18 nicklas 36   private final MultiplyFloatConverter toMilliG;
5099 15 Nov 18 nicklas 37   
5090 14 Nov 18 nicklas 38   public LysateWriter(DbControl dc, ReleaseWriterOptions options)
3939 16 May 16 nicklas 39   {
5090 14 Nov 18 nicklas 40     super(dc, options);
5099 15 Nov 18 nicklas 41     this.multPiecesConverter = new BooleanToEnumConverter<String>("Yes", "No", "Unknown");
5099 15 Nov 18 nicklas 42     this.toMilliG = new MultiplyFloatConverter(0.001f); // Convert µg to mg
3939 16 May 16 nicklas 43   }
3945 17 May 16 nicklas 44
3945 17 May 16 nicklas 45   @Override
5091 14 Nov 18 nicklas 46   public JSONArray toJSONObjects(CohortItem item) 
3939 16 May 16 nicklas 47   {
5099 15 Nov 18 nicklas 48     List<Lysate> lysates = item.getLysates();
5099 15 Nov 18 nicklas 49     if (lysates.size() == 0) return null;
5099 15 Nov 18 nicklas 50     
5099 15 Nov 18 nicklas 51     JSONArray json = new JSONArray();
5099 15 Nov 18 nicklas 52     for (Lysate lys : lysates)
5099 15 Nov 18 nicklas 53     {
5099 15 Nov 18 nicklas 54       Extract lysate = lys.getItem();
5099 15 Nov 18 nicklas 55       BioMaterialEvent creationEvent = lysate.getCreationEvent();
5099 15 Nov 18 nicklas 56       Sample specimen = lys.getSpecimen().getItem();
5099 15 Nov 18 nicklas 57       
5099 15 Nov 18 nicklas 58       JSONObject jsonLys = new JSONObject();
5099 15 Nov 18 nicklas 59       jsonLys.put("name", item.toReleaseId(lysate.getName()));
5099 15 Nov 18 nicklas 60       jsonLys.put("type", lysate.getType().name());
5099 15 Nov 18 nicklas 61       jsonLys.put("subtype", getName(lysate.getItemSubtype()));
5099 15 Nov 18 nicklas 62       jsonLys.put("parent", specimen.getExternalId());
5099 15 Nov 18 nicklas 63       
5099 15 Nov 18 nicklas 64       JSONArray jsonAnnotations = new FilteredJSONArray(new NotNullFilter<>(false));
5099 15 Nov 18 nicklas 65       jsonLys.put("annotations", jsonAnnotations);
5099 15 Nov 18 nicklas 66       
5099 15 Nov 18 nicklas 67       String partitionDate = Reggie.CONVERTER_DATE_TO_STRING.convert((Date)item.getAnnotationValue(Annotationtype.PARTITION_DATE, lysate));
5099 15 Nov 18 nicklas 68       String lysDate = Reggie.CONVERTER_DATE_TO_STRING.convert(creationEvent.getEventDate());
5099 15 Nov 18 nicklas 69       Float usedFromParent = creationEvent.getEventSource(specimen).getUsedQuantity();
5099 15 Nov 18 nicklas 70       Boolean multPieces = (Boolean)item.getAnnotationValue(Annotationtype.MULTIPLE_PIECES, lysate);
5099 15 Nov 18 nicklas 71
5099 15 Nov 18 nicklas 72       jsonAnnotations.add(item.createBatchIndexAnnotationJSON("PartitionBatchNo", partitionDate));
5099 15 Nov 18 nicklas 73       jsonAnnotations.add(item.createBatchIndexAnnotationJSON("LysBatchNo", lysDate));
5099 15 Nov 18 nicklas 74       jsonAnnotations.add(item.createAnnotationJSON("LysUsedFromParent", toMilliG.convert(usedFromParent)));
5166 03 Dec 18 nicklas 75       jsonAnnotations.add(item.createAnnotationJSON("LysOriginalVolume", lysate.getOriginalQuantity()));
5166 03 Dec 18 nicklas 76       jsonAnnotations.add(item.createAnnotationJSON("LysRemainingVolume", lysate.getRemainingQuantity()));
5099 15 Nov 18 nicklas 77       jsonAnnotations.add(item.createAnnotationJSON("LysMultPieces", multPiecesConverter.convert(multPieces)));
5166 03 Dec 18 nicklas 78       jsonAnnotations.add(item.createAnnotationJSON("LysProtocol", getName(creationEvent.getProtocol())));
5099 15 Nov 18 nicklas 79       
5099 15 Nov 18 nicklas 80       json.add(jsonLys);
5099 15 Nov 18 nicklas 81     }
5099 15 Nov 18 nicklas 82     
5099 15 Nov 18 nicklas 83     return json;
3939 16 May 16 nicklas 84   }
5099 15 Nov 18 nicklas 85   
5099 15 Nov 18 nicklas 86   @Override
5099 15 Nov 18 nicklas 87   public List<CohortTypeDef> getTypeDefsInJSON()
5099 15 Nov 18 nicklas 88   {
5099 15 Nov 18 nicklas 89     DbControl dc = getDbControl();
5099 15 Nov 18 nicklas 90     CohortTypeDefFactory lysateFactory = new CohortTypeDefFactory(dc, Item.EXTRACT, "Lysate");
3939 16 May 16 nicklas 91
5099 15 Nov 18 nicklas 92     lysateFactory.createAnnotationType("PartitionBatchNo", Type.INT).setProjectSpecificValues(true);
5099 15 Nov 18 nicklas 93
5166 03 Dec 18 nicklas 94     Unit mg = UnitUtil.getUnit(dc, Quantity.MASS, "mg");
5166 03 Dec 18 nicklas 95     Unit µl = UnitUtil.getUnit(dc, Quantity.VOLUME, "µl");
5166 03 Dec 18 nicklas 96     
5099 15 Nov 18 nicklas 97     // Lysate annotation
5099 15 Nov 18 nicklas 98     lysateFactory.setNamePrefix("Lys");
5099 15 Nov 18 nicklas 99     lysateFactory.createAnnotationType("BatchNo", Type.INT).setProjectSpecificValues(true);
5166 03 Dec 18 nicklas 100     lysateFactory.createAnnotationType("UsedFromParent", Type.FLOAT).setUnit(mg);
5166 03 Dec 18 nicklas 101     lysateFactory.createAnnotationType("OriginalVolume", Type.FLOAT).setUnit(µl);
5166 03 Dec 18 nicklas 102     lysateFactory.createAnnotationType("RemainingVolume", Type.FLOAT).setUnit(µl).setProjectSpecificValues(true);
5099 15 Nov 18 nicklas 103     lysateFactory.createAnnotationType("MultPieces", Type.STRING).setEnumeration(multPiecesConverter.getEnum());
5166 03 Dec 18 nicklas 104     lysateFactory.createAnnotationType("Protocol", Type.STRING);
5099 15 Nov 18 nicklas 105     
5099 15 Nov 18 nicklas 106     return lysateFactory.allCreated();
5099 15 Nov 18 nicklas 107   }
5099 15 Nov 18 nicklas 108
3939 16 May 16 nicklas 109 }