-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape.js
208 lines (177 loc) · 7.67 KB
/
scrape.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
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
// load module dependencies
const fs = require('fs');
const axios = require('axios');
const fasta = require('./fasta.js');
// Add debugger
// eval(pry.it) to pause execution
// https://github.com/blainesch/pry.js
const pry = require('pryjs');
let debug = true;
let show_data = false;
// start script
initialize();
function initialize() {
scrapeGeolocations()
.then(results => {
fs.writeFileSync(process.argv[3], JSON.stringify(results, null, " "));
if (debug == true) { console.log('Completed'); }
process.exit();
});
}
async function scrapeGeolocations() {
if (debug == true) { console.log('scrapeGeolocations'); }
try {
// get source and destination from command line arguments
let srcFilePath = process.argv[2];
let destFilePath = process.argv[3];
// if there is no source or destination file paths provided, error and exit
if(!srcFilePath || !destFilePath){
console.log('Error: Missing Source File Path and Destination File Path Parameters.\nExample:');
console.log('node geolocate ./source.fas ./destination.json');
process.exit();
}
// convert source file to JSON
let srcJSONArray = fasta(srcFilePath);
if (debug == true) { console.log('converted source file'); }
// scrape from JSON
let scrapedRecords = await scrapeRecords(srcJSONArray);
if (debug == true) { console.log('scraped records'); }
return scrapedRecords;
} catch(error) {
if (debug == true) { console.log('scrapeGeolocations failed try'); }
console.log(error);
process.exit();
}
}
async function scrapeRecords(records) {
if (debug == true) { console.log('scrapeRecords'); }
try {
let resultArray = [];
for(let i = 0; i < records.length; i++){
let scrapeURL = `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&id=${records[i].assession}&rettype=json`;
if (debug == true) { console.log('scrapeURL ' + scrapeURL); }
if (debug == true) { console.log(`scraping ${i + 1}/${records.length} - ${records[i].name}`); }
let scrapeResponse = await scrapeNCBI(scrapeURL);
if (debug == true) { console.log('scrapeResponse ' + scrapeResponse); }
resultArray.push(scrapeResponse);
}
return resultArray;
} catch(error) {
console.log('scrapeRecoreds error: ');
console.log(error);
process.exit();
}
}
async function scrapeNCBI(url) {
if (debug == true) { console.log('scrapeNCBI url: ' + url); }
try {
let response = await axios.get(url);
if (debug == true && show_data == true) { console.log('response: ' + response.data); }
let data = await convertXMLToJSON(response.data);
if (debug == true && show_data) { console.log('data: ' + data); }
return data;
} catch(error) {
console.log('scrapeNCBI error: ');
console.log(error);
process.exit();
}
}
async function convertXMLToJSON(xml) {
if (debug == true) { console.log('convertXMLToJSON'); }
if (debug == true && show_data == true) { console.log('xml: ' + xml); }
let assessionNo = '';
let version = '';
let description = '';
let lat = '';
let lng = '';
let assessionObj = {};
let record = {};
try {
if (debug == true) { console.log('inside try'); }
const convert = require('xml-to-json-promise');
let convertedData = await convert.xmlDataToJSON(xml);
let isSeq = convertedData['Bioseq-set']['Bioseq-set_seq-set'][0]['Seq-entry'][0]['Seq-entry_seq'] ? true : false;
if (debug == true) { console.log('isSeq' + isSeq); }
if(isSeq){
if (debug == true) { console.log('obj is a seq'); }
record = convertedData['Bioseq-set']['Bioseq-set_seq-set'][0]['Seq-entry'][0]['Seq-entry_seq'][0]['Bioseq'][0];
if (debug == true) { console.log('record: ' + record); }
//console.log('hmm : ' + record['Bioseq_id'][0]['Seq-id'][0]['Seq-id_genbank'][0]['Textseq-id'][0]);
if (record['Bioseq_id'][0]['Seq-id'][0]['Seq-id_genbank'][0]['Textseq-id'][0] != null){
assessionObj = record['Bioseq_id'][0]['Seq-id'][0]['Seq-id_genbank'][0]['Textseq-id'][0];
}
console.log('assesionObj: ' + assessionObj);
console.log('assesionObj Textseq-id_accession: ' + assessionObj['Textseq-id_accession'][0]);
if (assessionObj['Textseq-id_accession'][0] != null) {
if (debug == true) { console.log('assessionObj position for assessionNo is not null'); }
if (debug == true) { console.log('data: ' + assessionObj['Textseq-id_accession'][0]); }
//assessionNo = assessionObj['Textseq-id_accession'][0];
if (debug == true) { console.log('assessionNo: ' + assessionNo); }
} else {
if (debug == true) { console.log('assessionObj position for assessionNo is not null'); }
}
if (debug == true) { console.log('after assesion no'); }
//version = assessionObj['Textseq-id_version'][0];
//let latLngString = record['Bioseq_descr'][0]['Seq-descr'][0]['Seqdesc'][1]['Seqdesc_source'][0]['BioSource'][0]['BioSource_subtype'][0]['SubSource'][2]['SubSource_name'][0];
//let latLngStringArray = latLngString.split(" ");
//lat = `${latLngStringArray[0]} ${latLngStringArray[1]}`;
//lng = `${latLngStringArray[2]} ${latLngStringArray[3]}`;
//description = record['Bioseq_descr'][0]['Seq-descr'][0]['Seqdesc'][0]['Seqdesc_title'][0];
//let sequence = record['Bioseq_inst'][0]['Seq-inst'][0]['Seq-inst_seq-data'][0]['Seq-data'][0]['Seq-data_ncbi4na'][0]['NCBI4na'][0];
//let sequenceConverted = sequence.replace(/[1]/g, 'A').replace(/[2]/g, 'C').replace(/[4]/g, 'G').replace(/[8]/g, 'T');
if (debug == true) { console.log('end obj is a seq'); }
} else {
if (debug == true) { console.log('obj is not a seq'); }
let record = convertedData['Bioseq-set']['Bioseq-set_seq-set'][0]['Seq-entry'][0]['Seq-entry_set'][0]['Bioseq-set'][0];
let assessionObj = record['Bioseq-set_seq-set'][0]['Seq-entry'][0]['Seq-entry_seq'][0]['Bioseq'][0]['Bioseq_id'][0]['Seq-id'][0]['Seq-id_genbank'][0]['Textseq-id'][0];
if (assessionObj['Textseq-id_accession'][0] != null) {
assessionNo = assessionObj['Textseq-id_accession'][0];
}
version = assessionObj['Textseq-id_version'][0];
description = record['Bioseq-set_descr'][0]['Seq-descr'][0]['Seqdesc'][0]['Seqdesc_title'][0];
let sequence = record['Bioseq-set_seq-set'][0]['Seq-entry'][0]['Seq-entry_seq'][0]['Bioseq'][0]['Bioseq_inst'][0]['Seq-inst'][0]['Seq-inst_seq-data'][0]['Seq-data'][0]['Seq-data_ncbi2na'][0]['NCBI2na'][0];
}
if (debug == true) { console.log('Before Converting to json'); }
if (debug == true) { console.log('assessionNo: ' + assessionNo); }
if (debug == true) { console.log('version: ' + version); }
if (debug == true) { console.log('description: ' + description); }
if (debug == true) { console.log('lat: ' + lat); }
if (debug == true) { console.log('lng: ' + lng); }
let convertedObj = {
assession: {
no: assessionNo,
version: version
},
description: description,
lat: lat,
lng: lng
};
//let convertedObj = {
// assession: {
// no: assessionNo,
// version: version
// },
// description: description,
// lat: lat,
// lng: lng
//};
if (debug == true) { console.log('After Converting to json'); }
if (debug == true) { console.log('convertedObj' + convertedObj); }
return convertedObj;
} catch(error) {
if (debug == true) { console.log('failed try'); }
if (debug == true) { console.log(error); }
let convertedObj = {
assession: {
no: '',
version: ''
},
description: '',
lat: '',
lng: ''
};
//return convertedObj;
process.exit();
}
if (debug == true) { console.log('end convertXMLToJSON'); }
}