#!/usr/bin/perl

use strict;

die "program csfasta shift\nif shift is 1, the first base is omitted in the output\n" if (@ARGV < 1);

my $fasta_file = $ARGV[0];
my %colourspace = colour_space();
my $shift = 0;
my $sequences;
my $order;
($sequences, $order) = read_fasta( $fasta_file );
#print $$order[0];
$shift = $ARGV[1] if( @ARGV == 2 );

#foreach my $key ( keys %sequences ){
foreach my $key(@{$order}){
    print ">$key\n"; 
    my $seq = $$sequences{ $key };
    my @letters = split( //, $seq );
    my $first_base = $letters[0];
    my $current = $letters[0];

    for( my $i = 1; $i < @letters ; $i++ ){
	
	my $next = $letters[$i];
	my $encoding = $current.$next;
	
	my $color = $colourspace{ $encoding };
	$letters[ $i ] = $color;    
	$current=$next;
    }
    
    shift( @letters ) if( $shift );
    $" = "";
    print "@letters\n";

}


sub colour_space{

    my %hash = (
	"AA" => "0",
	"AC" => "1",
	"AG" => "2",
	"AT" => "3",
	"CA" => "1",
	"CC" => "0",
	"CG" => "3",
	"CT" => "2",
	"GA" => "2",
	"GC" => "3",
	"GG" => "0",
	"GT" => "1",
	"TA" => "3",
	"TC" => "2",
	"TG" => "1",
	"TT" => "0" );
    return %hash;


}




sub read_fasta{
    my ( $fasta_file) = @_;
    my %sequences;
    my $first = 1;
	my @order;
    
    my $header;
    my $sequence;
    
    open(FASTA, $fasta_file);
    while( my $line = <FASTA> ){
        
	chomp $line;    
	
	if( ! $first ){     # $sequence and header are not initialized in first iteration 
	    
	    if(  $line =~ /\>/   ){         # encounters a new sequence             
		
		$sequences{$header} = $sequence;
		push(@order,$header);
		$header = $line; $header =~ tr/\>//d;  # read a new header line
		$sequence = "";
		
	    }
	    
	    else{ $sequence .= $line;       # concatenate sequences
	    }
	}
	else{                                       # read the header line
	    if(  $line =~ /\>/   ){ 
	    
		$header = $line; $header =~ tr/\>//d;$sequence = "";
		
	    }
	}       
	$first = 0;
    }
    close( FASTA );
    
    ## the last one
    $sequences{$header} = $sequence;  
	push(@order,$header);
  
    return (\%sequences, \@order);
}


