#!/usr/bin/perl

use strict;
use warnings;
use POSIX;
use Data::Dumper;
use lib '/Users/jkwan/scripts/sequence-toolkit';
use Sequence_toolkit;

my %L2_fastas = (
	'ef_46'		=>	'L2_ef_46.fasta',
	'ef_427'	=>	'L2_ef_427.fasta',
	'ef_432'	=>	'L2_ef_432.fasta',
	'ef_1545'	=>	'L2_ef_1545.fasta',
	'Guan'		=>	'L2_Guan.fasta',
	'L5_783'	=>	'L2_L5_783.fasta',
	'L5_1092'	=>	'L2_L5_1092.fasta',
	'UTPglc'	=>	'L2_UTPglc.fasta',
	'23Smt'		=>	'L2_23Smt3.fasta'
);

my %directions = (
	'ccb3'		=>	'forward',
	'ef_46'		=>	'forward',
	'ef_427'	=>	'reverse',
	'ef_432'	=>	'forward',
	'ef_1545'	=>	'reverse',
	'Guan'		=>	'forward',
	'L5_783'	=>	'forward',
	'L5_1092'	=>	'forward',
	'UTPglc'	=>	'forward',
	'23Smt'		=>	'forward',
);

my %L5_fastas = (
	'ef_46'		=>	'L5_ef_46.fasta',
	'ef_427'	=>	'L5_ef_427.fasta',
	'ef_432'	=>	'L5_ef_432.fasta',
	'ef_1545'	=>	'L5_ef_1545.fasta',
	'Guan'		=>	'L5_Guan.fasta',
	'L5_783'	=>	'L5_L5_783.fasta',
	'L5_1092'	=>	'L5_L5_1092.fasta',
	'UTPglc'	=>	'L5_UTPglc.fasta',
	'23Smt'		=>	'L5_23Smt.fasta',
);

# Make track files for L2
my $L2_frame1_output = 'L2_frame1_startstop_track.circos';
my $L2_frame2_output = 'L2_frame2_startstop_track.circos';
my $L2_frame3_output = 'L2_frame3_startstop_track.circos';
my $karyotype_file = 'Pseudogene_karyotype.txt';
open (my $track1, ">$L2_frame1_output") or die "\nCouldn't open $L2_frame1_output\n";
open (my $track2, ">$L2_frame2_output") or die "\nCouldn't open $L2_frame2_output\n";
open (my $track3, ">$L2_frame3_output") or die "\nCouldn't open $L2_frame3_output\n";
open (KARYOTYPE, ">$karyotype_file") or die "\nCouldn't open $karyotype_file\n";
my @trackFiles = ( $track1, $track2, $track3 );

# Work out start/stop coordinates and write karyotype file
# In the output file, 1 = Start codon, 2 = Stop codon
foreach my $chromosome ( keys %L2_fastas )
{
	my $seqHashRef = Sequence_toolkit::fastaToHash( $L2_fastas{ $chromosome } );
	my @seqNames = keys %{ $seqHashRef }; 
	my $seqName = $seqNames[0]; # Will only be one seq in file
	my $seq = $seqHashRef->{ $seqName };
	my $chromosomeLength = length( $seq );

	# Print to karyotype file
	print KARYOTYPE "chr - $chromosome $chromosome 1 $chromosomeLength gpos75\n";

	my $sixFramesHashRef = Sequence_toolkit::getSixFramesFromSeq( $seq );

	my @framesToGet;
	my $startingCoordinate = 0;
	if ( $directions{ $chromosome } eq 'forward' )
	{
		@framesToGet = ( 1, 2, 3 );
	} elsif ( $directions{ $chromosome } eq 'reverse' ) {
		@framesToGet = ( -1, -2, -3 );
	}

	foreach my $frame ( @framesToGet )
	{
		# Always counting forwards, because reverse chromosomes are flipped
		my $currentCoord = $startingCoordinate + abs( $frame ); 
		my $seqInFrame = $sixFramesHashRef->{ $frame };

		my @codons = Sequence_toolkit::getCodonsFromSeq( $seqInFrame );
		my $fh = $trackFiles[ ( abs($frame) - 1 ) ];
		foreach my $codon ( @codons )
		{
			if ( Sequence_toolkit::getTranslationForCodon( $codon, 11 ) eq '*' ) {
				my $endCoord = $currentCoord + 2;
				print $fh "$chromosome\t$currentCoord\t$endCoord\t2\n";
			}

			$currentCoord += 3;
		}
	}
}
# Close files
close $track1;
close $track2;
close $track3;
close KARYOTYPE;

# Do the same thing for L5
my $L5_frame1_output = 'L5_frame1_startstop_track.circos';
my $L5_frame2_output = 'L5_frame2_startstop_track.circos';
my $L5_frame3_output = 'L5_frame3_startstop_track.circos';
open ($track1, ">$L5_frame1_output") or die "\nCouldn't open $L5_frame1_output\n";
open ($track2, ">$L5_frame2_output") or die "\nCouldn't open $L5_frame2_output\n";
open ($track3, ">$L5_frame3_output") or die "\nCouldn't open $L5_frame3_output\n";

foreach my $chromosome ( keys %L5_fastas )
{
	my $seqHashRef = Sequence_toolkit::fastaToHash( $L5_fastas{ $chromosome } );
	my @seqNames = keys %{ $seqHashRef }; 
	my $seqName = $seqNames[0]; # Will only be one seq in file
	my $seq = $seqHashRef->{ $seqName };

	my $sixFramesHashRef = Sequence_toolkit::getSixFramesFromSeq( $seq );

	my @framesToGet;
	my $startingCoordinate = 0;
	if ( $directions{ $chromosome } eq 'forward' )
	{
		@framesToGet = ( 1, 2, 3 );
	} elsif ( $directions{ $chromosome } eq 'reverse' ) {
		@framesToGet = ( -1, -2, -3 );
	}

	foreach my $frame ( @framesToGet )
	{
		# Always counting forwards, because reverse chromosomes are flipped
		my $currentCoord = $startingCoordinate + abs( $frame ); 
		my $seqInFrame = $sixFramesHashRef->{ $frame };

		my @codons = Sequence_toolkit::getCodonsFromSeq( $seqInFrame );
		my $fh = $trackFiles[ ( abs($frame) - 1 ) ];
		foreach my $codon ( @codons )
		{
			if ( Sequence_toolkit::getTranslationForCodon( $codon, 11 ) eq '*' ) {
				my $endCoord = $currentCoord + 2;
				print $fh "$chromosome\t$currentCoord\t$endCoord\t2\n";
			}

			$currentCoord += 3;
		}
	}
}

# Close files
close $track1;
close $track2;
close $track3;