#!/usr/bin/perl
# converting landscape.gtf file into refomated tab file
########### code body ###########
if ($#ARGV != 1)
{
	print "\n";
	print "usage: gtf2events.pl landscape.gtf output.tab\n";
	exit;
}

$inF = $ARGV[0];
$outF = $ARGV[1];

open (IN, $inF) || die ("Cannot open input file $inF\n\n");
print "In progress ...\n";
while (<IN>)
{
	$line = $_; #$_ default input variable;
	chomp ($line);
	print $int++."\n";
	@tA = split(/[\t]/, $line);
	$gID = $tA[0]; $gID =~ s/chr//;
	$start = $tA[3];
	$end = $tA[4];
	$strand = $tA[6];
	$id_line = $tA[8];
	@tA1 = split(/\;/, $id_line);
	$code1 = $tA1[0]; #print $code1."\n";
	@tC = split(/\"/, $code1); $code = $tC[1]; $code =~ s/\s//g; #print $code."\n";
	if ($code eq "1-2^,0")
	{
 		$type = "exon skipping"; $es++;
	}
	elsif ($code eq "1^,2^")
	{
 		$type = "alternative donor site"; $ad++;
	}
	elsif ($code eq "1-,2-")
	{
 		$type = "alternative acceptor site"; $aa++;
	}
	elsif ($code eq "1^2-,0")
	{
 		$type = "intron retention"; $ir++;
	}
	else
	{
		$type = "complex events"; $ce++;
	}

	$id1 = $tA1[1]; @tA2 = split(/\"/, $id1); $id1 = $tA2[1];
	$id2 = $tA1[3]; @tA3 = split(/\"/, $id2); $id2 = $tA3[1];

	$in = $gID."\t".$id1."\t".$id2."\t".$start."\t".$end."\t".$strand."\t".$code."\t".$type."\n";
	push (@outA,$in);
}
close (IN);
open(OUT, ">$outF");
print OUT (@outA);
close (OUT);
$sumF = "summary.events";
open(OUT, ">$sumF");
print OUT ("exon skipping"."\t".$es."\n");
print OUT ("alternative donor sites"."\t".$ad."\n");
print OUT ("alternative acceptor sites"."\t".$aa."\n");
print OUT ("intron retention"."\t".$ir."\n");
print OUT ("others (complex events)"."\t".$ce."\n");
close (OUT);

print "The job is done!\n";

exit;





