#! /usr/bin/perl -w
#
#

# usage:
#  ./gen_local_sequence.pl query_oneline nseq shift
#
#  query_oneline: the query sequence, Q in one line
#  nseq         : number of sequences to be generated
#  nshift       : from this position (e.g. nshift = 0 from begining) 

$seq_p = "TCAATATCCACCTGCAGATTCTACCAAAAGTGTATTTGGAAACTGCTCCATCAAAAGGCATGTTCAGCTGAATTCAGCTGAACATGCCTTTTGATGGAGCAGTTTCCAAATACACTTTTGGTAGAATCTGCAGGTGGATATTGAGGG";
  $length = length($seq_p);

  $file_chr = $ARGV[0];
  $nfile    = $ARGV[1];
  $shift    = $ARGV[2];
  open(FILE_CHR,"$file_chr");  
  $nline = 0;
  while (<FILE_CHR>) {
        chomp; $nline++;
        $line_chr[$nline] = $_; 
  }

# Get the mutated sequences and write them out in files
  $file_num = "00000";

 for ($k=1;$k<=$shift;$k++) {$file_num++;}

 for ($k=1;$k<=$nfile;$k++) {

# Get the primary sequence
  for ($i=1;$i<=$length;$i++) { 
       $res_prim = substr($line_chr[1],$shift+$k-1+$i-1,1); 
       substr($seq_p,$i-1,1) = $res_prim; 
  }#endfor#  

# Get the complementary sequence
  $seq_c = $seq_p;
  for ($i=1;$i<=$length;$i++) {
       $res = substr($seq_p,$length-$i,1);
#DEBUG
#       print "$res\n";
#DEBUG
       if ($res eq 'A') {$res_cmpl = 'T';}
       if ($res eq 'T') {$res_cmpl = 'A';}
       if ($res eq 'G') {$res_cmpl = 'C';}
       if ($res eq 'C') {$res_cmpl = 'G';}
       substr($seq_c,$i-1,1) = $res_cmpl; 
  }#endfor#

       $file_num++;
       $file_name = "SEQ_".$file_num.".dat";
       open(FILE,">$file_name");
       print FILE "SEQ I >$seq_p\n";
       print FILE "SEQ J >$seq_c\n";
 }#endfor#

