-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuniprot.java
More file actions
885 lines (788 loc) · 39.3 KB
/
uniprot.java
File metadata and controls
885 lines (788 loc) · 39.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
* Copyright (c) 2005-2018 Fred Hutchinson Cancer Research Center
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.protein.uniprot;
import org.apache.logging.log4j.Logger;
import org.labkey.api.data.CoreSchema;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.dialect.SqlDialect;
import org.labkey.api.protein.ProteinSchema;
import org.labkey.api.util.StringUtilsLabKey;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;
// Repository for routines specific to org.fhcrc.edi.protein.uniprot.uniprot parsing
// And initial stuff to do with the root element
public class uniprot extends ParseActions
{
private final Logger _log;
private static final SqlDialect _dialect = CoreSchema.getInstance().getSqlDialect();
private long _startTime;
private static final int TRANSACTION_ROW_COUNT = 100;
// n of top-level "entries" to skip
protected int _skipEntries = 0;
public uniprot(Logger log)
{
_log = log;
}
public void setSkipEntries(int s)
{
_skipEntries = s;
}
public int getSkipEntries()
{
return _skipEntries;
}
public boolean unBumpSkip()
{
if (getSkipEntries() > 0)
{
setSkipEntries(getSkipEntries() - 1);
return false;
}
else
{
return true;
}
}
@Override
public void beginElement(ParseContext context, Attributes attrs) throws SAXException
{
_startTime = System.currentTimeMillis();
context.setUniprotRoot(this);
// Annotations and Identifiers are Vectors of Maps
// The Vectors get cleared by insertTables
try
{
setupNames(context.getConnection());
if (getCurrentInsertId() == 0)
{
_initialInsertion.setString(1, getFile().getPath());
if (getComment() == null) setComment("");
_initialInsertion.setString(2, getComment());
_initialInsertion.setTimestamp(3, new java.sql.Timestamp(new java.util.Date().getTime()));
try (ResultSet idrs = _dialect.executeWithResults(_initialInsertion))
{
idrs.next();
setCurrentInsertId(idrs.getInt(1));
}
context.getConnection().commit();
}
else
{
try (ResultSet rs = context.getConnection().createStatement().executeQuery(
"SELECT RecordsProcessed FROM " + ProteinSchema.getTableInfoAnnotInsertions() + " WHERE InsertId=" + getCurrentInsertId()))
{
rs.next();
setSkipEntries(rs.getInt("RecordsProcessed"));
}
}
}
catch (SQLException e)
{
throw new SAXException(e);
}
}
@Override
public void endElement(ParseContext context) throws SAXException
{
try
{
context.insert();
_finalizeInsertion.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime()));
_finalizeInsertion.setInt(2, getCurrentInsertId());
_finalizeInsertion.executeUpdate();
executeUpdate("DROP TABLE " + _oTableName, context.getConnection());
executeUpdate("DROP TABLE " + _aTableName, context.getConnection());
executeUpdate("DROP TABLE " + _sTableName, context.getConnection());
executeUpdate("DROP TABLE " + _iTableName, context.getConnection());
}
catch (SQLException e)
{
throw new SAXException(e);
}
if (_addOrg != null) { try { _addOrg.close(); } catch (SQLException e) {} }
if (_addSeq != null) { try { _addSeq.close(); } catch (SQLException e) {} }
if (_addAnnot != null) { try { _addAnnot.close(); } catch (SQLException e) {} }
if (_addIdent != null) { try { _addIdent.close(); } catch (SQLException e) {} }
//c.commit();
long totalTime = System.currentTimeMillis() - _startTime;
_log.info("Finished uniprot upload in " + totalTime + " milliseconds");
}
// All Database Stuff Follows
// Some day this should be refactored into per-DBM modules
private String _oTableName = null;
private String _sTableName = null;
private String _iTableName = null;
private String _aTableName = null;
private PreparedStatement _addOrg = null;
private PreparedStatement _addSeq = null;
private PreparedStatement _addAnnot = null;
private PreparedStatement _addIdent = null;
private String _insertIntoOrgCommand = null;
private String _deleteFromTmpOrgCommand = null;
private String _insertOrgIDCommand = null;
private String _updateOrgCommand = null;
private String _insertIntoSeqCommand = null;
private String _clearExistingIdentifiersCommand;
private String _clearExistingAnnotationsCommand;
private String _updateSeqTableCommand = null;
private String _insertIdentTypesCommand = null;
private String _insertIntoIdentsCommand = null;
private String _insertInfoSourceFromSeqCommand = null;
private String _insertAnnotTypesCommand = null;
private String _insertIntoAnnotsCommand = null;
private String _updateAnnotsWithSeqsCommand = null;
private String _updateAnnotsWithIdentsCommand = null;
private String _updateIdentsWithSeqsCommand = null;
private PreparedStatement _initialInsertion = null;
private PreparedStatement _updateInsertion = null;
private PreparedStatement _finalizeInsertion;
private PreparedStatement _getCurrentInsertStats = null;
private void setupNames(Connection c) throws SQLException
{
String randomTableSuffix = StringUtilsLabKey.getUniquifier(9);
_oTableName = _dialect.getTempTablePrefix() + "organism" + randomTableSuffix;
String createOTableCommand = "CREATE " + _dialect.getTempTableKeyword() + " TABLE " + _oTableName + " ( " +
"common_name varchar(100) NULL, " +
"genus varchar(100), " +
"species varchar(100), " +
"comments varchar(200) NULL, " +
"identID varchar(50), " +
"entry_date " + _dialect.getDefaultDateTimeDataType() + " NULL " +
")";
executeUpdate(createOTableCommand, c);
_sTableName = _dialect.getTempTablePrefix() + "sequences" + randomTableSuffix;
String createSTableCommand =
"CREATE " + _dialect.getTempTableKeyword() + " TABLE " + _sTableName + " ( " +
"ProtSequence text NULL , " +
"hash varchar(100) NULL , " +
"description varchar(200) NULL ," +
"source_change_date " + _dialect.getDefaultDateTimeDataType() + " NULL ," +
"source_insert_date " + _dialect.getDefaultDateTimeDataType() + " NULL ," +
"genus varchar(100) NULL, " +
"species varchar(100) NULL, " +
"mass float NULL , " +
"length int NULL ," +
"best_name varchar(50) NULL, " +
"source varchar(50) NULL," +
"best_gene_name varchar(50) NULL, " +
"entry_date " + _dialect.getDefaultDateTimeDataType() + " NULL" +
")";
executeUpdate(createSTableCommand, c);
_iTableName = _dialect.getTempTablePrefix() + "identifiers" + randomTableSuffix;
String createITableCommand = "CREATE " + _dialect.getTempTableKeyword() + " TABLE " + _iTableName + " ( " +
"identifier varchar(50) NOT NULL, " +
"identType varchar(50) NULL, " +
"genus varchar(100) NULL, " +
"species varchar(100) NULL, " +
"hash varchar(100) NULL, " +
"seq_id int NULL, " +
"entry_date " + _dialect.getDefaultDateTimeDataType() + " NULL" +
")";
executeUpdate(createITableCommand, c);
_aTableName = _dialect.getTempTablePrefix() + "annotations" + randomTableSuffix;
String createATableCommand =
"CREATE " + _dialect.getTempTableKeyword() + " TABLE " + _aTableName + " ( " +
"annot_val varchar(200) NOT NULL, " +
"annotType varchar(50) NULL, " +
"genus varchar(100) NULL, " +
"species varchar(100) NULL, " +
"hash varchar(100) NULL, " +
"seq_id int NULL, " +
"start_pos int NULL, " +
"end_pos int NULL, " +
"identifier varchar(50) NULL," +
"identType varchar(50) NULL, " +
"ident_id int NULL, " +
"entry_date " + _dialect.getDefaultDateTimeDataType() + " NULL" +
")";
executeUpdate(createATableCommand, c);
_addOrg = c.prepareStatement(
"INSERT INTO " + _oTableName + " (common_name,genus,species,comments,identID,entry_date) " +
" VALUES (?,?,?,?,?,?) "
);
_addSeq = c.prepareStatement(
"INSERT INTO " + _sTableName +
" (ProtSequence,hash,description,source_change_date,source_insert_date,genus,species," +
" mass,length,source,best_name,best_gene_name,entry_date) " +
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) "
);
_addIdent = c.prepareStatement(
"INSERT INTO " + _iTableName + " (identifier,identType,genus,species,hash,entry_date)" +
" VALUES (?,?,?,?,?,?)");
_addAnnot = c.prepareStatement(
"INSERT INTO " + _aTableName + " (annot_val,annotType,genus,species,hash,start_pos,end_pos,identifier,identType,entry_date)" +
" VALUES (?,?,?,?,?,?,?,?,?,?)"
);
_insertIntoOrgCommand =
"INSERT INTO " + ProteinSchema.getTableInfoOrganisms() + " (Genus,Species,CommonName,Comments) " +
"SELECT genus,species,common_name,comments FROM " + _oTableName +
" WHERE NOT EXISTS (" +
"SELECT * FROM " + ProteinSchema.getTableInfoOrganisms() + " WHERE " + _oTableName + ".genus = " + ProteinSchema.getTableInfoOrganisms() + ".genus AND " +
_oTableName + ".species = " + ProteinSchema.getTableInfoOrganisms() + ".species)";
_deleteFromTmpOrgCommand =
"DELETE FROM " + _oTableName +
" WHERE EXISTS (" +
" SELECT * FROM " + ProteinSchema.getTableInfoOrganisms() + "," + _oTableName +
" WHERE " + _oTableName + ".genus=" + ProteinSchema.getTableInfoOrganisms() + ".genus AND " +
_oTableName + ".species=" + ProteinSchema.getTableInfoOrganisms() + ".species AND " + ProteinSchema.getTableInfoOrganisms() + ".IdentId IS NOT NULL" +
" )";
final int taxonomyTypeIndex;
// TODO: Just use a TableSelector
try (ResultSet rs = c.createStatement().executeQuery("SELECT IdentTypeId FROM " + ProteinSchema.getTableInfoIdentTypes() + " WHERE name='NCBI Taxonomy'"))
{
rs.next();
taxonomyTypeIndex = rs.getInt(1);
}
_insertOrgIDCommand =
"INSERT INTO " + ProteinSchema.getTableInfoIdentifiers() + " (identifier,IdentTypeId,EntryDate) " +
"SELECT DISTINCT identID," + taxonomyTypeIndex + ",entry_date " +
"FROM " + _oTableName + " " +
"WHERE NOT EXISTS (SELECT * FROM " + ProteinSchema.getTableInfoIdentifiers() + " WHERE " +
"" + ProteinSchema.getTableInfoIdentifiers() + ".identifier = " + _oTableName + ".identID AND " +
"" + ProteinSchema.getTableInfoIdentifiers() + ".identtypeid=" + taxonomyTypeIndex + ")";
_updateOrgCommand =
"UPDATE " + ProteinSchema.getTableInfoOrganisms() + " SET identid=c.identid " +
"FROM " + ProteinSchema.getTableInfoOrganisms() + " a," + _oTableName + " b, " + ProteinSchema.getTableInfoIdentifiers() + " c " +
"WHERE a.genus=b.genus AND a.species=b.species AND " +
" c.identtypeid=" + taxonomyTypeIndex + " AND " +
" c.identifier=b.identID";
_clearExistingIdentifiersCommand =
"DELETE FROM " + ProteinSchema.getTableInfoIdentifiers() + " WHERE SeqId IN (SELECT seq_id FROM " + _iTableName + ")";
_clearExistingAnnotationsCommand =
"DELETE FROM " + ProteinSchema.getTableInfoAnnotations() + " WHERE SeqId IN (SELECT seq_id FROM " + _aTableName + ")";
_insertIntoSeqCommand =
"INSERT INTO " + ProteinSchema.getTableInfoSequences() + " (ProtSequence,hash,description," +
"SourceChangeDate,SourceInsertDate,mass,length,OrgId," +
"SourceId,BestName,InsertDate,BestGeneName) " +
"SELECT a.ProtSequence,a.hash,a.description,a.source_change_date," +
"a.source_insert_date,a.mass,a.length,b.OrgId,c.SourceId," +
"a.best_name, a.entry_date, a.best_gene_name " +
" FROM " + _sTableName + " a, " + ProteinSchema.getTableInfoOrganisms() + " b, " + ProteinSchema.getTableInfoInfoSources() + " c " +
" WHERE NOT EXISTS (" +
"SELECT * FROM " + ProteinSchema.getTableInfoSequences() + " WHERE " +
"a.hash = " + ProteinSchema.getTableInfoSequences() + ".hash AND b.OrgId=" + ProteinSchema.getTableInfoSequences() + ".OrgId AND " +
" UPPER(b.genus)=UPPER(a.genus) AND " +
" UPPER(a.species)=UPPER(b.species)) AND " +
" UPPER(a.species)=UPPER(b.species) AND " + " " +
" UPPER(a.genus)=UPPER(b.genus) AND " +
"c.name=a.source";
_updateSeqTableCommand =
"UPDATE "+ ProteinSchema.getTableInfoSequences() +
" SET description=a.description, bestgenename=a.best_gene_name " +
" FROM " + _sTableName + " a, "+ ProteinSchema.getTableInfoOrganisms() + " b " +
" WHERE " + ProteinSchema.getTableInfoSequences()+".hash = a.hash AND " +
ProteinSchema.getTableInfoSequences()+".orgid=b.orgid AND UPPER(a.genus)=UPPER(b.genus) AND " +
" UPPER(a.species)=UPPER(b.species)";
_insertIdentTypesCommand =
"INSERT INTO " + ProteinSchema.getTableInfoIdentTypes() + " (name,EntryDate) " +
" SELECT DISTINCT a.identType,max(a.entry_date) FROM " +
_iTableName + " a WHERE NOT EXISTS (SELECT * FROM " + ProteinSchema.getTableInfoIdentTypes() + " " +
" WHERE a.identType = " + ProteinSchema.getTableInfoIdentTypes() + ".name) GROUP BY a.identType";
_insertInfoSourceFromSeqCommand =
"INSERT INTO " + ProteinSchema.getTableInfoInfoSources() + " (name,InsertDate) SELECT DISTINCT source,max(entry_date) FROM " +
_sTableName + " a WHERE NOT EXISTS (SELECT * FROM " + ProteinSchema.getTableInfoInfoSources() + " " +
" WHERE a.source = " + ProteinSchema.getTableInfoInfoSources() + ".name) GROUP BY a.source";
_insertIntoIdentsCommand =
"INSERT INTO " + ProteinSchema.getTableInfoIdentifiers() + " " +
" (identifier,IdentTypeId,SeqId,EntryDate) " +
" SELECT DISTINCT b.identifier,a.identtypeid,b.seq_id,max(b.entry_date) " +
" FROM " + ProteinSchema.getTableInfoIdentTypes() + " a," + _iTableName + " b " +
" WHERE " +
" a.name = b.identType AND " +
" NOT EXISTS (" +
" SELECT * FROM " + ProteinSchema.getTableInfoIdentifiers() + " c WHERE " +
" a.name = b.identType AND " +
" c.identtypeid = a.identtypeid AND " +
" b.seq_id=c.seqid AND " +
" b.identifier = c.identifier " +
" ) GROUP BY b.identifier,a.identtypeid,b.seq_id";
_insertAnnotTypesCommand =
"INSERT INTO " + ProteinSchema.getTableInfoAnnotationTypes() + " (name,EntryDate) SELECT DISTINCT a.annotType,max(a.entry_date) FROM " +
_aTableName + " a WHERE NOT EXISTS (SELECT * FROM " + ProteinSchema.getTableInfoAnnotationTypes() + " " +
" WHERE a.annotType = " + ProteinSchema.getTableInfoAnnotationTypes() + ".name) GROUP BY a.annotType";
_insertIntoAnnotsCommand =
"INSERT INTO " + ProteinSchema.getTableInfoAnnotations() + " " +
" (annotval,annottypeid,annotident,seqid,startpos,endpos,insertdate) " +
" SELECT DISTINCT b.annot_val,a.annottypeid, b.ident_id, " +
"b.seq_id, b.start_pos, b.end_pos,max(b.entry_date) " +
" FROM " + ProteinSchema.getTableInfoAnnotationTypes() + " a," + _aTableName + " b " +
" WHERE " +
" a.name = b.annotType AND " +
" NOT EXISTS (" +
" SELECT * FROM " + ProteinSchema.getTableInfoAnnotations() + " c WHERE " +
" a.name = b.annotType AND " +
" b.annot_val = c.annotval AND " +
" b.seq_id = c.seqid AND " +
" a.annottypeid = c.annottypeid AND " +
" b.start_pos = c.startpos AND b.end_pos=c.endpos " +
" ) GROUP BY b.annot_val,a.annottypeid,b.ident_id,b.seq_id,b.start_pos,b.end_pos";
_updateAnnotsWithSeqsCommand =
"UPDATE " + _aTableName + " SET seq_id = " +
"(SELECT c.seqId FROM " +
ProteinSchema.getTableInfoOrganisms() + " b, " +
ProteinSchema.getTableInfoSequences() + " c " +
" WHERE c.hash=" + _aTableName + ".hash AND " + _aTableName + ".genus=b.genus AND " + _aTableName + ".species=b.species AND b.orgid=c.orgid" +
")"
;
_updateAnnotsWithIdentsCommand =
"UPDATE " + _aTableName + " SET ident_id = (SELECT DISTINCT b.identID FROM " +
ProteinSchema.getTableInfoIdentifiers() + " b, " + ProteinSchema.getTableInfoIdentTypes() + " c " +
" WHERE " + _aTableName + ".seq_id=b.seqid AND " + _aTableName + ".identifier=b.identifier AND " +
" b.identtypeid=c.identtypeid AND " + _aTableName + ".identType=c.name)";
_updateIdentsWithSeqsCommand =
"UPDATE " + _iTableName + " SET seq_id = " +
"(SELECT c.seqId FROM " +
ProteinSchema.getTableInfoOrganisms() + " b, " +
ProteinSchema.getTableInfoSequences() + " c " +
" WHERE c.hash=" + _iTableName + ".hash AND " + _iTableName + ".genus=b.genus AND " + _iTableName + ".species=b.species AND b.orgid=c.orgid" +
")";
SQLFragment initialInsertionCommand = new SQLFragment("INSERT INTO " + ProteinSchema.getTableInfoAnnotInsertions() + " (FileName,FileType,Comment,InsertDate) VALUES (?,'uniprot',?,?)");
_dialect.addReselect(initialInsertionCommand, ProteinSchema.getTableInfoAnnotInsertions().getColumn("InsertId"), null);
_initialInsertion = c.prepareStatement(initialInsertionCommand.getSQL());
String getCurrentInsertStatsCommand =
"SELECT SequencesAdded,AnnotationsAdded,IdentifiersAdded,OrganismsAdded,Mouthsful,RecordsProcessed FROM " + ProteinSchema.getTableInfoAnnotInsertions() + " WHERE InsertId=?";
_getCurrentInsertStats = c.prepareStatement(getCurrentInsertStatsCommand);
String updateInsertionCommand = "UPDATE " + ProteinSchema.getTableInfoAnnotInsertions() + " SET " +
" Mouthsful=?,SequencesAdded=?,AnnotationsAdded=?,IdentifiersAdded=?,OrganismsAdded=?, " +
" MRMSequencesAdded=?,MRMAnnotationsAdded=?,MRMIdentifiersAdded=?,MRMOrganismsAdded=?,MRMSize=?," +
" RecordsProcessed=?,ChangeDate=? " +
" WHERE InsertId=?";
_updateInsertion = c.prepareStatement(updateInsertionCommand);
String finalizeInsertionCommand = "UPDATE " + ProteinSchema.getTableInfoAnnotInsertions() + " SET " +
" CompletionDate=? WHERE InsertId=?";
_finalizeInsertion = c.prepareStatement(finalizeInsertionCommand);
}
protected void handleThreadStateChangeRequests()
{
/* if (Thread.currentThread() instanceof XMLProteinLoader.BackgroundAnnotInsertions)
{
XMLProteinLoader.BackgroundAnnotInsertions thisThread =
(XMLProteinLoader.BackgroundAnnotInsertions) (Thread.currentThread());
thisThread.getParser().handleThreadStateChangeRequests();
}
*/
}
public void insertTables(ParseContext context, Connection conn) throws SQLException
{
conn.setAutoCommit(false);
handleThreadStateChangeRequests();
int orgsAdded = insertOrganisms(context, conn);
handleThreadStateChangeRequests();
int seqsAdded = insertSequences(context, conn);
handleThreadStateChangeRequests();
int identsAdded;
int annotsAdded;
try
{
int identsProcessed = insertIdentifiers(context, conn);
_log.debug("Inserted " + identsProcessed + " identifiers into temp table");
handleThreadStateChangeRequests();
try
{
int annotsProcessed = insertAnnotations(context, conn);
_log.debug("Inserted " + annotsProcessed + " annotations into temp table");
identsAdded = mergeIdentifiers(context, conn);
annotsAdded = mergeAnnotations(context, conn);
}
finally
{
try { executeUpdate(_dialect.getDropIndexCommand(_aTableName, "aAnnot_val"), conn); } catch (SQLException e) {}
try { executeUpdate(_dialect.getDropIndexCommand(_aTableName, "aAnnotType"), conn); } catch (SQLException e) {}
try { executeUpdate(_dialect.getDropIndexCommand(_aTableName, "aHashGenusSpecies"), conn); } catch (SQLException e) {}
try { executeUpdate("TRUNCATE TABLE " + _aTableName, conn, "TRUNCATE TABLE " + _aTableName); } catch (SQLException e) {}
}
}
finally
{
try { executeUpdate(_dialect.getDropIndexCommand(_iTableName, "iIdentifier"), conn); } catch (SQLException e) {}
try { executeUpdate(_dialect.getDropIndexCommand(_iTableName, "iIdenttype"), conn); } catch (SQLException e) {}
try { executeUpdate(_dialect.getDropIndexCommand(_iTableName, "iSpeciesGenusHash"), conn); } catch (SQLException e) {}
try { executeUpdate("TRUNCATE TABLE " + _iTableName, conn, "TRUNCATE TABLE " + _iTableName); } catch (SQLException e) {}
}
conn.setAutoCommit(true);
handleThreadStateChangeRequests();
_log.info("Batch complete. Added: " +
orgsAdded + " organisms; " +
seqsAdded + " sequences; " +
identsAdded + " identifiers; " +
annotsAdded + " annotations");
_getCurrentInsertStats.setInt(1, getCurrentInsertId());
int priorseqs;
int priorannots;
int prioridents;
int priororgs;
int mouthsful;
int records;
// TODO: Just use TableSelector.getMap()
try (ResultSet r = _getCurrentInsertStats.executeQuery())
{
r.next();
priorseqs = r.getInt("SequencesAdded");
priorannots = r.getInt("AnnotationsAdded");
prioridents = r.getInt("IdentifiersAdded");
priororgs = r.getInt("OrganismsAdded");
mouthsful = r.getInt("Mouthsful");
records = r.getInt("RecordsProcessed");
}
int curNRecords = context.getSequences().size();
_updateInsertion.setInt(1, mouthsful + 1);
_updateInsertion.setInt(2, priorseqs + seqsAdded);
_updateInsertion.setInt(3, priorannots + annotsAdded);
_updateInsertion.setInt(4, prioridents + identsAdded);
_updateInsertion.setInt(5, priororgs + orgsAdded);
_updateInsertion.setInt(6, seqsAdded);
_updateInsertion.setInt(7, annotsAdded);
_updateInsertion.setInt(8, identsAdded);
_updateInsertion.setInt(9, orgsAdded);
_updateInsertion.setInt(10, curNRecords);
_updateInsertion.setInt(11, records + curNRecords);
_updateInsertion.setInt(13, getCurrentInsertId());
_updateInsertion.setTimestamp(12, new java.sql.Timestamp(new java.util.Date().getTime()));
_updateInsertion.executeUpdate();
//conn.commit();
_log.info(
"Added: " +
orgsAdded + " organisms; " +
seqsAdded + " sequences; " +
identsAdded + " identifiers; " +
annotsAdded + " annotations"
);
}
public int insertOrganisms(ParseContext context, Connection conn) throws SQLException
{
int transactionCount = 0;
_addOrg.setTimestamp(6, new Timestamp(new Date().getTime()));
//Add current mouthful of Organisms
_log.debug((new java.util.Date()) + " Processing organisms");
// All organism records. Each one is a HashMap
for (UniprotOrganism curOrg : context.getOrganisms())
{
transactionCount++;
if (curOrg.getCommonName() == null)
{
_addOrg.setNull(1, Types.VARCHAR);
}
else
{
_addOrg.setString(1, curOrg.getCommonName());
}
_addOrg.setString(2, curOrg.getGenus());
_addOrg.setString(3, curOrg.getSpecies());
if (curOrg.getComments() == null)
{
_addOrg.setNull(4, Types.VARCHAR);
}
else
{
_addOrg.setString(4, curOrg.getComments());
}
_addOrg.setString(5, curOrg.getIdentID());
// Timestamp at index 6 is set once for the whole PreparedStatement
_addOrg.addBatch();
if (transactionCount == TRANSACTION_ROW_COUNT)
{
transactionCount = 0;
_addOrg.executeBatch();
conn.commit();
_addOrg.clearBatch();
}
handleThreadStateChangeRequests();
}
_addOrg.executeBatch();
conn.commit();
handleThreadStateChangeRequests();
_addOrg.clearBatch();
// Insert Organisms into real table
int result = executeUpdate(_insertIntoOrgCommand, conn);
//get rid of previously entered vals in temp table
executeUpdate(_deleteFromTmpOrgCommand, conn);
//insert identifiers associated with the organism
executeUpdate(_insertOrgIDCommand, conn);
// update missing ident_ids in newly inserted organism records
executeUpdate(_updateOrgCommand, conn);
executeUpdate("TRUNCATE TABLE " + _oTableName, conn);
return result;
}
public int insertSequences(ParseContext context, Connection conn) throws SQLException
{
int transactionCount = 0;
_addSeq.setTimestamp(13, new Timestamp(new Date().getTime()));
//Process current mouthful of sequences
_log.debug(new java.util.Date() + " Processing sequences");
for (UniprotSequence curSeq : context.getSequences())
{
transactionCount++;
_addSeq.setString(1, curSeq.getProtSequence());
_addSeq.setString(2, curSeq.getHash());
if (curSeq.getDescription() == null)
{
_addSeq.setNull(3, Types.VARCHAR);
}
else
{
String tmp = curSeq.getDescription();
if (tmp.length() >= 200) tmp = StringUtilsLabKey.leftSurrogatePairFriendly(tmp, 190) + "...";
_addSeq.setString(3, tmp);
}
if (curSeq.getSourceChangeDate() == null)
{
_addSeq.setNull(4, Types.TIMESTAMP);
}
else
{
_addSeq.setTimestamp(4, curSeq.getSourceChangeDate());
}
if (curSeq.getSourceInsertDate() == null)
{
_addSeq.setNull(5, Types.TIMESTAMP);
}
else
{
_addSeq.setTimestamp(5, curSeq.getSourceInsertDate());
}
_addSeq.setString(6, curSeq.getGenus());
_addSeq.setString(7, curSeq.getSpecies());
if (curSeq.getMass() == null)
{
_addSeq.setNull(8, Types.FLOAT);
}
else
{
_addSeq.setFloat(8, curSeq.getMass());
}
if (curSeq.getLength() == null)
{
_addSeq.setNull(9, Types.INTEGER);
}
else
{
_addSeq.setInt(9, curSeq.getLength());
}
if (curSeq.getSource() == null)
{
_addSeq.setNull(10, Types.VARCHAR);
}
else
{
_addSeq.setString(10, curSeq.getSource());
}
if (curSeq.getBestName() == null)
{
_addSeq.setNull(11, Types.VARCHAR);
}
else
{
String tmp = curSeq.getBestName();
if (tmp.length() >= 50) tmp = StringUtilsLabKey.leftSurrogatePairFriendly(tmp, 45) + "...";
_addSeq.setString(11, tmp);
}
if (curSeq.getBestGeneName() == null)
{
_addSeq.setNull(12, Types.VARCHAR);
}
else
{
String tmp = curSeq.getBestGeneName();
if (tmp.length() >= 50) tmp = StringUtilsLabKey.leftSurrogatePairFriendly(tmp, 45) + "...";
_addSeq.setString(12, tmp);
}
// Timestamp at index 13 is set once for the whole prepared statement
_addSeq.addBatch();
if (transactionCount == TRANSACTION_ROW_COUNT)
{
transactionCount = 0;
_addSeq.executeBatch();
conn.commit();
_addSeq.clearBatch();
}
handleThreadStateChangeRequests();
}
_addSeq.executeBatch();
handleThreadStateChangeRequests();
conn.commit();
_addSeq.clearBatch();
executeUpdate(_insertInfoSourceFromSeqCommand, conn);
executeUpdate(_updateSeqTableCommand, conn);
int result = executeUpdate(_insertIntoSeqCommand, conn);
executeUpdate("TRUNCATE TABLE " + _sTableName, conn);
return result;
}
public int insertIdentifiers(ParseContext context, Connection conn) throws SQLException
{
int transactionCount = 0;
// Process current mouthful of identifiers
_log.debug(new java.util.Date() + " Processing identifiers");
_addIdent.setTimestamp(6, new java.sql.Timestamp(new java.util.Date().getTime()));
for (UniprotIdentifier curIdent : context.getIdentifiers())
{
transactionCount++;
String curIdentVal = curIdent.getIdentifier();
if (curIdentVal.length() > 50) curIdentVal = StringUtilsLabKey.leftSurrogatePairFriendly(curIdentVal, 45) + "...";
_addIdent.setString(1, curIdentVal);
_addIdent.setString(2, curIdent.getIdentType());
UniprotSequence curSeq = curIdent.getSequence();
_addIdent.setString(3, curSeq.getGenus());
_addIdent.setString(4, curSeq.getSpecies());
_addIdent.setString(5, curSeq.getHash());
// Timestamp at index 6 is set once for the whole PreparedStatement
_addIdent.addBatch();
if (transactionCount == TRANSACTION_ROW_COUNT)
{
transactionCount = 0;
_addIdent.executeBatch();
conn.commit();
_addIdent.clearBatch();
}
handleThreadStateChangeRequests();
}
_addIdent.executeBatch();
handleThreadStateChangeRequests();
conn.commit();
_addIdent.clearBatch();
_log.debug("Starting to create indices on " + _iTableName);
executeUpdate("create index iIdentifier on " + _iTableName + "(Identifier)", conn);
executeUpdate("create index iIdenttype on " + _iTableName + "(IdentType)", conn);
executeUpdate("create index iSpeciesGenusHash on " + _iTableName + "(Species, Genus, Hash)", conn);
executeUpdate(_dialect.getAnalyzeCommandForTable(_iTableName), conn, "Analyzing " + _iTableName);
// Insert ident types
executeUpdate(_insertIdentTypesCommand, conn, "InsertIdentTypes");
executeUpdate(_updateIdentsWithSeqsCommand, conn, "UpdateIdentsWithSeqs");
return context.getIdentifiers().size();
}
private int mergeIdentifiers(ParseContext context, Connection conn) throws SQLException
{
if (context.isClearExisting())
{
// Clear these after the annotations that reference them have already been deleted
int identifiersDeleted = executeUpdate(_clearExistingIdentifiersCommand, conn, "DeleteExistingIdents");
_log.debug("Deleted " + identifiersDeleted + " existing identifiers");
}
int result = executeUpdate(_insertIntoIdentsCommand, conn, "InsertIntoIdents");
_log.debug("Done with identifiers");
return result;
}
private int executeUpdate(String sql, Connection conn, String description) throws SQLException
{
long startTime = System.currentTimeMillis();
int result = executeUpdate(sql, conn);
long totalTime = System.currentTimeMillis() - startTime;
_log.debug(description + " took " + totalTime + " milliseconds");
return result;
}
private int executeUpdate(String sql, Connection conn) throws SQLException
{
handleThreadStateChangeRequests();
try (PreparedStatement stmt = conn.prepareStatement(sql))
{
int result = stmt.executeUpdate();
if (!conn.getAutoCommit())
{
conn.commit();
}
return result;
}
}
private static final int MAX_ANNOT_SIZE = 190;
public int insertAnnotations(ParseContext context, Connection conn) throws SQLException
{
int transactionCount = 0;
// Process current mouthful of identifiers
_addAnnot.setTimestamp(10, new java.sql.Timestamp(new java.util.Date().getTime()));
transactionCount++;
_log.debug(new java.util.Date() + " Processing annotations");
for (UniprotAnnotation curAnnot : context.getAnnotations())
{
String annotVal = curAnnot.getAnnotVal();
if (annotVal.length() > MAX_ANNOT_SIZE)
annotVal = annotVal.substring(0, MAX_ANNOT_SIZE) + "...";
_addAnnot.setString(1, annotVal);
_addAnnot.setString(2, curAnnot.getAnnotType());
UniprotSequence curSeq = curAnnot.getSequence();
_addAnnot.setString(3, curSeq.getGenus());
_addAnnot.setString(4, curSeq.getSpecies());
_addAnnot.setString(5, curSeq.getHash());
if (curAnnot.getStartPos() == null)
{
_addAnnot.setInt(6, 0);
}
else
{
_addAnnot.setInt(6, curAnnot.getStartPos().intValue());
}
if (curAnnot.getEndPos() == null)
{
_addAnnot.setInt(7, 0);
}
else
{
_addAnnot.setInt(7, curAnnot.getEndPos().intValue());
}
UniprotIdentifier ident = curAnnot.getIdentifier();
if (ident == null)
{
_addAnnot.setNull(8, Types.VARCHAR);
_addAnnot.setNull(9, Types.VARCHAR);
}
else
{
_addAnnot.setString(8, ident.getIdentifier());
_addAnnot.setString(9, ident.getIdentType());
}
// Timestamp at index 10 is set once for the whole PreparedStatement
_addAnnot.addBatch();
if (transactionCount == TRANSACTION_ROW_COUNT)
{
transactionCount = 0;
_addAnnot.executeBatch();
conn.commit();
_addAnnot.clearBatch();
}
handleThreadStateChangeRequests();
}
_addAnnot.executeBatch();
conn.commit();
handleThreadStateChangeRequests();
_addAnnot.clearBatch();
_log.debug("Starting to create indices on " + _aTableName);
executeUpdate("create index aAnnot_val on " + _aTableName + "(Annot_Val)", conn);
executeUpdate("create index aAnnotType on " + _aTableName + "(AnnotType)", conn);
executeUpdate("create index aHashGenusSpecies on " + _aTableName + "(Hash, Genus, Species)", conn);
executeUpdate(_dialect.getAnalyzeCommandForTable(_aTableName), conn, "Analyzing " + _aTableName);
// Insert ident types
executeUpdate(_insertAnnotTypesCommand, conn, "InsertAnnotTypes");
executeUpdate(_updateAnnotsWithSeqsCommand, conn, "UpdateAnnotsWithSeqs");
if (context.isClearExisting())
{
int annotationsDeleted = executeUpdate(_clearExistingAnnotationsCommand, conn, "DeleteExistingAnnots");
_log.debug("Deleted " + annotationsDeleted + " existing annotations.");
}
return context.getAnnotations().size();
}
private int mergeAnnotations(ParseContext context, Connection conn) throws SQLException
{
executeUpdate(_updateAnnotsWithIdentsCommand, conn, "UpdateAnnotsWithIdents");
int result = executeUpdate(_insertIntoAnnotsCommand, conn, "InsertIntoAnnots");
_log.debug("Done with annotations");
return result;
}
}