#!/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 $csfasta_file = $ARGV[0];
my %colourspace = colour_space();
my $shift = 0;

$shift = $ARGV[1] if( @ARGV == 2 );

my $sequence="";
open(CSFASTA, $csfasta_file);
while( my $line = <CSFASTA> ){
        
	if(  $line =~ /\>/   ){         # encounters a new sequence             
    	if($sequence){convert($sequence);}
		print $line; 
		$sequence = "";
	 }
    else{ chomp($line); $sequence .= $line;   }    # concatenate sequences
}
close( CSFASTA );
    
## the last one
convert($sequence);    
    
exit;



sub convert{
    my ( $seq) = @_;
    my @letters = split( //, $seq );
    my $first_base = $letters[0];
    
    for( my $i = 1; $i < @letters ; $i++ ){
	
	my $colour = $letters[$i];
	my $encoding = $first_base.$colour;
	$first_base = $colourspace{ $encoding };
	$letters[ $i ] = $first_base;    
    }
    
    shift( @letters ) if( $shift );
    $" = "";
    print "@letters\n";

}


sub colour_space{

    my %hash = (
	"A0" => "A",
	"C0" => "C",
	"G0" => "G",
	"T0" => "T",
	"A1" => "C",
	"C1" => "A",
	"G2" => "A",
	"A2" => "G",
	"A3" => "T",
	"T3" => "A",
	"C2" => "T",
	"T2" => "C",
	"C3" => "G",
	"G3" => "C",
	"G1" => "T",
	"T1" => "G" );
    return %hash;


}




