@@ -19,11 +19,13 @@ public class KonstanzInserter extends DBInserter {
1919 private static final int START_ROW_INDEX = 4 ;
2020 private File sourceFile ;
2121 private HashMap <String , ArrayList <String >> lookupMapDMSO ;
22+ private ArrayList <String > casOutRows ;
2223
2324 public KonstanzInserter (File sourceFile , boolean attemptUnblinding ) {
2425 super (attemptUnblinding );
2526 this .sourceFile = sourceFile ;
2627 lookupMapDMSO = new HashMap <>();
28+ casOutRows = new ArrayList <>();
2729 }
2830
2931 private long decodeKonstanzEndpoint (String endpointName ) throws SQLException {
@@ -66,6 +68,10 @@ private long decodeKonstanzEndpoint(String endpointName) throws SQLException {
6668 }
6769 }
6870
71+ protected void addCASRow (String row ) {
72+ casOutRows .add (row );
73+ }
74+
6975 @ Override
7076 public void run () {
7177 long timestampExperiment = new Date (0 ).getTime ();
@@ -81,6 +87,7 @@ public void run() {
8187 FileInputStream excelFile = new FileInputStream (sourceFile );
8288 XSSFWorkbook workbook = new XSSFWorkbook (excelFile );
8389 workbook .setActiveSheet (0 );
90+ String CASCol = "K" ;
8491
8592 int rowIndex = START_ROW_INDEX ;
8693 SheetReader reader = new SheetReader (sourceFile , workbook );
@@ -109,25 +116,47 @@ public void run() {
109116 if (reader .hasValueAt ("J3" )) {
110117 extendedEndpoints = true ;
111118 endpoint3 = reader .getValueAt ("J3" );
119+ CASCol = "L" ;
112120 }
113121
114- for (int i = START_ROW_INDEX ; i < rowCount ; i ++) {
122+ int rowTarget = rowCount + START_ROW_INDEX ;
123+ for (int i = START_ROW_INDEX ; i < rowTarget ; i ++) {
115124 cachedRow = i ;
116125 cachedWell = "<Unknown>" ;
117126
118127 // EXTRACT INFORMATION FROM SHEET
119128 String sampleID = reader .getValueAt ("A" + i );
120129 String plateID = reader .getValueAt ("B" + i );
121130
122- String wellRow = reader .getValueAt ("C" + i );
123- int wellCol = (int ) Double .parseDouble (reader .getValueAt ("D" + i , true ));
124- WellBuilder wellBuilder = new WellBuilder (wellRow , wellCol );
125- String wellName = wellBuilder .getWellExtended ();
131+ boolean hasWell = true ;
132+ String wellName ;
133+ try {
134+ String wellRow = reader .getValueAt ("C" + i );
135+ int wellCol = (int ) Double .parseDouble (reader .getValueAt ("D" + i , true ));
136+ WellBuilder wellBuilder = new WellBuilder (wellRow , wellCol );
137+ wellName = wellBuilder .getWellExtended ();
138+ } catch (Exception e ) {
139+ hasWell = false ;
140+ wellName = "Z" + cachedRow ;
141+ CASCol = "J" ;
142+ }
126143 cachedWell = wellName ;
127144
128145 String wellType = reader .getValueAt ("E" + i );
129146 String wellQuality = reader .getValueAt ("F" + i );
130147
148+ String CAS = null ;
149+ try {
150+ String CASCell = CASCol + i ;
151+ CAS = reader .getValueAt (CASCell , false ).trim ();
152+ Log .v ("Cas at " + CASCell + " found: " + CAS );
153+ if (CAS .equals ("" ) || CAS .equals ("NaN" )) CAS = null ;
154+ } catch (Exception e ) {
155+ Log .e ("Failed to read the CAS Number. But that's okay, switching to 'No CAS Mode.'" , e );
156+ CAS = null ;
157+ }
158+ boolean CASMode = CAS != null ;
159+
131160 long outlierID = confirmedOutlierID ;
132161 try {
133162 int wellQualityNumeric = (int ) Double .parseDouble (wellQuality );
@@ -155,10 +184,12 @@ public void run() {
155184 } catch (Exception e ) {
156185 addError ("Failed to the response to " + endpoint1 + " at H" + i + ". Error: " + e .getMessage ());
157186 }
158- try {
159- response2 = Double .parseDouble (reader .getValueAt ("I" + i , true ));
160- } catch (Exception e ) {
161- addError ("Failed to the response to " + endpoint2 + " at I" + i + ". Error: " + e .getMessage ());
187+ if (hasWell ) {
188+ try {
189+ response2 = Double .parseDouble (reader .getValueAt ("I" + i , true ));
190+ } catch (Exception e ) {
191+ addError ("Failed to the response to " + endpoint2 + " at I" + i + ". Error: " + e .getMessage ());
192+ }
162193 }
163194 if (extendedEndpoints ) {
164195 try {
@@ -208,21 +239,62 @@ public void run() {
208239 long workgroupID = 3 ;
209240
210241 long compoundID ;
211- try {
212- compoundID = executor .getIDViaName ("compound" , sampleID );
213- } catch (Exception exx ) {
242+ if (CASMode ) {
243+ boolean foundCas ;
244+ try {
245+ //Let's see if that CAS exists in the DB as a registered compound
246+ compoundID = executor .getIDViaFeature ("compound" , "cas_no" , CAS );
247+ foundCas = true ;
248+ } catch (Exception exx ) {
249+ try {
250+ String unblindedCAS = executor .getFeatureViaFeature ("unblinded_compound_mapping" , "name" , "unblinded_cas_number" , CAS );
251+ addBlindingRow ("Blinded entry: " + CAS + " was unblinded to: " + unblindedCAS );
252+ compoundID = executor .getIDViaFeature ("compound" , "cas_no" , unblindedCAS );
253+ attemptUnblinding = false ;
254+ foundCas = true ;
255+ } catch (Exception exxx ) {
256+ //What if the CAS does not exists in the DB, but the Compound name?
257+ String actualCAS = null ;
258+ try {
259+ long actualID = executor .getIDViaName ("compound" , sampleID );
260+ actualCAS = executor .getFeatureViaID ("compound" , "cas_no" , actualID );
261+ } catch (Exception exxxx ) {
262+ // all good. nvm.
263+ }
264+ if (actualCAS != null ) {
265+ String outRowExtra = sfName + ";" + sampleID + ";" + plateID + ";" + CAS + ";DUPLICATE;;;;DUPLICATE COMPOUND NAME BUT DIFFERENT CAS:;" + actualCAS ;
266+ addCASRow (outRowExtra );
267+ }
268+
269+ //So our CAS was not in the DB and not in the unblinded mapping. Let's insert it as a new compound then.
270+ executor .insertCompound (sampleID , CAS , sampleID , true );
271+ foundCas = false ;
272+ compoundID = executor .getIDViaName ("compound" , sampleID );
273+ }
274+ }
275+
276+ String casName = executor .getNameViaID ("compound" , compoundID );
277+ String abbreviation = executor .getFeatureViaID ("compound" , "abbreviation" , compoundID );
278+ boolean equals = casName .equals (sampleID );
279+ String outRow = sfName + ";" + sampleID + ";" + plateID + ";" + CAS + ";" + String .valueOf (foundCas ).toUpperCase () + ";" + casName + ";" + abbreviation + ";" + String .valueOf (equals ).toUpperCase ();
280+ addCASRow (outRow );
281+ } else {
214282 try {
215- compoundID = executor .getIDViaFeature ("compound" , "abbreviation" , sampleID );
216- } catch (Exception ex ) {
217- //ex.printStackTrace();
218- executor .insertCompound (sampleID , sampleID , sampleID , true );
219283 compoundID = executor .getIDViaName ("compound" , sampleID );
284+ } catch (Exception exx ) {
285+ try {
286+ compoundID = executor .getIDViaFeature ("compound" , "abbreviation" , sampleID );
287+ } catch (Exception ex ) {
288+ //ex.printStackTrace();
289+ executor .insertCompound (sampleID , sampleID , sampleID , true );
290+ compoundID = executor .getIDViaName ("compound" , sampleID );
291+ }
220292 }
221293 }
222294
223295 if (attemptUnblinding ) {
224296 if (isControl ) {
225- addBlindingRow (sampleID + " is not a blinded compound." );
297+ addBlindingRow (sampleID + " [Line " + i + "] is not a blinded compound. It's a control ." );
226298 } else {
227299 CompoundHolder compoundHolder = attemptUnblinding (sampleID );
228300 if (compoundHolder != null ) {
@@ -240,7 +312,7 @@ public void run() {
240312 experimentID = executor .getNextSequenceTableVal ("experiment" );
241313 executor .insertExperiment (experimentID , timestampExperiment , experimentName , projectID , workgroupID , individualID , compoundID , cellTypeID , assayID , plateFormatID , solventID , solventConcentration , controlPlateID );
242314 }
243- Log .i ("Experiment ID extracted: " + experimentID );
315+ Log .v ("Experiment ID extracted: " + experimentID );
244316 }
245317
246318 long wellID ;
@@ -295,19 +367,26 @@ public void run() {
295367 } catch (Exception e ) {
296368 Log .e (e );
297369 addError ("FATAL Error! " + sfName + ": Error on row " + i + " to be inserted into the experiment: " + e .getMessage ());
370+ addCASRow (sfName + ";" + sampleID + ";" + plateID + ";FATAL ERROR on row " + i );
298371 continue ;
299372 }
300- Log .i ("Successfully inserted row " + i + " from file: " + sfName );
373+ Log .v ("Successfully inserted row " + i + " from file: " + sfName );
301374 }
302375 workbook .close ();
303376 } catch (Throwable e ) {
304377 Log .e ("Fatal Error in sheet " + sourceFile .getName (), e );
305378 addError ("Fatal Error in: " + sourceFile .getName () + ". Type: " + e .getClass ().getSimpleName () + ": '" + e .getMessage () + "'! Last known row: " + cachedRow + ". Last known well: " + cachedWell );
306379 }
380+
381+ setFinished ();
307382 }
308383
309384 @ Override
310385 public String getName () {
311386 return sourceFile .getName ();
312387 }
388+
389+ public ArrayList <String > getCASRows () {
390+ return new ArrayList <>(casOutRows );
391+ }
313392}
0 commit comments