-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_disease.js
executable file
·138 lines (123 loc) · 4.46 KB
/
get_disease.js
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
#!/usr/bin/env node
var fs = require('fs');
var xml2json = require('xml2json');
var util = require('util');
var _ = require('underscore');
var Promise = require('bluebird'); // Node JS promise library
var MongoClient = Promise.promisifyAll(require('mongodb').MongoClient);
// Command-line argument processing
var opts = require('nomnom')
.script('./get_disease.js')
.option('replace', {
abbr: 'r',
flag: true,
help: 'Replace collection if it exists; append otherwise'
})
.option('disease', {
abbr: 'd',
metavar: 'FILE',
help: 'Name of disease XML file'
})
.option('db', {
metavar: 'NAME',
default: 'Spindle',
help: 'Name of the Mongo database to write to'
})
.option('collection', {
abbr: 'c',
metavar: 'NAME',
default: 'Disease',
help: 'Name of the database collection to write to'
})
.parse();
var url = 'mongodb://localhost:27017/' + opts.db;
// Grab the path from the command line
var path = opts.disease;
if (!path) {
util.log('Must specify an XML file as a source.');
return;
}
// Read from the specified file in xmlData
try {
var xmlData = fs.readFileSync(path, {encoding: 'UTF-8'});
} catch(err) {
util.log(err);
return;
}
if (!xmlData) {
util.log('Could not read from the file ' + path);
return;
}
// Convert from XML to JSON in data
var data = xml2json.toJson(xmlData, {object: true});
if (!data) {
util.log('Couldn’t convert source file ' + path);
}
// Translate and insert the JSON data into the Mongo DB.
MongoClient.connectAsync(url).then(function(db) {
var coll = Promise.promisifyAll(db.collection(opts.collection));
var bulk = Promise.promisifyAll(coll.initializeUnorderedBulkOp());
// Convert each disorder in the given source file to a Mongo bulk entry
var count = 0;
data.JDBOR.DisorderList.Disorder.forEach(function(disorder) {
var disease = {
ORDOID: disorder.OrphaNumber,
FullName: disorder.Name.$t,
Type: disorder.DisorderType.Name.$t,
Active: "Yes"
};
// Build array of synonyms
if (disorder.SynonymList.count > 1) {
// More than one synonym; pull each array item
disease.Synonym = disorder.SynonymList.Synonym.map(function(synonym) {
return synonym.$t;
});
} else if (disorder.SynonymList.count === 1) {
// Only one synonym; convert single object to single-item array
disease.Synonym = [disorder.SynonymList.Synonym.$t];
}
// Build array of OMIMID
if (disorder.ExternalReferenceList.count > 1) {
// More than one external reference; pull each array item with a source of OMIM
var omimids = _(disorder.ExternalReferenceList.ExternalReference).filter(function(ref) {
return ref.Source === 'OMIM';
}).map(function(ref) {
return ref.Reference;
});
if (omimids && omimids.length) {
disease.OMIMID = omimids;
}
} else if (disorder.ExternalReferenceList.count === 1) {
// Only one external reference; convert item to array if source is OMIM
if (disorder.ExternalReferenceList.ExternalReference.Source === 'OMIM') {
disease.OMIMID = [disorder.ExternalReferenceList.ExternalReference.Reference];
}
}
bulk.insert(disease);
count++;
});
// Delete all existing entries in the collection if requested
var wait;
if (opts.replace) {
wait = coll.deleteManyAsync({});
} else {
wait = Promise.resolve(undefined);
}
// Write the disease data to the database
wait.then(function() {
// We now have all the JSON data into a bulk entry. Write it to the DB
bulk.executeAsync().then(function() {
db.close();
if (opts.replace) {
console.log('Wrote %d entries to the %s collection in the %s database.', count, opts.collection, opts.db);
console.log('Existing collection was overwritten.');
} else {
console.log('Appended %d entries to the %s collection in the %s database.', count, opts.collection, opts.db);
}
}).catch(function(e) {
console.error('Error writing to database: ' + e);
});
});
}).catch(function(e) {
console.error('Error opening database: ' + e);
});