-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a module in t/lib for generating sets of random test sequence
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
=head1 NAME | ||
RandomSeq - supporting module for making random sequences, and files full of them | ||
=head1 METHODS | ||
=cut | ||
|
||
package RandomSeq; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
=head2 random_fasta( %args ) | ||
Example: | ||
random_fasta( | ||
file => 'path/to/target.fasta', | ||
num_seqs => 3, | ||
avg_length => 4000, | ||
length_plus_or_minus => 4000 * 0.1, | ||
); | ||
=cut | ||
|
||
sub random_fasta { | ||
my ( $self, %args ) = @_; | ||
|
||
$args{num_seqs} ||= 3; | ||
$args{avg_length} ||= 4000; | ||
$args{length_plus_or_minus} = $args{avg_length} * 0.1 | ||
unless defined $args{length_plus_or_minus}; | ||
|
||
open my $test_fasta, '>', $args{file} or die "$! writing $args{file}"; | ||
while ( $args{num_seqs}-- ) { | ||
my $length = int( $args{avg_length} + $args{length_plus_or_minus} * ( rand(2) - 1 ) ); | ||
$test_fasta->print( ">Random_$args{num_seqs}\n", $self->random_seq( $length ), "\n" ); | ||
} | ||
} | ||
|
||
=head2 random_seq( $length ) | ||
Return a random string of A,C,T,G,N of the given length. | ||
=cut | ||
|
||
sub random_seq { | ||
my ( $self, $length ) = @_; | ||
my $rand = '0' x $length; | ||
$rand =~ s/ . / [qw( A C T G N )]->[ int rand 5 ] /xge; | ||
return $rand; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use File::Temp; | ||
|
||
use lib 'tests/perl_tests/lib'; | ||
|
||
use_ok( 'RandomSeq' ); | ||
is( length RandomSeq->random_seq(10), 10 ); | ||
|
||
my $t = File::Temp->new; | ||
RandomSeq->random_fasta( file => "$t", length_plus_or_minus => 0 ); | ||
$t->close; | ||
|
||
is( -s "$t", 12033 ); | ||
|
||
done_testing; |