List<Sample> samples = new ArrayList<Sample>();
OdfTable oTable = oDoc.getTableList().get(0);
//process global headers
OdfTableCell pairedCell = oTable.getCellByPosition("A2");
boolean paired = false;
if (pairedCell.getBooleanValue() != null) {
paired = pairedCell.getBooleanValue();
log.info("Got paired: " + paired);
}
OdfTableCell platformCell = oTable.getCellByPosition("B2");
PlatformType pt = null;
if (!"".equals(platformCell.getStringValue())) {
pt = PlatformType.get(platformCell.getStringValue());
}
if (pt == null) {
throw new InputFormException("Cannot resolve Platform type from: '" + platformCell.getStringValue() + "'");
}
else {
log.info("Got platform type: " + pt.getKey());
}
OdfTableCell typeCell = oTable.getCellByPosition("C2");
LibraryType lt = null;
if (!"".equals(typeCell.getStringValue())) {
String[] split = typeCell.getStringValue().split("-");
String plat = split[0];
String type = split[1];
if (platformCell.getStringValue().equals(plat)) {
lt = manager.getLibraryTypeByDescriptionAndPlatform(type, pt);
}
else {
throw new InputFormException("Selected library type '" + typeCell.getStringValue() + "' doesn't match platform type: '" + platformCell.getStringValue() + "'");
}
}
if (lt == null) {
throw new InputFormException("Cannot resolve Library type from: '" + typeCell.getStringValue() + "'");
}
else {
log.info("Got library type: " + lt.getDescription());
}
OdfTableCell selectionCell = oTable.getCellByPosition("D2");
LibrarySelectionType ls = null;
if (!"".equals(selectionCell.getStringValue())) {
ls = manager.getLibrarySelectionTypeByName(selectionCell.getStringValue());
}
if (ls == null) {
throw new InputFormException("Cannot resolve Library Selection type from: '" + selectionCell.getStringValue() + "'");
}
else {
log.info("Got library selection type: " + ls.getName());
}
OdfTableCell strategyCell = oTable.getCellByPosition("E2");
LibraryStrategyType lst = null;
if (!"".equals(strategyCell.getStringValue())) {
lst = manager.getLibraryStrategyTypeByName(strategyCell.getStringValue());
}
if (lst == null) {
throw new InputFormException("Cannot resolve Library Strategy type from: '" + strategyCell.getStringValue() + "'");
}
else {
log.info("Got library strategy type: " + lst.getName());
}
OdfTableCell plateBarcodeCell = oTable.getCellByPosition("F2");
String plateBarcode = null;
if (!"".equals(plateBarcodeCell.getStringValue())) {
plateBarcode = plateBarcodeCell.getStringValue();
}
if (plateBarcode == null) {
throw new InputFormException("Cannot resolve plate barcode from: '" + plateBarcodeCell.getStringValue() + "'");
}
else {
log.info("Got plate parcode: " + plateBarcode);
}
//process entries
Simple384WellPlate libraryPlate = null;
//Map<String, Pool<Plate<LinkedList<Library>, Library>>> pools = new HashMap<String, Pool<Plate<LinkedList<Library>, Library>>>();
Map<String, PlatePool> pools = new HashMap<String, PlatePool>();
for (OdfTableRow row : oTable.getRowList()) {
int ri = row.getRowIndex();
if (ri > 3) {
// Ax - plate position
OdfTableCell platePosCell = oTable.getCellByPosition(0, ri);
if (!"".equals(platePosCell.getStringValue()) && libraryPlate == null) {
//plated libraries - process as plate
libraryPlate = new Simple384WellPlate();
libraryPlate.setIdentificationBarcode(plateBarcode);
libraryPlate.setCreationDate(new Date());
}
//cell defs
OdfTableCell sampleAliasCell = oTable.getCellByPosition(1, ri);
Sample s = null;
if (!"".equals(sampleAliasCell.getStringValue())) {
Collection<Sample> ss = manager.listSamplesByAlias(sampleAliasCell.getStringValue());
if (!ss.isEmpty()) {
if (ss.size() == 1) {
s = ss.iterator().next();
log.info("Got sample: " + s.getAlias());
}
else {
throw new InputFormException("Multiple samples retrieved with this alias: '" + sampleAliasCell.getStringValue() + "'. Cannot process.");
}
}
else {
throw new InputFormException("No such sample '" + sampleAliasCell.getStringValue() + "'in database. Samples need to be created before using the form input functionality");
}
}
else {
log.info("Blank sample row found. Ending import.");
break;
}
//sample OK - good to go
if (s != null) {
OdfTableCell entityIDCell = oTable.getCellByPosition(2, ri);
OdfTableCell poolNumberCell = oTable.getCellByPosition(3, ri);
OdfTableCell sampleQcCell = oTable.getCellByPosition(4, ri);
//OdfTableCell sampleAmountCell = oTable.getCellByPosition(5, ri);
//OdfTableCell sampleWaterAmountCell = oTable.getCellByPosition(6, ri);
OdfTableCell barcodeKitCell = oTable.getCellByPosition(7, ri);
OdfTableCell barcodeTagsCell = oTable.getCellByPosition(8, ri);
OdfTableCell libraryQcCell = oTable.getCellByPosition(9, ri);
OdfTableCell libraryQcInsertSizeCell = oTable.getCellByPosition(10, ri);
OdfTableCell libraryQcMolarityCell = oTable.getCellByPosition(11, ri);
OdfTableCell libraryQcPassFailCell = oTable.getCellByPosition(12, ri);
//OdfTableCell libraryAmountCell = oTable.getCellByPosition(13, ri);
//OdfTableCell libraryWaterAmountCell = oTable.getCellByPosition(14, ri);
//OdfTableCell dilutionQcCell = oTable.getCellByPosition(15, ri);
OdfTableCell dilutionMolarityCell = oTable.getCellByPosition(16, ri);
//OdfTableCell dilutionAmountCell = oTable.getCellByPosition(17, ri);
//OdfTableCell dilutionWaterAmountCell = oTable.getCellByPosition(18, ri);
OdfTableCell poolQcCell = oTable.getCellByPosition(19, ri);
//OdfTableCell poolAverageInsertSizeCell = oTable.getCellByPosition(20, ri);
OdfTableCell poolConvertedMolarityCell = oTable.getCellByPosition(21, ri);
//add pool, if any
if (!"".equals(poolNumberCell.getStringValue())) {
if (!pools.containsKey(poolNumberCell.getStringValue())) {
PlatePool pool = new PlatePool();
pool.setAlias(poolNumberCell.getStringValue());
pool.setPlatformType(pt);
pool.setReadyToRun(true);
pool.setCreationDate(new Date());
pools.put(poolNumberCell.getStringValue(), pool);
log.info("Added pool: " + poolNumberCell.getStringValue());
}
}
//process sample QC
if (!"".equals(sampleQcCell.getStringValue())) {
try {
SampleQC sqc = new SampleQCImpl();
sqc.setSample(s);
sqc.setResults(Double.valueOf(sampleQcCell.getStringValue()));
sqc.setQcCreator(u.getLoginName());
sqc.setQcDate(new Date());
sqc.setQcType(manager.getSampleQcTypeByName("Picogreen"));
if (!s.getSampleQCs().contains(sqc)) {
s.addQc(sqc);
log.info("Added sample QC: " + sqc.toString());
}
}
catch (NumberFormatException nfe) {
throw new InputFormException("Supplied Sample QC concentration for sample '" + sampleAliasCell.getStringValue() + "' is invalid", nfe);
}
}
//if (!"".equals(libraryQcCell.getStringValue())) {
if (barcodeKitCell.getStringValue() != null && barcodeTagsCell.getStringValue() != null) {
//create library
Library library = new LibraryImpl();
library.setSample(s);
Matcher mat = samplePattern.matcher(s.getAlias());
if (mat.matches()) {
String libAlias = plateBarcode + "_" + "L" + mat.group(2) + "-" + platePosCell.getStringValue() + "_" + entityIDCell.getStringValue();
//String libAlias = libraryNamingScheme.generateNameFor("alias", library);
//library.setAlias(libAlias);
library.setAlias(libAlias);
library.setSecurityProfile(s.getSecurityProfile());
library.setDescription(s.getDescription());
library.setCreationDate(new Date());
library.setPlatformName(pt.name());
library.setLibraryType(lt);
library.setLibrarySelectionType(ls);
library.setLibraryStrategyType(lst);
library.setPaired(paired);
if (!"".equals(libraryQcMolarityCell.getStringValue())) {
int insertSize = 0;
try {
String bp = libraryQcInsertSizeCell.getStringValue();
Matcher m = digitPattern.matcher(bp);
if (m.matches()) {
insertSize = Integer.valueOf(m.group(1));
}
else {
throw new InputFormException("Supplied Library insert size for library '" + libAlias + "' (" + s.getAlias() + ") is invalid");
}
}
catch (NumberFormatException nfe) {
throw new InputFormException("Supplied Library insert size for library '" + libAlias + "' (" + s.getAlias() + ") is invalid", nfe);
}
try {
LibraryQC lqc = new LibraryQCImpl();
lqc.setLibrary(library);
lqc.setInsertSize(insertSize);
lqc.setResults(Double.valueOf(libraryQcMolarityCell.getStringValue()));
lqc.setQcCreator(u.getLoginName());
lqc.setQcDate(new Date());
lqc.setQcType(manager.getLibraryQcTypeByName("Picogreen"));
if (!library.getLibraryQCs().contains(lqc)) {
library.addQc(lqc);
log.info("Added library QC: " + lqc.toString());
}
if (insertSize == 0 && lqc.getResults() == 0) {
library.setQcPassed(false);
}
else {
//TODO check libraryQcPassFailCell?
library.setQcPassed(true);
}
}
catch (NumberFormatException nfe) {
throw new InputFormException("Supplied Library QC concentration for library '" + libAlias + "' (" + s.getAlias() + ") is invalid", nfe);
}
}
if (!"".equals(barcodeKitCell.getStringValue())) {
Collection<TagBarcode> bcs = manager.listAllTagBarcodesByStrategyName(barcodeKitCell.getStringValue());
if (!bcs.isEmpty()) {
String tags = barcodeTagsCell.getStringValue();
if (!"".equals(tags)) {
HashMap<Integer, TagBarcode> tbs = new HashMap<Integer, TagBarcode>();
if (tags.contains("-")) {
String[] splits = tags.split("-");
int count = 1;
for (String tag : splits) {
for (TagBarcode tb : bcs) {
if (tb.getName().equals(tag)) {
//set tag barcodes
tbs.put(count, tb);
count++;
}
}
}
}
else {
for (TagBarcode tb : bcs) {
if (tb.getName().equals(tags)) {
//set tag barcode
tbs.put(1, tb);
}
}
}
library.setTagBarcodes(tbs);
}
else {
throw new InputFormException("Barcode Kit specified but no tag barcodes entered for: '" + sampleAliasCell.getStringValue() + "'.");
}
}
else {
throw new InputFormException("No tag barcodes associated with this kit definition: '" + barcodeKitCell.getStringValue() + "'.");
}
}
/*
if (!"".equals(dilutionMolarityCell.getStringValue())) {
try {
LibraryDilution ldi = new LibraryDilution();
ldi.setLibrary(library);
ldi.setSecurityProfile(library.getSecurityProfile());
ldi.setConcentration(Double.valueOf(dilutionMolarityCell.getStringValue()));
ldi.setCreationDate(new Date());
ldi.setDilutionCreator(u.getLoginName());
if (!library.getLibraryDilutions().contains(ldi)) {
library.addDilution(ldi);
log.info("Added library dilution: " + ldi.toString());
}
Pool<Plate<LinkedList<Library>, Library>> p = pools.get(poolNumberCell.getStringValue());
if (p != null) {
p.addPoolableElement(ldi);
log.info("Added library dilution to pool: " + p.toString());
}
}
catch (NumberFormatException nfe) {
throw new InputFormException("Supplied LibraryDilution concentration for library '"+libAlias+"' ("+s.getAlias()+") is invalid", nfe);
}
}
*/
if (!"".equals(poolConvertedMolarityCell.getStringValue())) {
Pool<Plate<LinkedList<Library>, Library>> p = pools.get(poolNumberCell.getStringValue());
if (p != null) {
log.debug("Retrieved pool " + poolNumberCell.getStringValue());
try {
double d = Double.valueOf(poolConvertedMolarityCell.getStringValue());
p.setConcentration(d);
}
catch (NumberFormatException nfe) {
throw new InputFormException("Supplied pool concentration for pool '" + poolNumberCell.getStringValue() + "' is invalid", nfe);
}