| 1 |
# $Id: ncbi_taxonomy_parser.pm,v 1.10 2008/02/05 00:34:15 cmungall Exp $ |
| 2 |
# |
| 3 |
# |
| 4 |
# see also - http://www.geneontology.org |
| 5 |
# - http://www.godatabase.org/dev |
| 6 |
# |
| 7 |
# You may distribute this module under the same terms as perl itself |
| 8 |
|
| 9 |
package GO::Parsers::ncbi_taxonomy_parserscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/; |
| 10 |
|
| 11 |
=head1 NAME |
| 12 |
|
| •13 |
GO::Parsers::ncbi_taxonomy_parser - Parser for NCBI taxonomy |
| 14 |
|
| 15 |
=head1 SYNOPSIS |
| 16 |
|
| 17 |
do not use this class directly; use GO::Parser |
| 18 |
|
| 19 |
=cut |
| 20 |
|
| 21 |
=head1 DESCRIPTION |
| 22 |
|
| 23 |
See L<ftp://ftp.ebi.ac.uk/pub/databases/taxonomy/taxonomy.dat> |
| 24 |
|
| 25 |
|
| 26 |
=head1 PARSER ARCHITECTURE |
| 27 |
|
| 28 |
This generates Stag event streams from one of the various GO flat file |
| 29 |
formats (ontology, defs, xref, associations). See GO::Parser for details |
| 30 |
|
| 31 |
Examples of these files can be found at http://www.geneontology.org |
| 32 |
|
| 33 |
A description of the event streams generated follows; Stag or an XML |
| 34 |
handler can be used to catch these events |
| 35 |
|
| 36 |
=cut |
| 37 |
|
| 38 |
use Exporterscip-perl cpan go-perl 0.15 Exporter/; |
| 39 |
use basescip-perl cpan go-perl 0.15 base/ qw(GO::Parsers::base_parser); |
| 40 |
use GO::Parsers::ParserEventNamesscip-perl cpan go-perl 0.15 GO/Parsers/ParserEventNames/; # declare XML constants |
| 41 |
|
| 42 |
use Carpscip-perl cpan go-perl 0.15 Carp/; |
| 43 |
use FileHandlescip-perl cpan go-perl 0.15 FileHandle/; |
| 44 |
use strictscip-perl cpan go-perl 0.15 strict/ qw(subs vars refs); |
| 45 |
|
| 46 |
my %synonymtypes = |
| 47 |
( |
| 48 |
'genbank synonym'=>['related'], |
| 49 |
'synonym'=>['related'], |
| 50 |
'in-part'=>['related'], |
| 51 |
'blast name'=>['related'], |
| 52 |
'misnomer'=>['related'], |
| 53 |
'misspelling'=>['related'], |
| 54 |
'scientific name'=>['exact'], |
| 55 |
'equivalent name'=>['exact'], |
| 56 |
'common name'=>['exact'], |
| 57 |
'genbank common name'=>['exact'], |
| 58 |
'acronym'=>['broad'], |
| 59 |
'genbank acronym'=>['broad'], |
| 60 |
'teleomorph'=>['related'], |
| 61 |
'anamorph'=>['related'], |
| 62 |
'genbank anamorph'=>['related'], |
| 63 |
); |
| 64 |
|
| 65 |
sub synscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/syn(). { |
| 66 |
my $name = shiftscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/shift().; |
| 67 |
$name =~ s/\s/_/g; |
| 68 |
$name =~ s/\-/_/g; |
| 69 |
$name; |
| 70 |
} |
| 71 |
|
| 72 |
sub parse_fhscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/parse_fh(). { |
| 73 |
my ($self, $fh) = @_scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/`@_`.; |
| 74 |
my $file = $self->filescip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/file().; |
| 75 |
$self->{name_h} = {}; |
| 76 |
|
| 77 |
$self->start_eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/start_event().(OBO); |
| 78 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(header=> |
| 79 |
[ |
| 80 |
['default-namespace'=>'ncbi_taxonomy'], |
| 81 |
['ontology'=>'ncbitaxon'], |
| 82 |
[remark=>'autogenerated via GO::Parsers::ncbi_taxonomy_parser'], |
| 83 |
(mapscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/map(). {[synonymtypedef=>[[id=>synscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/syn().($_scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/$_.)],[name=>$_scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/$_.],[scope=>$synonymtypes{$_scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/$_.}->[0]]]]} keysscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/keys(). %synonymtypes), |
| 84 |
]); |
| 85 |
|
| 86 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(typedef=> |
| 87 |
[ |
| 88 |
[id=>'has_rank'], |
| 89 |
[name=>"has_rank"], |
| 90 |
[def=>[[defstr=>"A metadata relation between a class and its taxonomic rank (eg species, family)"]]], |
| 91 |
[is_metadata_tag=>1], |
| 92 |
[comment=>"This is an abstract class for use with the NCBI taxonomy to name the depth of the node within the tree. The link between the node term and the rank is only visible if you are using an obo 1.3 aware browser/editor; otherwise this can be ignored"] |
| 93 |
]); |
| 94 |
|
| 95 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(term=> |
| 96 |
[ |
| 97 |
[id=>_fix_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_fix_id().("taxonomic_rank")], |
| 98 |
[name=>"taxonomic_rank"], |
| 99 |
[def=>[[defstr=>"A level of depth within a species taxonomic tree"]]], |
| 100 |
[comment=>"This is an abstract class for use with the NCBI taxonomy to name the depth of the node within the tree. The link between the node term and the rank is only visible if you are using an obo 1.3 aware browser/editor; otherwise this can be ignored"] |
| 101 |
]); |
| 102 |
|
| 103 |
my $lnum = 0; |
| 104 |
my %h = (); |
| 105 |
my $text; |
| 106 |
while (my $line = <$fh>) { |
| 107 |
chompscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/chomp(). $line; |
| 108 |
if ($line eq '//') { |
| 109 |
$self->emit_termscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/emit_term().(\%h,$text); |
| 110 |
%h = (); |
| 111 |
$text = ''; |
| 112 |
} |
| 113 |
else { |
| 114 |
if ($line =~ /^([\w\s\-]+)\s+:\s*(.*)/) { |
| 115 |
my ($k,$v) = ($1scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/$1.,$2scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/$2.); |
| 116 |
$k = lcscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/lc().($k); # lowercase |
| 117 |
$k =~ s/\s+$//; # removing trailing ws |
| 118 |
pushscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/push().(@{$h{$k}},$v); |
| 119 |
} |
| 120 |
else { |
| 121 |
$self->parse_errscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/parse_err().("Line: $line"); |
| 122 |
} |
| 123 |
$text .= "$line\n"; |
| 124 |
} |
| 125 |
} |
| 126 |
$self->pop_stack_to_depthscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/pop_stack_to_depth().(0); # end event obo |
| 127 |
} |
| 128 |
|
| 129 |
sub _rank_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_rank_id(). { |
| 130 |
my $rank = shiftscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/shift().; |
| 131 |
$rank =~ s/\s+/_/g; |
| 132 |
_fix_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_fix_id().($rank); |
| 133 |
} |
| 134 |
|
| 135 |
sub _fix_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_fix_id(). { |
| 136 |
return "NCBITaxon:$_[0]"; |
| 137 |
} |
| 138 |
|
| 139 |
sub emit_termscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/emit_term(). { |
| 140 |
my ($self, $h, $text) = @_scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/`@_`.; |
| 141 |
my $id = popscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/pop(). @{$h->{id}}; |
| 142 |
if (!$id) { |
| 143 |
$self->parse_errscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/parse_err().("No id! in:\n$text"); |
| 144 |
return; |
| 145 |
} |
| 146 |
$id = _fix_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_fix_id().($id); |
| 147 |
$self->start_eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/start_event().(TERM); |
| 148 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(ID,$id); |
| 149 |
my $name = popscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/pop(). @{$h->{'scientific name'}}; |
| 150 |
my ($gname) = @{$h->{'genbank common name'} || []}; |
| 151 |
if (!$name) { |
| 152 |
$name = $gname; |
| 153 |
popscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/pop(). @{$h->{'genbank common name'}} if $gname; |
| 154 |
} |
| 155 |
if ($self->{name_h}->{$name}) { |
| 156 |
$name .= " [$id]"; # force unique |
| 157 |
} |
| 158 |
$self->{name_h}->{$name} = $id; |
| 159 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(NAME, $name); |
| 160 |
foreach my $st (keysscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/keys(). %synonymtypes) { |
| 161 |
my $syns = $h->{$st}; |
| 162 |
next unless $syns; |
| 163 |
my $scope = $synonymtypes{$st}->[0]; |
| 164 |
foreach my $s (@$syns) { |
| 165 |
my @xrefs = (); |
| 166 |
if ($s =~ /\"(.+)\"\s*(.+)/ || |
| 167 |
$s =~ /\'(.+)\'\s*(.+)/) { |
| 168 |
my $xref = $2scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/$2.; |
| 169 |
$s = $1scip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/$1.; |
| 170 |
$xref =~ s/\s+/_/g; |
| 171 |
$xref =~ tr/\(\)//d; |
| 172 |
$xref =~ tr/\[\]//d; |
| 173 |
$xref =~ s@,@\\,@g; |
| 174 |
pushscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/push().(@xrefs, [dbxref=>[[acc=>$xref],[dbname=>"NCBITaxonRef"]]]); |
| 175 |
} |
| 176 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(SYNONYM,[ |
| 177 |
['@'=>[['scope',$scope], |
| 178 |
[SYNONYM_TYPE,synscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/syn().($st)]]], |
| 179 |
[SYNONYM_TEXT,$s], |
| 180 |
@xrefs]); |
| 181 |
} |
| 182 |
} |
| 183 |
my $rank = popscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/pop(). @{$h->{rank}}; |
| 184 |
if ($rank eq 'no rank') { |
| 185 |
$rank = undef; |
| 186 |
} |
| 187 |
if ($rank) { |
| 188 |
if (!$self->{__rank}->{$rank}) { |
| 189 |
$self->{__rank}->{$rank} = 1; |
| 190 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(TERM,[[ID,_rank_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_rank_id().($rank)], |
| 191 |
[NAME,$rank], |
| 192 |
[IS_A,_rank_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_rank_id().('taxonomic_rank')], |
| 193 |
]); |
| 194 |
} |
| 195 |
|
| 196 |
#this is the correct way to handle this - as annotation properties |
| 197 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(PROPERTY_VALUE,[[TYPE,'has_rank'], |
| 198 |
[TO,_rank_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_rank_id().($rank)]]) |
| 199 |
if $rank; |
| 200 |
|
| 201 |
# do it this way for now until oboedit is fixed: |
| 202 |
# $self->event(RELATIONSHIP,[[TYPE,'has_rank'], |
| 203 |
# [TO,_rank_id($rank)]]) |
| 204 |
} |
| 205 |
|
| 206 |
my $gc = popscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/pop(). @{$h->{'gc id'}}; |
| 207 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(XREF, "GC_ID:$gc") if $gc; |
| 208 |
my $parent_id = popscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/pop(). @{$h->{'parent id'}}; |
| 209 |
if ($parent_id) { |
| 210 |
$parent_id = _fix_idscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/_fix_id().($parent_id); |
| 211 |
$self->eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/event().(IS_A,$parent_id); |
| 212 |
} |
| 213 |
$self->end_eventscip-perl cpan go-perl 0.15 GO/Parsers/ncbi_taxonomy_parser/end_event().(TERM); |
| 214 |
} |
| 215 |
|
| 216 |
1; |