#!/usr/bin/perl
=head2 SYNOPSIS

	Description:  Calling TPM for each expressed gene resulted from FeatureCounts
             
=head2 AUTHOR

	Kang Zhang  Email:  demut@foxmail.com
	
=cut

open IN,"<$ARGV[0]" or die;

while(<IN>){
	chomp;
	next if (/^#|^Geneid/);
	my @arr=split;
	$count{$arr[0]}=$arr[2];
	$len{$arr[0]}=$arr[1];
	$fpk{$arr[0]}=$arr[2]/$arr[1];
	$TotalFPK+=$fpk{$arr[0]};
}

foreach my $tpt (sort keys %fpk){
	$tpm{$tpt}=1000000 * $fpk{$tpt}/$TotalFPK;
	print "$tpt\t$len{$tpt}\t$count{$tpt}\t$tpm{$tpt}\n";
}

