-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSim.groovy
176 lines (145 loc) · 5.11 KB
/
Sim.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
@Grab(group='com.github.sharispe', module='slib-sml', version='0.9.1')
@Grab(group='org.codehaus.gpars', module='gpars', version='1.1.0')
import java.net.*
import org.openrdf.model.vocabulary.*
import slib.sglib.io.loader.*
import slib.sml.sm.core.metrics.ic.utils.*
import slib.sml.sm.core.utils.*
import slib.sglib.io.loader.bio.obo.*
import org.openrdf.model.URI
import slib.graph.algo.extraction.rvf.instances.*
import slib.sglib.algo.graph.utils.*
import slib.utils.impl.Timer
import slib.graph.algo.extraction.utils.*
import slib.graph.model.graph.*
import slib.graph.model.repo.*
import slib.graph.model.impl.graph.memory.*
import slib.sml.sm.core.engine.*
import slib.graph.io.conf.*
import slib.graph.model.impl.graph.elements.*
import slib.graph.algo.extraction.rvf.instances.impl.*
import slib.graph.model.impl.repo.*
import slib.graph.io.util.*
import slib.graph.io.loader.*
import groovyx.gpars.GParsPool
System.setProperty("jdk.xml.entityExpsansionLimit", "0");
System.setProperty("jdk.xml.totalEntitySizeLimit", "0");
def factory = URIFactoryMemory.getSingleton()
def annotationsPath = args[0] // "data/gene_annotations.tab";
def resSimPath = args[1] // "data/sim.txt";
println(annotationsPath + "\t" + resSimPath)
class Gene {
int id
Set annotations
public Gene(id, annotations) {
setId(id)
setAnnotations(annotations)
}
void addAnnotation(annotation) {
annotations.add(annotation);
}
def getAnnotations() {
annotations
}
}
def getOntology = {
URI graph_uri = factory.getURI("http://purl.obolibrary.org/obo/")
G graph = new GraphMemory(graph_uri)
// Load OBO file to graph "go.obo"
GDataConf goConf = new GDataConf(GFormat.RDF_XML, "data/go-basic.owl")
GraphLoaderGeneric.populate(goConf, graph)
// Add virtual root for 3 subontologies__________________________________
URI virtualRoot = factory.getURI("http://purl.obolibrary.org/obo/virtualRoot")
graph.addV(virtualRoot)
new File(annotationsPath).splitEachLine('\t') { items ->
String geneId = items[0];
for (int i = 1; i < items.size(); i++) {
URI idURI = factory.getURI("http://purl.obolibrary.org/obo/" + geneId);
String go_id = items[i];
URI goURI = factory.getURI("http://purl.obolibrary.org/obo/" + go_id);
Edge e = new Edge(idURI, RDF.TYPE, goURI);
graph.addE(e);
}
}
GAction rooting = new GAction(GActionType.REROOTING)
rooting.addParameter("root_uri", virtualRoot.stringValue())
GraphActionExecutor.applyAction(factory, rooting, graph)
return graph
}
def getURIfromName = { name ->
def id = name.split('\\:')
name = id[0] + "_" + id[1]
return factory.getURI("http://purl.obolibrary.org/obo/$name")
}
def getGenes = {
def genes = []
new File(annotationsPath).splitEachLine('\t') { items ->
def annots = new Gene(0, new LinkedHashSet())
for (int j = 1; j < items.size(); j++) {
annots.addAnnotation(getURIfromName(items[j]))
}
genes.push(annots)
}
return genes
}
graph = getOntology()
genes = getGenes()
def sim_id = 0 //this.args[0].toInteger()
SM_Engine engine = new SM_Engine(graph)
// BMA+Resnik, BMA+Schlicker2006, BMA+Lin1998, BMA+Jiang+Conrath1997,
// DAG-GIC, DAG-NTO, DAG-UI
String[] flags = [
// SMConstants.FLAG_SIM_GROUPWISE_AVERAGE,
// SMConstants.FLAG_SIM_GROUPWISE_AVERAGE_NORMALIZED_GOSIM,
SMConstants.FLAG_SIM_GROUPWISE_BMA,
SMConstants.FLAG_SIM_GROUPWISE_BMM,
SMConstants.FLAG_SIM_GROUPWISE_MAX,
SMConstants.FLAG_SIM_GROUPWISE_MIN,
SMConstants.FLAG_SIM_GROUPWISE_MAX_NORMALIZED_GOSIM
]
// List<String> pairFlags = new ArrayList<String>(SMConstants.PAIRWISE_MEASURE_FLAGS);
String[] pairFlags = [
SMConstants.FLAG_SIM_PAIRWISE_DAG_NODE_RESNIK_1995,
SMConstants.FLAG_SIM_PAIRWISE_DAG_NODE_SCHLICKER_2006,
SMConstants.FLAG_SIM_PAIRWISE_DAG_NODE_LIN_1998,
SMConstants.FLAG_SIM_PAIRWISE_DAG_NODE_JIANG_CONRATH_1997_NORM
]
ICconf icConf = new IC_Conf_Corpus("ResnikIC", SMConstants.FLAG_IC_ANNOT_RESNIK_1995_NORMALIZED);
String flagGroupwise = flags[sim_id.intdiv(pairFlags.size())];
String flagPairwise = pairFlags[sim_id % pairFlags.size()];
SMconf smConfGroupwise = new SMconf(flagGroupwise);
SMconf smConfPairwise = new SMconf(flagPairwise);
smConfPairwise.setICconf(icConf);
// // Schlicker indirect
// ICconf prob = new IC_Conf_Topo(SMConstants.FLAG_ICI_PROB_OCCURENCE_PROPAGATED);
// smConfPairwise.addParam("ic_prob", prob);
def result = new Double[genes.size() * genes.size()]
for (i = 0; i < result.size(); i++) {
result[i] = i
}
def c = 0
GParsPool.withPool {
result.eachParallel { val ->
def i = val.toInteger()
def x = i.intdiv(genes.size())
def y = i % genes.size()
result[i] = engine.compare(
smConfGroupwise,
smConfPairwise,
genes[x].getAnnotations(),
genes[y].getAnnotations())
if (c % 100000 == 0)
println c
c++
}
}
def out = new PrintWriter(new BufferedWriter(
new FileWriter(resSimPath)))
for (i = 0; i < result.size(); i++) {
if ((i + 1) % genes.size() == 0)
out.print(String.format("%.3f", result[i]) + '\n')
else
out.print(String.format("%.3f", result[i]) + ' ')
}
out.flush()
out.close()