This file is indexed.

/usr/share/perl5/Bio/SeqIO/tinyseq.pm is in libbio-perl-perl 1.6.924-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# BioPerl module for Bio::SeqIO::tinyseq
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Donald Jackson, donald.jackson@bms.com
#
# Copyright Bristol-Myers Squibb
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::SeqIO::tinyseq - reading/writing sequences in NCBI TinySeq format

=head1 SYNOPSIS

Do not use this module directly; use the SeqIO handler system:

  $stream = Bio::SeqIO->new( -file => $filename, -format => 'tinyseq' );

  while ( my $seq = $stream->next_seq ) {
    ....
  }

=head1 DESCRIPTION

This object reads and writes Bio::Seq objects to and from TinySeq XML
format.  A TinySeq is a lightweight XML file of sequence information,
analgous to FASTA format.

See L<http://www.ncbi.nlm.nih.gov/dtd/NCBI_TSeq.mod.dtd> for the DTD.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:

  https://github.com/bioperl/bioperl-live/issues

=head1 SEE ALSO

L<Bio::SeqIO>, L<Bio::Seq>.

=head1 AUTHOR

Donald Jackson, E<lt>donald.jackson@bms.comE<gt>

Parts of this module and the test script were patterned after Sheldon
McKay's L<Bio::SeqIO::game>.  If it breaks, however, it's my fault not
his ;).

=head1 APPENDIX

The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _

=cut

package Bio::SeqIO::tinyseq;

use strict;
use Bio::Seq::SeqFastaSpeedFactory;
use Bio::Species;
use Bio::SeqIO::tinyseq::tinyseqHandler;
use XML::Parser::PerlSAX;
use XML::Writer;
use Bio::Root::Version;

use base qw(Bio::SeqIO);

sub _initialize {
    my ($self, @args) = @_;

    $self->SUPER::_initialize(@args);

    unless (defined $self->sequence_factory) {
        $self->sequence_factory(Bio::Seq::SeqFastaSpeedFactory->new);
    }

    $self->{'_species_objects'} = {};
    $self->{_parsed} = 0;
}

=head2 next_seq

  Title   : next_seq
  Usage   : $seq = $stream->next_seq()
  Function: returns the next sequence in the stream
  Returns : Bio::Seq object
  Args    : NONE

=cut

sub next_seq {
    my ($self) = @_;

    $self->_get_seqs() unless ($self->{_parsed});

    return shift @{$self->{_seqlist}};
}

=head2 write_seq

  Title   : write_seq
  Usage   : $seq = $stream->write_seq(@sequence_objects); undef $stream
  Function: outputs one or more sequence objects as TinySeq XML
  Returns : 1 on success
  Args    : one or more sequence objects as TinySeq XML

Because the TSeq dtd includes closing tags after all sets are written,
the output will not be complete until the program terminates or the
object is forced out of scope (see close_writer()).  May not perfectly
reproduce TSeq_sid element for all sequences

=cut

sub write_seq {
    my ($self, @seqobjs) = @_;

    $self->throw('write_seq must be called with at least one  Bio::SeqI or Bio::PrimarySeqI compliant object')
        unless (@seqobjs and ( $seqobjs[0]->isa('Bio::SeqI') || $seqobjs[0]->isa('Bio::PrimarySeqI')));

    my $writer = $self->_get_writer;

    foreach my $seqobj (@seqobjs) {
        my ($id_element, $id_value) = $self->_get_idstring($seqobj);
        $writer->startTag('TSeq');
        $writer->emptyTag('TSeq_seqtype', value => $self->_convert_seqtype($seqobj));

        $writer->dataElement('TSeq_gi',       $seqobj->primary_id || '');
        $writer->dataElement($id_element,     $id_value);
        #$writer->dataElement('TSeq_orgname',  $seqobj->taxid) if ($seqobj->can('taxid')); # just a placeholder
        $writer->dataElement('TSeq_defline',  $seqobj->desc);
        $writer->dataElement('TSeq_length',   $seqobj->length);
        $writer->dataElement('TSeq_sequence', $seqobj->seq);

        if ($seqobj->can('species') && $seqobj->species) {
            $self->_write_species($writer, $seqobj->species);
        }

        $writer->endTag('TSeq');
    }
    1;
}

=head2 _get_seqs

  Title   : _get_seqs
  Usage   : Internal function - use next_seq() instead
  Function: parses the XML and creates Bio::Seq objects
  Returns : 1 on success
  Args    : NONE

Currently stores all sequence objects into memory.  I will work on do
more of a stream-based approach

=cut

sub _get_seqs {
    my ($self) = @_;
    my $fh = $self->_fh;

    my $handler = Bio::SeqIO::tinyseq::tinyseqHandler->new();
    my $parser  = XML::Parser::PerlSAX->new( Handler => $handler );
    my @seqatts = $parser->parse( Source => { ByteStream => $fh });
    my $factory = $self->sequence_factory;

    $self->{_seqlist} ||= [];
    foreach my $seqatt(@seqatts) {
        foreach my $subatt(@$seqatt) { # why are there two hashes?
            my $seqobj = $factory->create(%$subatt);
            $self->_assign_identifier($seqobj, $subatt);

            if ($seqobj->can('species')) {
#               my $class = [reverse(split(/ /, $subatt->{'-organism'}))];
#               my $species = Bio::Species->new( -classification => $class,
#                                                -ncbi_taxid     => $subatt->{'-taxid'} );
                my $species = $self->_get_species($subatt->{'-organism'}, $subatt->{'-taxid'});
                $seqobj->species($species) if ($species);
            }

            push(@{$self->{_seqlist}}, $seqobj);
        }
    }
    $self->{_parsed} = 1;
}

=head2 _get_species

  Title   : _get_species
  Usage   : Internal function
  Function: gets a Bio::Species object from cache or creates as needed
  Returns : a Bio::Species object on success, undef on failure
  Args    : a classification string (eg 'Homo sapiens') and
            a NCBI taxon id (optional)

Objects are cached for parsing multiple sequence files.

=cut

sub _get_species {
     my ($self, $orgname, $taxid) = @_;

     unless ($self->{'_species_objects'}->{$orgname}) {
         my $species = $self->_create_species($orgname, $taxid);
         $self->{'_species_objects'}->{$orgname} = $species;
     }
     return $self->{'_species_objects'}->{$orgname};
}

=head2 _create_species

  Title   : _create_species
  Usage   : Internal function
  Function: creates a Bio::Species object
  Returns : a Bio::Species object on success, undef on failure
  Args    : a classification string (eg 'Homo sapiens') and
                  a NCBI taxon id (optional)

=cut

sub _create_species {
    my ($self, $orgname, $taxid) = @_;
    return unless ($orgname); # not required in TinySeq dtd so don't throw an error

    my %params;
    $params{'-classification'} = [reverse(split(/ /, $orgname))];
    $params{'-ncbi_taxid'} = $taxid if ($taxid);

    my $species = Bio::Species->new(%params)
        or return;

    return $species;
}


=head2 _assign_identifier

  Title   : _assign_identifier
  Usage   : Internal function
  Function: looks for sequence accession
  Returns : 1 on success
  Args    : NONE

NCBI puts refseq accessions in TSeq_sid, others in TSeq_accver.

=cut

sub _assign_identifier {
    my ($self, $seqobj, $atts) = @_;
    my ($accession, $version);

   if ($atts->{'-accver'}) {
        ($accession, $version) = split(/\./, $atts->{'-accver'});;
    }
    elsif ($atts->{'-sid'}) {
        my $sidstring =$atts->{'-sid'};
        $sidstring =~ s/^.+?\|//;
        $sidstring =~ s/\|[^\|]*//;
        ($accession, $version) = split(/\./, $sidstring);;
    }
    else {
        $self->throw('NO accession information found for this sequence');
    }
    $seqobj->accession_number($accession) if ($seqobj->can('accession_number'));
    $seqobj->version($version) if ($seqobj->can('version'));
}

=head2 _convert_seqtype

  Title   : _convert_seqtype
  Usage   : Internal function
  Function: maps Bio::Seq::alphabet() values [dna/rna/protein] onto
            TSeq_seqtype values [protein/nucleotide]

=cut

sub _convert_seqtype {
    my ($self, $seqobj) = @_;

    return 'protein'    if ($seqobj->alphabet eq 'protein');
    return 'nucleotide' if ($seqobj->alphabet eq 'dna');
    return 'nucleotide' if ($seqobj->alphabet eq 'rna');

    # if we get here there's a problem!
    $self->throw("Alphabet not defined, can't assign type for $seqobj");
}

=head2 _get_idstring

  Title   : _get_idstring
  Usage   : Internal function
  Function: parse accession and version info from TSeq_accver
            or TSeq_sid

=cut

sub _get_idstring {
    # NCBI puts refseq ids in TSeq_sid, others in TSeq_accver.  No idea why.
    my ($self, $seqobj) = @_;
    my $accver = $seqobj->accession_number;
    $accver .= '.' . $seqobj->version if ($seqobj->can('version') and $seqobj->version);
    if ($accver =~ /^(NM_|NP_|XM_|XP_|NT_|NC_|NG_)/) {
        return ('TSeq_sid', join('|', 'ref', $accver, ''));
    }
    else {
        return ('TSeq_accver', $accver);
    }
}

=head2 _get_writer

  Title   : _get_writer
  Usage   : Internal function
  Function: instantiate XML::Writer object if needed,
                  output initial XML

=cut

sub _get_writer {
    # initialize writer, start doc so write_seq can work one at a time
    my ($self) = @_;

    unless ($self->{_writer}) {
        my $fh = $self->_fh;
        my $writer = XML::Writer->new(OUTPUT      => $fh,
                                      DATA_MODE   => 1,
                                      DATA_INDENT => 2,
                                      NEWLINE     => 1,
                                      );
        $writer->doctype('TSeqSet', '-//NCBI//NCBI TSeq/EN', 'http://www.ncbi.nlm.nih.gov/dtd/NCBI_TSeq.dtd');
        $writer->comment("Generated by Bio::SeqIO::tinyseq VERSION " . $Bio::Root::Version::VERSION);
        $writer->startTag('TSeqSet');

        $self->{_writer} = $writer;
    }
    return $self->{_writer};
}

=head2 close_writer

  Title   : close_writer
  Usage   : $self->close_writer()
  Function: terminate XML output
  Args    : NONE
  Returns : 1 on success

Called automatically by DESTROY when object goes out of scope

=cut

sub close_writer {
    # close out any dangling writer
    my ($self) = @_;
    if ($self->{_writer}) {
        my $writer = $self->{_writer};
        $writer->endTag('TSeqSet');
        $writer->end;
        undef $writer;
    }
    close($self->_fh) if ($self->_fh);
    1;
}

sub _write_species {
    my ($self, $writer, $species) = @_;
    $writer->dataElement('TSeq_orgname', $species->binomial);
    $writer->dataElement('TSeq_taxid',   $species->ncbi_taxid)
        if($species->ncbi_taxid);
}

sub DESTROY {
    # primarily to close out a writer!
    my ($self) = @_;
    $self->close_writer;
    undef $self;
}

1;
__END__