Hello to Perl Masters in the world.
I have another trouble for programming. I am coding a program which selects random sequences from a proteom fasta file with a certain input number.
A general fasta file looks like this:
>seq_ID_1 descriptions etc ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH ASDSADGDASHDASHSAREWAWGDASHASGASGASGSDGASDGDSAHSHAS SFASGDASGDSSDFDSFSDFSD
>seq_ID_2 descriptions etc ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH ASDSADGDASHDASHSAREWAWGDASHASGASGASG
and so on.......
The letters represent amino acid peptides.
So I have a fasta file with 1000 sequences and want to retrieve 63.21% of them, which will be 632.1 sequenes. But sequence cannot be floting number so if it exceeds 0.5 I want to round up and if less than 0.5 round down.
This is my code for generating random sequence subset, but its slightly not so good at working.
#!/usr/bin/perl
#Selecting 63.21% of random sequnces from a proteom file.
use strict;
use warnings;
use List::Util qw(shuffle);
#Give the first argument as a proteom file.
if (@ARGV != 1)
{
print "Invalid arguments\n";
print "Usage: perl randseq.pl [proteom_file]";
exit(0);
}
my $FILE = $ARGV[0];
my $i = 0;
my %protseq = {};
my $nIdx = 0;
#Extraction and counting of the all headers from a proteom file.
open(LIST,$FILE);
open(TEMP1, ">temp1");
while (my $line = <LIST>){
chomp $line;
if ($line =~ />(\S+) (.+)/){
$i++;
print TEMP1 $1,"\n";
}
}
close(LIST);
close(TEMP1);
#Selection of random headers for generating a random subset of the proteom file.
my $GET_LINES = RoundToInt ($i*0.6321);
my @line_starts;
open(my $FH,'<','temp1');
open(TEMP2, ">temp2");
do {
push @line_starts, tell $FH
} while ( <$FH> );
my $count = @line_starts;
my @shuffled_starts = (shuffle @line_starts)[1..$GET_LINES+1];
for my $start ( @shuffled_starts ) {
seek $FH, $start, 0
or die "Unable to seek to line - $!\n";
print TEMP2 scalar <$FH>;
}
close(TEMP2);
#Assigning the sequence data to randomly generated header file.
open(DATA,'<','temp2');
while(my $line = <DATA>)
{
chomp($line);
$line =~ s/[\t\s]//g;
if($line =~ /^([^\s]+)/)
{
$protseq{$1}++;
}
}
close(DATA);
open(DATA, "$FILE");
open(OUT, ">random_seqs.fasta");
while(my $line = <DATA>)
{
chomp($line);
if($line =~ /^>([^\s]+)/)
{
if($protseq{$1} ne "")
{
$nIdx = 1;
print OUT "$line\n";
}
else
{
$nIdx = 0;
}
}
else
{
if($nIdx == 1)
{
print OUT "$line\n";
}
}
}
close(DATA);
close(OUT);
#subroutine for rounding
sub RoundToInt {
int($_[0] + .5 * ($_[0] <=> 0));
}
system("erase temp1");
system("erase temp2");
exit;
However, it sometimes give the proper number of sequences and sometimes with one more sequence. How can I get rid off that... any ideas please?
or Perhaps better shorter code?
here you can obtains a 75 yeast proteom file. [http://www.peroxisomedb.org/Download/Saccharomyces_cerevisiae.fas][1]
Hope I can fix this soon... :(