Bio::Seq
SimulatedRead
Toolbar
Summary
Bio::Seq::SimulatedRead - Read with sequencing errors taken from a reference sequence
Package variables
No package variables defined.
Included modules
Inherit
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";
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.
Methods
Methods description
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 |
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. |
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 |
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 |
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. |
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 |
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' |
Methods code
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;} |
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};} |
sub reference
{ my ($self, $reference) = @_;
if (defined $reference) {
if ( (not $reference->isa('Bio::SeqI')) && (not $reference->isa('Bio::PrimarySeqI')) ) {
$self->throw("Expected a Bio::SeqI object as reference, but got: $reference\n");
}
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");
}
if (not defined $self->start) {
$self->start(1);
}
if (not defined $self->end) {
$self->end($reference->length);
}
if (not defined $self->strand) {
$self->strand(1);
}
$self->{reference} = $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;
my $reference = $self->reference;
my $read_obj = $reference->trunc( $self->start, $self->end );
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
{ my $self = shift;
my $desc_str = '';
my $ref_id = $self->reference->id;
if (defined $ref_id) {
$desc_str .= 'reference='.$ref_id.' ';
}
my $strand = $self->strand;
if ($self->coord_style eq 'bioperl') {
$desc_str .= 'start='.$self->start.' end='.$self->end.' ';
if (defined $strand) {
$strand = '+1' if $strand == 1;
$desc_str .= 'strand='.$strand.' ';
}
} else {
if ( (defined $strand) && ($strand == -1) ) {
$desc_str .= 'position=complement('.$self->start.'..'.$self->end.') ';
} else {
$desc_str .= 'position='.$self->start.'..'.$self->end.' ';
}
}
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;
$self->desc($desc_str);
return 1; } |
sub mid
{ my ($self, $mid) = @_;
if (defined $mid) {
if (not defined $self->reference) {
$self->throw("Cannot add MID because the reference sequence was not ".
"set\n");
}
if ($self->{errors}) {
$self->throw("Cannot add an MID after sequencing errors have been ".
"introduced in the read\n");
}
if (not $self->validate_seq($mid)) {
$self->throw("MID is not a valid DNA sequence\n");
}
$self->_update_seq_mid($mid);
$self->_update_qual_mid($mid) if scalar @{$self->qual_levels};
$self->_update_desc_mid($mid) if $self->track;
$self->{mid} = $mid;
}
return $self->{mid}} |
sub _update_seq_mid
{ my ($self, $mid) = @_;
my $seq = $self->seq;
my $old_mid = $self->{mid};
if (defined $old_mid) {
$seq =~ s/^$old_mid//;
}
$seq = $mid . $seq;
$self->seq( $seq );
return 1; } |
sub _update_qual_mid
{ my ($self, $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
{ my ($self, $mid) = @_;
if ($mid) {
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; } |
sub errors
{ my ($self, $errors) = @_;
if (defined $errors) {
if ( (not defined ref $errors) || (not ref $errors eq 'HASH') ) {
$self->throw("Error specification has to be a hashref. Got: $errors\n");
}
if (not defined $self->reference) {
$self->throw("Cannot add errors because the reference sequence was not set\n");
}
$errors = $self->_scalar_to_arrayref($errors);
$errors = $self->_validate_error_specs($errors);
$self->{errors} = $errors;
if ($self->{_mutated}) {
$self->_create_seq;
$self->_create_qual if scalar @{$self->qual_levels};
$self->_create_desc if $self->track;
}
$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
{ 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
{ my ($self, $errors) = @_;
my %valid_ops = ('%' => undef, '-' => undef, '+' => undef);
my $read_length = $self->length;
while ( my ($pos, $ops) = each %$errors ) {
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};
}
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->{'+'};
}
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 {
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");
}
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");
}
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) ) {
for my $type ( '%', '-', '+' ) {
next if not exists $$errors{$pos}{$type};
my $arr = $$errors{$pos}{$type};
if ($type eq '%') {
substr $seq_str, $pos - 1 + $off, 1, $$arr[-1];
} elsif ($type eq '-') {
substr $seq_str, $pos - 1 + $off, 1, '';
$off--;
} elsif ($type eq '+') {
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) ) {
for my $type ( '%', '-', '+' ) {
next if not exists $$errors{$pos}{$type};
my $arr = $$errors{$pos}{$type};
if ($type eq '%') {
splice @$qual, $pos - 1 + $off, 1, $bad_qual;
} elsif ($type eq '-') {
splice @$qual, $pos - 1 + $off, 1;
$off--;
} elsif ($type eq '+') {
splice @$qual, $pos + $off, 0, ($bad_qual) x scalar(@$arr);
$off += scalar @$arr;
}
}
}
}
$self->qual($qual);
return 1;} |
sub _update_desc_errors
{ my $self = shift;
my $errors = $self->errors;
if (defined $errors and scalar keys %$errors > 0) {
my $err_str = 'errors=';
for my $pos ( sort {$a <=> $b} (keys %$errors) ) {
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; } |
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};} |
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;} |
General documentation
Florent E Angly <florent . angly @ gmail-dot-com>.
Copyright (c) 2011 Florent E Angly.
This library is free software; you can redistribute it under the GNU General
Public License version 3.