-
Notifications
You must be signed in to change notification settings - Fork 85
/
FeatureRelationshipService.groovy
226 lines (182 loc) · 7.34 KB
/
FeatureRelationshipService.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
package org.bbop.apollo
import grails.transaction.Transactional
@Transactional(readOnly = true)
class FeatureRelationshipService {
List<Feature> getChildrenForFeatureAndTypes(Feature feature, String... ontologyIds) {
def list = new ArrayList<Feature>()
if (feature?.parentFeatureRelationships != null) {
feature.parentFeatureRelationships.each { it ->
if (ontologyIds.size() == 0 || (it && ontologyIds.contains(it.childFeature.ontologyId))) {
list.push(it.childFeature)
}
}
}
return list
}
Feature getChildForFeature(Feature feature, String ontologyId) {
List<Feature> featureList = getChildrenForFeatureAndTypes(feature, ontologyId)
if (featureList.size() == 0) {
return null
}
if (featureList.size() > 1) {
log.error "More than one child feature relationships found for ${feature} and ID ${ontologyId}"
return null
}
return featureList.get(0)
}
Feature getParentForFeature(Feature feature, String... ontologyId) {
List<Feature> featureList = getParentsForFeature(feature, ontologyId)
if (featureList.size() == 0) {
return null
}
if (featureList.size() > 1) {
log.error "More than one feature relationships parent found for ${feature} and ID ${ontologyId}"
return null
}
return featureList.get(0)
}
List<Feature> getParentsForFeature(Feature feature, String... ontologyIds) {
def list = new ArrayList<Feature>()
if (feature?.childFeatureRelationships != null) {
feature.childFeatureRelationships.each { it ->
if (ontologyIds.size() == 0 || (it && ontologyIds.contains(it.parentFeature.ontologyId))) {
list.push(it.parentFeature)
}
}
}
return list
}
@Transactional
def deleteRelationships(Feature feature, String parentOntologyId, String childOntologyId) {
deleteChildrenForTypes(feature, childOntologyId)
deleteParentForTypes(feature, parentOntologyId)
}
@Transactional
def setChildForType(Feature parentFeature, Feature childFeature) {
List<FeatureRelationship> results = FeatureRelationship.findAllByParentFeature(parentFeature).findAll() {
it.childFeature.ontologyId == childFeature.ontologyId
}
if (results.size() == 1) {
results.get(0).childFeature = childFeature
return true
} else {
if (results.size() == 0) {
log.info "No feature relationships exist for parent ${parentFeature} and child ${childFeature}"
}
if (results.size() > 1) {
log.warn "${results.size()} feature relationships exist for parent ${parentFeature} and child ${childFeature}"
}
return false
}
}
@Transactional
def deleteChildrenForTypes(Feature feature, String... ontologyIds) {
def criteria = FeatureRelationship.createCriteria()
def featureRelationships = criteria {
eq("parentFeature", feature)
}.findAll() {
ontologyIds.length == 0 || it.childFeature.ontologyId in ontologyIds
}
int numRelationships = featureRelationships.size()
for (int i = 0; i < numRelationships; i++) {
FeatureRelationship featureRelationship = featureRelationships.get(i)
removeFeatureRelationship(featureRelationship.parentFeature, featureRelationship.childFeature)
}
}
@Transactional
def deleteParentForTypes(Feature feature, String... ontologyIds) {
// delete transcript -> non canonical 3' splice site child relationship
def criteria = FeatureRelationship.createCriteria()
criteria {
eq("childFeature", feature)
}.findAll() {
it.parentFeature.ontologyId in ontologyIds
}.each {
feature.removeFromChildFeatureRelationships(it)
}
}
// based on Transcript.setCDS
@Transactional
def addChildFeature(Feature parent, Feature child, boolean replace = true) {
// replace if of the same type
if (replace) {
boolean found = false
def criteria = FeatureRelationship.createCriteria()
criteria {
eq("parentFeature", parent)
}
.findAll() {
it.childFeature.ontologyId == child.ontologyId
}
.each {
found = true
it.childFeature = child
it.save()
return
}
if (found) {
return
}
}
FeatureRelationship fr = new FeatureRelationship(
parentFeature: parent
, childFeature: child
).save(flush: true);
parent.addToParentFeatureRelationships(fr)
child.addToChildFeatureRelationships(fr)
child.save(flush: true)
parent.save(flush: true)
}
@Transactional
void removeFeatureRelationship(Feature parentFeature, Feature childFeature) {
FeatureRelationship featureRelationship = FeatureRelationship.findByParentFeatureAndChildFeature(parentFeature, childFeature)
if (featureRelationship) {
parentFeature.parentFeatureRelationships?.remove(featureRelationship)
childFeature.childFeatureRelationships?.remove(featureRelationship)
parentFeature.save(flush: true)
childFeature.save(flush: true)
}
}
List<Frameshift> getFeaturePropertyForTypes(Transcript transcript, List<String> strings) {
return (List<Frameshift>) FeatureProperty.findAllByFeaturesInListAndOntologyIdsInList([transcript], strings)
}
List<Feature> getChildren(Feature feature) {
def exonRelations = feature.parentFeatureRelationships.findAll()
return exonRelations.collect { it ->
it.childFeature
}
}
/**
* Iterate to all of the children, and delete the child and thereby the feature relationship automatically.
*
*
* @param feature
* @return
*/
@Transactional
def deleteFeatureAndChildren(Feature feature) {
// if grandchildren then delete those
for (FeatureRelationship featureRelationship in feature.parentFeatureRelationships) {
if (featureRelationship.childFeature?.parentFeatureRelationships) {
deleteFeatureAndChildren(featureRelationship.childFeature)
}
}
// create a list of relationships to remove (assume we have no grandchildren here)
List<FeatureRelationship> relationshipsToRemove = []
for (FeatureRelationship featureRelationship in feature.parentFeatureRelationships) {
relationshipsToRemove << featureRelationship
}
// actually delete those
relationshipsToRemove.each {
it.childFeature.delete()
feature.removeFromParentFeatureRelationships(it)
it.delete()
}
// last, delete self or save updated relationships
if (!feature.parentFeatureRelationships && !feature.childFeatureRelationships) {
feature.delete(flush: true)
} else {
feature.save(flush: true)
}
}
}