Skip to content

Commit f7d084d

Browse files
author
Nils Fo
committed
Updated Konstanz inserter and implemented wait time ETAs
1 parent 90fdacc commit f7d084d

14 files changed

Lines changed: 442 additions & 53 deletions

src/main/java/de/rub/bph/omnineuro/client/Client.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import de.rub.bph.omnineuro.client.imported.log.Log;
55
import de.rub.bph.omnineuro.client.ui.DialogFrame;
66
import de.rub.bph.omnineuro.client.ui.OmniFrame;
7+
import de.rub.bph.omnineuro.client.util.TimeUtils;
78

89
import javax.swing.*;
910
import java.awt.*;
1011
import java.sql.SQLException;
12+
import java.util.Date;
1113
import java.util.concurrent.ExecutorService;
1214
import java.util.concurrent.Executors;
1315

@@ -29,6 +31,7 @@ public static void main(String[] args) {
2931
DateInterpreter.initMonthMap();
3032

3133
new OmniFrame();
34+
Log.i("Current time: " + new TimeUtils().formatAbsolute(new Date(), 0, 0));
3235
});
3336
}
3437

src/main/java/de/rub/bph/omnineuro/client/core/AXESSheetReaderManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ArrayList<JSONObject> startReading() {
5757
AXESSheetReader reader = new AXESSheetReader(f, outDir);
5858

5959
readers.add(reader);
60-
service.submit(reader);
60+
submitTask(reader);
6161
}
6262

6363
waitForTasks();
@@ -122,6 +122,11 @@ public ArrayList<File> discoverFiles(File currentDir) {
122122
return list;
123123
}
124124

125+
@Override
126+
protected void onWaitUpdate() {
127+
128+
}
129+
125130
public FileNameExtensionFilter getFilter() {
126131
return filter;
127132
}

src/main/java/de/rub/bph/omnineuro/client/core/db/QueryExecutor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public synchronized String getFeatureViaID(String tableName, String columnName,
111111
throw new IllegalStateException("Table '" + tableName + "' does not contain id " + id + "!");
112112
}
113113

114+
public synchronized String getFeatureViaFeature(String tableName, String sourceColumn, String targetColumn, String feature) throws SQLException, IllegalStateException {
115+
long id = getIDViaFeature(tableName, sourceColumn, feature);
116+
return getFeatureViaID(tableName, targetColumn, id);
117+
}
118+
114119
public synchronized String getNameViaID(String tableName, long id) throws SQLException, IllegalStateException {
115120
return getFeatureViaID(tableName, "name", id);
116121
}
@@ -291,5 +296,13 @@ public void setLogEnabled(boolean logEnabled) {
291296
this.logEnabled = logEnabled;
292297
}
293298

299+
public synchronized boolean isExecutableQuery(String query) {
300+
try {
301+
executeQuery(query);
302+
} catch (SQLException e) {
303+
return false;
304+
}
305+
return true;
306+
}
294307

295308
}

src/main/java/de/rub/bph/omnineuro/client/core/db/in/AXESInserter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ public void run() {
332332
addError(" == FATAL ERROR! == Failed to insert Experiment " + getName() + " into the database! Error Type: " + e.getClass().getSimpleName() + ". Reason: '" + e.getMessage() + "'");
333333
}
334334
Log.i("Finished inserting responses for " + getName() + ". Count: " + getInsertedResponsesCount());
335+
setFinished();
335336
}
336337

337338
@Override

src/main/java/de/rub/bph/omnineuro/client/core/db/in/DBInserter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import de.rub.bph.omnineuro.client.core.db.OmniNeuroQueryExecutor;
55
import de.rub.bph.omnineuro.client.core.db.out.holder.CompoundHolder;
66
import de.rub.bph.omnineuro.client.imported.log.Log;
7+
import de.rub.bph.omnineuro.client.util.concurrent.TimedRunnable;
78

89
import java.sql.Connection;
910
import java.util.ArrayList;
1011

11-
public abstract class DBInserter implements Runnable {
12+
public abstract class DBInserter extends TimedRunnable {
1213

1314
protected static OmniNeuroQueryExecutor executor;
1415
protected boolean attemptUnblinding;

src/main/java/de/rub/bph/omnineuro/client/core/db/in/InsertManager.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
import de.rub.bph.omnineuro.client.core.sheet.reader.versions.meta.ExternalMetadata;
99
import de.rub.bph.omnineuro.client.imported.filemanager.FileManager;
1010
import de.rub.bph.omnineuro.client.imported.log.Log;
11+
import de.rub.bph.omnineuro.client.util.TimeUtils;
12+
import de.rub.bph.omnineuro.client.util.concurrent.TimedRunnable;
1113
import org.json.JSONObject;
1214

1315
import java.awt.*;
1416
import java.io.File;
1517
import java.io.IOException;
1618
import java.sql.SQLException;
1719
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.Date;
1822
import java.util.concurrent.ExecutorService;
1923
import java.util.concurrent.Executors;
2024

@@ -198,7 +202,20 @@ public void insert() {
198202
} catch (InterruptedException e) {
199203
e.printStackTrace();
200204
}
201-
Log.i("Still waiting for threads to finish...");
205+
206+
ArrayList<TimedRunnable> timedRunnableList = new ArrayList<>();
207+
int finishedCount = 0;
208+
for (DBInserter in : inserters) {
209+
if (in.isFinished()) finishedCount++;
210+
TimedRunnable tr = in;
211+
timedRunnableList.add(tr);
212+
}
213+
214+
long etaTime = TimedRunnable.calculateETA(timedRunnableList, inserters.size() - finishedCount, getThreads() / -3);
215+
Date etaDate = new Date(new Date().getTime() + etaTime);
216+
String etaText = new TimeUtils().formatAbsolute(etaDate, 3, 0);
217+
218+
Log.i("Still waiting for inserter threads to finish: " + finishedCount + " / " + inserters.size() + ". ETA: " + etaText);
202219
}
203220
Log.i("Done waiting.");
204221

@@ -207,10 +224,12 @@ public void insert() {
207224
ArrayList<String> containsNaNList = new ArrayList<>();
208225
ArrayList<String> insertedResponsesList = new ArrayList<>();
209226
ArrayList<String> insertedBlindedCompounds = new ArrayList<>();
227+
ArrayList<String> konstanzCasList = new ArrayList<>();
210228
triviaList = new ArrayList<>();
211229

212230
int errorNaNCount = 0;
213231
int insertedResponsesCount = 0;
232+
konstanzCasList.add("Source;Sample ID;Plate ID;Read CAS;Existed in the DB;Database Compound;Database Abbreviation;Same DB Name");
214233

215234
for (DBInserter inserter : inserters) {
216235
String name = inserter.getName();
@@ -229,6 +248,14 @@ public void insert() {
229248
}
230249

231250
insertedBlindedCompounds.addAll(inserter.getBlindingInfo());
251+
252+
if (inserter instanceof KonstanzInserter) {
253+
KonstanzInserter ki = (KonstanzInserter) inserter;
254+
255+
ArrayList<String> temp = new ArrayList<>(ki.getCASRows());
256+
Collections.sort(temp);
257+
konstanzCasList.addAll(temp);
258+
}
232259
}
233260

234261
triviaList.add("Total errors discovered: " + errors.size());
@@ -265,6 +292,7 @@ public void insert() {
265292
manager.saveListFile(containsNaNList, new File(outDir, "contains_nan.csv"), false);
266293
manager.saveListFile(insertedResponsesList, new File(outDir, "inserted_responses.csv"), false);
267294
manager.saveListFile(triviaList, new File(outDir, "trivia.txt"), false);
295+
manager.saveListFile(konstanzCasList, new File(outDir, "konstanz_cas.csv"), false);
268296

269297
manager.saveListFile(insertedBlindedCompounds, new File(outDir, "blindedCompounds.txt"), true, false);
270298
} catch (IOException e) {

src/main/java/de/rub/bph/omnineuro/client/core/db/in/KonstanzInserter.java

Lines changed: 98 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)