#!usr/bin/perl
# to run the script provide the .diff file and the merged.gtf (the reference used by cuffdiff) as commandline paramters
# in that order

$diff_file=$ARGV[0];											# input isoform_exp.diff or other file as as commandline agrument 1.
$ref_gtf=$ARGV[1];												# get reference gtf file - generated by cuffmerge - as commandline argument2.
print "Opening input files $diff_file and $ref_gtf...\n";													# two blank lines to screen output
open (DIFFL, "< ".$diff_file) or die "Cannot open $diff_file\n";	# open the diff file as DIFFL
$diffwtid_file = $diff_file."wtid.txt";
open (NEWDIFFL, "> ".$diffwtid_file) or die "Cannot open output file\n";

print "\tSearching gtf file for old transcript ids...";
while ($diff_line=<DIFFL>)					# read each line in $diff_file till EOF
{
($tcons_id,$rest)=split(/\t/,$diff_line);				# split the line using tab delimiter in to two; first colum TCONSnnnnnnn number assigned
														# by cuffmerge and the rest of the line
open (GTFL,$ref_gtf) or die "Cannot open $ref_gtf\n";
while ($gtf_line=<GTFL>)
	{ 														
	if ($gtf_line =~ $tcons_id)
		{
		if ($gtf_line =~ /oId(.*?)\;/)
			{
			$old_tid=$1;
			print NEWDIFFL "$old_tid\t$diff_line";
			close GTFL;
			}
		}
	}
}
print "\nDone! Diff file with old transcripts ids = $diffwtid_file\n";		

__END__
Perl script written by Sen Subramanian, 2012 to read a cuffdiff output file (TOCNS ids) and obtain the corresponding transcript
IDs from the reference gtf file. Questions/Comments to manianslab@gmail.com
