Skip to content

Commit

Permalink
add a module in t/lib for generating sets of random test sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuels committed Feb 8, 2012
1 parent 8471134 commit 3897f39
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/perl_tests/lib/RandomSeq.pm
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;
18 changes: 18 additions & 0 deletions tests/perl_tests/random_seq.t
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;

0 comments on commit 3897f39

Please sign in to comment.