#!/usr/bin/perl -w

use strict;

#take the input file from command line
my $infile = $ARGV[0];

#define a read-only file handler
open(IN,"<$infile") || die "Can't open $infile:$!\n";

#boolean variable for if this is the first header
my $first = 1;


my $seq;
my $rna;
my $revcomp;
my $header;
#go through the input file line by line
while(<IN>)
{
    #get rid of the newline character
    chomp;

    

    #skip header line
    if($_=~/^>(.*)$/)
    {
	$header = $1;
	#call two subrountine
	if(!$first)
	{
	    print "$header:$seq\n";
	    $rna = my_tran($seq);
	    print "rna:$rna\n";
	    $revcomp = my_revcomp($seq);
	    print "revcomp: $revcomp\n";
	}
	else
	{
	    #change the flag to false
	    $first = 0;
	}
	
	#clean out the $seq variable
	$seq = "";
    }
    else
    {
	#concatenate the sequence
	$seq .=$_;
    }
	
}

#handle the last sequence
print "$header:$seq\n";
$rna = my_tran($seq);
print "rna:$rna\n";
$revcomp = my_revcomp($seq);
print "revcomp: $revcomp\n";


#check if the sequence is DNA or not
sub check_dna($)
{
    my($seq) = @_;
    
    if($seq !~/^[ACGTNacgtn]+$/)
    {
	return 0;
    }
    else
    {
	return 1;
    }
}

sub my_tran($)
{
    my($dna) = @_;
    
    my $rna;

    my $result = check_dna($dna);
    if($result)
    {
	$rna = $dna;
	$rna =~ s/T/U/g;
	$rna =~ s/t/u/g;
	return ($rna);
	
    }
    else
    {
	print STDERR "It seems the sequence is not DNA, you should double check the input!\n:$dna\n";
    }
    
}

sub my_revcomp($)
{
    my($seq) = @_;
    
    #should run the check function here too.
    my $rev = reverse($seq);
    my $revcom = $rev;

    #this won't work -- why?
    #revcom =~ s/A/T/g;
    #$revcom =~ s/T/A/g;
    #$revcom =~ s/G/C/g;
    #$revcom =~ s/C/G/g;


    
    $revcom =~ tr/ACGTacgt/TGCAtgca/;
    
    return($revcom);
}
	

