forked from egonw/create-bridgedb-metabolites
-
Notifications
You must be signed in to change notification settings - Fork 4
/
createDerby.groovy
877 lines (784 loc) · 32.4 KB
/
createDerby.groovy
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
@Grab(group='org.bridgedb', module='org.bridgedb.bio', version='3.1.2-java8')
@Grab(group='org.bridgedb', module='org.bridgedb.rdb.construct', version='3.1.2-java8')
@Grab(group='org.apache.derby', module='derby', version='10.4.2.0')
@Grab(group='org.apache.derby', module='derbytools', version='10.4.2.0')
@Grab(group='org.apache.derby', module='derbyclient', version='10.4.2.0')
import java.text.SimpleDateFormat;
import java.util.Date;
import groovy.xml.XmlSlurper
import groovy.xml.slurpersupport.NodeChildren;
import org.bridgedb.IDMapperException;
import org.bridgedb.DataSource;
import org.bridgedb.Xref;
import org.bridgedb.bio.DataSourceTxt;
import org.bridgedb.rdb.construct.DBConnector;
import org.bridgedb.rdb.construct.DataDerby;
import org.bridgedb.rdb.construct.GdbConstruct;
import org.bridgedb.rdb.construct.GdbConstructImpl3;
import org.bridgedb.rdb.construct.GdbConstructImpl4;
commitInterval = 500
genesDone = new java.util.HashSet();
linksDone = new java.util.HashSet();
DataSourceTxt.init()
// Class.forName("org.apache.derby.jdbc.ClientDriver"); // Java ?
Class.forName("org.apache.derby.jdbc.EmbeddedDriver") // Java 8
unitReport = new File("creation.xml")
// unitReport << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
unitReport << "<testsuite tests=\"12\">\n"
//GdbConstruct database = GdbConstructImpl4.createInstance( // Other
GdbConstruct database = GdbConstructImpl3.createInstance( // PathVisio 3.3
"hmdb_chebi_wikidata_metabolites", new DataDerby(), DBConnector.PROP_RECREATE
);
database.createGdbTables();
database.preInsert();
blacklist = new HashSet<String>();
blacklist.add("C00350")
blacklist.add("C00157")
blacklist.add("C00422")
blacklist.add("C00165")
blacklist.add("C02530")
blacklist.add("C00416")
blacklist.add("C02737")
blacklist.add("363-24-6")
blacklist.add("104404-17-3")
blacklist.add("CHEBI:17636")
blacklist.add("HMDB0000912") // see bug #6
blacklist.add("HMDB00912") // see bug #6
blacklist.add("HMDB0006316") //wrong mappings to ChEBI, Pubchem Compound = 0
blacklist.add("HMDB06316") //wrong mappings to ChEBI, Pubchem Compound = 0
blacklist.add("HMDB0010337") //wrong mappings to ChEBI, introduces duplicate InChI-Keys
blacklist.add("HMDB10337") //wrong mappings to ChEBI, introduces duplicate InChI-Keys
blacklist.add("HMDB0061717") //diff. stereochemistry, introduces duplicate InChI-Keys
blacklist.add("HMDB61717") //diff. stereochemistry, introduces duplicate InChI-Keys
// Some compounds have more than one Wikidata items, e.g. because there are two Wikipedia pages
// Therefore, we blacklist one of them, to ensure we do not get two Wikidata QIDs
genesDone.add("Q26981430") // the other QID is Q190875, which should show up anyway
//inchiDS = DataSource.getExistingBySystemCode("Cin")
inchikeyDS = DataSource.getExistingBySystemCode("Ik")
chemspiderDS = DataSource.getExistingBySystemCode("Cs")
casDS = DataSource.getExistingBySystemCode("Ca")
pubchemDS = DataSource.getExistingBySystemCode("Cpc")
chebiDS = DataSource.getExistingBySystemCode("Ce")
keggDS = DataSource.getExistingBySystemCode("Ck")
keggDrugDS = DataSource.getExistingBySystemCode("Kd")
wikidataDS = DataSource.getExistingBySystemCode("Wd")
lmDS = DataSource.getExistingBySystemCode("Lm")
knapsackDS = DataSource.getExistingBySystemCode("Cks")
dtxDS = DataSource.getExistingBySystemCode("Ect")
drugbankDS = DataSource.getExistingBySystemCode("Dr")
iupharDS = DataSource.getExistingBySystemCode("Gpl")
chemblDS = DataSource.getExistingBySystemCode("Cl")
swisslipidsDS = DataSource.getExistingBySystemCode("Sl")
hmdbDS = DataSource.getExistingBySystemCode("Ch")
//vmhmetaboliteDS = DataSource.register ("VmhM", "VMH metabolite").asDataSource() //Add this to BridgeDb, update libraries, add to website!
chebiVersionFile = new File('data/chebi.version')
chebiVersion = "232"
if (chebiVersionFile.exists() && chebiVersionFile.canRead()) {
chebiVersion = chebiVersionFile.text.trim()
}
String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
database.setInfo("BUILDDATE", dateStr);
database.setInfo("DATASOURCENAME", "HMDB-CHEBI-WIKIDATA");
database.setInfo("DATASOURCEVERSION", "HMDB5.0.20240416-CHEBI" + chebiVersion + "-WIKIDATA" + dateStr);
database.setInfo("DATATYPE", "Metabolite");
database.setInfo("SERIES", "standard_metabolite");
def addXRef(GdbConstruct database, Xref ref, String node, DataSource source, Set genesDone, Set linkesDone, boolean isPrimary) {
id = node.trim()
if (id.length() > 0) {
// println "id($source): $id"
ref2 = new Xref(id, source, isPrimary);
if (!genesDone.contains(ref2.toString())) {
if (database.addGene(ref2) != 0) {
println "Error (addXRef.addGene): " + database.recentException().getMessage()
println " id($source): $id"
}
genesDone.add(ref2.toString())
}
if (!linksDone.contains(ref.toString()+ref2.toString())) {
if (database.addLink(ref, ref2) != 0) {
println "Error (addXRef.addLink): " + database.recentException().getMessage()
println " id(origin): " + ref.toString()
println " id($source): $id"
}
linksDone.add(ref.toString()+ref2.toString())
}
}
}
def addXRef(GdbConstruct database, Xref ref, String node, DataSource source, Set genesDone, Set linkesDone) {
addXRef(database, ref, node, source, genesDone, linkesDone, (boolean)true)
}
def addAttribute(GdbConstruct database, Xref ref, String key, String value) {
id = value.trim()
// println "attrib($key): $id"
if (id.length() > 255) {
println "Warn: attribute does not fit the Derby SQL schema: $id"
} else if (id.length() > 0) {
if (database.addAttribute(ref, key, value) != 0) {
println "Error (addAttrib): " + database.recentException().getMessage()
}
}
}
def cleanKey(String inchikey) {
String cleanKey = inchikey.trim()
if (cleanKey.startsWith("InChIKey=")) cleanKey = cleanKey.substring(9)
cleanKey = cleanKey.replace("\"", "")
cleanKey
}
// read the secondary ChEBI identifiers
deprChEBIFile = new File("deprecated_ChEBI.csv")
deprChEBIIDs = new java.util.HashSet();
if (deprChEBIFile.exists()) {
deprChEBIFile.eachLine { line,number ->
fields = line.split(",")
secid = fields[0]
deprChEBIIDs.add(secid)
}
}
// load the HMDB content
counter = 0
hmdbFile = new File('hmdb_metabolites_split.zip')
if (hmdbFile.exists()) {
def zipFile = new java.util.zip.ZipFile(hmdbFile)
zipFile.entries().each { entry ->
if (!entry.isDirectory() && entry.name != "hmdb_metabolites.xml") {
inputStream = zipFile.getInputStream(entry)
def rootNode = null
try {
rootNode = new XmlSlurper().parse(inputStream)
} catch (Exception exception) {
println "Error while reading XML from file $entry: " + exception.message
return
}
error = 0
String rootid = rootNode.accession.toString()
String newid = null
// println "HMDB original: ${rootid}"
if (rootid.length() == 11) {
if (rootid.startsWith("HMDB00")) {
newid = rootid
rootid = "HMDB" + rootid.substring(6) // use the pre-16 August 2017 identifier pattern
} // otherwise: assume there only is a new ID
} else if (rootid.length() > 4 && rootid.length() < 11) {
newid = "HMDB00" + rootid.substring(4)
} else {
error = 1
println "Error (incorrect HMDB): " + rootid
}
// println "HMDB old: ${rootid} -> ${newid}"
Xref ref = new Xref(rootid, hmdbDS, false);
Xref newref = (newid == null) ? null : new Xref(newid, hmdbDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
if (newref != null) {
addError = database.addGene(newref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(newref,newref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
linkError = database.addLink(ref,newref);
if (linkError != 0) println "Error (addLinkNewOld): " + database.recentException().getMessage()
error += linkError
}
genesDone.add(ref.toString())
}
// add the synonyms
addAttribute(database, ref, "Symbol", rootNode.name.toString());
rootNode.synonyms.synonym.each { synonym ->
addAttribute(database, ref, "Synonym", synonym.toString())
}
addAttribute(database, ref, "Synonym", rootNode.traditional_iupac.toString());
addAttribute(database, ref, "Synonym", rootNode.iupac_name.toString());
// add the SMILES, InChIKey, etc
addAttribute(database, ref, "InChI", cleanKey(rootNode.inchi.toString()));
key = cleanKey(rootNode.inchikey.toString().trim());
if (key.length() == 27) {
addAttribute(database, ref, "InChIKey", key);
addXRef(database, ref, key, inchikeyDS, genesDone, linksDone);
}
addAttribute(database, ref, "SMILES", rootNode.smiles.toString());
addAttribute(database, ref, "BrutoFormula", rootNode.chemical_formula.toString());
addAttribute(database, ref, "Taxonomy Parent", rootNode.direct_parent.toString());
addAttribute(database, ref, "Monoisotopic Weight", rootNode.monisotopic_molecular_weight.toString());
// add external identifiers
// addXRef(database, ref, rootNode.accession.toString(), hmdbDS);
if (!blacklist.contains(rootid)) {
addXRef(database, ref, rootNode.cas_registry_number.toString(), casDS, genesDone, linksDone);
addXRef(database, ref, rootNode.pubchem_compound_id.toString(), pubchemDS, genesDone, linksDone);
addXRef(database, ref, rootNode.chemspider_id.toString(), chemspiderDS, genesDone, linksDone);
String chebID = rootNode.chebi_id.toString().trim()
if (chebID.startsWith("CHEBI:")) {
boolean isPrimary = !deprChEBIIDs.contains(chebID.substring(6))
addXRef(database, ref, chebID, chebiDS, genesDone, linksDone, isPrimary);
addXRef(database, ref, chebID.substring(6), chebiDS, genesDone, linksDone, isPrimary);
} else if (chebID.length() > 0) {
boolean isPrimary = !deprChEBIIDs.contains(chebID)
addXRef(database, ref, chebID, chebiDS, genesDone, linksDone, isPrimary);
addXRef(database, ref, "CHEBI:" + chebID, chebiDS, genesDone, linksDone, isPrimary);
}
String keggID = rootNode.kegg_id.toString();
if (keggID.length() > 0 && keggID.charAt(0) == 'C') {
if (!blacklist.contains(keggID)) {
addXRef(database, ref, keggID, keggDS, genesDone, linksDone);
} else {
println "Warn: No external IDs added for: " + keggID
}
} else if (keggID.length() > 0 && keggID.charAt(0) == 'D') {
addXRef(database, ref, keggID, keggDrugDS, genesDone, linksDone);
}
// addXRef(database, ref, rootNode.nugowiki.toString(), nugoDS);
// addXRef(database, ref, rootNode.drugbank_id.toString(), drugbankDS);
// addXRef(database, ref, rootNode.inchi.toString(), inchiDS);
} else {
println "Warn: No external IDs added for: " + rootid
}
if (error > 0) println "errors: " + error + " (HMDB: ${entry.name})"
counter++
if (counter % commitInterval == 0) database.commit()
}
}
unitReport << " <testcase classname=\"HMDBCreation\" name=\"HMDBInputFound\"/>\n"
} else { // else HMDB does not exist and do nothing
unitReport << " <testcase classname=\"HMDBCreation\" name=\"HMDBInputFound\">\n"
unitReport << " <failure type=\"FileNotFound\">HMDB input file not found</failure>\n"
unitReport << " </testcase>\n"
}
// load the ChEBI content
counter = 0
// load the names
def chebiNames = new File('data/chebi_names.tsv')
chebiNames.eachLine { line,number ->
if (number == 1) return // skip the first line
error = 0
columns = line.split('\t')
if (columns.length < 5) {
println "Error (chebi_names.tsv): unexpected line: " + line
return;
}
shortid = columns[1]
boolean isPrimary = !deprChEBIIDs.contains(shortid)
rootid = "CHEBI:" + shortid
name = columns[4]
// println rootid + " -> " + name
Xref shortRef = new Xref(shortid, chebiDS, false); // not primary by default
if (!genesDone.contains(shortRef.toString())) {
addError = database.addGene(shortRef);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(shortRef,shortRef);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(shortRef.toString())
}
Xref ref = new Xref(rootid, chebiDS, isPrimary);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
addAttribute(database, ref, "Symbol", name);
if (error > 0) println "errors: " + error + " (ChEBI: $rootid)"
counter++
if (counter % commitInterval == 0) database.commit()
}
unitReport << " <testcase classname=\"ChEBICreation\" name=\"NamesFound\"/>\n"
// load the mappings
def mappedIDs = new File('data/chebi_database_accession.tsv')
mappedIDs.eachLine { line,number ->
if (number == 1) return // skip the first line
columns = line.split('\t')
rootid = "CHEBI:" + columns[1]
type = columns[3]
error = 0
if (!blacklist.contains(rootid)) {
id = columns[4]
println "$rootid -($type)-> $id"
boolean isPrimary = !deprChEBIIDs.contains(rootid)
Xref ref = new Xref(rootid, chebiDS, isPrimary);
if (type == "CAS Registry Number") {
if (!id.contains(" ") && !id.contains(":") && id.contains("-")) {
addXRef(database, ref, id, casDS, genesDone, linksDone);
}
} else if (type == "KEGG COMPOUND accession") {
addXRef(database, ref, id, keggDS, genesDone, linksDone);
} else if (type == "Chemspider accession") {
addXRef(database, ref, id, chemspiderDS, genesDone, linksDone);
} else if (type == "Pubchem accession") {
addXRef(database, ref, id, pubchemDS, genesDone, linksDone);
} else if (type == "LIPID MAPS class accession") {
addXRef(database, ref, id, lmDS, genesDone, linksDone);
} else if (type == "LIPID MAPS instance accession") {
addXRef(database, ref, id, lmDS, genesDone, linksDone);
} else if (type == "KNApSAcK accession") {
addXRef(database, ref, id, knapsackDS, genesDone, linksDone);
}
} else {
println "No external IDs added for: " + rootid
}
counter++
if (error > 0) println "errors: " + error + " (ChEBI: $rootid)"
if (counter % commitInterval == 0) {
database.commit()
}
}
unitReport << " <testcase classname=\"ChEBICreation\" name=\"AccessionsFound\"/>\n"
//// load the Wikidata content
// CAS registry numbers
counter = 0
error = 0
new File("cas2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], casDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (CAS)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"CASNumbersFound\"/>\n"
// PubChem registry numbers
counter = 0
error = 0
new File("pubchem2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], pubchemDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (PubChem)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"PubChemFound\"/>\n"
// KEGG registry numbers
counter = 0
error = 0
new File("kegg2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
keggID = fields[1]
if (keggID.charAt(0) == 'C') {
addXRef(database, ref, keggID, keggDS, genesDone, linksDone);
} else if (keggID.charAt(0) == 'D') {
addXRef(database, ref, keggID, keggDrugDS, genesDone, linksDone);
} else {
println "unclear KEGG ID ($rootid): " + keggID
}
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (KEGG)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"KEGGFound\"/>\n"
// ChemSpider registry numbers
counter = 0
error = 0
new File("cs2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], chemspiderDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (ChemSpider)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"ChemSpiderFound\"/>\n"
// IUPHAR registry numbers
counter = 0
error = 0
new File("gpl2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], iupharDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (IUPHAR)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"IUPHARFound\"/>\n"
//// VMH Metabolite identifiers
//counter = 0
//error = 0
//new File("Recon_test.csv").eachLine { line,number ->
// if (number == 1) return // skip the first line
// fields = line.replace("\"","").split(",")
// rootid = fields[0].trim() //no substring split needed, since we're mapping on inchiKey (for now)
// recon = fields[1].trim()
// Xref ref = new Xref(rootid, inchikeyDS); // add first column to InchiKey mappings.
// if (!genesDone.contains(ref.toString())) {
// addError = database.addGene(ref);
// if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
// error += addError
// linkError = database.addLink(ref,ref);
// if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
// error += linkError
// genesDone.add(ref.toString())
// }
// // add external identifiers
// addXRef(database, ref, recon, vmhmetaboliteDS, genesDone, linksDone);
// counter++
// if (counter % commitInterval == 0) {
// println "Info: errors: " + error + " (VMH Metabolite)"
// database.commit()
// }
//}
//unitReport << " <testcase classname=\"VMHMetaboliteCreation\" name=\"ReconFound\"/>\n" //Need to check if adapted line functions properly.
// ChEMBL Compound registry numbers
counter = 0
error = 0
new File("chembl2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], chemblDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (ChEMBL)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"CHEMBLFound\"/>\n"
// Drugbank Compound registry numbers
counter = 0
error = 0
new File("drugbank2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
originalDrugBankID = fields[1]
longDrugbankID = "DB" + fields[1]
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers, check if they start with or without "DB" to avoid wrong links.
if (originalDrugBankID.startsWith("DB")) {
addXRef(database, ref, fields[1], drugbankDS, genesDone, linksDone);
}
else{
addXRef(database, ref, longDrugbankID, drugbankDS, genesDone, linksDone);
}
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (Drugbank)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"DrugbankFound\"/>\n"
// LIPID MAPS registry numbers
counter = 0
error = 0
new File("lm2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], lmDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (LIPIDMAPS)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"LipidMapsFound\"/>\n"
// HMDB registry numbers
counter = 0
error = 0
new File("hmdb2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
hmdbid = fields[1]
if (hmdbid.length() == 11 && hmdbid.startsWith("HMDB00")) {
hmdbid = "HMDB" + hmdbid.substring(6) // use the pre-16 August 2017 identifier pattern
}
addXRef(database, ref, hmdbid, hmdbDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (HMDB)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"HMDBFound\"/>\n"
// EPA CompTox Dashboard numbers
counter = 0
error = 0
new File("comptox2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], dtxDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (EPA CompTox Dashboard)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"EPACompToxFound\"/>\n"
// ChEBI registry numbers
counter = 0
error = 0
new File("chebi2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
shortid = fields[1]
chebiid = "CHEBI:" + shortid
Xref chebiRef = new Xref(rootid, chebiDS);
addXRef(database, ref, shortid, chebiDS, genesDone, linksDone);
addXRef(database, ref, chebiid, chebiDS, genesDone, linksDone);
addXRef(database, chebiRef, rootid, wikidataDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (ChEBI)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"ChEBIFound\"/>\n"
// KNApSAcK registry numbers
counter = 0
error = 0
new File("knapsack2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], knapsackDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (ChEBI)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"KNApSAcKFound\"/>\n"
// SwissLipids identifiers
counter = 0
error = 0
new File("swisslipids2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], swisslipidsDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (SwissLipds)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"SwissLipidsFound\"/>\n"
// InChIKeys
counter = 0
error = 0
new File("inchikey2wikidata.csv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.replace("\"","").split(",")
rootid = fields[0].substring(31)
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
// add external identifiers
addXRef(database, ref, fields[1], inchikeyDS, genesDone, linksDone);
counter++
if (counter % commitInterval == 0) {
println "Info: errors: " + error + " (InChIKey)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"InChIKeysFound\"/>\n"
// Wikidata names
counter = 0
error = 0
new File("names2wikidata.tsv").eachLine { line,number ->
if (number == 1) return // skip the first line
fields = line.split("\t")
if (fields.length >= 2) {
rootid = fields[0].replace("<","").replace(">","").substring(31)
synonym = fields[1].trim().replace("\"","").replace("@en","")
Xref ref = new Xref(rootid, wikidataDS);
if (!genesDone.contains(ref.toString())) {
addError = database.addGene(ref);
if (addError != 0) println "Error (addGene): " + database.recentException().getMessage()
error += addError
linkError = database.addLink(ref,ref);
if (linkError != 0) println "Error (addLinkItself): " + database.recentException().getMessage()
error += linkError
genesDone.add(ref.toString())
}
if (synonym.length() > 0 && !synonym.equals(rootid)) {
println "Adding synonym: " + synonym
addAttribute(database, ref, "Symbol", synonym)
} else {
println "Not adding synonym: " + synonym
}
}
counter++
if (counter % commitInterval == 0) {
println "errors: " + error + " (label)"
database.commit()
}
}
unitReport << " <testcase classname=\"WikidataCreation\" name=\"NamesFound\"/>\n"
unitReport << "</testsuite>\n"
database.commit();
database.finalize();