FAST/Bio/SeqIO/entrezgene.pm untested

filter: Statementstmt 0.0% 0 / 657 Branchbran 0.0% 0 / 240 Conditioncond 0.0% 0 / 126 MC/DCmcdc 0.0% 0 / 76 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 1150 CC 121 SCAR 58.8
CC
121
Cov
0%
next_seq
1056.0
_process_comments
600.0
_process_all_comments
210.0
CRAP 358.9
1 # BioPerl module for FAST::Bio::SeqIO::entrezgene
2 #
3 # You may distribute this module under the same terms as perl itself
4 #
5 # POD documentation - main docs before the code
6  
7 =head1 NAME
8  
9 FAST::Bio::SeqIO::entrezgene - Entrez Gene ASN1 parser
10  
11 =head1 SYNOPSIS
12  
13 use FAST::Bio::SeqIO;
14  
15 # don't instantiate directly - instead do
16 my $seqio = FAST::Bio::SeqIO->new(-format => 'entrezgene',
17 -file => $file);
18 my $gene = $seqio->next_seq;
19  
20 =head1 DESCRIPTION
21  
22 This is EntrezGene ASN bioperl parser. It is built on top of
23 L<FAST::Bio::ASN1::EntrezGene>, a low level ASN parser built by Mingyi Liu
24 (L<http://sourceforge.net/projects/egparser>). The easiest way to
25 use it is shown above.
26  
27 You will get most of the Entrez Gene annotation such as gene symbol,
28 gene name and description, accession numbers associated
29 with the gene, etc. Almost all of these are given as L<FAST::Bio::AnnotationI> objects.
30  
31 If you need all the data do:
32  
33 my $seqio = FAST::Bio::SeqIO->new(-format => 'entrezgene',
34 -file => $file,
35 -debug => 'on' );
36 my ($gene,$genestructure,$uncaptured) = $seqio->next_seq;
37  
38 The second variable returned, C<$genestructure>, is a L<FAST::Bio::Cluster::SequenceFamily>
39 object. It contains all Refseqs and the genomic contigs that are associated
40 with the particular gene. The third variable, C<$uncaptured>, is a reference
41 to a plain array.
42  
43 You can also modify the output to allow back compatibility with the old
44 LocusLink parser:
45  
46 my $seqio = FAST::Bio::SeqIO->new(-format => 'entrezgene',
47 -file => $file,
48 -locuslink => 'convert');
49  
50 The C<-debug> and C<-locuslink> options slow down the parser.
51  
52 Example code which looks for ontology terms:
53  
54 my $eio = new FAST::Bio::SeqIO(-file => $file,
55 -format => 'entrezgene',
56 -service_record => 'yes');
57  
58 while (my $seq = $eio->next_seq) {
59 my $gid = $seq->accession_number;
60 foreach my $ot ($ann->get_Annotations('OntologyTerm')) {
61 next if ($ot->term->authority eq 'STS marker'); # No STS markers
62 my $evid = $ot->comment;
63 $evid =~ s/evidence: //i;
64 my @ref = $ot->term->get_references;
65 my $id = $ot->identifier;
66 my $fid = 'GO:' . sprintf("%07u",$id);
67 print join("\t",$gid, $ot->ontology->name, $ot->name, $evid,
68 $fid, @ref?$ref[0]->medline:''), "\n";
69 }
70 }
71  
72 =head1 FEEDBACK
73  
74 =head2 Mailing Lists
75  
76 User feedback is an integral part of the evolution of this and other
77 Bioperl modules. Send your comments and suggestions preferably to
78 the Bioperl mailing list. Your participation is much appreciated.
79  
80 bioperl-l@bioperl.org - General discussion
81 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
82  
83 =head2 Support
84  
85 Please direct usage questions or support issues to the mailing list:
86  
87 I<bioperl-l@bioperl.org>
88  
89 rather than to the module maintainer directly. Many experienced and
90 reponsive experts will be able look at the problem and quickly
91 address it. Please include a thorough description of the problem
92 with code and data examples if at all possible.
93  
94 =head2 Reporting Bugs
95  
96 Report bugs to the Bioperl bug tracking system to help us keep track
97 of the bugs and their resolution. Bug reports can be submitted via
98 the web:
99  
100 https://redmine.open-bio.org/projects/bioperl/
101  
102 =head1 AUTHOR - Stefan Kirov
103  
104 Email skirov at utk.edu
105  
106 =head1 CONTRIBUTORS
107  
108 Hilmar Lapp, hlapp at gmx.net
109  
110 =head1 APPENDIX
111  
112 This parser is based on FAST::Bio::ASN1::EntrezGene module.
113  
114 The rest of the documentation details each of the object methods.
115 Internal methods are usually preceded with a _
116  
117 =cut
118  
119 package FAST::Bio::SeqIO::entrezgene;
120  
121 use strict;
122 use FAST::Bio::ASN1::EntrezGene;
123 use FAST::Bio::Seq;
124 use FAST::Bio::Species;
125 use FAST::Bio::Annotation::SimpleValue;
126 use FAST::Bio::Annotation::DBLink;
127 use FAST::Bio::Annotation::Comment;
128 use FAST::Bio::SeqFeature::Generic;
129 use FAST::Bio::Annotation::Reference;
130 use FAST::Bio::SeqFeature::Gene::Exon;
131 use FAST::Bio::SeqFeature::Gene::Transcript;
132 use FAST::Bio::SeqFeature::Gene::GeneStructure;
133 use FAST::Bio::Cluster::SequenceFamily;
134  
135 #use FAST::Bio::Ontology::Ontology; Relationships.... later
136 use FAST::Bio::Ontology::Term;
137 use FAST::Bio::Annotation::OntologyTerm;
138 use Data::Dumper;
139 use base qw(FAST::Bio::SeqIO);
140  
141 %main::eg_to_ll = (
142 'Official Full Name' => 'OFFICIAL_GENE_NAME',
143 'chromosome' => 'CHR',
144 'cyto' => 'MAP',
145 'Official Symbol' => 'OFFICIAL_SYMBOL'
146 );
147 @main::egonly = keys %main::eg_to_ll;
148  
149 # We define $xval and some other variables so we don't have
150 # to pass them as arguments
151 my ( $seq, $ann, $xval, %seqcollection, $buf );
152  
153 sub _initialize {
154 my ( $self, @args ) = @_;
155 $self->SUPER::_initialize(@args);
156 my %param = @args;
157 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
158 $self->{_debug} = $param{-debug} || 'off';
159 $self->{_locuslink} = $param{-locuslink} || 'no';
160 $self->{_service_record} = $param{-service_record} || 'no';
161 $self->{_parser} = FAST::Bio::ASN1::EntrezGene->new( file => $param{-file} );
162  
163 #Instantiate the low level parser here (it is -file in Bioperl
164 #-should tell M.)
165 #$self->{_parser}->next_seq; #First empty record- bug in FAST::Bio::ASN::Parser
166 }
167  
168 sub next_seq {
169 my $self = shift;
170 my $value = $self->{_parser}->next_seq(1);
171  
172 # $value contains data structure for the
173 # record being parsed. 2 indicates the recommended
174 # trimming mode of the data structure
175 #I use 1 as I prefer not to descend into size 0 arrays
176 return unless ($value);
177 my $debug = $self->{_debug};
178 $self->{_ann} = FAST::Bio::Annotation::Collection->new();
179 $self->{_currentann} = FAST::Bio::Annotation::Collection->new();
180 my @alluncaptured;
181  
182 # parse the entry
183 #my @keys=keys %{$value}; obsolete
184 $xval = $value->[0];
185  
186 #return unless ($xval->{gene}->{desc} eq 'albumin');
187 #return new FAST::Bio::Seq (-id=>'Generif service record', -seq=>'')
188 # unless ($xval->{'track-info'}{geneid}== 283);
189 return FAST::Bio::Seq->new( -id => 'Generif service record', -seq => '' )
190 if (
191 ( $self->{_service_record} ne 'yes' )
192 && ( $xval->{gene}->{desc} =~
193 /record to support submission of generifs for a gene not in entrez/i
194 )
195 );
196  
197 #Basic data
198 #$xval->{summary}=~s/\n//g;
199 my $seq = FAST::Bio::Seq->new(
200 -display_id => $xval->{gene}{locus},
201 -accession_number => $xval->{'track-info'}{geneid},
202 -desc => $xval->{summary}
203 );
204  
205 #Source data here
206 $self->_add_to_ann( $xval->{'track-info'}->{status}, 'Entrez Gene Status' );
207 my $lineage = $xval->{source}{org}{orgname}{lineage};
208 $lineage =~ s/[\s\n]//g;
209 my ( $comp, @lineage );
210 while ($lineage) {
211 ( $comp, $lineage ) = split( /;/, $lineage, 2 );
212 unshift @lineage, $comp;
213 }
214 unless ( exists( $xval->{source}{org}{orgname}{name}{binomial} ) ) {
215 shift @lineage;
216 my ( $gen, $sp ) = split( /\s/, $xval->{source}{org}{taxname} );
217 if ( ($sp) && ( $sp ne '' ) ) {
218 if ( $gen =~ /plasmid/i ) {
219 $sp = $gen . $sp;
220 }
221 unshift @lineage, $sp;
222 }
223 else {
224 unshift @lineage, 'unknown';
225 }
226 }
227 else {
228 my $sp = $xval->{source}{org}{orgname}{name}{binomial}{species};
229 if ( ($sp) && ( $sp ne '' ) ) {
230 my ( $spc, $strain ) = split( 'sp.', $sp ); #Do we need strain?
231 $spc =~ s/\s//g;
232 if ( ($spc) && ( $spc ne '' ) ) {
233 unshift @lineage, $spc;
234 }
235 else {
236 unshift @lineage, 'unknown';
237 }
238 }
239 else {
240 unshift @lineage, 'unknown';
241 }
242 }
243  
244 #print Dumper($xval->{source}{org});
245 my $ncbiid;
246 if ( ref( $xval->{source}{org}{db} ) eq 'ARRAY' ) {
247 foreach my $taxonomy ( @{ $xval->{source}{org}{db} } ) {
248 if ( lc( $taxonomy->{db} ) eq 'taxon' ) {
249 $ncbiid = $taxonomy->{tag}{id};
250 }
251 else {
252 push @alluncaptured, $taxonomy;
253 }
254 delete $xval->{source}{org}{db};
255 }
256 }
257 $ncbiid = $ncbiid || $xval->{source}{org}{db}{tag}{id};
258 my $s1 = shift @lineage;
259 my $s2 = shift @lineage;
260 my $specie = FAST::Bio::Species->new(
261 -classification => [ $s1, $s2 ],
262 -ncbi_taxid => $ncbiid
263 );
264 $specie->common_name( $xval->{source}{org}{common} );
265 if ( exists( $xval->{source}->{subtype} )
266 && ( $xval->{source}->{subtype} ) )
267 {
268 if ( ref( $xval->{source}->{subtype} ) eq 'ARRAY' ) {
269 foreach my $subtype ( @{ $xval->{source}->{subtype} } ) {
270 $self->_add_to_ann( $subtype->{name}, $subtype->{subtype} );
271 }
272 }
273 else {
274 $self->_add_to_ann(
275 $xval->{source}->{subtype}->{name},
276 $xval->{source}->{subtype}->{subtype}
277 );
278 }
279 }
280  
281 #Synonyms
282 if ( ref( $xval->{gene}->{syn} ) eq 'ARRAY' ) {
283 foreach my $symsyn ( @{ $xval->{gene}->{syn} } ) {
284 $self->_add_to_ann( $symsyn, 'ALIAS_SYMBOL' );
285 }
286 }
287 else {
288 $self->_add_to_ann( $xval->{gene}->{syn}, 'ALIAS_SYMBOL' )
289 if ( $xval->{gene}->{syn} );
290 }
291  
292 #COMMENTS (STS not dealt with yet)
293 if ( exists( $xval->{comments} ) ) {
294 if ( ref( $xval->{comments} ) eq 'ARRAY' ) {
295 for my $i ( 0 .. $#{ $xval->{comments} } ) {
296 $self->{_current} = $xval->{comments}->[$i];
297 push @alluncaptured, $self->_process_all_comments();
298 }
299 }
300 else {
301 $self->{_current} = $xval->{comments};
302 push @alluncaptured, $self->_process_all_comments();
303 }
304 }
305  
306 #Gene
307 if ( exists( $xval->{gene}->{db} ) ) {
308 if ( ref( $xval->{gene}->{db} ) eq 'ARRAY' ) {
309 foreach my $genedb ( @{ $xval->{gene}->{db} } ) {
310 my $id =
311 exists( $genedb->{tag}->{id} )
312 ? $genedb->{tag}->{id}
313 : $genedb->{tag}->{str};
314 $self->_add_to_ann( $id, $genedb->{db} );
315 }
316 }
317 else {
318 my $id =
319 ( $xval->{gene}->{db}->{tag}->{id} )
320 ? $xval->{gene}->{db}->{tag}->{id}
321 : $xval->{gene}->{db}->{tag}->{str};
322 $self->_add_to_ann( $id, $xval->{gene}->{db}->{db} );
323 }
324 $self->_add_to_ann( $xval->{gene}->{'locus-tag'}, 'LOCUS_SYNONYM' );
325 delete $xval->{gene}->{db} unless ( $debug eq 'off' );
326 }
327  
328 #LOCATION To do: uncaptured stuff
329 if ( exists( $xval->{location} ) ) {
330 if ( ref( $xval->{location} ) eq 'ARRAY' ) {
331 foreach my $loc ( @{ $xval->{location} } ) {
332 $self->_add_to_ann( $loc->{'display-str'},
333 $loc->{method}->{'map-type'} );
334 }
335 }
336 else {
337 $self->_add_to_ann(
338 $xval->{location}->{'display-str'},
339 $xval->{location}->{method}->{'map-type'}
340 );
341 }
342 delete $xval->{location} unless ( $debug eq 'off' );
343 }
344  
345 #LOCUS
346 if ( ref( $xval->{locus} ) eq 'ARRAY' ) {
347 foreach my $locus ( @{ $xval->{locus} } ) {
348 $self->{_current} = $locus;
349 push @alluncaptured, $self->_process_locus();
350 }
351 }
352 else {
353 push @alluncaptured, $self->_process_locus( $xval->{locus} );
354 }
355  
356 #Homology
357 my ( $uncapt, $hom, $anchor ) = _process_src( $xval->{homology}->{source} );
358 foreach my $homann (@$hom) {
359 $self->{_ann}->add_Annotation( 'dblink', $homann );
360 }
361 push @alluncaptured, $uncapt;
362  
363 #Index terms
364 if ( ( exists( $xval->{'xtra-index-terms'} ) )
365 && ( $xval->{'xtra-index-terms'} ) )
366 {
367 if ( ref( $xval->{'xtra-index-terms'} ) eq 'ARRAY' ) {
368 foreach my $term ( @{ $xval->{'xtra-index-terms'} } ) {
369 $self->_add_to_ann( $term, 'Index terms' );
370 }
371 }
372 else {
373 $self->_add_to_ann( $xval->{'xtra-index-terms'}, 'Index terms' );
374 }
375 }
376  
377 #PROPERTIES
378 my @prop;
379 if ( exists( $xval->{properties} ) ) {
380 if ( ref( $xval->{properties} ) eq 'ARRAY' ) {
381 foreach my $property ( @{ $xval->{properties} } ) {
382 push @alluncaptured, $self->_process_prop($property);
383 }
384 }
385 else {
386 push @alluncaptured, $self->_process_prop( $xval->{properties} );
387 }
388 }
389 $seq->annotation( $self->{_ann} );
390 $seq->species($specie);
391 my @seqs;
392 foreach my $key ( keys %seqcollection )
393 { #Optimize this, no need to go through hash?
394 push @seqs, @{ $seqcollection{$key} };
395 }
396 my $cluster = FAST::Bio::Cluster::SequenceFamily->new(
397 -family_id => $seq->accession_number,
398 -description => "Entrez Gene " . $seq->accession_number,
399 -members => \@seqs
400 ); #Our EntrezGene object
401 #clean
402 unless ( $debug eq 'off' ) {
403 delete $xval->{homology}->{source};
404 delete( $xval->{summary} );
405 delete( $xval->{'track-info'} );
406 delete( $xval->{gene}{locus} );
407 delete( $xval->{source}{org}{orgname}{lineage} );
408 delete $xval->{source}{org}{orgname}{name}{binomial}{species};
409 delete $xval->{gene}{syn};
410 delete $xval->{source}->{subtype};
411 delete $xval->{comments};
412 delete $xval->{properties};
413 delete $xval->{'xtra-index-terms'};
414 delete $xval->{status};
415 }
416 push @alluncaptured, $xval;
417 undef %seqcollection;
418 $seq->annotation( _backcomp_ll( $self->{_ann} ) )
419 if ( $self->{_locuslink} eq 'convert' ); #Fix this!
420 return
421 wantarray
422 ? ( $seq, $cluster, \@alluncaptured )
423 : $seq; #Hilmar's suggestion
424 }
425  
426 sub _process_refseq {
427 my $self = shift;
428 my $products = shift;
429 my $ns = shift;
430 my $iter = shift;
431 $iter++;
432 my $pid;
433 my ( @uncaptured, @products );
434 if ( ref($products) eq 'ARRAY' ) { @products = @{$products}; }
435 else { push @products, $products; }
436  
437 foreach my $product (@products) {
438 if ( ( ref($product) eq 'ARRAY' ) && ( $#{$product} > -1 ) ) {
439 $self->_process_refseq( $product, $ns, $iter );
440 next;
441 }
442 if (
443 (
444 exists( $product->{products} )
445 && ( !exists( $product->{accession} ) )
446 )
447 )
448 {
449 $self->_process_refseq( $product->{products}, $ns );
450 next;
451 }
452  
453 #if ((exists($product->{products})&&($product->{products}))) {
454 # $self->_process_refseq($product->{products},$ns,$iter);
455 #}
456 if ( ( exists( $product->{seqs}->{whole}->{gi} ) )
457 && ( ref( $product->{seqs}->{whole}->{gi} ) eq 'ARRAY' ) )
458 {
459 $product->{seqs}->{whole}->{gi} =
460 $product->{seqs}->{whole}->{gi}->[0];
461 } #Lose some data
462 if ( ( exists( $product->{seqs}->{whole}->{gi} ) )
463 || ( exists( $product->{accession} ) ) )
464 { #Minimal data required
465 my $cann = FAST::Bio::Annotation::Collection->new();
466 $pid = $product->{accession};
467 my $authority =
468 exists( $product->{type} )
469 ? $product->{type}
470 : $product->{heading};
471 my $nseq = FAST::Bio::Seq->new(
472 -accession_number => $product->{seqs}->{whole}->{gi},
473 -display_id => $product->{accession},
474 -authority => $authority,
475 -namespace => $ns
476 );
477 if ( exists( $product->{source} ) && ( $product->{source} ) ) {
478 if ( ( !defined( $nseq->authority ) )
479 && ( exists( $product->{source}->{src} ) )
480 && ( exists( $product->{source}->{src}->{db} ) ) )
481 {
482 $nseq->authority( $product->{source}->{src}->{db} );
483 }
484 my ( $uncapt, $allann ) = _process_src( $product->{source} );
485 push @uncaptured, $uncapt;
486 delete $product->{source};
487 foreach my $annotation ( @{$allann} ) {
488 $cann->add_Annotation( 'dblink', $annotation );
489 }
490 }
491  
492 delete $product->{seqs}->{whole}->{gi};
493 delete $product->{accession};
494 delete $product->{source};
495 delete $product->{heading};
496 my ( $uncapt, $ann, $cfeat ) =
497 $self->_process_comments( $product->{comment} )
498 if ( exists( $product->{comment} ) );
499 push @uncaptured, $uncapt;
500 foreach my $feat ( @{$cfeat} ) {
501 $nseq->add_SeqFeature($feat);
502 }
503 if ( exists( $product->{products} ) && ( $product->{products} ) ) {
504 my ( $uncapt, $prodid ) =
505 $self->_process_refseq( $product->{products} );
506 push @uncaptured, $uncapt;
507 my $simann = FAST::Bio::Annotation::SimpleValue->new(
508 -value => $prodid,
509 -tagname => 'product'
510 );
511 $cann->add_Annotation($simann);
512 }
513 foreach my $key ( keys %$ann ) {
514 foreach my $val ( @{ $ann->{$key} } ) {
515 $cann->add_Annotation( $key, $val );
516 }
517 }
518 $nseq->annotation($cann);
519 push @{ $seqcollection{seq} }, $nseq;
520 }
521 }
522 undef @products;
523 undef $products;
524  
525 #my $ti2=new Benchmark;
526 # my $td= timediff($ti2, $ti1);
527 # print "\tITER $iter:",timestr($td),"\n";
528 return \@uncaptured, $pid, $seqcollection{seq}, $iter;
529 }
530  
531 sub _process_links {
532 my $self = shift;
533 my $links = shift;
534 my ( @annot, @uncapt );
535 if ( ref($links) eq 'ARRAY' ) {
536 foreach my $link (@$links) {
537 my ( $uncapt, $annot ) = _process_src( $link->{source} )
538 if ( exists( $link->{source} ) );
539 push @uncapt, $uncapt;
540 foreach my $annotation (@$annot) {
541 $self->{_ann}->add_Annotation( 'dblink', $annotation );
542 }
543 }
544 }
545 else {
546 my ( $uncapt, $annot ) = _process_src( $links->{source} )
547 if ( exists( $links->{source} ) );
548 push @uncapt, $uncapt;
549 foreach my $annotation (@$annot) {
550 $self->{_ann}->add_Annotation( 'dblink', $annotation );
551 }
552 }
553 return @uncapt;
554 }
555  
556 sub _add_to_ann { #Highest level only
557 my ( $self, $val, $tag ) = @_;
558  
559 # $val=~s/\n//g;#Low level EG parser leaves this so we take care of them here
560 unless ($tag) {
561 $self->warn( "No tagname for value $val, tag $tag " . $seq->id . "\n" );
562 return;
563 }
564 my $simann =
565 FAST::Bio::Annotation::SimpleValue->new( -value => $val, -tagname => $tag );
566 $self->{_ann}->add_Annotation($simann);
567 }
568  
569 sub _process_comments {
570 my $self = shift;
571 my $prod = shift;
572 my ( %cann, @feat, @uncaptured, @comments, @sfann );
573 if ( ( ref($prod) eq 'HASH' ) && ( exists( $prod->{comment} ) ) ) {
574 $prod = $prod->{comment};
575 }
576 if ( ref($prod) eq 'ARRAY' ) {
577 @comments = @{$prod};
578 }
579 else {
580 push @comments, $prod;
581 }
582 my $i = 0;
583 for my $comm (@comments) { # Each comments is a hash reference
584 $self->throw("Comment not a hash reference")
585 unless ref($comm) eq 'HASH';
586 my ( $desc, $nfeat, $add, @ann, @comm );
587  
588 # next unless (exists($comm->{comment}));#Should be more careful when calling _process_comment:To do
589 my $heading = $comm->{heading} || 'description';
590 if ( !exists( $comm->{comment} ) ) {
591 if ( ( exists( $comm->{type} ) )
592 && ( $comm->{type} )
593 && ( $self->{_current_heading} ) )
594 {
595 $comm->{type} = $self->{_current_heading};
596 }
597 if ( ( exists( $comm->{source} ) )
598 && ( exists( $comm->{type} ) )
599 && ( exists( $comm->{text} ) )
600 && ( $comm->{type} ne 'comment' ) )
601 {
602 my ( $uncapt, $annot, $anchor ) =
603 _process_src( $comm->{source} );
604 my $cann = shift(@$annot);
605 if ( defined $cann ) {
606 $cann->optional_id( $comm->{text} );
607 $cann->authority( $comm->{type} );
608 $cann->version( $comm->{version} );
609 push @sfann, $cann;
610 }
611 }
612 }
613  
614 while (ref($comm) eq 'HASH'
615 && ( exists( $comm->{comment} ) )
616 && ( $comm->{comment} ) )
617 {
618 if ( ( exists( $comm->{source} ) ) && ( $comm->{source} ) ) {
619 my ( $uncapt, $allann, $anchor ) =
620 _process_src( $comm->{source} );
621 if ($allann) {
622 delete $comm->{source};
623 push @uncaptured, $uncapt;
624 foreach my $annotation ( @{$allann} ) {
625 if ( $annotation->{_anchor} ) {
626 $desc .= $annotation->{_anchor} . ' ';
627 }
628 $annotation->optional_id($heading);
629 push @sfann, $annotation;
630 push @{ $cann{'dblink'} }, $annotation;
631 }
632 }
633 }
634  
635 $comm = $comm->{comment};
636  
637 if ( ref($comm) eq 'ARRAY' ) {
638 @comm = @{$comm};
639 }
640 else {
641 push @comm, $comm if ($comm);
642 }
643  
644 foreach my $ccomm (@comm) {
645 next unless ($ccomm);
646 if ( exists( $ccomm->{source} ) ) {
647 my ( $uncapt, $allann, $anchor ) =
648 _process_src( $ccomm->{source} );
649 if ($allann) {
650 @sfann = @{$allann};
651 delete $ccomm->{source};
652 push @uncaptured, $uncapt;
653 }
654 }
655 $ccomm = $ccomm->{comment}
656 if ( exists( $ccomm->{comment} ) ); #Alice in Wonderland???
657 my @loc;
658 if ($ccomm) {
659 if ( ref($ccomm) eq 'ARRAY' ) {
660 @loc = @{$ccomm};
661 }
662 else {
663 push @loc, $ccomm;
664 }
665 }
666 foreach my $loc (@loc) {
667 if ( ( exists( $loc->{text} ) )
668 && ( $loc->{text} =~ /Location/i ) )
669 {
670 my ( $l1, $rest ) = split( /-/, $loc->{text} );
671 $l1 =~ s/\D//g;
672 $rest =~ s/^\s//;
673 my ( $l2, $scorestr ) = split( /\s/, $rest, 2 );
674 my ( $scoresrc, $score ) = split( /:/, $scorestr );
675 $score =~ s/\D//g;
676 my ( %tags, $tag );
677 unless ($l1) {
678 next;
679 }
680 $nfeat = FAST::Bio::SeqFeature::Generic->new(
681 -start => $l1,
682 -end => $l2,
683 -strand => $tags{strand},
684 -source => $loc->{type},
685 -seq_id => $desc,
686 -primary => $heading,
687 -score => $score,
688 -tag => { score_src => $scoresrc }
689 );
690 my $sfeatann = FAST::Bio::Annotation::Collection->new();
691 foreach my $sfann (@sfann) {
692 $sfeatann->add_Annotation( 'dblink', $sfann );
693 }
694 undef @sfann;
695 $nfeat->annotation($sfeatann)
696 ; #Thus the annotation will be available both in the seq and seqfeat?
697 push @feat, $nfeat;
698 delete $loc->{text};
699 delete $loc->{type};
700 }
701 elsif ( exists( $loc->{label} ) ) {
702 my $simann = FAST::Bio::Annotation::SimpleValue->new(
703 -value => $loc->{text},
704 -tagname => $loc->{label}
705 );
706 delete $loc->{text};
707 delete $loc->{label};
708 push @{ $cann{'simple'} }, $simann;
709 push @uncaptured, $loc;
710 }
711 elsif ( exists( $loc->{text} ) ) {
712 my $simann = FAST::Bio::Annotation::SimpleValue->new(
713 -value => $loc->{text},
714 -tagname => $heading
715 );
716 delete $loc->{text};
717 push @{ $cann{'simple'} }, $simann;
718 push @uncaptured, $loc;
719 }
720 }
721 } #Bit clumsy but that's what we get from the low level parser
722 }
723 $i++;
724 }
725 if (@sfann) {
726 push @{ $cann{'dblink'} }, @sfann;
727 } #Annotation that is not location specific, for example phenotype
728 #undef $self->{_current_heading};
729 return \@uncaptured, \%cann, \@feat;
730 }
731  
732 sub _process_src {
733 my $src = shift;
734  
735 #Trick we do because sometimes we have an array ref
736 my ( @ann, $anch, @uncapt );
737 if ( ref($src) eq 'ARRAY' ) {
738 foreach my $msrc (@$src) {
739 my ( $uncapt, $ann, $anchor ) = _process_src($msrc);
740 push @ann, @$ann;
741 push @uncapt, $uncapt;
742 $anch = $anchor;
743 }
744 return \@uncapt, \@ann, $anch;
745 }
746 return unless ( exists( $src->{src}->{tag} ) );
747  
748 #my $t0=new Benchmark
749 my $db = $src->{src}->{db};
750 delete $src->{src}->{db};
751 my $anchor = $src->{anchor} || '';
752 delete $src->{anchor};
753 my $url;
754 if ( exists( $src->{url} ) && ( $src->{url} ) ) {
755 $url = $src->{url};
756 $url =~ s/\n//g;
757 delete $src->{url};
758 }
759 if ( ( exists( $src->{src}->{tag}->{str} ) )
760 && ( $src->{src}->{tag}->{str} ) )
761 {
762 my @sq = split( /[,;]/, $src->{src}->{tag}->{str} );
763 delete $src->{src}->{tag};
764 foreach my $id (@sq) {
765 $id =~ s/\n//g;
766 undef $anchor if ( $anchor eq 'id' );
767 my $simann = FAST::Bio::Annotation::DBLink->new(
768 -database => $db,
769 -primary_id => $id,
770 -authority => $src->{heading}
771 );
772 $simann->url($url) if ($url); #DBLink should have URL!
773 push @ann, $simann;
774 }
775 }
776 else {
777 my $id = $src->{src}->{tag}->{id} || '';
778 delete $src->{src}->{tag};
779 undef $anchor if ( $anchor eq 'id' );
780 $id =~ s/\n//g;
781 my $simann = FAST::Bio::Annotation::DBLink->new(
782 -database => $db,
783 -primary_id => $id,
784 -authority => $src->{heading}
785 );
786 if ($anchor) {
787 $simann->{_anchor} = $anchor;
788 $simann->optional_id($anchor);
789 }
790 $simann->url($url) if ($url); #DBLink should have URL!
791 push @ann, $simann;
792 }
793  
794 #my $t1=new Benchmark;
795 #my $td= timediff($t1, $t0);
796 #print "\t\tSRC:",timestr($td),"\n";
797 return $src, \@ann, $anchor;
798 }
799  
800 sub _add_references {
801 my $self = shift;
802 my $refs = shift;
803 if ( ref($refs) eq 'ARRAY' ) {
804 foreach my $ref (@$refs) {
805 my $refan = FAST::Bio::Annotation::Reference->new(
806 -database => 'Pubmed',
807 -primary_id => $ref
808 );
809 $self->{_ann}->add_Annotation( 'Reference', $refan );
810 }
811 }
812 else {
813 my $refan = FAST::Bio::Annotation::Reference->new(
814 -database => 'Pubmed',
815 -primary_id => $refs
816 );
817 $self->{_ann}->add_Annotation( 'Reference', $refan );
818 }
819 }
820  
821 #Should we do this at all if no seq coord are present?
822 sub _process_locus {
823 my $self = shift;
824 my @uncapt;
825 return $self
826 unless ( exists( $self->{_current}->{accession} )
827 && ( $self->{_current}->{accession} ) );
828 my $gseq = FAST::Bio::Seq->new(
829 -display_id => $self->{_current}->{accession},
830 -version => $self->{_current}->{version},
831 -accession_number => $self->{_current}->{seqs}->{'int'}->{id}->{gi},
832 -authority => $self->{_current}->{type},
833 -namespace => $self->{_current}->{heading}
834 );
835 delete $self->{_current}->{accession};
836 delete $self->{_current}->{version};
837 delete $self->{_current}->{'int'}->{id}->{gi};
838 my ( $start, $end, $strand );
839  
840 if ( exists( $self->{_current}->{seqs}->{'int'}->{from} ) ) {
841 $start = $self->{_current}->{seqs}->{'int'}->{from};
842 delete $self->{_current}->{seqs}->{'int'}->{from};
843  
844 #unless ($start) {print $locus->{seqs}->{'int'}->{from},"\n",$locus,"\n";}
845 $end = $self->{_current}->{seqs}->{'int'}->{to};
846 delete $self->{_current}->{seqs}->{'int'}->{to};
847 delete $self->{_current}->{seqs}->{'int'}->{strand};
848 $strand =
849 $self->{_current}->{seqs}->{'int'}->{strand} eq 'minus' ? -1 : 1
850 if ( exists( $self->{_current}->{seqs}->{'int'}->{strand} ) )
851 ; #1 being default
852 my $nfeat = FAST::Bio::SeqFeature::Generic->new(
853 -start => $start,
854 -end => $end,
855 -strand => $strand,
856 primary => 'gene location'
857 );
858 $gseq->add_SeqFeature($nfeat);
859 }
860 my @products;
861 if ( ref( $self->{_current}->{products} ) eq 'ARRAY' ) {
862 @products = @{ $self->{_current}->{products} };
863 }
864 else {
865 push @products, $self->{_current}->{products};
866 }
867 delete $self->{_current}->{products};
868 my $gstruct = FAST::Bio::SeqFeature::Gene::GeneStructure->new();
869 foreach my $product (@products) {
870 my ( $tr, $uncapt ) =
871 _process_products_coordinates( $product, $start, $end, $strand );
872 $gstruct->add_transcript($tr) if ($tr);
873 undef $tr->{parent}; #Because of a cycleG
874 push @uncapt, $uncapt;
875 }
876 $gseq->add_SeqFeature($gstruct);
877 push @{ $seqcollection{genestructure} }, $gseq;
878 return @uncapt;
879 }
880  
881 =head1 _process_products_coordinates
882  
883 To do:
884  
885 =cut
886  
887 sub _process_products_coordinates {
888 my $coord = shift;
889 my $start = shift
890 || 0; #In case it is not known: should there be an entry at all?
891 my $end = shift || 1;
892 my $strand = shift || 1;
893 my ( @coords, @uncapt );
894 return unless ( exists( $coord->{accession} ) );
895 my $transcript = FAST::Bio::SeqFeature::Gene::Transcript->new(
896 -primary => $coord->{accession}, #Desc is actually non functional...
897 -start => $start,
898 -end => $end,
899 -strand => $strand,
900 -desc => $coord->{type}
901 );
902  
903 if ( ( exists( $coord->{'genomic-coords'}->{mix}->{'int'} ) )
904 || ( exists( $coord->{'genomic-coords'}->{'packed-int'} ) ) )
905 {
906 @coords =
907 exists( $coord->{'genomic-coords'}->{mix}->{'int'} )
908 ? @{ $coord->{'genomic-coords'}->{mix}->{'int'} }
909 : @{ $coord->{'genomic-coords'}->{'packed-int'} };
910 foreach my $exon (@coords) {
911 next unless ( exists( $exon->{from} ) );
912 my $exonobj = FAST::Bio::SeqFeature::Gene::Exon->new(
913 -start => $exon->{from},
914 -end => $exon->{to},
915 -strand => $strand
916 );
917 $transcript->add_exon($exonobj);
918 delete $exon->{from};
919 delete $exon->{to};
920 delete $exon->{strand};
921 push @uncapt, $exon;
922 }
923 }
924 my ( $prot, $uncapt );
925 if ( exists( $coord->{products} ) ) {
926 my ( $prot, $uncapt ) =
927 _process_products_coordinates( $coord->{products},
928 $start, $end, $strand );
929 $transcript->add_SeqFeature($prot);
930 push @uncapt, $uncapt;
931 }
932 return $transcript, \@uncapt;
933 }
934  
935 =head1 _process_prop
936  
937 To do: process GO
938  
939 =cut
940  
941 sub _process_prop {
942 my $self = shift;
943 my $prop = shift;
944 my @uncapt;
945 if ( exists( $prop->{properties} ) ) { #Iterate
946 if ( ref( $prop->{properties} ) eq 'ARRAY' ) {
947 foreach my $propn ( @{ $prop->{properties} } ) {
948 push @uncapt, $self->_process_prop($propn);
949 }
950 }
951 else {
952 push @uncapt, $self->_process_prop( $prop->{properties} );
953 }
954 }
955 unless ( ( exists( $prop->{heading} ) )
956 && ( $prop->{heading} eq 'GeneOntology' ) )
957 {
958 $self->_add_to_ann( $prop->{text}, $prop->{label} )
959 if ( exists( $prop->{text} ) );
960 delete $prop->{text};
961 delete $prop->{label};
962 push @uncapt, $prop;
963 return \@uncapt;
964 }
965  
966 #Will do GO later
967 if ( exists( $prop->{comment} ) ) {
968 push @uncapt, $self->_process_go( $prop->{comment} );
969 }
970 }
971  
972 sub _process_all_comments {
973 my $self = shift;
974 my $product = $self->{_current}; #Better without copying
975 my @alluncaptured;
976 my $heading = $product->{heading} if ( exists( $product->{heading} ) );
977 if ($heading) {
978  
979 #my $tx1=new Benchmark;
980 delete $product->{heading};
981 CLASS: {
982 if ( $heading =~ 'RefSeq Status' )
983 { #IN case NCBI changes slightly the spacing:-)
984 $self->_add_to_ann( $product->{label}, 'RefSeq status' );
985 last CLASS;
986 }
987 if ( $heading =~ 'NCBI Reference Sequences' )
988 { #IN case NCBI changes slightly the spacing:-)
989 if ( ( exists( $product->{comment} ) )
990 && ( !exists( $product->{products} ) ) )
991 {
992 $product->{products} = $product->{comment};
993 }
994  
995 #unless (($product->{products})&&(exists($product->{comment}))) {
996 #if (ref ($product->{comment}) eq 'ARRAY') {
997 # foreach my $pc (@{$product->{comment}}) {
998 # push @{$product->{products}},$pc->{products};
999 # }
1000 #}
1001 #else {
1002 # $product->{products}=exists($product->{comments}->{products})?$product->{comments}->{products}:$product->{comment};
1003 #}
1004 #}
1005 my @uncaptured =
1006 $self->_process_refseq( $product->{products}, 'refseq' );
1007 push @alluncaptured, @uncaptured;
1008 last CLASS;
1009 }
1010 if ( ( $heading =~ 'Related Sequences' )
1011 && ( exists( $product->{products} ) ) )
1012 { #IN case NCBI changes slightly the spacing:-)
1013 my @uncaptured = $self->_process_refseq( $product->{products} );
1014 push @alluncaptured, @uncaptured;
1015 last CLASS;
1016 }
1017 if ( ( $heading =~ 'Additional Links' )
1018 && ( exists( $product->{comment} ) ) )
1019 { #IN case NCBI changes slightly the spacing:-)
1020 push @alluncaptured,
1021 $self->_process_links( $product->{comment} );
1022 last CLASS;
1023 }
1024 if ( $heading =~ 'LocusTagLink' )
1025 { #IN case NCBI changes slightly the spacing:-)
1026 $self->_add_to_ann(
1027 $product->{source}->{src}->{tag}->{id},
1028 $product->{source}->{src}->{db}
1029 );
1030 last CLASS;
1031 }
1032 if ( ( $heading =~ 'Sequence Tagged Sites' )
1033 && ( exists( $product->{comment} ) ) )
1034 { #IN case NCBI changes slightly the spacing:-)
1035 push @alluncaptured, $self->_process_STS( $product->{comment} );
1036 delete $product->{comment};
1037 last CLASS;
1038 }
1039 if ( $heading =~ 'Pathways' ) {
1040 $self->{_current_heading} = 'Pathways';
1041 last CLASS;
1042 }
1043 }
1044  
1045 # my $tx2=new Benchmark;
1046 # my $td= timediff($tx2, $tx1);
1047 #print "\t\t$heading:",timestr($td),"\n";
1048  
1049 }
1050 if ( exists( $product->{type} ) && ( $product->{type} eq 'generif' ) ) {
1051 push @alluncaptured, $self->_process_grif($product);
1052 return @alluncaptured; #Maybe still process the comments?
1053 }
1054 if ( exists( $product->{refs} ) ) {
1055 $self->_add_references( $product->{refs}->{pmid} );
1056 delete $product->{refs}->{pmid};
1057 push @alluncaptured, $product;
1058 }
1059 if ( exists( $product->{comment} ) ) {
1060 my ( $uncapt, $allan, $allfeat ) =
1061 $self->_process_comments( $product->{comment} );
1062 foreach my $key ( keys %$allan ) {
1063 foreach my $val ( @{ $allan->{$key} } ) {
1064 $self->{_ann}->add_Annotation( $key, $val );
1065 }
1066 }
1067 delete $product->{refs}->{comment};
1068 push @alluncaptured, $uncapt;
1069 }
1070  
1071 #if (exists($product->{source})) {
1072 # my ($uncapt,$ann,$anchor)=_process_src($product->{source});
1073 # foreach my $dbl (@$ann) {
1074 # $self->{_ann}->add_Annotation('dblink',$dbl);
1075 # }
1076 #}
1077 return @alluncaptured;
1078 }
1079  
1080 sub _process_STS {
1081 my $self = shift;
1082 my $comment = shift;
1083 my @comm;
1084 push @comm, ( ref($comment) eq 'ARRAY' ) ? @{$comment} : $comment;
1085 foreach my $product (@comm) {
1086 my $sts = FAST::Bio::Ontology::Term->new(
1087 -identifier => $product->{source}->{src}->{tag}->{id},
1088 -name => $product->{source}->{anchor},
1089 -comment => $product->{source}->{'post-text'}
1090 );
1091 $sts->namespace( $product->{source}->{src}->{db} );
1092 $sts->authority('STS marker');
1093 my @alt;
1094 if ( exists( $product->{comment} ) ) {
1095 push @alt,
1096 ( ref( $product->{comment} ) eq 'ARRAY' )
1097 ? @{ $product->{comment} }
1098 : $product->{comment};
1099 foreach my $alt (@alt) {
1100 $sts->add_synonym( $alt->{text} );
1101 }
1102 }
1103 my $annterm = FAST::Bio::Annotation::OntologyTerm->new();
1104 $annterm->term($sts);
1105 $self->{_ann}->add_Annotation( 'OntologyTerm', $annterm );
1106 }
1107 }
1108  
1109 sub _process_go {
1110 my $self = shift;
1111 my $comm = shift;
1112 my @comm;
1113 push @comm, ( ref($comm) eq 'ARRAY' ) ? @{$comm} : $comm;
1114 foreach my $comp (@comm) {
1115 my $category = $comp->{label};
1116 if ( ref( $comp->{comment} ) eq 'ARRAY' ) {
1117 foreach my $go ( @{ $comp->{comment} } ) {
1118 my $term = _get_go_term( $go, $category );
1119 my $annterm = FAST::Bio::Annotation::OntologyTerm->new(
1120 -tagname => 'Gene Ontology' );
1121 $annterm->term($term);
1122 $self->{_ann}->add_Annotation( 'OntologyTerm', $annterm );
1123 }
1124 }
1125 else {
1126 my $term = _get_go_term( $comp->{comment}, $category );
1127 my $annterm =
1128 FAST::Bio::Annotation::OntologyTerm->new( -tagname => 'Gene Ontology' );
1129 $annterm->term($term);
1130 $self->{_ann}->add_Annotation( 'OntologyTerm', $annterm );
1131 }
1132 }
1133 }
1134  
1135 sub _process_grif {
1136 my $self = shift;
1137 my $grif = shift;
1138 if ( ( exists( $grif->{comment} ) )
1139 && ( ref( $grif->{comment} ) eq 'ARRAY' ) )
1140 {
1141 my @uncapt;
1142 foreach my $product ( @{ $grif->{comment} } ) {
1143 next unless ( exists( $product->{text} ) );
1144 my $uproduct = $self->_process_grif($product);
1145  
1146 #$self->{_ann->add_Annotation($type,$grifobj);
1147 push @uncapt, $uproduct;
1148 }
1149 return \@uncapt;
1150 }
1151 if ( exists( $grif->{comment}->{comment} ) ) {
1152 $grif = $grif->{comment};
1153 }
1154 my $ref =
1155 ( ref( $grif->{refs} ) eq 'ARRAY' )
1156 ? shift @{ $grif->{refs} }
1157 : $grif->{refs};
1158 my $refergene = '';
1159 my $refdb = '';
1160 my ( $obj, $type );
1161 if ( $ref->{pmid} ) {
1162 if ( exists( $grif->{source} ) )
1163 { #unfortunatrely we cannot put yet everything in
1164 $refergene = $grif->{source}->{src}->{tag}->{id};
1165 $refdb = $grif->{source}->{src}->{db};
1166 }
1167 my $grifobj = FAST::Bio::Annotation::Comment->new( -text => $grif->{text} );
1168 $obj = FAST::Bio::Annotation::DBLink->new(
1169 -database => 'generif',
1170 -primary_id => $ref->{pmid}
1171 , #The pubmed id (at least the first one) which is a base for the conclusion
1172 -version => $grif->{version},
1173 -optional_id => $refergene,
1174 -authority => $refdb
1175 );
1176 $obj->comment($grifobj);
1177 $type = 'dblink';
1178 }
1179 else {
1180 $obj = FAST::Bio::Annotation::SimpleValue->new( $grif->{text}, 'generif' );
1181 $type = 'generif';
1182 }
1183 delete $grif->{text};
1184 delete $grif->{version};
1185 delete $grif->{type};
1186 delete $grif->{refs};
1187 $self->{_ann}->add_Annotation( $type, $obj );
1188 return $grif;
1189 }
1190  
1191 sub _get_go_term {
1192 my $go = shift;
1193 my $category = shift;
1194 my $refan = FAST::Bio::Annotation::Reference->new( #We expect one ref per GO
1195 -medline => $go->{refs}->{pmid},
1196 -title => 'no title'
1197 );
1198 my $term = FAST::Bio::Ontology::Term->new(
1199 -identifier => $go->{source}->{src}->{tag}->{id},
1200 -name => $go->{source}->{anchor},
1201 -definition => $go->{source}->{anchor},
1202 -comment => $go->{source}->{'post-text'},
1203 -version => $go->{version}
1204 );
1205 $term->add_reference($refan);
1206 $term->namespace($category);
1207 return $term;
1208 }
1209  
1210 sub _backcomp_ll {
1211 my $ann = shift;
1212 my $newann = FAST::Bio::Annotation::Collection->new();
1213  
1214 #$newann->{_annotation}->{ALIAS_SYMBOL}=$ann->{_annotation}->{ALIAS_SYMBOL};
1215 # $newann->{_annotation}->{CHR}=$ann->{_annotation}->{chromosome};
1216 # $newann->{_annotation}->{MAP}=$ann->{_annotation}->{cyto};
1217 foreach my $tagmap ( keys %{ $ann->{_typemap}->{_type} } ) {
1218 next if ( grep( /$tagmap/, @main::egonly ) );
1219 $newann->{_annotation}->{$tagmap} = $ann->{_annotation}->{$tagmap};
1220 }
1221  
1222 #$newann->{_annotation}->{Reference}=$ann->{_annotation}->{Reference};
1223 #$newann->{_annotation}->{generif}=$ann->{_annotation}->{generif};
1224 #$newann->{_annotation}->{comment}=$ann->{_annotation}->{comment};
1225 # $newann->{_annotation}->{OFFICIAL_GENE_NAME}=$ann->{_annotation}->{'Official Full Name'};
1226 $newann->{_typemap}->{_type} = $ann->{_typemap}->{_type};
1227 foreach my $ftype ( keys %main::eg_to_ll ) {
1228 my $newkey = $main::eg_to_ll{$ftype};
1229 $newann->{_annotation}->{$newkey} = $ann->{_annotation}->{$ftype};
1230 $newann->{_typemap}->{_type}->{$newkey} =
1231 'FAST::Bio::Annotation::SimpleValue';
1232 delete $newann->{_typemap}->{_type}->{$ftype};
1233 $newann->{_annotation}->{$newkey}->[0]->{tagname} = $newkey;
1234 }
1235 foreach my $dblink ( @{ $newann->{_annotation}->{dblink} } ) {
1236 next unless ( $dblink->{_url} );
1237 my $simann = FAST::Bio::Annotation::SimpleValue->new(
1238 -value => $dblink->{_url},
1239 -tagname => 'URL'
1240 );
1241 $newann->add_Annotation($simann);
1242 }
1243  
1244 # my $simann=FAST::Bio::Annotation::SimpleValue->new(-value=>$seq->desc,-tagname=>'comment');
1245 # $newann->add_Annotation($simann);
1246  
1247 return $newann;
1248 }
1249  
1250 1;