-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathannotator.js
More file actions
149 lines (116 loc) · 3.53 KB
/
annotator.js
File metadata and controls
149 lines (116 loc) · 3.53 KB
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
var request = require('request'),
memwatch = require('memwatch'),
fs = require('fs')
memwatch.on('leak', function(info) {
console.log(info)
})
//API_KEY= '24e050ca-54e0-11e0-9d7b-005056aa3316'
var API_KEY= '5758f84b-562e-46cb-890b-ff787cc52bed',
annotatorUrl = 'http://data.bioontology.org/annotator'
//submitUrl = annotatorUrl + '/submit/jhostetter@gmail.com'
var textToAnnotate = "Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye"
/*---------------------
// virtual ontology ids:
// Radlex: 1057/50767
// LOINC: 1350/47637
// SNOMED: 1353/46896
*/
var params = {
'stop_words':'<table>, </table>, <tr>, </tr>, <td>, </td>',
'minimum_match_length':'',
'ontologies':'RADLEX, LOINC, SNOMEDCT',
'semantic_types':'', //T017,T047,T191&" #T999&"
'max_level':'0',
'text': textToAnnotate,
'apikey': API_KEY,
'include': 'prefLabel'
}
getAnnotations = function (text, cb) {
var result = [],
testing = false
params.text = text || params.textToAnnotate
//var hd = new memwatch.HeapDiff()
// Submit job
/**
* Post request to JSON API
*/
console.log('querying api...')
request.post(annotatorUrl, {form: params}, function(err, res, body){
if (testing){
fs.writeFile('./annotation_result.js', body)
}
body = JSON.parse(body)
body.forEach(function(match){
var term = {},
an = match.annotations[0]
term.term = an.text
term.from = an.from
term.to = an.to
term.isA = an.matchType
term.link = match.annotatedClass.links.ui
term.ontology = match.annotatedClass.links.ontology.split('/').pop()
term.prefLabel = match.annotatedClass.prefLabel
result.push(term)
})
console.log('finished parsing')
//fs.writeFile('./annotation_result.js', JSON.stringify(result))
if (typeof cb === 'function'){
cb(null, result)
} else {
return result
}
})
/*
var fileStream = request.post(submitUrl, {form: params})
var parser = xml.parse(fileStream)
parser.each('annotationBean', function(val){
var term = {}
if (val.context.contextName.$text === 'CLOSURE'){
term.term = val.context.concept.preferredName.$text
term.from = parseInt(val.context.from.$text)
term.to = parseInt(val.context.to.$text)
term.isA = val.concept.preferredName.$text
term.link = val.context.concept.fullId.$text
term.ontology = val.concept.localOntologyId.$text
} else { // others I know of are mgrepContextBean - seems to mean it does not have is_a closure info
term.term = val.context.term.concept.preferredName.$text
term.from = parseInt(val.context.from.$text)
term.to = parseInt(val.context.to.$text)
term.link = val.context.term.concept.fullId.$text
term.ontology = val.context.term.concept.localOntologyId.$text
}
result.push(term)
})
parser.on('error', function(err){
console.log('Error: ', err)
})
parser.on('end', function(){
//console.log('finished parsing')
if (typeof cb === 'function'){
cb(null, result)
} else {
return result
}
//var diff = hd.end()
//console.log('diff: ', diff, diff.change.details)
})*/
/*
request.post(submitUrl, {form: params}, function(error, response, body){
if (error){
console.log(error)
return new Error(error)
}
fs.writeFile('./annotation.txt', response.body)
if (typeof cb === 'function'){
cb(null, {annotations: response.body})
} else {
return {annotations: response.body}
}
var diff = hd.end()
console.log('diff: ', diff, diff.change.details)
})*/
}
module.exports = {
getAnnotations: getAnnotations,
params: params
}