forked from CeciliaOstertag/pathway_comparison_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.java
307 lines (262 loc) · 7.44 KB
/
Query.java
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
package com.github.pathway_comparison_project;
import org.apache.log4j.BasicConfigurator;
import org.biojava.nbio.core.sequence.ProteinSequence;
import org.biojava.nbio.core.sequence.io.FastaReaderHelper;
import com.algosome.eutils.EntrezSearch;
import com.algosome.eutils.EntrezFetch;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.net.*;
import com.algosome.eutils.io.*;
/**
* This class is used to query NCBI Protein DB for enzyme sequences in fasta
* format
*
* @see PathwayComparisonProject
* @see EnzymeFinder
* @see RsdResultsParser
* @see SbmlAnnotator
*
* @author Peter Bock, Guillamaury Debras, Mercia Ngoma-Komb, Cécilia Ostertag,
* Franck Soubes
*/
public class Query {
/*
* Enzyme EntrezSearch id
*/
protected String idstring = "";
/*
* Url used to download the text file
*/
protected String finalUrl = "";
/*
* Enzyme NCBI GI id (called BiggId because the sbml file comes from BiGG database)
*/
protected String enzymeBiggId;
/*
* Enzyme NCBI Protein id
*/
protected String enzymeNcbiId;
/*
* NCBI database to query
*/
protected String dataBase;
/*
* Name to give to the fasta file when downloading
*/
protected String fastaFile;
/**
* Automated query to NCBI Protein database. Uses a Protein GI id to search
* the corresponding sequence, and download the file in fasta format
*
* @param enzymeBiggId
* : Protein GI id corresponding to an enzyme
**/
public Query(String enzymeBiggId) {
this.enzymeBiggId = enzymeBiggId;
dataBase = EntrezSearch.DB_PROTEIN;
}
/**
* Copied from jeutils EntrezSearch class to avoid bugs. Parses out an XML
* value tag out of line using a regular expression.
*
* @param line
* A line of text to parse.
* @param tag
* The XML tag identifier.
* @return A string representation of the text between the given tags, or an
* empty string if nothing was found.
*/
protected String parseTextFromLine(String line, String tag) {
Pattern pattern = Pattern.compile(tag + ">(.+)</" + tag);
Matcher matcher = pattern.matcher(line);
matcher.find();
try {
return matcher.group(1);
} catch (IllegalStateException ise) {
return "";
}
}
/**
* Copied from jeutils EntrezSearch class to avoid bugs. Parses an ID
* identifier out of the give line.
*
* @param line
* A line of text to parse.
* @return A string representation of the ID. This parses any text between
* <Id> and </Id>
* @see com.algosome.eutils.io.InputStreamParser
*/
protected String parseID(String line) {
return parseTextFromLine(line, "Id");
}
/**
* Parses an new hyperlink reference out of the give line, in case the
* document was moved to another url.
*
* @param line
* A line of text to parse.
* @return A string representation of the new url.
*/
protected String parseMove(String line, String tag) {
Pattern pattern = Pattern.compile(tag + ">The document has moved <a href=\"(.+)\">here</a>.</" + tag);
Matcher matcher = pattern.matcher(line);
matcher.find();
try {
return matcher.group(1);
} catch (IllegalStateException ise) {
return "";
}
}
/**
* Execution of the query to NCBI Protein databank.
*
* @param fastaName
* : Name of the downloaded fasta file
**/
public void execute(String fastaName) throws Exception {
// BasicConfigurator.configure();
EntrezSearch search = new EntrezSearch();
search.setDatabase(dataBase);
search.setTerm(enzymeBiggId);
search.setMaxRetrieval(1);
InputStreamParser myInputStreamParser1 = new InputStreamParser() {
public void parseFrom(int start) {
}
public void parseTo(int end) {
}
public void parseInput(InputStream is) throws IOException {
String url = "";
BufferedReader line_reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = line_reader.readLine()) != null) {
if (line.indexOf("<Id>") != -1) {
String id = parseID(line);
if (id != null) {
idstring += id + " ";
}
}
// si l'url a change, on remplace la premiere url par la
// deuxieme
else if (line.indexOf("<p>") != -1) {
url = parseMove(line, "p");
is = new URL(url).openStream();
BufferedReader line_reader2 = new BufferedReader(new InputStreamReader(is));
String line2 = null;
while ((line2 = line_reader2.readLine()) != null) {
if (line2.indexOf("<Id>") != -1) {
String id = parseID(line2);
if (id != null) {
idstring += id + " ";
}
}
}
}
}
}
};
if ((myInputStreamParser1 != null))
search.doQuery(myInputStreamParser1);
else
System.out.println("myInputStreamParser1 NULL!");
if (idstring != "") {
search.setIds(idstring); // on donne directement les ids trouves par
// le
// parseur pour contourner le bug
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
throw new Exception(e);
}
try {
EntrezFetch fetch = new EntrezFetch(search);
fetch.setRetType("fasta");
fetch.setRetMode("text");
InputStreamParser myInputStreamParser2 = new InputStreamParser() {
public void parseFrom(int start) {
}
public void parseTo(int end) {
}
public void parseInput(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
if (line.indexOf("<p>") != -1) {
finalUrl = parseMove(line, "p");
}
}
}
};
if ((myInputStreamParser2 != null))
fetch.doQuery(myInputStreamParser2);
else
System.out.println("myInputStreamParser2 NULL!");
fastaFile = download(finalUrl, fastaName);
System.out.println(fastaFile);
readProtFasta(fastaFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
throw new IllegalArgumentException();
}
}
/**
* Download the file in fasta format
*
* @param url
* : Name of the efetch url used to download the file
* @param fastaName
* : Name of the downloaded fasta file
**/
public String download(String url, String fastaName) {
Runtime runtime = Runtime.getRuntime();
String commande = "wget -O " + fastaName + " " + url;
try {
Process p = runtime.exec(commande);
p.waitFor();
} catch (IOException e) {
System.out.println(e);
} catch (InterruptedException e) {
System.out.println(e);
}
return fastaName;
}
public String getFileName() {
return fastaFile;
}
/**
* Reads a fasta file containing proteic sequences
*
* @param filename
* : name of the fasta file to read from
* @param seqList
* : list of sequences found in the file
**/
public void readProtFasta(String filename) {
LinkedHashMap<String, ProteinSequence> helper;
try {
File file = new File(filename);
helper = FastaReaderHelper.readFastaProteinSequence(file);
for (Entry<String, ProteinSequence> entry : helper.entrySet()) {
enzymeNcbiId = entry.getValue().getAccession().toString();
String[] fields = enzymeNcbiId.split(" ");
enzymeNcbiId = fields[0];
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the corresponding NCBI id
*
* @return enzymeNcbiId : NCBI id
**/
public String getNcbiId() {
return enzymeNcbiId;
}
}