11package de .rub .bph .omnineuro .client .core .db .in ;
22
3+ import de .rub .bph .omnineuro .client .core .db .QueryExecutor ;
34import de .rub .bph .omnineuro .client .core .db .out .holder .CompoundHolder ;
45import de .rub .bph .omnineuro .client .core .sheet .data .DateInterpreter ;
56import 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 \t But 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 ;
0 commit comments