Mercurial > repos > ktnyt > gembassy
comparison GEMBASSY-1.0.3/include/gfile.c @ 0:8300eb051bea draft
Initial upload
author | ktnyt |
---|---|
date | Fri, 26 Jun 2015 05:19:29 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8300eb051bea |
---|---|
1 /****************************************************************************** | |
2 ** @source GEMBASSY file routines | |
3 ** | |
4 ** @version 1.0 | |
5 ** @modified December 27 2012 Hidetoshi Itaya Created this file | |
6 ** @@ | |
7 ** | |
8 ** This library is free software; you can redistribute it and/or | |
9 ** modify it under the terms of the GNU Library General Public | |
10 ** License as published by the Free Software Foundation; either | |
11 ** version 2 of the License, or (at your option) any later version. | |
12 ** | |
13 ** This library is distributed in the hope that it will be useful, | |
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 ** Library General Public License for more details. | |
17 ** | |
18 ** You should have received a copy of the GNU Library General Public | |
19 ** License along with this library; if not, write to the | |
20 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 ** Boston, MA 02111-1307, USA. | |
22 ******************************************************************************/ | |
23 | |
24 #include "gfile.h" | |
25 | |
26 | |
27 | |
28 | |
29 /* @func gValID *************************************************************** | |
30 ** | |
31 ** Checks if an input string is a valid ID | |
32 ** | |
33 ** @param id [r] [AjPStr] ID to check | |
34 ** @return [AjBool] | |
35 ** @@ | |
36 ******************************************************************************/ | |
37 | |
38 ajint gValID(AjPStr id){ | |
39 AjPFilebuff buff = NULL; | |
40 AjPStr url = NULL; | |
41 AjPStr line = NULL; | |
42 AjPRegexp pval = NULL; | |
43 | |
44 url = ajStrNewC("http://web.sfc.keio.ac.jp/~t11080hi/valID/valID.cgi?id="); | |
45 url = ajStrNew(); | |
46 ajFmtPrintS(&url, "http://rest.g-language.org/%S", id); | |
47 | |
48 //ajStrAppendS(&url, id); | |
49 | |
50 if(!gFilebuffURLS(url, &buff)) { | |
51 return ajFalse; | |
52 } | |
53 | |
54 return ajTrue; | |
55 | |
56 ajBuffreadLine(buff, &line); | |
57 | |
58 pval = ajRegCompC("^0"); | |
59 | |
60 if(ajRegExec(pval, line)) | |
61 return ajFalse; | |
62 | |
63 return ajTrue; | |
64 } | |
65 | |
66 | |
67 | |
68 | |
69 /* @func gStrAppendURLS ******************************************************* | |
70 ** | |
71 ** Downloads file from a specified URL and writes to given output file | |
72 ** | |
73 ** @param [r] url [AjPStr] URL to download file from | |
74 ** @param [r] string [AjPStr] String to write into | |
75 ** @return [AjBool] | |
76 ** @@ | |
77 ******************************************************************************/ | |
78 | |
79 AjBool gStrAppendURLS(AjPStr url, AjPStr* string){ | |
80 AjPFilebuff buff = NULL; | |
81 AjPStr file = NULL; | |
82 AjPStr line = NULL; | |
83 | |
84 if(!*string) | |
85 *string = ajStrNew(); | |
86 | |
87 if(!gFilebuffURLS(url, &buff)) | |
88 return ajFalse; | |
89 | |
90 while(ajBuffreadLine(buff, &line)){ | |
91 ajStrAppendS(string, line); | |
92 } | |
93 | |
94 ajFilebuffDel(&buff); | |
95 | |
96 return ajTrue; | |
97 } | |
98 | |
99 | |
100 | |
101 | |
102 /* @func gStrAppendURLC ******************************************************* | |
103 ** | |
104 ** Downloads file from a specified URL and writes to given output file | |
105 ** | |
106 ** @param [r] url [char*] URL to download file from | |
107 ** @param [r] string [AjPStr] String to write into | |
108 ** @return [AjBool] | |
109 ** @@ | |
110 ******************************************************************************/ | |
111 | |
112 AjBool gStrAppendURLC(char* url, AjPStr* string){ | |
113 if(!gStrAppendURLS(ajStrNewC(url), string)) | |
114 { | |
115 return ajFalse; | |
116 } | |
117 | |
118 return ajTrue; | |
119 } | |
120 | |
121 | |
122 | |
123 | |
124 /* @func gFileOutURLS ********************************************************* | |
125 ** | |
126 ** Downloads file from a specified URL and writes to given output file | |
127 ** | |
128 ** @param [r] url [AjPStr] URL to download file from | |
129 ** @param [r] outf [AjPFile] File object to write into | |
130 ** @return [AjBool] | |
131 ** @@ | |
132 ******************************************************************************/ | |
133 | |
134 AjBool gFileOutURLS(AjPStr url, AjPFile* outf){ | |
135 AjPFilebuff buff = NULL; | |
136 AjPStr file = NULL; | |
137 AjPStr line = NULL; | |
138 | |
139 if(!gFilebuffURLS(url, &buff)) | |
140 return ajFalse; | |
141 | |
142 while(ajBuffreadLine(buff, &line)){ | |
143 ajWriteline(*outf, line); | |
144 } | |
145 | |
146 ajFilebuffDel(&buff); | |
147 | |
148 return ajTrue; | |
149 } | |
150 | |
151 | |
152 | |
153 | |
154 /* @func gFileOutURLC ********************************************************* | |
155 ** | |
156 ** Downloads file from a specified URL and writes to given output file | |
157 ** | |
158 ** @param [r] url [char*] URL to download file from | |
159 ** @param [r] outf [AjPFile] File object to write into | |
160 ** @return [AjBool] | |
161 ** @@ | |
162 ******************************************************************************/ | |
163 | |
164 AjBool gFileOutURLC(char* url, AjPFile* outf){ | |
165 if(!gFileOutURLS(ajStrNewC(url), outf)) | |
166 { | |
167 return ajFalse; | |
168 } | |
169 | |
170 return ajTrue; | |
171 } | |
172 | |
173 | |
174 | |
175 | |
176 /* @func gFilebuffURLS ******************************************************** | |
177 ** | |
178 ** Downloads file from a specified URL and inputs in file buffer | |
179 ** | |
180 ** @param [r] url [AjPStr] URL to download file from | |
181 ** @param [r] buff [AjPFilebuff] File buffer to set | |
182 ** @return [AjBool] | |
183 ** @@ | |
184 ******************************************************************************/ | |
185 | |
186 AjBool gFilebuffURLS(AjPStr url, AjPFilebuff* buff){ | |
187 AjPStr line = NULL; | |
188 AjPStr host = NULL; | |
189 AjPStr path = NULL; | |
190 ajint port = 80; | |
191 | |
192 ajHttpUrlDeconstruct(url, &port, &host, &path); | |
193 | |
194 *buff = ajHttpRead(NULL, NULL, NULL, host, port, path); | |
195 | |
196 if(!*buff) | |
197 return ajFalse; | |
198 | |
199 ajFilebuffHtmlNoheader(*buff); | |
200 | |
201 return ajTrue; | |
202 } | |
203 | |
204 | |
205 | |
206 | |
207 /* @func gFilebuffURLC ******************************************************** | |
208 ** | |
209 ** Downloads file from a specified URL and inputs in file buffer | |
210 ** | |
211 ** @param [r] url [char*] URL to download file from | |
212 ** @param [r] buff [AjPFilebuff] File buffer to set | |
213 ** @return [AjBool] | |
214 ** @@ | |
215 ******************************************************************************/ | |
216 | |
217 AjBool gFilebuffURLC(char* url, AjPFilebuff* buff){ | |
218 gFilebuffURLS(ajStrNewC(url), buff); | |
219 | |
220 if(!*buff) | |
221 return ajFalse; | |
222 | |
223 return ajTrue; | |
224 } | |
225 | |
226 | |
227 | |
228 | |
229 /* @func gAssignUniqueName **************************************************** | |
230 ** | |
231 ** Creates a unique filename | |
232 ** | |
233 ** @return [AjPStr] the unique filename | |
234 ** @@ | |
235 ******************************************************************************/ | |
236 | |
237 void gAssignUniqueName(AjPStr *string) { | |
238 static char ext[2] = "A"; | |
239 | |
240 ajFmtPrintS(string, "%08d%s", getpid(), ext); | |
241 | |
242 if( ++ext[0] > 'Z' ) { | |
243 ext[0] = 'A'; | |
244 } | |
245 } | |
246 | |
247 | |
248 | |
249 | |
250 /* @func gCreateUniqueName **************************************************** | |
251 ** | |
252 ** Returns a unique filename | |
253 ** | |
254 ** @return [AjPStr] the unique filename | |
255 ** @@ | |
256 ******************************************************************************/ | |
257 | |
258 AjPStr gCreateUniqueName() { | |
259 AjPStr string; | |
260 | |
261 gAssignUniqueName(&string); | |
262 | |
263 return string; | |
264 } | |
265 | |
266 | |
267 | |
268 | |
269 /* @func gFormatGenbank ******************************************************* | |
270 ** | |
271 ** Creates a genbank format string with sequence and features | |
272 ** | |
273 ** @param [r] seq [AjPSeq] Sequence object to write | |
274 ** @param [r] inseq [AjPStr] String to write to | |
275 ** @return [AjBool] | |
276 ** @@ | |
277 ******************************************************************************/ | |
278 | |
279 AjBool gFormatGenbank(AjPSeq seq, AjPStr *inseq){ | |
280 AjPSeqout seqout = NULL; | |
281 AjPFeattabOut featout = NULL; | |
282 AjPFeattable feat = NULL; | |
283 AjPStr seqline = NULL; | |
284 AjPStr featline = NULL; | |
285 AjPFile seqfile = NULL; | |
286 AjPFile featfile = NULL; | |
287 AjPStr filename = NULL; | |
288 AjBool hasfeats = ajTrue; | |
289 | |
290 gAssignUniqueName(&filename); | |
291 feat = ajSeqGetFeatCopy(seq); | |
292 | |
293 if(!feat) { | |
294 hasfeats = ajFalse; | |
295 } | |
296 | |
297 seqout = ajSeqoutNew(); | |
298 | |
299 if(!ajSeqoutOpenFilename(seqout,filename)) | |
300 embExitBad(); | |
301 | |
302 ajSeqoutSetFormatS(seqout,ajStrNewC("genbank")); | |
303 ajSeqoutWriteSeq(seqout,seq); | |
304 ajSeqoutClose(seqout); | |
305 ajSeqoutDel(&seqout); | |
306 | |
307 seqfile = ajFileNewInNameS(filename); | |
308 ajSysFileUnlinkS(filename); | |
309 | |
310 if(hasfeats) { | |
311 featout = ajFeattabOutNew(); | |
312 | |
313 if(!ajFeattabOutOpen(featout,filename)) | |
314 return ajFalse; | |
315 | |
316 ajFeattableWriteGenbank(featout,feat); | |
317 | |
318 ajFeattableDel(&feat); | |
319 //ajFeattabOutDel(&featout); | |
320 ajFileClose(&(featout->Handle)); | |
321 | |
322 featfile = ajFileNewInNameS(filename); | |
323 ajSysFileUnlinkS(filename); | |
324 } | |
325 | |
326 while(ajReadline(seqfile,&seqline)){ | |
327 if(hasfeats && ajStrMatchC(seqline,"ORIGIN\n")){ | |
328 while(ajReadline(featfile,&featline)){ | |
329 ajStrAppendS(inseq, featline); | |
330 } | |
331 } | |
332 ajStrAppendS(inseq, seqline); | |
333 } | |
334 | |
335 ajStrDel(&seqline); | |
336 ajStrDel(&featline); | |
337 ajStrDel(&filename); | |
338 ajFileClose(&seqfile); | |
339 ajFileClose(&featfile); | |
340 | |
341 return hasfeats; | |
342 } | |
343 | |
344 | |
345 | |
346 | |
347 /* @func gGetFileContent ****************************************************** | |
348 ** | |
349 ** Reads file content and sets it to string pointer | |
350 ** | |
351 ** @param [r] content [AjPSeq] String to write to | |
352 ** @param [r] filename [AjPSeq] Filename to open | |
353 ** @return [AjBool] | |
354 ** @@ | |
355 ******************************************************************************/ | |
356 | |
357 AjBool gGetFileContent(AjPStr* content, AjPStr filename){ | |
358 AjPFile file = NULL; | |
359 AjPStr line = NULL; | |
360 | |
361 if((file = ajFileNewInNameS(filename)) == NULL) | |
362 return ajFalse; | |
363 | |
364 while(ajReadline(file, &line)) | |
365 ajStrAppendS(content, line); | |
366 | |
367 if(file) | |
368 ajFileClose(&file); | |
369 | |
370 ajSysFileUnlinkS(filename); | |
371 | |
372 return ajTrue; | |
373 } | |
374 | |
375 | |
376 | |
377 | |
378 /* @func gtaiFileOutURLS ****************************************************** | |
379 ** | |
380 ** Downloads file from a specified URL and inputs in file buffer | |
381 ** | |
382 ** @param [r] url [AjPStr] URL to download file from | |
383 ** @param [r] buff [AjPFilebuff] File buffer to set | |
384 ** @return [AjBool] | |
385 ** @@ | |
386 ******************************************************************************/ | |
387 | |
388 AjBool gtaiFileOutURLS(AjPStr url, AjPFile* outf, AjBool tai){ | |
389 if(tai) | |
390 { | |
391 CURL *curl; | |
392 CURLcode curl_res; | |
393 | |
394 Memory *mem = malloc(sizeof(Memory*)); | |
395 | |
396 mem->size = 0; | |
397 mem->memory = NULL; | |
398 | |
399 curl_global_init(CURL_GLOBAL_ALL); | |
400 | |
401 curl = curl_easy_init(); | |
402 | |
403 if(curl) | |
404 { | |
405 curl_easy_setopt(curl, CURLOPT_URL, ajCharNewS(url)); | |
406 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); | |
407 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write); | |
408 curl_easy_setopt(curl, CURLOPT_WRITEDATA, mem); | |
409 } | |
410 | |
411 curl_res = curl_easy_perform(curl); | |
412 | |
413 if(CURLE_OK == curl_res) | |
414 { | |
415 char* redir; | |
416 curl_res = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &redir); | |
417 | |
418 if((CURLE_OK == curl_res) && redir) { | |
419 ajStrAssignC(&url, redir); | |
420 ajStrExchangeCC(&url, "cai.csv", "tai.csv"); | |
421 } | |
422 } | |
423 | |
424 free(mem); | |
425 curl_easy_cleanup(curl); | |
426 curl_global_cleanup(); | |
427 } | |
428 | |
429 return gFileOutURLS(url, outf); | |
430 } |