title: Generating a random subset sequences from a fasta file
body: <p>Hello to Perl Masters in the world.</p>
  
  <p>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.</p>
  
  <p>A general fasta file looks like this:</p>
  
  <blockquote>
    <p>>seq_ID_1 descriptions etc
    ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH
    ASDSADGDASHDASHSAREWAWGDASHASGASGASGSDGASDGDSAHSHAS
    SFASGDASGDSSDFDSFSDFSD</p>
    
    <p>>seq_ID_2 descriptions etc
    ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH
    ASDSADGDASHDASHSAREWAWGDASHASGASGASG</p>
    
    <p>and so on.......</p>
  </blockquote>
  
  <p>The letters represent amino acid peptides.</p>
  
  <p>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.</p>
  
  <p>This is my code for generating random sequence subset, but its slightly not so good at working.</p>
  
  <pre><code>#!/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, "&gt;temp1");
  while (my $line = &lt;LIST&gt;){
      chomp $line;
      if ($line =~ /&gt;(\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,'&lt;','temp1');
  open(TEMP2, "&gt;temp2");
  
  do {
       push @line_starts, tell $FH
  } while ( &lt;$FH&gt; );
  
  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 &lt;$FH&gt;;
  }
  close(TEMP2);
  
  #Assigning the sequence data to randomly generated header file.
  open(DATA,'&lt;','temp2');
  while(my $line = &lt;DATA&gt;)
  {
      chomp($line);
      $line =~ s/[\t\s]//g;
      if($line =~ /^([^\s]+)/)
      {
          $protseq{$1}++;
      }
  }
  close(DATA);
  
  open(DATA, "$FILE");
  open(OUT, "&gt;random_seqs.fasta");
  while(my $line = &lt;DATA&gt;)
  {
      chomp($line);
      if($line =~ /^&gt;([^\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] &lt;=&gt; 0));
  }
  
  system("erase temp1");
  system("erase temp2");
  exit;
  </code></pre>
  
  <p>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?</p>
  
  <p>or Perhaps better shorter code?</p>
  
  <p>here you can obtains a 75 yeast proteom file. [http://www.peroxisomedb.org/Download/Saccharomyces_cerevisiae.fas][1]</p>
  
  <p>Hope I can fix this soon... :(</p>