Perl:
#!/usr/bin/perl
use strict;
use warnings;
my %seqs =();
$/="\n>";
while (<>) {
s/>//g;
my ($name, @seq) = split (/\n/, $_);
my ($sp, $id) = split (/\./, $name);
push @{ $seqs{$sp} }, join "\n", ">$name", @seq;
}
foreach my $sp (keys %seqs) {
print $seqs{$sp}[ int( rand @{ $seqs{$sp} }) ], "\n";
}
Examples:
perl rand.pl < seqs.fa
>SpeciesC.seq0
GCCCTTTA.....
>SpeciesA.seq0
CCACTTTA......
>SpeciesB.seq1
GCCCTTTA.....
$ perl rand.pl < seqs.fa
>SpeciesB.seq0
GCCCTTTA......
>SpeciesC.seq1
GCCCTTTA.....
>SpeciesA.seq1
CCTCTTTA......
Explanation: use a hash of arrays, each species collects their sequences in an array, then just iterate over species getting one random value of the array length.
If an answer below was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
