annotate filterReadsByLength_1.pl @ 19:e0884a4b996b draft

Uploaded
author big-tiandm
date Wed, 05 Nov 2014 01:17:26 -0500
parents 07745c0958dd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
1 #!/usr/bin/perl -w
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
2 #Filename:
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
3 #Author: Tian Dongmei
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
4 #Email: tiandm@big.ac.cn
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
5 #Date: 2010-01
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
6 #Modified:
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
7 #Description:
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
8 my $version=1.00;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
9
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
10 use strict;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
11 use Getopt::Long;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
12 use File::Basename;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
13
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
14 my %opts;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
15 GetOptions(\%opts,"i=s","min=i","max=i","o=s","mark:s","h");
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
16 if (!(defined $opts{i} and defined $opts{o} and defined $opts{min} and defined $opts{max}) || defined $opts{h}) { #necessary arguments
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
17 &usage;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
18 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
19
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
20 my $mark=defined $opts{'mark'} ? $opts{'mark'} : "Sample";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
21 my @mark=split /\#/,$mark;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
22
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
23
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
24 open OUT,">$opts{o}";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
25 open IN,"<$opts{i}";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
26 my %hash;my %reads;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
27 while (my $aline=<IN>) {
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
28 chomp $aline;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
29 my $seq=<IN>;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
30 chomp $seq;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
31
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
32 if($aline=~/:([\d|_]+)_x(\d+)$/){
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
33 my @ss=split/_/,$1;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
34 for (my $i=0;$i<@ss;$i++) {
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
35 $hash{length($seq)}[$i]++ if($ss[$i]>0);
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
36 $hash{length($seq)}[$i] +=0 if($ss[$i]==0);
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
37 $reads{length($seq)}[$i]+=$ss[$i];
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
38 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
39 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
40 #else{$reads{length($seq)}+=1;}
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
41 if (length ($seq)>=$opts{'min'} && length ($seq) <=$opts{'max'}) {
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
42 print OUT "$aline\n$seq\n";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
43 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
44 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
45 close IN;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
46 close OUT;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
47
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
48 my $dir=dirname($opts{'o'});
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
49 chdir $dir;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
50 my $lengthfile=$dir."/reads_length_distribution.txt";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
51 print "$lengthfile\n";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
52 open OUT, ">$lengthfile";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
53 open R,">$dir/length_distribution.R";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
54
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
55 print OUT "Tags length\t@mark\n";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
56
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
57 my $samNo=@mark;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
58 my $avalue="";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
59 my @length=sort{$a<=>$b} keys %hash;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
60 foreach (@length) {
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
61 print OUT $_,"\t@{$hash{$_}}\n";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
62 my $vv=join ", ",@{$hash{$_}};
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
63 $avalue .="$vv,";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
64 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
65 $avalue =~s/,$//;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
66 my $lengths=join ",",@length;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
67 my $marks=join "\",\"",@mark;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
68
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
69 print R "a<-c($avalue)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
70 b<-matrix(a,ncol=$samNo,byrow=T)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
71 cl<-colors()
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
72 names=c($lengths)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
73 legends=c(\"$marks\")
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
74 png(\"Tags_length.png\",width=800,height=600)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
75 barplot(t(b),beside=TRUE,col=cl[1:$samNo],main=\"Tags Length Distribution\",names.arg=names,ylim=c(0,max(a)),legend.text=legends,args.legend=\"topleft\")
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
76 abline(h=0)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
77 dev.off()
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
78
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
79 ";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
80 $avalue="";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
81 print OUT "\nReads length\t@mark\n";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
82 foreach (@length) {
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
83 print OUT $_,"\t@{$reads{$_}}\n";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
84 my $vv=join ", ", @{$reads{$_}};
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
85 $avalue .= "$vv,";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
86 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
87 $avalue =~s/,$//;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
88
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
89 print R "a<-c($avalue)\n
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
90 b<-matrix(a,ncol=$samNo,byrow=T)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
91
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
92 png(\"Reads_length.png\",width=800,height=600)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
93 barplot(t(b),beside=TRUE,col=cl[1:$samNo],main=\"Reads Length Distribution\",names.arg=names,ylim=c(0,max(a)),legend.text=legends,args.legend=\"topleft\")
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
94 abline(h=0)
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
95 dev.off()
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
96
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
97 ";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
98 close OUT;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
99 close R;
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
100
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
101 system ("R CMD BATCH $dir/length_distribution.R");
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
102
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
103 #system ("rm $dir/length_distribution.R");
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
104 #system ("rm $dir/length_distribution.Rout");
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
105 #system ("rm $dir/.RData");
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
106 sub usage{
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
107 print <<"USAGE";
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
108 Version $version
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
109 Usage:
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
110 $0 -i -o -min -max -mark
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
111 options:
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
112
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
113 -i input file
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
114 -o output file
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
115 -min reads min length.
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
116 -max reads max length.
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
117 -mark string #sample name eg: samA#samB#samC
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
118 -h help
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
119 USAGE
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
120 exit(1);
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
121 }
07745c0958dd Uploaded
big-tiandm
parents:
diff changeset
122