]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/NCBI.pm
removed & from all Perl Modules
[biopieces.git] / code_perl / Maasha / NCBI.pm
1 package Maasha::NCBI;
2
3 # Copyright (C) 2007 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24
25 # Stuff for interacting with NCBI Entrez
26
27
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29
30
31 use strict;
32 use Data::Dumper;
33 use LWP::Simple;
34 use Maasha::Common;
35
36 use vars qw( @ISA @EXPORT );
37
38 @ISA = qw( Exporter );
39
40
41 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
42
43
44 sub get_entry
45 {
46     # Martin A. Hansen, March 2007.
47
48     # connects to the ncbi website and retrieves a genbank record,
49     # which is returned.
50
51     my ( $db,    # database <nucleotide|protein>
52          $id,    # genbank id
53          $type,  # retrieval type <gb|gp>
54        ) = @_;
55
56     # returns string
57
58     my ( $content, @lines, $i, $seq );
59
60     $content = get "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=$db&id=$id&rettype=$type";
61
62     return $content;
63 }
64
65
66 sub get_seq
67 {
68     # Martin A. Hansen, March 2007.
69
70     # connects to the ncbi website and retrieves a genbank record,
71     # from which the sequence is parsed and returned.
72
73     my ( $db,    # database <nucleotide|protein>
74          $id,    # genbank id
75          $type,  # retrieval type <gb|gp>
76        ) = @_;
77
78     # returns string
79
80     my ( $content, @lines, $i, $seq );
81
82     $content = get "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=$db&id=$id&rettype=$type";
83
84     @lines = split "\n", $content;
85
86     $i = 0;
87
88     while ( $lines[ $i ] !~ /^ORIGIN/ ) {
89         $i++
90     }
91
92     $i++;
93
94     while ( $lines[ $i ] !~ /^\/\// )
95     {
96         $lines[ $i ] =~ s/^\s*\d+//;
97
98         $seq .= $lines[ $i ];
99
100         $i++;
101     }
102
103     $seq =~ tr/ //d;
104
105     return $seq;
106 }
107
108
109 sub soft_parse
110 {
111     # Martin A. Hansen, February 2008.
112
113     # !!! NOT USED ANYMORE !!! #
114
115     # Reads in and parses a file in SOFT format.
116
117     my ( $path,   # full path to SOFT file
118        ) = @_;
119
120     # Returns a list.
121
122     my ( $fh, @lines, $i, $c, $num, %key_hash, @fields, %id_hash, $id, $seq, $count, $record, @records, $platform_id, $sample_id, $sample_title );
123
124     $fh = Maasha::Common::read_open( $path );
125
126     @lines = <$fh>;
127
128     close $fh;
129
130     chomp @lines;
131
132     $i = 0;
133
134     $num = 1;
135
136     while ( $i < @lines ) 
137     {
138         if ( $lines[ $i ] =~ /^\^PLATFORM = (.+)/ )
139         {
140             $platform_id = $1;
141         }
142         elsif ( $lines[ $i ] =~ /^!platform_table_begin$/ )
143         {
144             @fields = split "\t", $lines[ $i + 1 ];
145
146             for ( $c = 0; $c < @fields; $c++ ) {
147                 $key_hash{ $fields[ $c ] } = $c;
148             }
149
150             $c = $i + 2;
151
152             while ( $lines[ $c ] !~ /^!platform_table_end$/ )
153             {
154                 @fields = split "\t", $lines[ $c ];
155
156                 $id_hash{ $fields[ $key_hash{ "ID" } ] } = $fields[ $key_hash{ "SEQUENCE" } ];
157             
158                 $c++;
159             }
160
161             $i = $c;
162         }
163         elsif ( $lines[ $i ] =~ /^\^SAMPLE = (.+)/ )
164         {
165             $sample_id = $1;
166         }
167         elsif ( $lines[ $i ] =~ /^!Sample_title = (.+)/ )
168         {
169             $sample_title = $1;
170         }
171         elsif ( $lines[ $i ] =~ /^!sample_table_begin/ )
172         {
173             undef %key_hash;
174
175             @fields = split "\t", $lines[ $i + 1 ];
176
177             for ( $c = 0; $c < @fields; $c++ ) {
178                 $key_hash{ $fields[ $c ] } = $c;
179             }
180
181             $c = $i + 2;
182
183             while ( $lines[ $c ] !~ /^!sample_table_end$/ )
184             {
185                 undef $record;
186
187                 @fields = split "\t", $lines[ $c ];
188     
189                 $id    = $fields[ $key_hash{ "ID_REF" } ];
190                 $seq   = $id_hash{ $id };
191                 $count = $fields[ $key_hash{ "VALUE" } ];
192
193                 $seq =~ tr/./N/;
194
195                 $record->{ "SAMPLE_TITLE" } = $sample_title;
196                 $record->{ "SEQ" }          = $seq;
197                 $record->{ "SEQ_NAME" }     = join( "_", $platform_id, $sample_id, $num, $count );
198             
199                 push @records, $record;
200
201                 $c++;
202                 $num++;
203             }
204
205             $i = $c;
206        
207             $num = 1;
208         }
209
210         $i++;
211     }
212
213     return wantarray ? @records : \@records;
214 }
215
216
217 sub soft_index_file
218 {
219     # Martin A. Hansen, June 2008.
220
221     # Create a index with linenumbers of the different tables
222     # in a soft file. The index is returned.
223
224     my ( $file,   # file to index
225        ) = @_;
226
227     # Returns
228     
229     my ( $fh, $line, $i, $c, @index, $first );
230
231     $fh = Maasha::Common::read_open( $file );
232
233     $first = 1;
234
235     $i = 0;
236     $c = 0;
237
238     while ( $line = <$fh> )
239     {
240         chomp $line;
241
242         if ( $line =~ /^\^/ )
243         {
244             push @index, [ $line, $i ];
245
246             if ( not $first )
247             {
248                 push @{ $index[ $c - 1 ] }, $i - 1;
249             }
250             else
251             {
252                 $first = 0;
253             }
254
255             $c++;
256         }
257     
258         $i++; 
259     }
260
261     push @{ $index[ $c - 1 ] }, $i - 1;
262
263     close $fh;
264
265     return wantarray ? @index : \@index;
266 }
267
268
269 sub soft_get_platform
270 {
271     # Martin A. Hansen, June 2008.
272
273     # Given a filehandle to a SOFT file parses the platform table
274     # which is returned.
275
276     my ( $fh,    # filehandle
277          $beg,   # line number where platform tables begin
278          $end,   # line number where platform tables end
279        ) = @_;
280
281     # Returns hashref
282
283     my ( $line, @lines, $i, $c, @fields, %key_hash, %id_hash );
284
285     $i = 0;
286
287     while ( $line = <$fh> )
288     {
289         chomp $line;
290
291         push @lines, $line if $i >= $beg;
292     
293         last if $i == $end;
294
295         $i++;
296     }
297
298     $i = 0;
299
300     while ( $i < @lines )
301     {
302         if ( $lines[ $i ] =~ /^!platform_table_begin$/ )
303         {
304             @fields = split "\t", $lines[ $i + 1 ];
305
306             for ( $c = 0; $c < @fields; $c++ ) {
307                 $key_hash{ $fields[ $c ] } = $c;
308             }
309
310             $c = $i + 2;
311
312             while ( $lines[ $c ] !~ /^!platform_table_end$/ )
313             {
314                 @fields = split "\t", $lines[ $c ];
315
316                 $id_hash{ $fields[ $key_hash{ "ID" } ] } = $fields[ $key_hash{ "SEQUENCE" } ];
317             
318                 $c++;
319             }
320
321             $i = $c;
322         }
323
324         $i++;
325     }
326
327     return wantarray ? %id_hash : \%id_hash;
328 }
329
330
331 sub soft_get_sample
332 {
333     # Martin A. Hansen, June 2008.
334
335     # Given a filehandle to a SOFT file parses the platform table
336     # which is returned.
337
338     my ( $fh,         # filehandle
339          $plat_table,  # hashref with platform tables
340          $beg,        # line number where sample table begin
341          $end,        # line number where sample table end
342        ) = @_;
343
344     # Returns hashref
345
346     my ( $line, @lines, $i, $c, $platform_id, @fields, %key_hash, $num, $sample_id, $sample_title, $id, $seq, $count, @records, $record );
347
348     $i = 0;
349
350     while ( $line = <$fh> )
351     {
352         chomp $line;
353
354         push @lines, $line if $i >= $beg;
355     
356         last if $i == $end;
357
358         $i++;
359     }
360
361     $i = 0;
362
363     $num = 1;
364
365     while ( $i < @lines ) 
366     {
367         if ( $lines[ $i ] =~ /^\^SAMPLE = (.+)/ )
368         {
369             $sample_id = $1;
370         }
371         elsif ( $lines[ $i ] =~ /!Sample_platform_id = (.+)/ )
372         {
373             $platform_id = $1;
374         }
375         elsif ( $lines[ $i ] =~ /^!Sample_title = (.+)/ )
376         {
377             $sample_title = $1;
378         }
379         elsif ( $lines[ $i ] =~ /^!sample_table_begin/ )
380         {
381             undef %key_hash;
382
383             @fields = split "\t", $lines[ $i + 1 ];
384
385             for ( $c = 0; $c < @fields; $c++ ) {
386                 $key_hash{ $fields[ $c ] } = $c;
387             }
388
389             $c = $i + 2;
390
391             while ( $lines[ $c ] !~ /^!sample_table_end$/ )
392             {
393                 undef $record;
394
395                 @fields = split "\t", $lines[ $c ];
396     
397                 $id    = $fields[ $key_hash{ "ID_REF" } ];
398                 $seq   = $plat_table->{ $id };
399                 $count = $fields[ $key_hash{ "VALUE" } ];
400
401                 $seq =~ tr/./N/;
402
403                 $record->{ "SAMPLE_TITLE" } = $sample_title;
404                 $record->{ "SEQ" }          = $seq;
405                 $record->{ "SEQ_NAME" }     = join( "_", $platform_id, $sample_id, $num, $count );
406             
407                 push @records, $record;
408
409                 $c++;
410                 $num++;
411             }
412
413             $i = $c;
414        
415             $num = 1;
416         }
417
418         $i++;
419     }
420
421     return wantarray ? @records : \@records;
422 }
423
424
425 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
426
427
428 __END__