Skip to content

Commit 1a29145

Browse files
author
Nils Fo
committed
Implemented Mutation Meta Data Support
1 parent 31f25d5 commit 1a29145

5 files changed

Lines changed: 143 additions & 34 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public synchronized boolean insertIndividual(long id, String name, long sexID, l
2121
return execute("INSERT INTO individual VALUES (" + id + ",'" + name + "'," + sexID + "," + speciesID + ");");
2222
}
2323

24+
public synchronized boolean insertMutation(long id, String name) throws SQLException {
25+
return execute("INSERT INTO mutation VALUES (" + id + ",'" + name + "');");
26+
}
27+
2428
public synchronized boolean insertComment(String text, long experimentID) throws SQLException {
2529
return execute("INSERT INTO comment VALUES (DEFAULT, '" + text + "'," + experimentID + ");");
2630
}
@@ -33,9 +37,9 @@ public synchronized boolean insertPassage(long experimentID, long timestamp, int
3337
return execute("INSERT INTO passage VALUES (DEFAULT, " + experimentID + "," + timestamp + "," + p + ");");
3438
}
3539

36-
public synchronized boolean insertExperiment(long id, long timestamp, String name, long projectID, long labID, long individualID, long compoundID, long cellTypeID, long assayID, long plateFormatID, long solventID, double solventConcentration, String controlWellID) throws SQLException {
40+
public synchronized boolean insertExperiment(long id, long timestamp, String name, long projectID, long labID, long individualID, long mutationID, long compoundID, long cellTypeID, long assayID, long plateFormatID, long solventID, double solventConcentration, String controlWellID) throws SQLException {
3741
return execute("INSERT INTO experiment VALUES (" + id + "," + timestamp + ",'" + name + "'," + projectID +
38-
"," + labID + "," + individualID + "," + compoundID + "," + cellTypeID + "," + assayID + "," + plateFormatID + "," + solventID + "," + solventConcentration + ", '" + controlWellID + "');");
42+
"," + labID + "," + individualID + "," + compoundID + "," + cellTypeID + "," + assayID + "," + plateFormatID + "," + solventID + "," + solventConcentration + ", '" + controlWellID + "', " + mutationID + ");");
3943
}
4044

4145
public synchronized boolean insertCompound(String name, String casNR, String abbreviation, boolean blinded) throws SQLException {
@@ -50,6 +54,8 @@ public synchronized boolean resetDatabase() throws SQLException {
5054
b &= deleteTable("passage", true);
5155
b &= deleteTable("experiment", true);
5256
b &= deleteTable("well", true);
57+
b &= deleteTable("individual", true);
58+
b &= deleteTable("mutation", true);
5359
b &= deleteBlindedCompounds();
5460

5561
b &= insertWell("Unknown");

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class QueryExecutor {
1212

1313
private Connection connection;
1414
private boolean logEnabled;
15+
private static String lastCachedQuery = null;
1516

1617
public QueryExecutor(Connection connection) {
1718
this.connection = connection;
@@ -20,6 +21,7 @@ public QueryExecutor(Connection connection) {
2021

2122
public synchronized ResultSet executeQuery(String query) throws SQLException {
2223
synchronized (connection) {
24+
setLastCachedQuery(query);
2325
if (connection.isClosed()) {
2426
Log.w("Connection closed unexpectedly! Reconnecting...");
2527
DBConnection c = DBConnection.getDBConnection();
@@ -37,6 +39,7 @@ public synchronized ResultSet executeQuery(String query) throws SQLException {
3739
}
3840

3941
public synchronized boolean execute(String query) throws SQLException {
42+
setLastCachedQuery(query);
4043
Statement stmt = connection.createStatement();
4144

4245
if (isLogEnabled()) {
@@ -284,10 +287,27 @@ public boolean deleteTable(String tableName, String sequenceName) throws SQLExce
284287
return b;
285288
}
286289

290+
public synchronized boolean isExecutableQuery(String query) {
291+
try {
292+
executeQuery(query);
293+
} catch (SQLException e) {
294+
return false;
295+
}
296+
return true;
297+
}
298+
287299
public Connection getConnection() {
288300
return connection;
289301
}
290302

303+
public static synchronized String getLastCachedQuery() {
304+
return lastCachedQuery;
305+
}
306+
307+
private static synchronized void setLastCachedQuery(String pLastCachedQuery) {
308+
lastCachedQuery = pLastCachedQuery;
309+
}
310+
291311
public boolean isLogEnabled() {
292312
return logEnabled;
293313
}
@@ -296,13 +316,4 @@ public void setLogEnabled(boolean logEnabled) {
296316
this.logEnabled = logEnabled;
297317
}
298318

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

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

Lines changed: 106 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.rub.bph.omnineuro.client.core.db.in;
22

3+
import de.rub.bph.omnineuro.client.core.db.QueryExecutor;
34
import de.rub.bph.omnineuro.client.core.db.out.holder.CompoundHolder;
45
import de.rub.bph.omnineuro.client.core.sheet.data.DateInterpreter;
56
import de.rub.bph.omnineuro.client.imported.log.Log;
@@ -21,6 +22,11 @@ public class AXESInserter extends DBInserter implements Runnable {
2122
private JSONObject data;
2223
private String name;
2324

25+
public static final String INVALID_INDIVIDUAL_NAME = "<unknown individual>";
26+
public static final String INVALID_MUTATION_NAME = "<unknown mutation>";
27+
public static final String INVALID_SEX_NAME = "undefined";
28+
public static final String INVALID_SPECIES_NAME = "unknown";
29+
2430
public AXESInserter(JSONObject data, boolean attemptUnblinding) {
2531
super(attemptUnblinding);
2632
this.data = data;
@@ -34,24 +40,19 @@ public AXESInserter(JSONObject data, boolean attemptUnblinding) {
3440
}
3541
}
3642

37-
private synchronized long getIndividualID(String individual, String sex, String species) throws JSONException, SQLException {
38-
long individualID;
43+
private synchronized long getMutationID(String mutation) throws SQLException {
44+
long mutationID;
3945

4046
synchronized (executor) {
4147
try {
42-
individualID = executor.getIDViaName("individual", individual);
48+
mutationID = executor.getIDViaName("mutation", mutation);
4349
} catch (Throwable e) {
44-
long sexID = executor.getIDViaFeature("sex", "label", sex);
45-
long speciesID = executor.getIDViaName("species", species);
46-
47-
individualID = executor.getNextSequenceTableVal("individual");
48-
executor.insertIndividual(individualID, individual, sexID, speciesID);
49-
Log.i("New individual inserted into the database. Name: '" + individual + "'. ID: " + individualID);
50-
addError("Warning. Individual '" + individual + "' was not in the database. It has been added. Species: " + species + ". Sex: " + sex);
50+
mutationID = executor.getNextSequenceTableVal("mutation");
51+
executor.insertMutation(mutationID, mutation);
5152
}
5253
}
5354

54-
return individualID;
55+
return mutationID;
5556
}
5657

5758
@Override
@@ -70,16 +71,24 @@ public void run() {
7071
String projectName = metaDataGeneral.getString("Project");
7172
String assay = metaDataGeneral.getString("Assay");
7273
String experimentName = metaDataGeneral.getString("ExperimentID");
73-
String individual = metaDataGeneral.getString("Individual");
7474
String compound = metaDataGeneral.getString("Compound");
7575
String casNR = metaDataGeneral.getString("CAS No.");
76-
String compuntAbbreviation = metaDataGeneral.getString("Compound abbreviation");
76+
String compoundAbbreviation = metaDataGeneral.getString("Compound abbreviation");
7777
String cellType = metaDataGeneral.getString("Cell type");
7878
String species = metaDataGeneral.getString("Species");
7979
String plateFormat = metaDataGeneral.getString("Plateformat");
8080
String sex = metaDataGeneral.getString("Sex");
8181
String workgroup = metaDataGeneral.getString("Department"); //Workgroup under department? Yep. This is intentional.
8282

83+
String individual = null;
84+
String mutation = null;
85+
if (metaDataGeneral.has("Individual")) {
86+
individual = metaDataGeneral.getString("Individual");
87+
}
88+
if (metaDataGeneral.has("Mutation")) {
89+
mutation = metaDataGeneral.getString("Mutation");
90+
}
91+
8392
if (compound.equals("0.0")) {
8493
//If there was no compound meta data sheet provided, the formula evaluator returns '0.0'. That's an error.
8594
throw new IllegalStateException("Failed to read the meta data sheet containing the compound for " + experimentName + "!");
@@ -92,10 +101,14 @@ public void run() {
92101

93102
String solvent = metaDataSolvent.getString("Solvent");
94103
solvent = solvent.toUpperCase().trim();
95-
double solventConcentration = -1;
104+
double solventConcentration = -1.0;
96105
if (metaDataSolvent.has("Solvent conc.*")) {
97106
solventConcentration = metaDataSolvent.getDouble("Solvent conc.*");
98107
}
108+
if (Double.isNaN(solventConcentration)) {
109+
addError("Warning: Solvent has illegal values!");
110+
solventConcentration = -1.0;
111+
}
99112

100113
long solventID = -1;
101114
try {
@@ -125,17 +138,30 @@ public void run() {
125138
addError("Compound is unknown!", true);
126139
}
127140

128-
//Next: Insert it into the database
129-
Log.i("TODO: Actually insert it now...");
141+
long individualID;
142+
long mutationID;
143+
if (individual == null && mutation == null) {
144+
addError("Sheet has neither an individual or a mutation!");
145+
}
146+
147+
if (individual == null) {
148+
individualID = getInvalidIndividualID();
149+
} else {
150+
individualID = getIndividualID(individual, sex, species);
151+
}
152+
if (mutation == null) {
153+
mutationID = getInvalidMutationID();
154+
} else {
155+
mutationID = getMutationID(mutation);
156+
}
130157

131158
//But first: Let me take a sel... the necessary IDs of existing meta data from the Database
132159
long assayID = executor.getIDViaName("assay", assay);
133160
long cellTypeID = executor.getIDViaName("cell_type", cellType);
134161
long projectID = executor.getIDViaName("project", projectName);
135-
long plateformatID = executor.getIDViaName("plate_format", plateFormat);
162+
long plateFormatID = executor.getIDViaName("plate_format", plateFormat);
136163
long workgroupID = executor.getIDViaName("workgroup", workgroup);
137164

138-
long individualID = getIndividualID(individual, sex, species);
139165
long wellNotAvailableID = executor.getIDViaName("well", "Unknown");
140166
long detectionMethodNotAvailableID = executor.getIDViaName("detection_method", "Unknown");
141167

@@ -157,7 +183,7 @@ public void run() {
157183
}
158184
}
159185
} else {
160-
addError("Failed to resolve compound name: '" + compound + "' [" + compuntAbbreviation + "]. That's okay, if a valid Cas Nr. is provided instead: '" + casNR + "'.");
186+
addError("Failed to resolve compound name: '" + compound + "' [" + compoundAbbreviation + "]. That's okay, if a valid Cas Nr. is provided instead: '" + casNR + "'.");
161187
compoundID = executor.getIDViaFeature("compound", "cas_no", casNR);
162188
String newCompoundName = executor.getNameViaID("compound", compoundID);
163189
addError("\t\t\tBut that CAS Nr. existed in the DB and the compound was resolved as '" + newCompoundName + "'.");
@@ -182,7 +208,7 @@ public void run() {
182208
if (compoundHolder != null) {
183209
compound = compoundHolder.getName();
184210
casNR = compoundHolder.getCas();
185-
compuntAbbreviation = compoundHolder.getAbbreviation();
211+
compoundAbbreviation = compoundHolder.getAbbreviation();
186212
compoundID = compoundHolder.getCompoundID();
187213
}
188214
} else {
@@ -193,7 +219,7 @@ public void run() {
193219
long experimentID;
194220
synchronized (executor) {
195221
experimentID = executor.getNextSequenceTableVal("experiment");
196-
executor.insertExperiment(experimentID, date.getTime(), experimentName, projectID, workgroupID, individualID, compoundID, cellTypeID, assayID, plateformatID, solventID, solventConcentration, controlPlateID);
222+
executor.insertExperiment(experimentID, date.getTime(), experimentName, projectID, workgroupID, individualID, mutationID, compoundID, cellTypeID, assayID, plateFormatID, solventID, solventConcentration, controlPlateID);
197223
}
198224

199225
for (String passageP : JSONOperator.getKeys(metaDataPassages)) {
@@ -328,13 +354,71 @@ public void run() {
328354
}
329355
}
330356
} catch (Throwable e) {
357+
String fatalErrorText = " == FATAL ERROR! == Failed to insert Experiment " + getName() + " into the database! Error Type: " + e.getClass().getSimpleName() + ". Reason: '" + e.getMessage() + "'";
358+
if (e instanceof SQLException) {
359+
fatalErrorText = fatalErrorText + ". Last SQL query: " + QueryExecutor.getLastCachedQuery();
360+
}
361+
331362
Log.e("Failed to insert Experiment: " + getName(), e);
332-
addError(" == FATAL ERROR! == Failed to insert Experiment " + getName() + " into the database! Error Type: " + e.getClass().getSimpleName() + ". Reason: '" + e.getMessage() + "'");
363+
addError(fatalErrorText);
333364
}
334365
Log.i("Finished inserting responses for " + getName() + ". Count: " + getInsertedResponsesCount());
335366
setFinished();
336367
}
337368

369+
private synchronized long getIndividualID(String individual, String sex, String species) throws JSONException, SQLException {
370+
long individualID;
371+
372+
synchronized (executor) {
373+
try {
374+
individualID = executor.getIDViaName("individual", individual);
375+
} catch (Throwable e) {
376+
long sexID = executor.getIDViaFeature("sex", "label", sex);
377+
long speciesID = executor.getIDViaName("species", species);
378+
379+
individualID = executor.getNextSequenceTableVal("individual");
380+
executor.insertIndividual(individualID, individual, sexID, speciesID);
381+
Log.i("New individual inserted into the database. Name: '" + individual + "'. ID: " + individualID);
382+
addError("Warning. Individual '" + individual + "' was not in the database. It has been added. Species: " + species + ". Sex: " + sex);
383+
}
384+
}
385+
386+
return individualID;
387+
}
388+
389+
public static synchronized long getInvalidIndividualID() throws JSONException, SQLException {
390+
long individualID;
391+
392+
synchronized (executor) {
393+
try {
394+
individualID = executor.getIDViaName("individual", INVALID_INDIVIDUAL_NAME);
395+
} catch (Throwable e) {
396+
long sexID = executor.getIDViaFeature("sex", "label", INVALID_SEX_NAME);
397+
long speciesID = executor.getIDViaName("species", INVALID_SPECIES_NAME);
398+
399+
individualID = executor.getNextSequenceTableVal("individual");
400+
executor.insertIndividual(individualID, INVALID_INDIVIDUAL_NAME, sexID, speciesID);
401+
}
402+
}
403+
404+
return individualID;
405+
}
406+
407+
public static synchronized long getInvalidMutationID() throws SQLException {
408+
long mutationID;
409+
410+
synchronized (executor) {
411+
try {
412+
mutationID = executor.getIDViaName("mutation", INVALID_MUTATION_NAME);
413+
} catch (Throwable e) {
414+
mutationID = executor.getNextSequenceTableVal("mutation");
415+
executor.insertMutation(mutationID, INVALID_MUTATION_NAME);
416+
}
417+
}
418+
419+
return mutationID;
420+
}
421+
338422
@Override
339423
public String getName() {
340424
return name;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,11 @@ public void run() {
330330
//TODO Add solvent ID to UKN assay experiments
331331
long solventID = -1;
332332
double solventConcentration = -1d;
333+
long mutationID = AXESInserter.getInvalidMutationID();
333334
String controlPlateID = "<Unknown>";
334335

335336
experimentID = executor.getNextSequenceTableVal("experiment");
336-
executor.insertExperiment(experimentID, timestampExperiment, experimentName, projectID, workgroupID, individualID, compoundID, cellTypeID, assayID, plateFormatID, solventID, solventConcentration, controlPlateID);
337+
executor.insertExperiment(experimentID, timestampExperiment, experimentName, projectID, workgroupID, individualID, mutationID, compoundID, cellTypeID, assayID, plateFormatID, solventID, solventConcentration, controlPlateID);
337338
}
338339
Log.v("Experiment ID extracted: " + experimentID);
339340
}

src/main/java/de/rub/bph/omnineuro/client/imported/log/Log.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package de.rub.bph.omnineuro.client.imported.log;
22

33
import de.rub.bph.omnineuro.client.Client;
4+
import de.rub.bph.omnineuro.client.core.db.QueryExecutor;
45
import de.rub.bph.omnineuro.client.imported.filemanager.FileManager;
56

67
import java.io.*;
8+
import java.sql.SQLException;
79
import java.text.SimpleDateFormat;
810
import java.util.Date;
911
import java.util.concurrent.ExecutorService;
@@ -83,6 +85,11 @@ public static void log(LogLevel level, String text, Throwable throwable) {
8385
text = "V" + VERSION + " +" + level.getSign() + classPath + " - " + text;
8486
if (throwable != null) {
8587
text = text + "\n" + throwable.getClass().getName() + ": '" + throwable.getMessage() + "'";
88+
if (throwable instanceof SQLException) {
89+
String cachedQuery = QueryExecutor.getLastCachedQuery();
90+
text = text + "\nLast known SQL query: '" + cachedQuery + "'";
91+
}
92+
8693
for (StackTraceElement trace : throwable.getStackTrace()) {
8794
text = text + "\n\t" + trace.toString();
8895
}

0 commit comments

Comments
 (0)