This file is indexed.

/usr/share/perl5/Bio/Seq/SimulatedRead.pm is in libbio-perl-perl 1.6.924-3.

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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package Bio::Seq::SimulatedRead;


=head1 NAME

Bio::Seq::SimulatedRead - Read with sequencing errors taken from a reference sequence

=head1 SYNOPSIS

    use Bio::Seq::SimulatedRead;
    use Bio::PrimarySeq;
  
    # Create a reference sequence
    my $genome = Bio::PrimarySeq->new( -id    => 'human_chr2',
                                       -seq   => 'TAAAAAAACCCCTG',
                                       -desc  => 'The human genome' );

    # A 10-bp error-free read taken from a genome
    my $read = Bio::Seq::SimulatedRead->new(
       -reference => $genome  , # sequence to generate the read from
       -id        => 'read001', # read ID
       -start     => 3        , # start of the read on the genome forward strand
       -end       => 12       , # end of the read on the genome forward strand
       -strand    => 1        , # genome strand that the read is on
    );

    # Display the sequence of the read
    print $read->seq."\n";

    # Add a tag or MID to the beginning of the read
    $read->mid('ACGT');

    # Add sequencing errors (error positions are 1-based and relative to the
    # error-free MID-containing read)
    my $errors = {};
    $errors->{'8'}->{'+'} = 'AAA'; # insertion of AAA after residue 8
    $errors->{'1'}->{'%'} = 'G';   # substitution of residue 1 by a G
    $errors->{'4'}->{'-'} = undef; # deletion of residue 4
    $read->errors($errors);

    # Display the sequence of the read with errors
    print $read->seq."\n";

    # String representation of where the read came from and its errors
    print $read->desc."\n";

=head1 DESCRIPTION

This object is a simulated read with sequencing errors. The user can provide a
reference sequence to take a read from, the position and orientation of the 
read on the reference sequence, and the sequencing errors to generate.

The sequence of the read is automatically calculated based on this information.
By default, the description of the reads contain tracking information and will
look like this (Bioperl-style):

  reference=human_chr2 start=3 end=12 strand=-1 mid=ACGT errors=1%G,4-,8+AAA description="The human genome"

or Genbank-style:

  reference=human_chr2 position=complement(3..12) mid=ACGT errors=1%G,4-,8+AAA description="The human genome"

Creating a simulated read follows these steps:
  1/ Define the read start(), end(), strand() and qual_levels() if you want
     quality scores to be generated. Do not change these values once set because
     the read will not be updated.
  2/ Specify the reference sequence that the read should be taken from. Once
     this is done, you have a fully functional read. Do not use the reference()
     method again after you have gone to the next step.
  3/ Use mid() to input a MID (or tag or barcode) to add to the beginning of the
     read. You can change the MID until you go to next step.
  4/ Give sequencing error specifications using errors() as the last step. You
     can do that as many times as you like, and the read will be updated.

=head1 AUTHOR

Florent E Angly E<lt>florent . angly @ gmail-dot-comE<gt>.

Copyright (c) 2011 Florent E Angly.

This library is free software; you can redistribute it under the GNU General
Public License version 3.

=cut


use strict;
use warnings;
use Bio::LocatableSeq;
use base qw( Bio::Seq::Quality Bio::LocatableSeq );


=head2 new

 Title    : new
 Function : Create a new simulated read object
 Usage    : my $read = Bio::Seq::SimulatedRead->new(
               -id        => 'read001',
               -reference => $seq_obj ,
               -errors    => $errors  ,
               -start     => 10       ,
               -end       => 135      ,
               -strand    => 1        ,
            );
 Arguments: -reference   => Bio::SeqI, Bio::PrimarySeqI object representing the
                            reference sequence to take the read from. See
                            reference().
            -errors      => Hashref representing the position of errors in the read
                            See errors().
            -mid         => String of a MID to prepend to the read. See mid().
            -track       => Track where the read came from in the read description?
                            See track().
            -coord_style => Define what coordinate system to use. See coord_style().
            All other methods from Bio::LocatableSeq are available.
 Returns  : new Bio::Seq::SimulatedRead object

=cut

sub new {
   my ($class, @args) = @_;
   my $self = $class->SUPER::new(@args);
   my ($qual_levels, $reference, $mid, $errors, $track, $coord_style) =
      $self->_rearrange([qw(QUAL_LEVELS REFERENCE MID ERRORS TRACK COORD_STYLE)], @args);
   $coord_style = defined $coord_style ? $coord_style : 'bioperl';
   $self->coord_style($coord_style);
   $track = defined $track ? $track : 1;
   $self->track($track);
   $qual_levels = defined $qual_levels ? $qual_levels : [];
   $self->qual_levels($qual_levels) if defined $qual_levels;
   $self->reference($reference) if defined $reference;
   $self->mid($mid) if defined $mid;
   $self->{_mutated} = 0;
   $self->errors($errors) if defined $errors;
   return $self;
}


=head2 qual_levels

 Title    : qual_levels
 Function : Get or set the quality scores to give to the read. By default, if your
            reference sequence does not have quality scores, no quality scores
            are generated for the simulated read. The generated quality scores
            are very basic. If a residue is error-free, it gets the quality score
            defined for good residues. If the residue has an error (is an
            addition or a mutation), the residue gets the quality score specified
            for bad residues. Call the qual_levels() method before using the
            reference() method.
 Usage    : my $qual_levels = $read->qual_levels( );
 Arguments: Array reference containing the quality scores to use for:
            1/ good residues (e.g. 30)
            2/ bad residues (e.g. 10)
 Returns  : Array reference containing the quality scores to use.

=cut

sub qual_levels {
   my ($self, $qual_levels) = @_;
   if (defined $qual_levels) {
      if ( (scalar @$qual_levels != 0) && (scalar @$qual_levels != 2) ) {
         $self->throw("The quality score specification must define the score".
            " to use for good and for bad residues\n");
      }
      $self->{qual_levels} = $qual_levels;
   }
   return $self->{qual_levels};
}


=head2 reference

 Title    : reference
 Function : Get or set the reference sequence that the read comes from. Once the
            reference has been set, you have a functional simulated read which
            supports all the Bio::LocatableSeq methods. This method must be
            called after qual_levels() but before mid() or errors().
 Usage    : my $seq_obj = $read->reference();
 Arguments: Bio::SeqI or Bio::PrimarySeqI object
 Returns  : Bio::SeqI or Bio::PrimarySeqI object

=cut

sub reference {
   my ($self, $reference) = @_;
   if (defined $reference) {
      # Sanity check 1
      if ( (not $reference->isa('Bio::SeqI')) && (not $reference->isa('Bio::PrimarySeqI')) ) {
         $self->throw("Expected a Bio::SeqI object as reference, but got: $reference\n");
      }
      # Sanity check 2
      if ($self->{mid} || $self->{errors}) {
         $self->throw("Cannot change the reference sequence after an MID or ".
            "sequencing errors have been added to the read\n");
      }
      # Use beginning of reference sequence as start default
      if (not defined $self->start) {
         $self->start(1);
      }
      # Use end of reference sequence as end default
      if (not defined $self->end) {
         $self->end($reference->length);
      }
      # Use strand 1 as strand default
      if (not defined $self->strand) {
         $self->strand(1);
      }
      # Set the reference sequence object
      $self->{reference} = $reference;
      # Create a sequence, quality scores and description from the reference
      $self->_create_seq;
      $self->_create_qual if scalar @{$self->qual_levels};
      $self->_create_desc if $self->track;
   }
   return $self->{reference};
}


sub _create_seq {
   my $self = shift;
   # Get a truncation of the reference sequence
   my $reference = $self->reference;
   my $read_obj = $reference->trunc( $self->start, $self->end );
   # Reverse complement the read if needed
   if ($self->strand == -1) {
      $read_obj = $read_obj->revcom();
   }
   $self->seq($read_obj->seq);
   return 1;
}


sub _create_qual {
   my $self = shift;
   $self->qual([ ($self->qual_levels->[0]) x ($self->end - $self->start + 1) ]);
   return 1;
}


sub _create_desc {
   # Create the read description of the error-free read
   my $self = shift;
   # Reference sequence ID
   my $desc_str = '';
   my $ref_id = $self->reference->id;
   if (defined $ref_id) {
      $desc_str .= 'reference='.$ref_id.' ';
   }
   # Position of read on reference sequence: start, end and strand
   my $strand = $self->strand;
   if ($self->coord_style eq 'bioperl') {
     $desc_str .= 'start='.$self->start.' end='.$self->end.' ';
     if (defined $strand) {
        # Strand of the reference sequence that the read is on
        $strand = '+1' if $strand == 1;
        $desc_str .= 'strand='.$strand.' ';
     }
   } else {
     if ( (defined $strand) && ($strand == -1) ) {
       # Reverse complemented
       $desc_str .= 'position=complement('.$self->start.'..'.$self->end.') ';
     } else {
       # Regular (forward) orientation
       $desc_str .= 'position='.$self->start.'..'.$self->end.' ';
     }
   }
   # Description of the original sequence
   my $ref_desc = $self->reference->desc;
   if ( (defined $self->reference->desc) && ($self->reference->desc !~ m/^\s*$/) ) {
      $ref_desc =~ s/"/\\"/g; # escape double-quotes to \"
      $desc_str .= 'description="'.$ref_desc.'" ';
   }
   $desc_str =~ s/\s$//g;
   # Record new description
   $self->desc($desc_str);
   return 1;
}


=head2 mid

 Title    : mid
 Function : Get or set a multiplex identifier (or MID, or tag, or barcode) to
            add to the read. By default, no MID is used. This method must be
            called after reference() but before errors().
 Usage    : my $mid = read->mid();
 Arguments: MID sequence string (e.g. 'ACGT')
 Returns  : MID sequence string

=cut

sub mid {
   my ($self, $mid) = @_;
   if (defined $mid) {
      # Sanity check 1
      if (not defined $self->reference) {
         $self->throw("Cannot add MID because the reference sequence was not ".
            "set\n");
      }
      # Sanity check 2
      if ($self->{errors}) {
         $self->throw("Cannot add an MID after sequencing errors have been ".
            "introduced in the read\n");
      }
      # Sanity check 3
      if (not $self->validate_seq($mid)) {
         $self->throw("MID is not a valid DNA sequence\n");
      }
      # Update sequence, quality scores and description with the MID
      $self->_update_seq_mid($mid);
      $self->_update_qual_mid($mid) if scalar @{$self->qual_levels};
      $self->_update_desc_mid($mid) if $self->track;
      # Set the MID value
      $self->{mid} = $mid;
   }
   return $self->{mid}
}


sub _update_seq_mid {
   # Update the MID of a sequence
   my ($self, $mid) = @_;
   # Remove old MID
   my $seq = $self->seq;
   my $old_mid = $self->{mid};
   if (defined $old_mid) {
      $seq =~ s/^$old_mid//;
   }
   # Add new MID
   $seq = $mid . $seq;
   $self->seq( $seq );
   return 1;
}


sub _update_qual_mid {
   # Update the MID of a quality scores
   my ($self, $mid) = @_;
   # Remove old MID
   my $qual = $self->qual;
   my $old_mid = $self->{mid};
   if (defined $old_mid) {
      splice @$qual, 0, length($old_mid);
   }
   $qual =  [($self->qual_levels->[0]) x length($mid), @$qual];
   $self->qual( $qual );
   return 1; 
}


sub _update_desc_mid {
   # Update MID specifications in the read description
   my ($self, $mid) = @_;
   if ($mid) {
      # Sequencing errors introduced in the read
      my $mid_str = "mid=".$mid;
      my $desc_str = $self->desc;
      $desc_str =~ s/((position|strand)=\S+)( mid=\S+)?/$1 $mid_str/g;
      $self->desc( $desc_str );
   }
   return 1;
}


=head2 errors

 Title    : errors
 Function : Get or set the sequencing errors and update the read. By default, no
            errors are made. This method must be called after the mid() method.
 Usage    : my $errors = $read->errors();
 Arguments: Reference to a hash of the position and nature of sequencing errors.
            The positions are 1-based relative to the error-free MID-containing
            read (not relative to the reference sequence). For example:
               $errors->{34}->{'%'} = 'T'  ; # substitution of residue 34 by a T
               $errors->{23}->{'+'} = 'GG' ; # insertion of GG after residue 23
               $errors->{45}->{'-'} = undef; # deletion of residue 45
            Substitutions and deletions are for a single residue, but additions
            can be additions of several residues.
            An alternative way to specify errors is by using array references
            instead of scalar for the hash values. This allows to specify
            redundant mutations. For example, the case presented above would
            result in the same read sequence as the example below:
               $errors->{34}->{'%'} = ['C', 'T']    ; # substitution by a C and then a T
               $errors->{23}->{'+'} = ['G', 'G']    ; # insertion of G and then a G
               $errors->{45}->{'-'} = [undef, undef]; # deletion of residue, and again
 Returns  : Reference to a hash of the position and nature of sequencing errors.

=cut

sub errors {
   my ($self, $errors) = @_;
   if (defined $errors) {
      # Verify that we have a hashref
      if ( (not defined ref $errors) || (not ref $errors eq 'HASH') ) {
         $self->throw("Error specification has to be a hashref. Got: $errors\n");
      }
      # Verify that we have a reference sequence
      if (not defined $self->reference) {
         $self->throw("Cannot add errors because the reference sequence was not set\n");
      }
      # Convert scalar error specs to arrayref specs
      $errors = $self->_scalar_to_arrayref($errors);
      # Check validity of error specifications
      $errors = $self->_validate_error_specs($errors);
      # Set the error specifications
      $self->{errors} = $errors;
      # Need to recalculate the read from the reference if previously mutated
      if ($self->{_mutated}) {
         $self->_create_seq;
         $self->_create_qual if scalar @{$self->qual_levels};
         $self->_create_desc if $self->track;
      }
      # Now mutate the read, quality score and description
      $self->_update_seq_errors;
      $self->_update_qual_errors if scalar @{$self->qual_levels};
      $self->_update_desc_errors if $self->track;

   }
   return $self->{errors};
}


sub _scalar_to_arrayref {
   # Replace the scalar values in the error specs by more versatile arrayrefs
   my ($self, $errors) = @_;
   while ( my ($pos, $ops) = each %$errors ) {
      while ( my ($op, $res) = each %$ops ) {
         if (ref $res eq '') {
            my $arr = [ split //, ($res || '') ];
            $arr = [undef] if scalar @$arr == 0;
            $$errors{$pos}{$op} = $arr;
         }
      }
   }
   return $errors;
}


sub _validate_error_specs {
   # Clean error specifications and warn of any issues encountered
   my ($self, $errors) = @_;
   my %valid_ops = ('%' => undef, '-' => undef, '+' => undef); # valid operations

   # Calculate read length
   my $read_length = $self->length;
   while ( my ($pos, $ops) = each %$errors ) {

      # Position cannot be no longer than the read length
      if ( (defined $read_length) && ($pos > $read_length) ) {
         $self->warn("Position $pos is beyond end of read ($read_length ".
            "residues). Skipping errors specified at this position.\n");
         delete $errors->{$pos};
      }

      # Position has to be 0+ for addition, 1+ for substitution and deletion
      if ( $pos < 1 && (exists $ops->{'%'} || exists $ops->{'-'}) ) {
         $self->warn("Positions of substitutions and deletions have to be ".
            "strictly positive but got $pos. Skipping substitution or deletion".
            " at this position\n");
         delete $ops->{'%'};
         delete $ops->{'-'};
      }
      if ( $pos < 0 && exists $ops->{'+'}) {
         $self->warn("Positions of additions have to be zero or more. ".
            "Skipping addition at position $pos.\n");
         delete $ops->{'+'};
      }

      # Valid operations are '%', '+' and '-'
      while ( my ($op, $res) = each %$ops ) {
         if (not exists $valid_ops{$op}) {
            $self->warn("Skipping unknown error operation '$op' at position".
               " $pos\n");
            delete $ops->{$op};
         } else {
            # Substitutions: have to have at least one residue to substitute
            if ( ($op eq '%') && (scalar @$res < 1) ) {
               $self->warn("At least one residue must be provided for substitutions,".
                  "but got ".scalar(@$res)." at position $pos.\n");
            }
            # Additions: have to have at least one residue to add
            if ( ($op eq '+') && (scalar @$res < 1) ) {
               $self->warn("At least one residue must be provided for additions,".
                  "but got ".scalar(@$res)." at position $pos.\n");
            }
            # Deletions
            if ( ($op eq '-') && (scalar @$res < 1) ) {
               $self->warn("At least one 'undef' must be provided for deletions,".
                  "but got ".scalar(@$res)." at position $pos.\n");
            }
         }
      }

      delete $errors->{$pos} unless scalar keys %$ops;
   }

   return $errors;
}


sub _update_seq_errors {
   my $self = shift;
   my $seq_str = $self->seq;
   my $errors = $self->errors;
   if (scalar keys %$errors > 0) {
      my $off = 0;
      for my $pos ( sort {$a <=> $b} (keys %$errors) ) {
         # Process sequencing errors at that position
         for my $type ( '%', '-', '+' ) {
            next if not exists $$errors{$pos}{$type};
            my $arr = $$errors{$pos}{$type};
            if ($type eq '%') {
               # Substitution at residue position. If there are multiple
               # substitutions to do, directly skip to the last one.
               substr $seq_str, $pos - 1 + $off, 1, $$arr[-1];
            } elsif ($type eq '-') {
               # Deletion at residue position
               substr $seq_str, $pos - 1 + $off, 1, '';
               $off--;
            } elsif ($type eq '+') {
               # Insertion after residue position
               substr $seq_str, $pos + $off, 0, join('', @$arr);
               $off += scalar @$arr;
            }
         }
      }
      $self->{_mutated} = 1;
   } else {
      $self->{_mutated} = 0;
   }
   $self->seq($seq_str);
   return 1;
}


sub _update_qual_errors {
   my $self = shift;
   my $qual = $self->qual;
   my $errors = $self->errors;
   my $bad_qual = $self->qual_levels->[1];
   if (scalar keys %$errors > 0) {
      my $off = 0;
      for my $pos ( sort {$a <=> $b} (keys %$errors) ) {
         # Process sequencing errors at that position
         for my $type ( '%', '-', '+' ) {
            next if not exists $$errors{$pos}{$type};
            my $arr = $$errors{$pos}{$type};
            if ($type eq '%') {
               # Substitution at residue position
               splice @$qual, $pos - 1 + $off, 1, $bad_qual;
            } elsif ($type eq '-') {
               # Deletion at residue position
               splice @$qual, $pos - 1 + $off, 1;
               $off--;
            } elsif ($type eq '+') {
               # Insertion after residue position
               splice @$qual, $pos + $off, 0, ($bad_qual) x scalar(@$arr);
               $off += scalar @$arr;
            }
         }
      }
   }
   $self->qual($qual);
   return 1;
}


sub _update_desc_errors {
   # Add or update error specifications in the read description
   my $self = shift;
   my $errors = $self->errors;
   if (defined $errors and scalar keys %$errors > 0) {
      # Sequencing errors introduced in the read
      my $err_str = 'errors=';
      for my $pos ( sort {$a <=> $b} (keys %$errors) ) {
         # Process sequencing errors at that position
         for my $type ( '%', '-', '+' ) {
            next if not exists $$errors{$pos}{$type};
            for my $val ( @{$$errors{$pos}{$type}} ) {
               $val = '' if not defined $val;
               $err_str .= $pos . $type . $val . ',';
            }
         }
      }
      $err_str =~ s/,$//;
      my $desc_str = $self->desc;
      $desc_str =~ s/((position|strand)=\S+( mid=\S+)?)( errors=\S+)?/$1 $err_str/g;
      $self->desc( $desc_str );
   }
   return 1;
}


=head2 track

 Title    : track
 Function : Get or set the tracking status in the read description. By default,
            tracking is on. This method can be called at any time.
 Usage    : my $track = $read->track();
 Arguments: 1 for tracking, 0 otherwise
 Returns  : 1 for tracking, 0 otherwise

=cut

sub track {
   my ($self, $track) = @_;
   if (defined $track) {
      if (defined $self->reference) {
         if ($track == 1) {
            $self->_create_desc;
            $self->_update_desc_mid($self->mid);
            $self->_update_desc_errors;
         } else {
            $self->desc(undef);
         }
      }
      $self->{track} = $track;
   }
   return $self->{track};
}


=head2 coord_style

 Title    : coord_style
 Function : When tracking is on, define which 1-based coordinate system to use
            in the read description:
              * 'bioperl' uses the start, end and strand keywords (default),
                similarly to the GFF3 format. Example:
                  start=1 end=10 strand=+1
                  start=1 end=10 strand=-1
              * 'genbank' does only provide the position keyword. Example:
                  position=1..10
                  position=complement(1..10)
 Usage    : my $coord_style = $read->track();
 Arguments: 'bioperl' or 'genbank'
 Returns  : 'bioperl' or 'genbank'

=cut

sub coord_style {
   my ($self, $coord_style) = @_;
   my %styles = ( 'bioperl' => undef, 'genbank' => undef );
   if (defined $coord_style) {
      if (not exists $styles{$coord_style}) {
         die "Error: Invalid coordinate style '$coord_style'\n";
      }
      $self->{coord_style} = $coord_style;
   }
   return $self->{coord_style};
}


1;