-
Notifications
You must be signed in to change notification settings - Fork 85
/
ChadoHandlerService.groovy
1459 lines (1316 loc) · 59.5 KB
/
ChadoHandlerService.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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.bbop.apollo
import grails.transaction.Transactional
import org.codehaus.groovy.grails.web.json.JSONObject
import java.security.MessageDigest
import java.sql.Timestamp
/**
*
* Chado Compliance Layers
* Level 0: Relational schema - this basically means that the schema is adhered to
* Level 1: Ontologies - this means that all features in the feature table are of a type represented in SO and
* all feature relationships in feature_relationship table must be SO relationship types
* Level 2: Graph - all features relationships between a feature of type X and Y must correspond to relationship of
* that type in SO.
*
* Relevant Chado modules:
* Chado General Module
* Chado CV Module
* Chado Organism Module
* Chado Sequence Module
* Chado Publication Module
*
*/
@Transactional
class ChadoHandlerService {
def configWrapperService
def sequenceService
def featureRelationshipService
def transcriptService
def cdsService
private static final String SEQUENCE_ONTOLOGY = "sequence"
private static final String RELATIONSHIP_ONTOLOGY = "relationship"
private static final String FEATURE_PROPERTY = "feature_property"
private static final def topLevelFeatureTypes = [Gene.cvTerm, Pseudogene.cvTerm, Terminator.cvTerm, TransposableElement.cvTerm, RepeatRegion.cvTerm,
InsertionArtifact.cvTerm, DeletionArtifact.cvTerm, SubstitutionArtifact.cvTerm]
private static final ontologyDb = ["SO", "GO", "RO"]
Map<String, org.gmod.chado.Organism> chadoOrganismsMap = new HashMap<String, org.gmod.chado.Organism>()
Map<String, Integer> exportStatisticsMap = new HashMap<String, Integer>();
ArrayList<org.bbop.apollo.Feature> processedFeatures = new ArrayList<org.bbop.apollo.Feature>()
ArrayList<org.bbop.apollo.Feature> failedFeatures = new ArrayList<org.bbop.apollo.Feature>()
def writeFeatures(Organism organism, ArrayList<Sequence> sequenceList, ArrayList<Feature> features, boolean exportAllSequences = false) {
JSONObject returnObject = new JSONObject()
if (!configWrapperService.hasChadoDataSource()) {
log.error("Cannot export annotations to Chado as Chado data source has not been configured")
returnObject.error = "Cannot export annotations to Chado as Chado data source has not been configured."
}
else {
if (!checkForOntologies()) {
log.error "No ontologies loaded into the Chado database"
returnObject.error = "No ontologies loaded into the Chado database. Refer to "
}
else if (organism.genus == null || organism.species == null) {
log.error "Apollo Organism must have genus and species defined."
returnObject.error = "Apollo Organism must have genus and species defined."
}
else {
returnObject = writeFeaturesToChado(organism, sequenceList, features, exportAllSequences)
}
}
return returnObject
}
/**
* Writes all features in features array into Chado for the given organism
* @param organism
* @param features
* @return
*/
def writeFeaturesToChado(Organism organism, ArrayList<Sequence> sequenceList, ArrayList<Feature> features, boolean exportAllSequences = false) {
/*
The exporter assumes that the following ontologies are pre-loaded into the Chado data source:
1. Sequence Ontology
2. Gene Ontology
3. Relations Ontology
*/
initializeExportStatistics()
long totalTime = System.currentTimeMillis()
// Create the organism
long startTime = System.currentTimeMillis()
createChadoOrganism(organism)
long endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado Organism for ${organism.commonName}: ${endTime - startTime} ms"
// Create chado feature for sequence in sequenceList
if (sequenceList.size() > 0) {
createChadoFeatureForSequences(organism, sequenceList, configWrapperService.getChadoExportFastaForSequence())
}
else
if (exportAllSequences) {
sequenceList = Sequence.findAllByOrganism(organism)
createChadoFeatureForSequences(organism, sequenceList, configWrapperService.getChadoExportFastaForSequence())
}
def existingChadoAnnotations = org.gmod.chado.Feature.executeQuery(
"SELECT DISTINCT f.uniquename FROM org.gmod.chado.Feature f WHERE f.type.name IN :topLevelFeatureTypes",
[topLevelFeatureTypes: topLevelFeatureTypes])
// creating Chado feature for each Apollo feature
features.each { apolloFeature ->
startTime = System.currentTimeMillis()
createChadoFeaturesForAnnotation(organism, apolloFeature)
existingChadoAnnotations.remove(apolloFeature.uniqueName)
endTime = System.currentTimeMillis()
log.debug "Time taken to process annotation ${apolloFeature.name} of type ${apolloFeature.class.canonicalName}: ${endTime - startTime} ms"
}
// delete features that are in Chado but not in Apollo
existingChadoAnnotations.each { uniquename ->
org.gmod.chado.Feature chadoFeature = getChadoFeature(uniquename)
log.debug "Deleting ${uniquename}"
deleteChadoFeature(chadoFeature)
}
JSONObject exportStatistics = new JSONObject()
exportStatistics = [ "Organism count" : chadoOrganismsMap.size(),
"Sequence count" : exportStatisticsMap.get("sequence_feature_count"),
"Feature count" : exportStatisticsMap.get("feature_count"),
"Featureloc count" : exportStatisticsMap.get("featureloc_count"),
"Time Taken" : (System.currentTimeMillis() - totalTime) / 1000 + " seconds" ]
return exportStatistics
}
/**
* For each Apollo annotation, traverses through the model hierarchy and creates a Chado representation of the annotation.
* @param organism
* @param topLevelFeature
* @return
*/
def createChadoFeaturesForAnnotation(org.bbop.apollo.Organism organism, org.bbop.apollo.Feature topLevelFeature) {
/*
Top-level features are gene, pseudogene, transposable_element, repeat_region, insertion, deletion, substitution.
A top-level feature that is not an instance of type Gene is likely to be a singleton feature.
*/
org.gmod.chado.Feature topLevelChadoFeature = getChadoFeature(topLevelFeature.uniqueName)
if (topLevelChadoFeature) {
// if a top level annotation already exists we delete it
long startTime = System.currentTimeMillis()
deleteChadoFeature(topLevelChadoFeature)
long endTime = System.currentTimeMillis()
log.debug "Time taken to delete existing annotation ${endTime - startTime} ms"
}
createChadoFeature(organism, topLevelFeature)
if (topLevelFeature instanceof Gene) {
// annotation is a Gene / Pseudogene
def transcripts = transcriptService.getTranscripts(topLevelFeature)
transcripts.each { transcript ->
org.gmod.chado.Feature chadoFeature = createChadoFeature(organism, transcript)
transcript.childFeatureRelationships.each { featureRelationship ->
createChadoFeatureRelationship(organism, chadoFeature, featureRelationship)
}
def exons = transcriptService.getSortedExons(transcript,false)
/*
In GMOD Chado Best Practices, it is noted that exons can be part_of more than one mRNA and that
no two distinct exon rows should have exact same featureloc coordinates (this would indicate they are the same exon).
TODO: Do we factor this logic into export if two exons from two separate transcripts/isoforms have same fmin and fmax?
*/
exons.each { exon ->
org.gmod.chado.Feature chadoExonFeature = createChadoFeature(organism, exon)
exon.childFeatureRelationships.each { featureRelationship ->
createChadoFeatureRelationship(organism, chadoExonFeature, featureRelationship)
}
}
if (transcript instanceof MRNA) {
// TODO: Do we create a chado feature for stop_codon_read_through
def cds = transcriptService.getCDS(transcript)
org.gmod.chado.Feature chadoCdsFeature = createChadoCdsFeature(organism, transcript, cds, configWrapperService.getChadoExportFastaForCds())
cds.childFeatureRelationships.each { featureRelationship ->
createChadoFeatureRelationship(organism, chadoCdsFeature, featureRelationship, "part_of")
}
}
}
}
}
/**
* Queries the Chado database to find feature that has the given uniquename.
* @param uniqueName
* @return
*/
def getChadoFeature(String uniqueName) {
org.gmod.chado.Feature chadoFeature
def featureResult = org.gmod.chado.Feature.findAllByUniquename(uniqueName)
if (featureResult.size() == 0) {
chadoFeature = null
}
else if (featureResult.size() == 1) {
chadoFeature = featureResult.get(0)
}
else {
log.error "${featureResult} - More than one result found for feature uniquename '${uniqueName}'. Returning null."
chadoFeature = null
}
return chadoFeature
}
/**
* Deletes a given feature from the Chado database.
* @param chadoFeature
* @return
*/
def deleteChadoFeature(org.gmod.chado.Feature chadoFeature) {
def chadoFeatureLocs = getChadoFeatureloc(chadoFeature)
chadoFeatureLocs.each { fl ->
fl.delete()
}
def chadoFeatureDbxrefs = getChadoFeatureDbxrefs(chadoFeature)
chadoFeatureDbxrefs.each { fd ->
if (! ontologyDb.contains(fd.dbxref.db.name)) {
fd.dbxref.delete()
}
}
def chadoFeatureProperties = getChadoFeatureProps(chadoFeature)
chadoFeatureProperties.each { fp ->
fp.delete()
}
def chadoChildFeatureRelationships = getChildFeatureRelationships(chadoFeature)
chadoChildFeatureRelationships.each { child ->
deleteChadoFeature(child.subject)
}
// TODO: featureSynonyms, featurePubs, featureGenotypes, featurePhenotypes
chadoFeature.delete(flush: true)
}
/**
* Queries the Chado database and returns all featurelocs for a given Chado Feature
* @param chadoFeature
* @return
*/
def getChadoFeatureloc(org.gmod.chado.Feature chadoFeature) {
long startTime = System.currentTimeMillis()
def results = org.gmod.chado.Featureloc.executeQuery(
"SELECT DISTINCT fl FROM org.gmod.chado.Featureloc fl WHERE fl.feature.uniquename = :queryUniqueName",
[queryUniqueName: chadoFeature.uniquename]
)
long endTime = System.currentTimeMillis()
log.debug "Time taken to query featurelocs for ChadoFeature: ${chadoFeature.uniquename} - ${endTime - startTime} ms"
return results
}
/**
* Queries the Chado database and returns all FeatureDbxrefs for a given Chado Feature
* @param chadoFeature
* @return
*/
def getChadoFeatureDbxrefs(org.gmod.chado.Feature chadoFeature) {
long startTime = System.currentTimeMillis()
def results = org.gmod.chado.FeatureDbxref.executeQuery(
"SELECT DISTINCT fd FROM org.gmod.chado.FeatureDbxref fd JOIN fd.dbxref dbxref JOIN dbxref.db db WHERE fd.feature.uniquename = :queryUniqueName",
[queryUniqueName: chadoFeature.uniquename]
)
long endTime = System.currentTimeMillis()
log.debug "Time taken to query featureDbxrefs for ChadoFeature: ${chadoFeature.uniquename} - ${endTime - startTime} ms"
return results
}
/**
* Queries the Chado database and returns all FeatureProps for a given Chado Feature
* @param chadoFeature
* @return
*/
def getChadoFeatureProps(org.gmod.chado.Feature chadoFeature) {
long startTime = System.currentTimeMillis()
def results = org.gmod.chado.Featureprop.executeQuery(
"SELECT DISTINCT fp FROM org.gmod.chado.Featureprop fp JOIN fp.feature f WHERE f.uniquename = :queryUniqueName",
[queryUniqueName: chadoFeature.uniquename]
)
long endTime = System.currentTimeMillis()
log.debug "Time taken to query featureprops for ChadoFeature: ${chadoFeature.uniquename} - ${endTime - startTime} ms"
return results
}
/**
* Queries the Chado database and retuns all child feature relationships for a given Chado Feature
* @param chadoFeature
* @return
*/
def getChildFeatureRelationships(org.gmod.chado.Feature chadoFeature) {
long startTime = System.currentTimeMillis()
def results = org.gmod.chado.FeatureRelationship.executeQuery(
"SELECT DISTINCT fr FROM org.gmod.chado.FeatureRelationship fr JOIN fr.subject s JOIN fr.object o WHERE o.uniquename = :queryUniqueName",
[queryUniqueName: chadoFeature.uniquename]
)
long endTime = System.currentTimeMillis()
log.debug "Time taken to query feature_relationships for ChadoFeature: ${chadoFeature.uniquename} - ${endTime - startTime} ms"
return results
}
/**
* Create a Chado feature for a given Apollo feature.
* @param Organism
* @param feature
* @return
*/
def createChadoFeature(org.bbop.apollo.Organism organism, org.bbop.apollo.Feature feature) {
long startTime, endTime
String type = feature.cvTerm
// feature
startTime = System.currentTimeMillis()
org.gmod.chado.Feature chadoFeature = new org.gmod.chado.Feature(
uniquename: feature.uniqueName,
name: feature.name,
seqlen: feature.sequenceLength,
md5checksum: feature.md5checksum,
isAnalysis: feature.isAnalysis,
isObsolete: feature.isObsolete,
timeaccessioned: feature.dateCreated,
timelastmodified: feature.lastUpdated,
organism: chadoOrganismsMap.get(organism.commonName),
type: getChadoCvterm(type, SEQUENCE_ONTOLOGY)
).save()
exportStatisticsMap['feature_count'] += 1
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado feature of type ${feature.class.simpleName}: ${endTime - startTime} ms"
// featureloc
createChadoFeatureloc(organism, chadoFeature, feature)
/*
TODO: Cannot treat feature.symbol as a Chado synonym
because feature -> synonym link requires a Publication, according to Chado specification
*/
//if (feature.symbol) {
// org.gmod.chado.Synonym chadoSynonym = getChadoSynonym(feature.symbol, "symbol")
//
// // Creating a linking relationship between Chado feature and Chado synonym
// org.gmod.chado.FeatureSynonym chadoFeatureSynonym = new org.gmod.chado.FeatureSynonym(
// feature: chadoFeature,
// synonym: chadoSynonym,
// pub: null,
// isCurrent: true,
// isInternal: false
// ).save()
//}
// As an alternative, feature.symbol is treated as Chado featureprop
if (feature.symbol) {
org.gmod.chado.Featureprop chadoFeatureprop = new org.gmod.chado.Featureprop(
value: feature.symbol,
rank: 0,
feature: chadoFeature,
type: getChadoCvterm("symbol", FEATURE_PROPERTY)
).save()
}
// Feature description treated as Chado featureprop
if (feature.description) {
org.gmod.chado.Featureprop chadoFeatureProp = new org.gmod.chado.Featureprop(
value: feature.description,
rank: 0,
feature: chadoFeature,
type: getChadoCvterm("description", FEATURE_PROPERTY)
).save()
}
/* TODO: How to determine the primary dbxref?
Currently the feature.dbxref_id will remain empty as we do not know which of
the dbxref will serve as the primary identifier.
Chado specification suggests using feature.dbxref_id to link to primary
identifier and to use feature_dbxref table for all additional identifiers.
*/
// dbxref
if (feature.featureDBXrefs) {
createChadoDbxref(chadoFeature, feature.featureDBXrefs)
}
// properties
if (feature.featureProperties) {
createChadoProperty(chadoFeature, feature.featureProperties)
}
// Feature owner treated as featureprop
if (feature.owners) {
int rank = 0
feature.owners.each { owner ->
org.gmod.chado.Featureprop chadoFeatureProp = new org.gmod.chado.Featureprop(
value: owner.username,
rank: rank,
feature: chadoFeature,
type: getChadoCvterm("owner", FEATURE_PROPERTY)
).save()
rank++
}
}
// publications
//if (feature.featurePublications) {
// createChadoFeaturePub(chadoFeature, feature.featurePublications)
//}
// synonyms
//if (feature.featureSynonyms) {
// createChadoSynonym(chadoFeature, feature.featureSynonyms)
//}
// genotypes - TODO: When Apollo has Genotype associated with annotations
//if (feature.featureGenotypes) {
// createChadoGenotype(chadoFeature, feature.featureGenotypes)
//}
// phenotypes - TODO: When Apollo has Phenotype associated with annotations
//if (feature.featurePhenotypes) {
// createChadoPhenotype(chadoFeature, feature.featurePhenotypes)
//}
//feature.featureLocations - TODO: If Apollo has features with multiple Feature Locations
//feature.featureCVTerms - TODO: If Apollo has features with multiple CvTerms
chadoFeature.save(flush: true)
return chadoFeature
}
/**
* Create an instance of Chado featureloc, for a given Chado feature, with location information
* from an Apollo feature location.
* @param chadoFeature
* @param feature
* @return chadoFeatureLoc
*/
def createChadoFeatureloc(org.bbop.apollo.Organism organism, org.gmod.chado.Feature chadoFeature, org.bbop.apollo.Feature feature) {
/*
In Chado, locgroup and rank are used to uniquely identify featureloc for features that
have more than one featureloc.
In Apollo, we currently do not use locgroup and rank for any purposes and their values
are set to 0, the default, as suggested by standard Chado specification.
*/
long startTime, endTime
startTime = System.currentTimeMillis()
org.gmod.chado.Featureloc chadoFeatureLoc = new org.gmod.chado.Featureloc(
fmin: feature.featureLocation.fmin,
fmax: feature.featureLocation.fmax,
isFminPartial: feature.featureLocation.isFminPartial,
isFmaxPartial: feature.featureLocation.isFmaxPartial,
strand: feature.featureLocation.strand,
locgroup: feature.featureLocation.locgroup,
rank: feature.featureLocation.rank,
feature: chadoFeature,
srcfeature: getSrcFeatureForFeature(organism, feature.featureLocation.sequence)
).save(flush: true)
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado featureloc for feature fmin: ${feature.fmin} fmax: ${feature.fmax}: ${endTime - startTime} ms"
exportStatisticsMap['featureloc_count'] += 1
feature.featureLocation.featureLocationPublications.each { featureLocationPublication ->
createChadoFeaturelocPub(chadoFeatureLoc, featureLocationPublication)
}
return chadoFeatureLoc
}
/**
* Create an instance of Chado feature of type 'CDS' for a given Apollo CDS.
* @param organism
* @param transcript
* @param cds
* @param storeSequence
* @return chadoCdsFeature
*/
def createChadoCdsFeature(org.bbop.apollo.Organism organism, org.bbop.apollo.Transcript transcript, org.bbop.apollo.CDS cds, boolean storeSequence = false) {
long startTime, endTime
startTime = System.currentTimeMillis()
org.gmod.chado.Feature chadoCdsFeature = new org.gmod.chado.Feature(
uniquename: cds.uniqueName,
name: cds.name,
isAnalysis: cds.isAnalysis,
isObsolete: cds.isObsolete,
timeaccessioned: cds.dateCreated,
timelastmodified: cds.lastUpdated,
organism: chadoOrganismsMap.get(organism.commonName),
type: getChadoCvterm(cds.cvTerm, SEQUENCE_ONTOLOGY)
)
if (storeSequence) {
String sequence = cdsService.getResiduesFromCDS(cds)
chadoCdsFeature.residues = sequence
chadoCdsFeature.seqlen = sequence.length()
chadoCdsFeature.md5checksum = generateMD5checksum(sequence)
}
chadoCdsFeature.save()
exportStatisticsMap['feature_count'] += 1
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado feature of CDS ${cds.uniqueName}: ${endTime - startTime} ms"
createChadoFeatureloc(organism, chadoCdsFeature, cds)
return chadoCdsFeature
}
/**
* Create an instance of Chado feature of type 'polypeptide' from a given Apollo CDS and also
* store its amino acid sequence.
* @param organism
* @param transcript
* @param cds
* @param storeSequence
* @return chadoPolyPeptideFeature
*/
def createChadoPolypeptide(org.bbop.apollo.Organism organism, org.bbop.apollo.Transcript transcript, org.bbop.apollo.CDS cds, boolean storeSequence = false) {
long startTime, endTime
Timestamp timestamp = generateTimeStamp()
startTime = System.currentTimeMillis()
org.gmod.chado.Feature chadoPolypeptideFeature = new org.gmod.chado.Feature(
uniquename: generateUniqueName(),
name: transcript.name + "-pep",
isAnalysis: true,
isObsolete: false,
timeaccessioned: timestamp,
timelastmodified: timestamp,
organism: chadoOrganismsMap.get(organism.commonName),
type: getChadoCvterm("polypeptide", SEQUENCE_ONTOLOGY)
)
if (storeSequence) {}
chadoPolypeptideFeature.save()
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado feature of Polypeptide for Transcript ${transcript.name}: ${endTime - startTime} ms"
createChadoFeatureloc(organism, chadoPolypeptideFeature, cds)
return chadoPolypeptideFeature
}
/**
* Checks if there is a Chado feature representation for a given Apollo Sequence.
* @param sequence
* @return
*/
def getSrcFeatureForFeature(org.bbop.apollo.Organism organism, org.bbop.apollo.Sequence sequence) {
org.gmod.chado.Feature srcFeature
long startTime = System.currentTimeMillis()
def sequenceFeatureResult = org.gmod.chado.Feature.executeQuery(
"SELECT DISTINCT sf FROM org.gmod.chado.Feature sf WHERE sf.name = :querySequenceName AND sf.organism.genus = :queryGenus AND sf.organism.species = :querySpecies AND sf.type.name = :querySequenceType",
[querySequenceName: sequence.name, queryGenus: organism.genus, querySpecies: organism.species, querySequenceType: "chromosome"])
long endTime = System.currentTimeMillis()
log.debug "Time taken for querying for sequence ${sequence.name} of organism ${organism.genus} ${organism.species}: ${endTime - startTime} ms"
if (sequenceFeatureResult.size() == 0) {
srcFeature = createChadoFeatureForSequence(organism, sequence, configWrapperService.getChadoExportFastaForSequence())
}
else if (sequenceFeatureResult.size() == 1) {
srcFeature = sequenceFeatureResult.get(0)
}
else {
log.error "${sequenceFeatureResult} - More than one result found for sequence name '${sequence.name}'. Returning null."
srcFeature = null
}
return srcFeature
}
/**
* Creates an instance of Chado FeaturelocPub and creates a linking relationship between Chado feature and Chado pub.
* @param chadoFeatureloc
* @param featureLocationPublication
* @return
*/
def createChadoFeaturelocPub(org.gmod.chado.Featureloc chadoFeatureloc, org.bbop.apollo.Publication featureLocationPublication) {
org.gmod.chado.Pub chadoPublication = createChadoPublication(featureLocationPublication)
org.gmod.chado.FeaturelocPub chadoFeaturelocPub = new org.gmod.chado.FeaturelocPub(
featureloc: chadoFeatureloc,
pub: chadoPublication
).save()
return chadoFeaturelocPub
}
/**
* Creates an instance of Chado feature_relationship for a given Apollo FeatureRelationship.
* Default relationship type is 'part_of'.
* @param organism
* @param feature
* @param featureRelationship
* @param relationshipType
* @return chadoFeatureRelationship
*/
def createChadoFeatureRelationship(org.bbop.apollo.Organism organism, org.gmod.chado.Feature feature, org.bbop.apollo.FeatureRelationship featureRelationship, String relationshipType = "part_of") {
long startTime, endTime
startTime = System.currentTimeMillis()
org.gmod.chado.FeatureRelationship chadoFeatureRelationship = new org.gmod.chado.FeatureRelationship(
subject: feature,
object: getChadoFeature(featureRelationship.parentFeature.uniqueName),
value: featureRelationship.value,
rank: featureRelationship.rank,
type: getChadoCvterm(relationshipType, RELATIONSHIP_ONTOLOGY)
).save()
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado feature_relationship ${feature.class.simpleName} ${relationshipType} ${featureRelationship.parentFeature.class.simpleName}: ${endTime - startTime} ms"
featureRelationship.featureRelationshipPublications.each { featureRelationshipPublication ->
createChadoFeatureRelationshipPublication(chadoFeatureRelationship, featureRelationshipPublication)
}
featureRelationship.featureRelationshipProperties.each { featureRelationshipProperty ->
createChadoFeatureRelationshipProperty(chadoFeatureRelationship, featureRelationshipProperty)
}
return chadoFeatureRelationship
}
/**
* Creates a Chado feature_relationship_pub for a Chado feature_relationship based on a given Apollo Publication.
* @param chadoFeatureRelationship
* @param publication
* @return
*/
def createChadoFeatureRelationshipPublication(org.gmod.chado.FeatureRelationship chadoFeatureRelationship, org.bbop.apollo.Publication publication) {
org.gmod.chado.Pub chadoPublication = createChadoPublication(publication)
org.gmod.chado.FeatureRelationshipPub chadoFeatureRelationshipPub = new org.gmod.chado.FeatureRelationshipPub(
featureRelationship: chadoFeatureRelationship,
pub: chadoPublication
).save()
return chadoFeatureRelationshipPub
}
/**
* Creates a Chado feature_relationshipprop for a Chado feature_relationship based on a given Apollo FeatureProperty
* @param chadoFeatureRelationship
* @param featureProperty
* @return
*/
def createChadoFeatureRelationshipProperty(org.gmod.chado.FeatureRelationship chadoFeatureRelationship, org.bbop.apollo.FeatureProperty featureProperty) {
org.gmod.chado.FeatureRelationshipprop chadoFeatureRelationshipprop = new org.gmod.chado.FeatureRelationshipprop(
featureRelationship: chadoFeatureRelationship,
rank: featureProperty.rank,
value: featureProperty.value,
type: getChadoCvterm(featureProperty.type.name, FEATURE_PROPERTY)
).save()
featureProperty.featurePropertyPublications.each { featurePropertyPublication ->
createChadoFeatureRelationshipPropertyPublication(chadoFeatureRelationshipprop, featurePropertyPublication)
}
return chadoFeatureRelationshipprop
}
/**
* Creates a Chado feature_relationshipprop_pub for a Chado feature_relationshipprop based on a given Apollo Publication
* @param chadoFeatureRelationshipprop
* @param featurePropertyPublication
* @return
*/
def createChadoFeatureRelationshipPropertyPublication(org.gmod.chado.FeatureRelationshipprop chadoFeatureRelationshipprop, org.bbop.apollo.Publication featurePropertyPublication) {
org.gmod.chado.Pub chadoPublication = createChadoPublication(featurePropertyPublication)
org.gmod.chado.FeatureRelationshippropPub chadoFeatureRelationshippropPub = new org.gmod.chado.FeatureRelationshippropPub(
featureRelationshipprop: chadoFeatureRelationshipprop,
pub: chadoPublication
).save()
return chadoFeatureRelationshippropPub
}
/**
* Wrapper for createChadoSynonym() for handling multiple featureSynonyms.
* @param chadoFeature
* @param featureSynonyms
* @return
*/
def createChadoSynonym(org.gmod.chado.Feature chadoFeature, Set<org.bbop.apollo.FeatureSynonym> featureSynonyms) {
featureSynonyms.each { featureSynonym ->
createChadoSynonym(chadoFeature, featureSynonym)
}
}
/**
*
* @param synonym
* @return
*/
def getChadoSynonym(org.bbop.apollo.Synonym synonym) {
org.gmod.chado.Synonym chadoSynonym
def synonymResult = org.gmod.chado.Synonym.executeQuery(
"SELECT DISTINCT s FROM org.gmod.chado.Synonym s JOIN s.type c WHERE s.name = :querySynonym AND c.name = :queryCvterm",
[querySynonym: synonym.name, queryCvterm: synonym.type.name])
if (synonymResult.size() == 0) {
chadoSynonym = createChadoSynonym(synonym.name)
}
else if (synonymResult.size() == 1) {
chadoSynonym = synonymResult.get(0)
}
else {
log.error "${synonymResult} - More than one result found for synonym '${synonym.name}'of type '${synonym.type.name}'. Returning null."
chadoSynonym = null
}
return chadoSynonym
}
/**
*
* @param synonymName
* @param synonymType
* @return
*/
def getChadoSynonym(String synonymName, String synonymType) {
org.gmod.chado.Synonym chadoSynonym
def synonymResult = org.gmod.chado.Synonym.executeQuery(
"SELECT DISTINCT s FROM org.gmod.chado.Synonym s JOIN s.type c WHERE s.name = :querySynonym AND c.name = :queryCvterm",
[querySynonym: synonymName, queryCvterm: synonymType])
if (synonymResult.size() == 0) {
chadoSynonym = createChadoSynonym(synonymName)
}
else if (synonymResult.size() == 1) {
chadoSynonym = synonymResult.get(0)
}
else {
log.error "${synonymResult} - More than one result found for synonym '${synonymName}'of type '${synonymType}'. Returning null."
chadoSynonym = null
}
return chadoSynonym
}
/**
* Creates an instance of Chado synonym for a given Apollo FeatureSynonym and creates a linking relationship between
* Chado feature and Chado synonym via feature_synonym.
* @param chadoFeature
* @param featureSynonym
* @return chadoSynonym
*/
def createChadoSynonym(org.gmod.chado.Feature chadoFeature, org.bbop.apollo.FeatureSynonym featureSynonym) {
org.gmod.chado.Synonym chadoSynonym = createChadoSynonym(featureSynonym.synonym)
//Publications from FeatureSynonym
org.gmod.chado.Pub chadoPublication = createChadoPublication(featureSynonym.publication)
org.gmod.chado.FeatureSynonym chadoFeatureSynonym = new org.gmod.chado.FeatureSynonym(
feature: chadoFeature,
synonym: chadoSynonym,
pub: chadoPublication,
isCurrent: featureSynonym.isCurrent, // default
isInternal: featureSynonym.isInternal // default
).save()
return chadoSynonym
}
/**
* Creates an instance of Chado synonym for a given Apollo Synonym
* @param synonymName
* @param synonymType
* @return
*/
def createChadoSynonym(String synonymName) {
org.gmod.chado.Synonym chadoSynonym = new org.gmod.chado.Synonym(
name: synonymName,
synonymSgml: synonymName,
type: getChadoCvterm("synonym", FEATURE_PROPERTY)
).save()
return chadoSynonym
}
/**
* Wrapper for createChadoFeaturePub() for handling multiple publications.
* @param chadoFeature
* @param publications
* @return
*/
def createChadoFeaturePub(org.gmod.chado.Feature chadoFeature, Set<org.bbop.apollo.Publication> publications) {
publications.each { publication ->
createChadoFeaturePub(chadoFeature, publication)
}
}
/**
* Creates an instance of Chado pub for a given Apollo Publication and creates a linking relationship between
* Chado feature and Chado pub via Chado feature_pub.
* @param chadoFeature
* @param publication
*/
def createChadoFeaturePub(org.gmod.chado.Feature chadoFeature, org.bbop.apollo.Publication publication) {
org.gmod.chado.Pub chadoPublication = createChadoPublication(publication)
// Linking feature to publication via feature_pub
org.gmod.chado.FeaturePub chadoFeaturePub = new org.gmod.chado.FeaturePub(
feature: chadoFeature,
pub: chadoPublication
).save()
return chadoPublication
}
/**
* Wrapper for createChadoProperty() for handling multiple FeatureProperties
* @param chadoFeature
* @param featureProperties
* @return
*/
def createChadoProperty(org.gmod.chado.Feature chadoFeature, Set<org.bbop.apollo.FeatureProperty> featureProperties) {
int commentRank = 0
featureProperties.each { featureProperty ->
if (featureProperty instanceof org.bbop.apollo.Comment) {
createChadoFeaturePropertyForComment(chadoFeature, featureProperty, commentRank)
commentRank++;
}
else {
createChadoProperty(chadoFeature, featureProperty)
}
}
}
/**
* Creates an instance of Chado featureprop for an Apollo FeatureProperty.
* @param chadoFeature
* @param featureProperty
*/
def createChadoProperty(org.gmod.chado.Feature chadoFeature, org.bbop.apollo.FeatureProperty featureProperty) {
long startTime, endTime
String type = "feature_property"
startTime = System.currentTimeMillis()
org.gmod.chado.Featureprop chadoFeatureProp = new org.gmod.chado.Featureprop(
value: featureProperty.value,
rank: featureProperty.rank,
feature: chadoFeature,
type: getChadoCvterm(type, FEATURE_PROPERTY)
).save()
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado featureprop of type '${type}' and value ${featureProperty.value}: ${endTime - startTime} ms"
featureProperty.featurePropertyPublications.each { featurePropertyPublication ->
createChadoPropertyPub(chadoFeatureProp, featurePropertyPublication)
}
return chadoFeatureProp
}
/**
* Creates an instance of Chado featureprop for a given Apollo FeatureProperty of type 'comment'.
* @param chadoFeature
* @param comment
* @param rank
* @return
*/
def createChadoFeaturePropertyForComment(org.gmod.chado.Feature chadoFeature, org.bbop.apollo.FeatureProperty comment, int rank) {
long startTime, endTime
startTime = System.currentTimeMillis()
org.gmod.chado.Featureprop chadoFeatureProp = new org.gmod.chado.Featureprop(
value: comment.value,
rank: rank,
feature: chadoFeature,
type: getChadoCvterm("comment", FEATURE_PROPERTY)
).save()
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado featureprop of type 'comment' and value ${comment.value}: ${endTime - startTime} ms"
return chadoFeatureProp
}
/**
* Creates an instance of Chado pub for a given Apollo publication and creates a linking relationship
* beteen Chado featureprop and Chado pub via Chado featureprop_pub.
* @param chadoFeatureProp
* @param publication
*/
def createChadoPropertyPub(org.gmod.chado.Featureprop chadoFeatureProp, org.bbop.apollo.Publication publication) {
org.gmod.chado.Pub chadoPublication = createChadoPublication(publication)
org.gmod.chado.FeaturepropPub featurePropertyPublication = new org.gmod.chado.FeaturepropPub(
featureprop: chadoFeatureProp,
pub: chadoPublication
).save()
return featurePropertyPublication
}
/**
* Experimental
* @param cvTerm
*/
def createChadoCvterm(org.bbop.apollo.CVTerm cvTerm) {
// TODO: Must be careful in creating and setting all properties
org.gmod.chado.Cv chadoCv = new org.gmod.chado.Cv(
name: cvTerm.cv.name,
definition: cvTerm.cv.definition
).save()
org.gmod.chado.Db chadoDb = new org.gmod.chado.Db(
name: cvTerm.dbxref.db.name,
description: cvTerm.dbxref.db.description,
urlprefix: cvTerm.dbxref.db.urlPrefix,
url: cvTerm.dbxref.db.url
).save()
org.gmod.chado.Dbxref chadoDbxref = new org.gmod.chado.Dbxref(
accession: cvTerm.dbxref.accession,
description: cvTerm.dbxref.description,
version: cvTerm.dbxref.version,
db: chadoDb
).save()
org.gmod.chado.Cvterm chadoCvterm = new org.gmod.chado.Cvterm(
name: cvTerm.name,
definition: cvTerm.definition,
isObsolete: cvTerm.isObsolete,
isRelationshiptype: cvTerm.isRelationshipType,
cv: chadoCv,
dbxref: chadoDbxref,
).save()
org.gmod.chado.CvtermDbxref chadoCvTermDbxref = new org.gmod.chado.CvtermDbxref(
cvTerm: chadoCvterm,
dbxref: chadoDbxref,
isForDefinition: 0 // TODO: How to set this attribute for new Cvterm from Apollo?
).save()
org.gmod.chado.CvtermRelationship chadoCvTermRelationship = new org.gmod.chado.CvtermRelationship(
)
//cvTerm.childCVTermRelationships
//cvTerm.parentCVTermRelationships
}
/**
*
* @param dbxref
* @return
*/
def getChadoDbxref(org.bbop.apollo.DBXref dbxref) {
org.gmod.chado.Dbxref chadoDbxref
def dbxrefResult
if (dbxref.version == null) {
dbxrefResult = org.gmod.chado.Dbxref.executeQuery(
"SELECT DISTINCT d FROM org.gmod.chado.Dbxref d JOIN d.db db WHERE d.accession = :queryDbxref AND db.name = :queryDb",
[queryDbxref: dbxref.accession, queryDb: dbxref.db.name])
}
else {
dbxrefResult = org.gmod.chado.Dbxref.executeQuery(
"SELECT DISTINCT d FROM org.gmod.chado.Dbxref d JOIN d.db db WHERE d.accession = :queryDbxref AND d.version = :queryDbxrefVersion AND db.name = :queryDb",
[queryDbxref: dbxref.accession, queryDbxrefVersion: dbxref.version, queryDb: dbxref.db.name])
}
if (dbxrefResult.size() == 0) {
chadoDbxref = createChadoDbxref(dbxref)
}
else if (dbxrefResult.size() == 1) {
chadoDbxref = dbxrefResult.get(0)
}
else {
log.error "${dbxrefResult} - More than one result found for dbxref '${dbxref.db.name}:${dbxref.accession}'. Returning null."
chadoDbxref = null
}
return chadoDbxref
}
/**
* A wrapper for createChadoDbxref() for handling multiple dbxrefs
* @param chadoFeature
* @param dbxrefs
* @return
*/
def createChadoDbxref(org.gmod.chado.Feature chadoFeature, Set<org.bbop.apollo.DBXref> dbxrefs) {
dbxrefs.each { dbxref ->
createChadoDbxref(chadoFeature, dbxref)
}
}
/**
* Creates an instance of Chado Dbxref, creates a linking relationship between Chado Feature and
* Chado Dbxref via Chado feature_dbxref and creates Chado dbxrefprop.
* @param chadoFeature
* @param dbxref
* @return
*/
def createChadoDbxref(org.gmod.chado.Feature chadoFeature, org.bbop.apollo.DBXref dbxref) {
long startTime, endTime
startTime = System.currentTimeMillis()
org.gmod.chado.Dbxref chadoDbxref = getChadoDbxref(dbxref)
// create feature_dbxref relationship
org.gmod.chado.FeatureDbxref chadoFeatureDbxref = new org.gmod.chado.FeatureDbxref(
feature: chadoFeature,
dbxref: chadoDbxref,
isCurrent: true
).save()
endTime = System.currentTimeMillis()
log.debug "Time taken to create Chado dbxref for ${dbxref.db.name}:${dbxref.accession} : ${endTime - startTime} ms"
// dbxref properties
dbxref.dbxrefProperties.each { dbxrefProperty ->
createChadoDbxrefProp(chadoDbxref, dbxrefProperty)
}
return chadoDbxref
}
/**
* Creates an instance of Chado db for a given Apollo DB.
* @param db
* @return
*/
def createChadoDb(org.bbop.apollo.DB db) {
long startTime, endTime
startTime = System.currentTimeMillis()
org.gmod.chado.Db chadoDb = new org.gmod.chado.Db(
name: db.name,
description: db.description,
urlprefix: db.urlPrefix,