| 1 | # | ||
| 2 | # BioPerl module for interpro | ||
| 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 | Bio::SeqIO::interpro - InterProScan XML input/output stream | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | # do not call this module directly, use Bio::SeqIO | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use Bio::SeqIO; | ||
| 17 | |||
| 18 | my $io = Bio::SeqIO->new(-format => "interpro", | ||
| 19 | -file => $interpro_file); | ||
| 20 | |||
| 21 | while (my $seq = $io->next_seq) { | ||
| 22 | # use the Sequence object | ||
| 23 | } | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | L<Bio::SeqIO::interpro> will parse Interpro scan XML (version 1.2) and | ||
| 28 | create L<Bio::SeqFeature::Generic> objects based on the contents of the | ||
| 29 | XML document. | ||
| 30 | |||
| 31 | L<Bio::SeqIO::interpro> will also attach the annotation given in the XML | ||
| 32 | file to the L<Bio::SeqFeature::Generic> objects that it creates. | ||
| 33 | |||
| 34 | =head1 FEEDBACK | ||
| 35 | |||
| 36 | =head2 Mailing Lists | ||
| 37 | |||
| 38 | User feedback is an integral part of the evolution of this and other | ||
| 39 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 40 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 41 | |||
| 42 | bioperl-l@bioperl.org - General discussion | ||
| 43 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 44 | |||
| 45 | =head2 Support | ||
| 46 | |||
| 47 | Please direct usage questions or support issues to the mailing list: | ||
| 48 | |||
| 49 | I<bioperl-l@bioperl.org> | ||
| 50 | |||
| 51 | rather than to the module maintainer directly. Many experienced and | ||
| 52 | reponsive experts will be able look at the problem and quickly | ||
| 53 | address it. Please include a thorough description of the problem | ||
| 54 | with code and data examples if at all possible. | ||
| 55 | |||
| 56 | =head2 Reporting Bugs | ||
| 57 | |||
| 58 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 59 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 60 | the web: | ||
| 61 | |||
| 62 | https://github.com/bioperl/bioperl-live/issues | ||
| 63 | |||
| 64 | =head1 AUTHOR - Jared Fox | ||
| 65 | |||
| 66 | Email jaredfox@ucla.edu | ||
| 67 | |||
| 68 | =head1 CONTRIBUTORS | ||
| 69 | |||
| 70 | Allen Day allenday@ucla.edu | ||
| 71 | |||
| 72 | =head1 APPENDIX | ||
| 73 | |||
| 74 | The rest of the documentation details each of the object methods. | ||
| 75 | Internal methods are usually preceded with a _ | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | # Let the code begin... | ||
| 80 | |||
| 81 | package Bio::SeqIO::interpro; | ||
| 82 | $Bio::SeqIO::interpro::VERSION = '1.7.7'; | ||
| 83 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 84 | 1 | ▶ | use Bio::SeqFeature::Generic; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 85 | 1 | ▶ | use XML::DOM; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 86 | use XML::DOM::XPath; | ||
| 87 | use Bio::Seq::SeqFactory; | ||
| 88 | use Bio::Annotation::Collection; | ||
| 89 | use Bio::Annotation::DBLink; | ||
| 90 | use base qw(Bio::SeqIO); | ||
| 91 | |||
| 92 | my $idcounter = {}; # Used to generate unique id values | ||
| 93 | my $nvtoken = ": "; # The token used if a name/value pair has to be stuffed | ||
| 94 | # into a single line | ||
| 95 | |||
| 96 | =head2 next_seq | ||
| 97 | |||
| 98 | Title : next_seq | ||
| 99 | Usage : my $seqobj = $stream->next_seq | ||
| 100 | Function: Retrieves the next sequence from a SeqIO::interpro stream. | ||
| 101 | Returns : A Bio::Seq::RichSeq object | ||
| 102 | Args : | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub next_seq { | ||
| 107 | my $self = shift; | ||
| 108 | my ($desc); | ||
| 109 | my $bioSeq = $self->_sequence_factory->create(-verbose =>$self->verbose()); | ||
| 110 | |||
| 111 | my $zinc = "(\"zincins\")"; | ||
| 112 | my $wing = "\"Winged helix\""; | ||
| 113 | my $finger = "\"zinc finger\""; | ||
| 114 | |||
| 115 | my $xml_fragment = undef; | ||
| 116 | while(my $line = $self->_readline()){ | ||
| 117 | |||
| 118 | my $where = index($line, $zinc); | ||
| 119 | my $wherefinger = index($line, $finger); | ||
| 120 | my $finishedline = $line; | ||
| 121 | my $wingwhere = index($line, $wing); | ||
| 122 | |||
| 123 | # the interpro XML is not fully formed, so we need to convert the | ||
| 124 | # extra double quotes and ampersands into appropriate XML character codes | ||
| 125 | if($where > 0){ | ||
| 126 | my @linearray = split /$zinc/, $line; | ||
| 127 | $finishedline = join ""zincins"", $linearray[0], $linearray[2]; | ||
| 128 | } | ||
| 129 | if(index($line, "&") > 0){ | ||
| 130 | my @linearray = split /&/, $line; | ||
| 131 | $finishedline = join "&", $linearray[0], $linearray[1]; | ||
| 132 | } | ||
| 133 | if($wingwhere > 0){ | ||
| 134 | my @linearray = split /$wing/, $line; | ||
| 135 | $finishedline = join ""Winged helix"", $linearray[0], $linearray[1]; | ||
| 136 | } | ||
| 137 | |||
| 138 | $xml_fragment .= $finishedline; | ||
| 139 | last if $finishedline =~ m!</protein>!; | ||
| 140 | } | ||
| 141 | # Match <protein> but not other similar elements like <protein-matches> | ||
| 142 | return unless $xml_fragment =~ /<protein[\s>]/; | ||
| 143 | |||
| 144 | $self->_parse_xml($xml_fragment); | ||
| 145 | |||
| 146 | my $dom = $self->_dom; | ||
| 147 | |||
| 148 | my ($protein_node) = $dom->findnodes('/protein'); | ||
| 149 | my @interproNodes = $protein_node->findnodes('/protein/interpro'); | ||
| 150 | my @DBNodes = $protein_node->findnodes('/protein/interpro/match'); | ||
| 151 | for(my $interpn=0; $interpn<scalar(@interproNodes); $interpn++){ | ||
| 152 | my $ipnlevel = join "", "/protein/interpro[", $interpn + 1, "]"; | ||
| 153 | my @matchNodes = $protein_node->findnodes($ipnlevel); | ||
| 154 | for(my $match=0; $match<scalar(@matchNodes); $match++){ | ||
| 155 | my $matlevel = join "", "/protein/interpro[", $interpn+1, "]/match[", | ||
| 156 | $match+1, "]/location"; | ||
| 157 | my @locNodes = $protein_node->findnodes($matlevel); | ||
| 158 | my $class_level = join "", "/protein/interpro[",$interpn+1, "]/classification"; | ||
| 159 | my @goNodes = $protein_node->findnodes($class_level); | ||
| 160 | my @seqFeatures = map { Bio::SeqFeature::Generic->new( | ||
| 161 | -start => $_->getAttribute('start'), | ||
| 162 | -end => $_->getAttribute('end'), | ||
| 163 | -score => $_->getAttribute('score'), | ||
| 164 | -source_tag => 'IPRscan', | ||
| 165 | -primary_tag => 'region', | ||
| 166 | -display_name => $interproNodes[$interpn]->getAttribute('name'), | ||
| 167 | -seq_id => $protein_node->getAttribute('id') ), | ||
| 168 | } @locNodes; | ||
| 169 | foreach my $seqFeature (@seqFeatures){ | ||
| 170 | $bioSeq->add_SeqFeature($seqFeature); | ||
| 171 | |||
| 172 | my $annotation1 = Bio::Annotation::DBLink->new; | ||
| 173 | $annotation1->database($matchNodes[$match]->getAttribute('dbname')); | ||
| 174 | $annotation1->primary_id($matchNodes[$match]->getAttribute('id')); | ||
| 175 | $annotation1->comment($matchNodes[$match]->getAttribute('name')); | ||
| 176 | $seqFeature->annotation->add_Annotation('dblink',$annotation1); | ||
| 177 | |||
| 178 | my $annotation2 = Bio::Annotation::DBLink->new; | ||
| 179 | $annotation2->database('INTERPRO'); | ||
| 180 | $annotation2->primary_id($interproNodes[$interpn]->getAttribute('id')); | ||
| 181 | $annotation2->comment($interproNodes[$interpn]->getAttribute('name')); | ||
| 182 | $seqFeature->annotation->add_Annotation('dblink',$annotation2); | ||
| 183 | |||
| 184 | # Bug 1908 (enhancement) | ||
| 185 | my $annotation3 = Bio::Annotation::DBLink->new; | ||
| 186 | $annotation3->database($DBNodes[$interpn]->getAttribute('dbname')); | ||
| 187 | $annotation3->primary_id($DBNodes[$interpn]->getAttribute('id')); | ||
| 188 | $annotation3->comment($DBNodes[$interpn]->getAttribute('name')); | ||
| 189 | $seqFeature->annotation->add_Annotation('dblink',$annotation3); | ||
| 190 | # need to put in the go annotation here! | ||
| 191 | foreach my $g (@goNodes) | ||
| 192 | { | ||
| 193 | my $goid = $g->getAttribute('id'); | ||
| 194 | my $go_annotation = Bio::Annotation::DBLink->new; | ||
| 195 | $go_annotation->database('GO'); | ||
| 196 | $go_annotation->primary_id($goid); | ||
| 197 | $go_annotation->comment($goid); | ||
| 198 | $seqFeature->annotation->add_Annotation('dblink', $go_annotation); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | my $accession = $protein_node->getAttribute('id'); | ||
| 204 | my $displayname = $protein_node->getAttribute('name'); | ||
| 205 | $bioSeq->accession($accession); | ||
| 206 | $bioSeq->display_name($displayname); | ||
| 207 | return $bioSeq; | ||
| 208 | } | ||
| 209 | |||
| 210 | =head2 _initialize | ||
| 211 | |||
| 212 | Title : _initialize | ||
| 213 | Usage : | ||
| 214 | Function: | ||
| 215 | Returns : | ||
| 216 | Args : | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub _initialize { | ||
| 221 | my($self,@args) = @_; | ||
| 222 | |||
| 223 | $self->SUPER::_initialize(@args); | ||
| 224 | # hash for functions for decoding keys. | ||
| 225 | $self->{'_func_ftunit_hash'} = {}; | ||
| 226 | |||
| 227 | my %param = @args; # From SeqIO.pm | ||
| 228 | @param{ map { lc $_ } keys %param } = values %param; # lowercase keys | ||
| 229 | |||
| 230 | my $line = undef; | ||
| 231 | # fast forward to first <protein/> record. | ||
| 232 | while($line = $self->_readline()){ | ||
| 233 | # Match <protein> but not other similar elements like <protein-matches> | ||
| 234 | if($line =~ /<protein[\s>]/){ | ||
| 235 | $self->_pushback($line); | ||
| 236 | last; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | $self->_xml_parser( XML::DOM::Parser->new() ); | ||
| 241 | |||
| 242 | $self->_sequence_factory( Bio::Seq::SeqFactory->new | ||
| 243 | ( -verbose => $self->verbose(), | ||
| 244 | -type => 'Bio::Seq::RichSeq')) | ||
| 245 | if ( ! defined $self->sequence_factory ); | ||
| 246 | } | ||
| 247 | |||
| 248 | =head2 _sequence_factory | ||
| 249 | |||
| 250 | Title : _sequence_factory | ||
| 251 | Usage : | ||
| 252 | Function: | ||
| 253 | Returns : | ||
| 254 | Args : | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub _sequence_factory { | ||
| 259 | my $self = shift; | ||
| 260 | my $val = shift; | ||
| 261 | |||
| 262 | $self->{'sequence_factory'} = $val if defined($val); | ||
| 263 | return $self->{'sequence_factory'}; | ||
| 264 | } | ||
| 265 | |||
| 266 | =head2 _xml_parser | ||
| 267 | |||
| 268 | Title : _xml_parser | ||
| 269 | Usage : | ||
| 270 | Function: | ||
| 271 | Returns : | ||
| 272 | Args : | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub _xml_parser { | ||
| 277 | my $self = shift; | ||
| 278 | my $val = shift; | ||
| 279 | |||
| 280 | $self->{'xml_parser'} = $val if defined($val); | ||
| 281 | return $self->{'xml_parser'}; | ||
| 282 | } | ||
| 283 | |||
| 284 | =head2 _parse_xml | ||
| 285 | |||
| 286 | Title : _parse_xml | ||
| 287 | Usage : | ||
| 288 | Function: | ||
| 289 | Returns : | ||
| 290 | Args : | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub _parse_xml { | ||
| 295 | my ($self,$xml) = @_; | ||
| 296 | $self->_dom( $self->_xml_parser->parse($xml) ); | ||
| 297 | return 1; | ||
| 298 | } | ||
| 299 | |||
| 300 | =head2 _dom | ||
| 301 | |||
| 302 | Title : _dom | ||
| 303 | Usage : | ||
| 304 | Function: | ||
| 305 | Returns : | ||
| 306 | Args : | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | sub _dom { | ||
| 311 | my $self = shift; | ||
| 312 | my $val = shift; | ||
| 313 | |||
| 314 | $self->{'dom'} = $val if defined($val); | ||
| 315 | return $self->{'dom'}; | ||
| 316 | } | ||
| 317 | |||
| 318 | 1; | ||
| 319 | |||
| 320 | __END__ | ||