#!/usr/bin/perl

$fname = $ARGV[0];

open( MYFILE, $fname ) or die "Cannot open  $fname : $!";

$discarded = 0;
$print_seq = false;

while ( <MYFILE> )
{
  $a = $_;

  if( $a =~ /\>/ )
    {
      $a =~ s/\>//;
      @parts = split( "\t" , $a);
      my $length=$parts[9]-$parts[4]+1;
      if( $parts[2] eq "-" && $parts[8] eq "+" )
	{
	  print ">$parts[6] $parts[7] $parts[8] $parts[4] $parts[9] $length\n";
	  $line = <MYFILE>;
	  $line =~ s/N//g;
	  $line =~ s/\-//g;
	  $line =~ s/\n//g;
	  print "$line\n";
	  print ">$parts[0] $parts[1] $parts[2] $parts[4] $parts[9] $length\n";
	  printRevComp( $line );
	}
      elsif( $parts[2] eq "+" && $parts[8] eq "+" )
	    {
	      print ">$parts[6] $parts[7] $parts[8] $parts[4] $parts[9] $length\n";
	      $line = <MYFILE>;
	      $line =~ s/N//g;
	      $line =~ s/\-//g;
	      $line =~ s/\n//g;
	      print "$line\n";
	    }
      elsif( $parts[2] eq "-" && $parts[8] eq "-" )
	{
	  print ">$parts[0] $parts[1] $parts[2] $parts[4] $parts[9] $length\n";
	  $line = <MYFILE>;
	  $line =~ s/N//g;
	  $line =~ s/\-//g;
	  $line =~ s/\n//g;
	  printRevComp( $line );
	}
      elsif( $parts[2] eq "+" && $parts[8] eq "-" )
	{
          ++$discarded;
	}
      else
	{
	}
#     for ( $i = 0; $i < @parts; ++$i ) 
#       {
#	 print "Substring $i: $parts[$i]\n";
#       }
    }
}
#print "$discarded\n";


sub printRevComp( $$ )
{
  $seq = $_[0];
  $seq =~ s/A/x/g;
  $seq =~ s/T/A/g;
  $seq =~s/x/T/g;
  $seq =~ s/C/x/g;
  $seq =~ s/G/C/g;
  $seq =~s/x/G/g;
  $seq = reverse( $seq );
  $seq =~ s/\n//g;
  print "$seq\n";
}
