-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathParseFloraFile.groovy
340 lines (314 loc) · 9.95 KB
/
ParseFloraFile.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
/* Parses all the flora files */
import opennlp.tools.sentdetect.*
import opennlp.tools.dictionary.*
import opennlp.tools.tokenize.*
import opennlp.tools.util.*
import opennlp.tools.chunker.*
import opennlp.tools.postag.*
import org.apache.commons.io.IOUtils
import opennlp.tools.namefind.*
import java.util.concurrent.*
def MINLENGTH = 2 // minimum length of a token to recognize
def fout = new PrintWriter(new BufferedWriter(new FileWriter("eq.txt")))
def fout2 = new PrintWriter(new BufferedWriter(new FileWriter("missing-e-or-q.txt")))
def name2id = [:]
new File("ont").eachFile { ontfile ->
def id = ""
def fname = ""
ontfile.eachLine { line ->
if (line.startsWith("id:")) {
id = line.substring(3).trim()
}
if (line.startsWith("name:")) {
def name = line.substring(5).trim()
fname = name
if (name2id[name] == null) {
name2id[name] = new TreeSet()
}
name2id[name].add(id)
}
if (line.startsWith("synonym:")) {
def syn = line.substring(line.indexOf("\"")+1, line.lastIndexOf("\"")).trim()
if (name2id[syn] == null) {
name2id[syn] = new TreeSet()
}
name2id[syn].add(id)
}
if (line.startsWith("xref:")) {
if (line.indexOf("\"")>-1) {
def syn = line.substring(line.indexOf("\"")+1, line.lastIndexOf("\"")).trim()
if (name2id[syn] == null) {
name2id[syn] = new TreeSet()
}
name2id[syn].add(id)
}
}
if (line.startsWith("replaced_by:")) {
def i = line.substring(12).trim()
name2id[fname].remove(id)
name2id[fname].add(i)
}
}
}
/* Now remove the " to" part in PATO qualities */
def newmap = [:]
name2id.each { name, id ->
if (name.endsWith(" to")) {
def newname = name.replaceAll(" to","")
newmap[newname] = id
}
}
newmap.each { n, i ->
if (!name2id[n]) {
name2id[n] = i
}
}
/* Now add the glossary terms, many of which will not be in one of the ontologies */
new File("glossary/Plant_glossary_term_category.csv").eachLine { line ->
if (!line.startsWith("#") && line.length()>5) {
def tok = line.split(",").collect { it.replaceAll("\"","") }
if (name2id[tok[0]] == null) {
name2id[tok[0]] = new TreeSet()
}
name2id[tok[0]].add(tok[-1])
}
}
/* Now add the french - english translations */
new File("glossary/Lexicon-english-french.csv").splitEachLine("\t") { line ->
def english = line[1]
def french = line[2]
def frenchpretty = line[3]
if (name2id[english]) {
if (french?.length()>1) {
if (name2id[french]) {
name2id[french].addAll(name2id[english])
} else {
name2id[french] = name2id[english]
}
}
if (frenchpretty?.length()>1) {
if (name2id[frenchpretty]) {
name2id[frenchpretty].addAll(name2id[english])
} else {
name2id[frenchpretty] = name2id[english]
}
}
}
}
new File("glossary/terms/").eachFile { file ->
file.splitEachLine("\\|") { line ->
def name = line[3].trim().toLowerCase()
def type = line[0].trim().toLowerCase()
def defi = line[4].trim().toLowerCase()
def flag = false // false if anatomy, true if quality
if (type.endsWith("types")) {
flag = true
}
/* now we merge the ids of all the synonyms */
def splits = name.split(";")
if (splits.size()>1) {
def ss = new TreeSet()
splits.each { s ->
if (name2id[s]) {
ss.addAll(name2id[s])
}
}
splits.each { s ->
name2id[s] = ss
}
}
name.split(";").each { syn ->
syn = syn.trim()
// if (flag) { // quality
def flag2 = false // false if no PATO term in name2id[syn]
name2id[syn]?.each { if (it.startsWith("PATO") || (it.startsWith("PO"))) flag2 = true }
if (!name2id[syn] || !flag2) {
println type+"\t"+syn+"\t"+name2id[syn]+"\t$defi\tMISSING"
}
// }
/*else { // anatomy
def flag2 = false // false if no PO term in name2id[syn]
name2id[syn]?.each { if (it.startsWith("PO")) flag2 = true }
if (!name2id[syn] || !flag2) {
println type+"\t"+syn+"\t"+name2id[syn]+"\t$defi\tMISSING ENTITY"
}
}
*/
}
}
}
TokenizerModel tokenizerModel = new TokenizerModel(new FileInputStream("en-token.bin"))
Tokenizer tokenizer = new TokenizerME(tokenizerModel)
SentenceModel sentenceModel = new SentenceModel(new FileInputStream("en-sent.bin"))
SentenceDetectorME sentenceDetector = new SentenceDetectorME(sentenceModel)
def tokens = name2id.keySet()
Dictionary dict = new Dictionary(false)
tokens.each { tok ->
tok = tok?.toLowerCase()
if (tok && tok.length()>MINLENGTH) {
StringList l = tokenizer.tokenize(tok)
dict.put(l)
}
}
DictionaryNameFinder finder = new DictionaryNameFinder(dict)
//println name2id["petiole"]
Map taxon2string = [:] // maps taxon node to taxon string
def author = null
def year = null
List<String> rankOrder = ["order", "family", "subfamily", "tribe", "subtribe", "genus", "subgenus", "species", "subspecies", "variety"]
Map<String, Set<EntityQuality>> previousCharacters = [:] // this maps taxonomic rank name to EQs
Map<String, String> previousNames = [:] // this keeps the previously encountered taxon names; ordername -> value
def taxon2eq = [:] // this is the raw EQ data for each taxon
def taxon2classes = [:] // this is the processed phenotype data (PPO identifiers) for each taxon
XmlSlurper slurper = new XmlSlurper(false, false)
slurper.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
new File("floras").eachFile { florafile ->
def flora = slurper.parse(florafile)
flora.treatment.each { treatment ->
treatment.taxon.each { taxon ->
def name = null
taxon.nomenclature.homotypes.nom.each { nom ->
if (nom.@class.text() == "accepted") {
taxon2eq[nom] = new LinkedHashSet()
name = nom
def lastOrderRank = ""
nom.name.each { nomname -> /* first we determine the level of the tree in which we currently are; we will reuse information from the higher orders
// mentioned before */
def cname = nomname.@class.text()
if (cname == "author") { author = nomname.text() }
if (cname == "year") { year = nomname.text() }
if (cname in rankOrder) {
lastOrderRank = cname
def cvalue = nomname.text()
// we delete everything from the end of the list to the current rank from previousCharacters map
/* rankOrder[-1..rankOrder.indexOf(cname)].each {
previousCharacters[it] = null
previousNames[it] = null
}*/
previousCharacters[cname] = taxon2eq[nom]
previousNames[cname] = cvalue
rankOrder[0..rankOrder.indexOf(cname)].each { if (previousCharacters[it]) {taxon2eq[nom].addAll(previousCharacters[it]) } }
}
}
def taxonString = ""
rankOrder[0..rankOrder.indexOf(lastOrderRank)].each {
if (previousNames[it] != null) {
taxonString += "$it: "+previousNames[it]+"; "
}
}
taxonString += "$author; $year"
taxon2string[nom] = taxonString
}
}
if (name) {
taxon.feature.each { feature ->
if (feature.@class.text() == "description") {
feature.char.each { character ->
def cclass = character.@class.text().toLowerCase()
cclass = cclass.replaceAll("leaves","leaf")
if (cclass.endsWith("s")) {
cclass = cclass.substring(0,cclass.length()-1)
}
EntityQuality eq = new EntityQuality()
taxon2eq[name].add(eq)
eq.entity = new LinkedHashSet()
eq.entityName = new LinkedHashSet()
eq.quality = new LinkedHashSet()
eq.qualityName = new LinkedHashSet()
def tokenizedCClass = tokenizer.tokenize(cclass)
def classMatches = finder.find(tokenizedCClass)
def classOccurrences = Span.spansToStrings(classMatches, tokenizedCClass)
classOccurrences.each { match ->
def matchids = name2id[match]
eq.entityName.add(match)
matchids.each {
eq.entity.add(it)
}
}
def ctext = character.text()
def sentences = sentenceDetector.sentDetect(ctext)
sentences.each { sentence ->
sentence = sentence.toLowerCase()
def mainStructure = sentence.split(";")[0]
def tokenizedText = tokenizer.tokenize(mainStructure)
def matches = finder.find(tokenizedText)
def occurrences = Span.spansToStrings(matches, tokenizedText)
// print "$cclass ("+name2id[cclass]+")\t$ctext\t"
occurrences.each { match ->
def matchids = name2id[match]
eq.qualityName.add(match)
matchids.each {
// print "$it("+match+")\t"
eq.quality.add(it)
}
}
}
/*
def pattern = ~/\d+(\.|\,)*\d*\s*(\-|x)\s*\d+(\.\d+|\,\d+)*\s*[a-z]+/
(ctext =~ pattern).each { println it[0] }
*/
ctext = ctext.toLowerCase()
def tokenizedText = tokenizer.tokenize(ctext)
def matches = finder.find(tokenizedText)
def occurrences = Span.spansToStrings(matches, tokenizedText)
// print "$cclass ("+name2id[cclass]+")\t$ctext\t"
occurrences.each { match ->
def matchids = name2id[match]
eq.qualityName.add(match)
matchids.each {
// print "$it("+match+")\t"
eq.quality.add(it)
}
}
// println "\n"
}
}
}
}
}
}
}
taxon2eq.each { taxon, eqset ->
def taxonstring = taxon2string[taxon]
eqset.each { eq ->
eq?.entity.each { ent ->
if (ent.indexOf("PO")>-1) {
eq?.quality.each { qual ->
if (qual.indexOf("PATO")>-1) {
fout.println("$taxonstring\t$ent\t$qual")
}
}
}
}
}
}
fout.flush()
fout.close()
taxon2eq.each { taxon, eqset ->
eqset.each { eq ->
def checked = new TreeSet()
eq?.entity.each { ent ->
if (ent.indexOf("PO:")>-1) {
checked.add(ent)
}
}
if (checked.size() == 0) {
fout2.println (eq.entityName+"\t"+"ENTITY-MISMATCH")
}
checked = new TreeSet()
eq?.qualityName.each { ent ->
name2id[ent]?.each { e ->
if (e.indexOf("PATO:")>-1) {
checked.add(ent)
}
if (e.indexOf("PO:")>-1) {
checked.add(ent)
}
}
}
eq?.qualityName.removeAll { it in checked }
eq?.qualityName.each { fout2.println (it+"\t"+name2id[it]+"\tQUALITY") }
}
}
fout2.flush()
fout2.close()