if ($#ARGV < 2) {
print "## Calcualte read distribution around CDS start and stop codons"."\n";
print "usage: perl readDist.pl -f readFile -g geneFile -o outputDir [-d readLength] [-l leftNum] [-r rightNum]"."\n";
print "-f readFile: read alignments to the transcriptome and reference genome, in SAM format;"."\n";
print "-g geneFile: ORF annotation file in genepred format;"."\n";
print "-o outputDir: output directory;"."\n";
print "-d readLength [optional]: specified RPF length, default: 25,26,27,28,29,30,31,32,33,34,;"."\n";
print "-l leftNum [optional]: N nucleotides upstream start codon and downstream stop codon, default: 30;"."\n";
print "-r rightNum [optional]: N nucleotides downstream start codon and upstream stop codon, default: 50."."\n";
exit;
}
use Getopt::Std;
get arguments
my %args;
getopt("fgodlr",%args);
my readfile=args{f};
if (! readfile)print"Noreadmappingfile"."\n";exit;mygeneFile=argsg;if(!geneFile) {
print "No ORF annotation file"."\n";
exit;
}
my outputDir=args{o};
if (! geneFile)print"Nooutputdirectory"."lenDist=argsd;mystart=argsl;myend=argsr;if(!lenDist) {
lenDist="18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,";
}
my %len;
my @s=split /,/, lenDist;
for (my i=0;i<=#s; i++) {
len{s[i]}=1;
}
if (! start) {
start=30;
}
if (! end) {
$end=50;
}
my %sta;
foreach my readLength(sortkeysopen(IN,"readfile");
my %read;
my tot=0;
while (<IN>) {
chomp;
my @s1=split /\t/, _;
my @s2=split /\D+/, s1[5];
if (length(readLength) {
if (k=s1[3]-1);
k}++;
s1[1] == 16) {
my s1[3];
for (my i<i++) {
s2[k=loc-2);
k}++;
sta{tot;
my %count5;
my %count3;
open (AN, "geneFile");
while (<AN>) {
chomp;
my @s1=split /\t/, _;
my @s2=split /,/, s1[#s1-1];
my @s3=split /,/, s1[#s1];
my @val;
my @ex;
my k=−1;mycodon5;
my i=0; #s2; j=i]; s3[j++) {
j;
if (exists (j})) {
push @ex, j};
} else {
push @ex, "0";
}
if (j==codon5=j==(codon3=j==(codon5=j==(codon3=codon3-end) {
if (start && (codon5) > #val-start) {
count3{"0"}++;
for (my start; end; k;
if (k=s1[2].":".codon5+k=s1[2].":".codon3-read{count5{start+1}+=k};
}
}
for (my end; start; k;
if (k=s1[2].":".codon3+k=s1[2].":".codon5-read{count3{end+1}+=k};
}
}
}
}
close AN;
if (! exists ($count5{"0"})) {
print "Error: no hits!"."\n";
exit;
}
open (OUT, ">outputDir/read.dist.sample.readLength.txt");
my i=1; end+i++) {
if (! exists (i})) {
i}=0;
}
count5{count5{"0"}/out."\n";
my out="stop.codon";for(myi=1; i<=(end+i++) {
if (! exists (i})) {
i}=0;
}
count3{count5{"0"}/out."\n";
close OUT;
for (my i=(end+start+1);i>=start;i-=3) {
my count5{k2=i-1};
my count5{tk=k2+tk==0) {
k1=1;
k3=1;
}
readLength}[1]+=tk;
readLength}[2]+=tk;
readLength}[3]+=tk;
}
open (RI, ">outputDir/readDist.plot.readLength.R");
print RI "A <- read.table ("readLength.txt", sep="\t")"."\n";
print RI "loc1 <- c(-end)"."\n";
print RI "loc2 <- c(-start)"."\n";
print RI "B1 <- apply(A[1,2:ncol(A)], 2, sum)"."\n";
print RI "B2 <- apply(A[2,2:ncol(A)], 2, sum)"."\n";
print RI "pdf (file="readLength.pdf")"."\n";
print RI "par(mfrow=c(2,2), mar=c(2,2,2,1))"."\n";
print RI "plot(loc1, B1, xlim=c(-20, 30), type="h", lwd=2, ylim=c(0,max(c(B1,B2))), main=readLength, xlab="Flanking stop codon")"."\n";
print RI "abline (v=-30, col=8, lwd=1, lty="dotted")"."\n";
print RI "abline (v=-20, col=8, lwd=1, lty="dotted")"."\n";
print RI "abline (v=-10, col=8, lwd=1, lty="dotted")"."\n";
print RI "abline (v=0, col=8, lwd=1, lty="dotted")"."\n";
print RI "abline (v=10, col=8, lwd=1, lty="dotted")"."\n";
print RI "abline (v=20, col=8, lwd=1, lty="dotted")"."\n";
print RI "dev.off()"."\n";
close RI;
my comm="RscriptoutputDir/readDist.plot.readLength.R";syscomm);
}
open (ST, ">outputDir/sta.read.dist.lenDist.txt");
print ST "fragment.length"."\t"."read.num"."\t"."frame2"."\t"."frame3"."\t"."frame1"."\n";
foreach my k(sortsu=k}[1]+k}[2]+k}[3];
if (su=1;
}
print ST sta{sta{su)."\t".sprintf("%.5f", k}[2]/sta{su)."\n";
}
close ST;
open (RE, ">outputDir/sta.readDist.plot.lenDist.R");
print RE "A <- read.table ("lenDist.txt", sep="\t", header=T)"."\n";
print RE "pdf (file="lenDist.pdf")"."\n";
print RE "barplot(A[,2], names.arg = A[,1], beside=T, ylab="Read number")"."\n";
print RE "barplot(as.matrix(t(A[,3:5])), names.arg = A[,1], beside=T, ylim=c(0,1), legend=T)"."\n";
print RE "abline(h=0.5, lty="dotted")"."\n";
print RE "dev.off()"."\n";
close RE;
my comm="RscriptoutputDir/sta.readDist.plot.lenDist.R";system(comm);