comparison siRNA.pl @ 0:87fe81de0931 draft default tip

Uploaded
author bigrna
date Sun, 04 Jan 2015 02:47:25 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:87fe81de0931
1 #!/usr/bin/perl -w
2 my $version=1.00;
3 use strict;
4 use warnings;
5 use Getopt::Long;
6 use Getopt::Std;
7 use threads;
8 #use threads::shared;
9 use Parallel::ForkManager;
10 #use lib '/leofs/biotrans/chentt/perl_module/';
11 #perl ../siRNA.pl -i config -g /leofs/biotrans/projects/rice/smallRNA/sRNA_package/bin/test/ref/genome.fa -f /share_bio/hs4/disk3-4/Reference/Plants/Rice_TIGR/Reference/TIGR/version_6.1/all.dir/all.gff3 -path /leofs/biotrans/projects/rice/smallRNA/sRNA_package/bin/ -o /leofs/biotrans/projects/rice/smallRNA/sRNA_package/bin/test -t 3 -rfam /leofs/biotrans/projects/rice/smallRNA/sRNA_package/bin/test/ref/Rfam.fasta -idx /leofs/biotrans/projects/rice/smallRNA/sRNA_package/bin/test/ref/genome -idx2 /leofs/biotrans/projects/rice/smallRNA/sRNA_package/bin/test/ref/rfam -deg deg -n 25 -nat class/nat_1 -repeat class/repeat_1 -cen centromere_TIGR.txt -format fastq
12 print "
13 #####################################
14 # #
15 # sRNA cluster #
16 # #
17 #####################################
18 ";
19 ###########################################################################################
20 my $usage="$0
21 Options:
22 -i input file# fasta
23 -config input file
24 -g genome file
25 -f gff file
26
27 -o workdir file
28 -path script path
29 -t int, number of threads [1]
30 -format fastq, fq, fasta or fa
31 -idx string, genome file index, file-prefix #(must be indexed by bowtie-build) The parameter
32 string must be the prefix of the bowtie index. For instance, if
33 the first indexed file is called 'h_sapiens_37_asm.1.ebwt' then
34 the prefix is 'h_sapiens_37_asm'.##can be null
35 -mis int number of allowed mismatches when mapping reads to genome, default 0
36
37 -n int max hits number,default 25; used in genome alignment
38 -d int distance of tag to merged a cluster; default 100
39 -p cluster method F :conventional default is F
40 T :NIBLES
41 -l int the length of the upstream and downstream,default 1000;used in position annotate
42
43 -nat natural antisense transcripts file
44 -repeat repeat information file out of Repeatmasker
45 -deg file config of de sample
46 -cen centromere file input
47 -span plot span, default 50000
48 ";
49
50 my %options;
51 GetOptions(\%options,"i:s","config=s","g=s","f=s","o=s","path:s","p=s","format=s","nat:s","repeat:s","deg:s","n:i","mis:i","t:i","d:i","l:i","idx:s","cen:s","span:s","h");
52 #print help if that option is used
53 if($options{h}){die $usage;}
54
55 my $filein=$options{'i'};
56
57 #my $config=$options{'i'};
58 my $genome_fa=$options{'g'};
59 my $gff=$options{'f'};
60
61
62 ##########################################################################################
63 my $predir=`pwd`;
64 chomp $predir;
65 my $workdir=defined($options{'o'}) ? $options{'o'}:$predir;
66
67 my $path=$options{'path'};
68
69 my $t=defined($options{'t'})? $options{'t'}:1; #threads number
70
71 my $mis=defined $options{'mis'} ? $options{'mis'}:0;
72
73
74 my $hit=defined $options{'n'}?$options{'n'}:25;
75
76 my $distance_of_merged_tag=defined $options{'d'} ? $options{'d'}:100;
77
78 my $up_down_dis=defined $options{'l'} ?$options{'l'}:1000;
79
80 my $cluster_mothod=defined $options{'p'}?$options{'p'}:"F";
81
82 my $format=$options{'format'};
83 #if ($format ne "fastq" && $format ne "fq" && $format ne "fasta" && $format ne "fa") {
84 # die "Parameter \"-format\" is error! Parameter is fastq, fq, fasta or fa\n";
85 #}
86
87
88
89 my $sample_number;
90 my ($dir,$dir_tmp);
91 ################################ MAIN ##################################################
92 print "\ncluster program start:";
93 my $time=Time();
94 make_dir_tmp();
95
96 my $mark;
97 my $sample_mark;
98
99 my $config=$options{'config'};
100 my (@filein,@mark);
101 &read_config();
102 $sample_number=@mark;
103 $mark=join "\t",@mark;
104 $sample_mark=join "\#",@mark;
105
106
107
108 my $data3=$filein; ### rfam not mapped reads
109 genome();
110
111 my $bed=$dir."cluster\/"."sample\.bed";
112 my $read=$dir."cluster\/"."sample_reads\.cluster";
113 my $read_txt=$dir."cluster\/"."cluster\.txt";
114 my $rpkm=$dir."cluster\/"."sample_rpkm\.cluster";
115 my $preprocess;
116 my $cluster_file;
117 my $annotate_dir;
118 my $deg_dir;
119 my $plot_dir;
120 my %id;
121 for (my $i=0;$i<@mark ;$i++) {
122 $id{$mark[$i]}=$i+4;
123 }
124
125
126 my @map_read;
127 my $map_tag=0;
128
129 bwt2bed();
130
131 cluster();
132
133 quantify();
134
135 phase();
136
137 if (defined($options{'nat'})&&defined($options{'repeat'})) {
138 class();
139 }
140 else{
141 get_genelist();
142 }
143
144 annotate();
145
146 genome_length();
147
148 plot();
149
150 my @pairdir;
151 if (defined($options{'deg'})) {
152 dec();
153 infor_merge();
154 }
155 else{infor_merge_no_dec()}
156 html();
157 print "\ncluster program end:";
158 Time();
159 ############################sub program###################################################
160 sub make_dir_tmp{
161
162 #make temporary directory
163 if(not -d "$workdir\/cluster_runs"){
164 mkdir("$workdir\/cluster_runs");
165 mkdir("$workdir\/cluster_runs\/ref\/");
166 }
167
168 $dir="$workdir\/cluster_runs\/";
169 #print STDERR "mkdir $dir\n\n";
170 return;
171 }
172
173 sub genome{
174 if(defined $options{'idx'}){
175 system("perl $path\/matching.pl -i $data3 -g $genome_fa -v $mis -p $t -r $hit -o $dir -index $options{idx}") ;
176 }else{
177 system("perl $path\/matching.pl -i $data3 -g $genome_fa -v $mis -p $t -r $hit -o $dir ") ;
178 }
179 #=================== mapping sta ===================================================
180 my $map_file=$dir."genome_match\/genome_mapped\.fa";
181 open (MAP,"<$map_file")||die"$!";
182 print "\n#each sample mapping reads sta:\n\n";
183 print "#$mark\ttotal\n";
184 while (my $ID=<MAP>) {
185 chomp $ID;
186 my @tmp=split/\:/,$ID;
187 my @exp=split/\_/,$tmp[1];
188 $exp[-1] =~ s/^x//;
189 for (my $i=0;$i<@exp ;$i++) {
190 $map_read[$i]+=$exp[$i];
191 }
192 $map_tag++;
193 my $seq=<MAP>;
194 }
195 my $map_read=join"\t",@map_read;
196 print "$map_read\n\n";
197 print "#total mapped tags:$map_read\n\n";
198 close MAP;
199 return 0;
200 }
201
202 sub bwt2bed{
203 $cluster_file=$dir."cluster\/";
204 mkdir ("$cluster_file");
205 print "sam file changed to bed file\n";
206 my ($file) = $dir."genome_match\/genome_mapped\.bwt";
207
208 my $sam2bed=`perl $path\/sam2Bed_bowtie.pl -i $file -mark $sample_mark -o $bed `;
209 print "perl $path\/sam2Bed_bowtie.pl -i $file -mark $sample_mark -o $bed\n\n";
210 return 0;
211 }
212
213 sub cluster{
214 print "tags is ready to merged clusters\n\n";
215 my ($file) =$bed;
216 if ($cluster_mothod eq "F") {
217 my $cluster=`perl $path\/conventional.pl -i $file -d $distance_of_merged_tag -n $sample_number -mark $sample_mark -o $read -t $read_txt`;
218 print "Using converntional method\n perl $path\/conventional.pl -i $file -d $distance_of_merged_tag -n $sample_number -mark $sample_mark -o $read -t $read_txt\n\n";
219 }
220 elsif($cluster_mothod eq "T"){
221 my $cluster=`perl $path\/nibls.pl -f $file -m $distance_of_merged_tag -o $read -t $read_txt -k $sample_mark`;
222 print "Using nibls method\n perl $path\/nibls.pl -f $file -m $distance_of_merged_tag -o $read -t $dir\/cluster.txt -k $sample_mark\n\n";
223 }
224 else{print "\-p is wrong!\n\n";}
225 return 0;
226 }
227
228
229 sub quantify{
230 print "clusters is ready to quantified\n\n";
231 my @depth=@map_read;
232 pop @depth;
233 my $depth=join ",",@depth;
234 my $quantify=`perl $path\/quantify_siRNA.pl -i $read -d $depth -o $rpkm`;
235 print "perl $path\/quantify_siRNA.pl -i $read -d $depth -o $rpkm\n\n\n";
236 return 0;
237 }
238
239 sub phase{
240 $annotate_dir=$dir."annotate\/";
241 mkdir ("$annotate_dir");
242 print "clusters is to predict phase siRNA\n";
243 my $phase=`perl $path\/phased_siRNA.pl -i $read_txt -o $annotate_dir\/phase.out`;
244 print "perl $path\/phased_siRNA.pl -i $read_txt -o $annotate_dir\/phase.out\n\n\n";
245 return 0;
246 }
247
248 sub class{
249 print "clusters is ready to annotate by sources\n\n";
250 my $nat=$options{'nat'};
251 my $repeat=$options{'repeat'};
252 my $class=`perl $path\/ClassAnnotate.pl -i $rpkm -g $gff -n $nat -r $repeat -p $annotate_dir\/phase.out -o $annotate_dir\/sample_class.anno -t $annotate_dir\/nat.out -l $dir\/ref\/genelist.txt`;
253 print "perl $path\/ClassAnnotate.pl -i $rpkm -g $gff -n $nat -r $repeat -p $annotate_dir\/phase.out -o $annotate_dir\/sample_class.anno -t $annotate_dir\/nat.out -l $dir\/ref\/genelist.txt\n\n";
254 }
255
256 sub annotate{
257 print "clusters is ready to annotate by gff file\n\n";
258 my $file;
259 if (defined($options{'nat'})&&defined($options{'repeat'})) {
260 $file="$annotate_dir\/sample_class.anno";
261 }
262 else{
263 $file=$rpkm;
264 }
265 my $annotate=`perl $path\/Annotate.pl -i $file -g $dir\/ref\/genelist.txt -d $up_down_dis -o $annotate_dir\/sample_c_p.anno`;
266 print "perl $path\/Annotate.pl -i $file -g $dir\/ref\/genelist.txt -d $up_down_dis -o $annotate_dir\/sample_c_p.anno\n\n";
267 return 0;
268 }
269 sub get_genelist{
270
271 my $get_genelist=`perl $path\/get_genelist.pl -i $gff -o $dir\/ref\/genelist.txt`;
272 print "perl $path\/get_genelist.pl -i $gff -o $dir\/ref\/genelist.txt";
273 }
274 sub dec{
275 print "deg reading\n\n";
276 my $deg_file=$options{'deg'};
277 open IN,"<$deg_file";
278 my @deg;
279 my $s=0;
280 while (my $aline=<IN>) {
281 chomp $aline;
282 next if($aline=~/^\#/);
283 $deg[$s]=$aline;
284 my @ea=split/\s+/,$aline;
285 push @pairdir,"$ea[0]_VS_$ea[1]\/";
286 #print "$deg[$s]\n";
287 $s++;
288 }
289 close IN;
290 $deg_dir=$dir."deg\/";
291 mkdir ("$deg_dir");
292 my $max_process = 10;
293 my $pm = new Parallel::ForkManager( $max_process );
294 my $number=@deg-1;
295 foreach(0..$number){
296 $pm->start and next;
297 &dec_pel($deg[$_]);
298 $pm->finish;
299 }
300 $pm->wait_all_children;
301 }
302
303 sub dec_pel{
304 print "\n******************\nstart:\n";
305 Time();
306 my $sample=shift(@_);
307 my @each=split/\s+/,$sample;
308 print "$each[0]\t$each[1]\n";
309 my $deg_sample_dir=$deg_dir."$each[0]_VS_$each[1]\/";
310 mkdir ("$deg_sample_dir");
311 print "read: $read\n";
312 print "deg_sample_dir: $deg_sample_dir\n";
313 print "$id{$each[0]}\t$each[0]\n";
314 print "$id{$each[1]}\t$each[1]\n";
315 my $deg=`perl $path\/DEGseq_2.pl -i $read -outdir $deg_sample_dir -column1 $id{$each[0]} -mark1 $each[0] -column2 $id{$each[1]} -mark2 $each[1]`; #-depth1 -depth2
316 my $time2=time();
317 print "end:\n*************************\n";
318 Time();
319 sleep 1;
320 }
321
322 sub infor_merge{
323 my ($input,$mark);
324 foreach (@pairdir) {
325 print "@pairdir\n";
326 $mark.=" -mark $_ ";
327 $input.=" -i $dir/deg\/$_\/output_score\.txt ";
328 print "$input\n$mark\n";
329 }
330 my $infor_merge=`perl $path\/SampleDEGseqMerge.pl $input $mark -f $annotate_dir\/sample_c_p.anno -n $sample_number -o $dir\/total.result `;
331 print "perl $path\/SampleDEGseqMerge.pl $input $mark -f $annotate_dir\/sample_c_p.anno -n $sample_number -o $dir\/total.result\n\n";
332 }
333
334 sub infor_merge_no_dec{
335 my $infor_merge_no_dec=`cp $annotate_dir\/sample_c_p.anno $dir\/total.result`;
336 }
337
338 sub genome_length{
339 my $length=`perl $path\/count_ref_length.pl -i $genome_fa -o $dir\/ref\/genome\.length`;
340 print "perl $path\/count_ref_length.pl -i $genome_fa -o $dir\/ref\/genome\.length\n\n"
341
342 }
343
344 sub plot{
345 $plot_dir="$dir\/plot\/";
346 mkdir ("$plot_dir");
347 my $span=defined($options{span})?$options{span}:50000;
348 my $cen="";
349 if (defined $options{cen}) {
350 $cen="-cen $options{cen}";
351 }
352 my $plot=`perl $path/sRNA_plot.pl -c $rpkm -g $dir/ref/genelist.txt -span 50000 -mark $sample_mark -l $dir/ref/genome\.length $cen -o $plot_dir/cluster.html -out $plot_dir/cluster.txt `;
353 "print perl $path/sRNA_plot.pl -c $rpkm -g $dir/ref/genelist.txt -span 50000 -mark $sample_mark -l $dir/ref/genome.length $cen -o $plot_dir/cluster.html -out $plot_dir/cluster.txt \n";
354
355 }
356
357 sub html{
358 my $pathfile="$dir/path.txt";
359 open PA,">$pathfile";
360 print PA "$config\n";
361 print PA "$preprocess\n";
362 print PA "$dir"."rfam_match\n";
363 print PA "$dir"."genome_match\n";
364 print PA "$cluster_file\n";
365 print PA "$annotate_dir\n";
366 print PA "$plot_dir\n";
367 if (defined($deg_dir)) {
368 print PA "$deg_dir\n";
369 }
370 close PA;
371 #my $html=`perl $path\/html.pl -i $pathfile -format $format -o $dir/result.html`;
372 }
373
374 sub Time{
375 my $time=time();
376 my ($sec,$min,$hour,$day,$month,$year) = (localtime($time))[0,1,2,3,4,5,6];
377 $month++;
378 $year+=1900;
379 if (length($sec) == 1) {$sec = "0"."$sec";}
380 if (length($min) == 1) {$min = "0"."$min";}
381 if (length($hour) == 1) {$hour = "0"."$hour";}
382 if (length($day) == 1) {$day = "0"."$day";}
383 if (length($month) == 1) {$month = "0"."$month";}
384 print "$year-$month-$day $hour:$min:$sec\n";
385 return("$year-$month-$day-$hour-$min-$sec");
386 }
387 #################################################################################
388 sub read_config{
389 open CON,"<$config";
390 while (my $aline=<CON>) {
391 chomp $aline;
392 my @tmp=split/\t/,$aline;
393 push @filein,$tmp[0];
394 push @mark,$tmp[1];
395 #&check_rawdata($tmp[0]);
396 }
397 close CON;
398 if (@filein != @mark) {
399 #&printErr();
400 die "Maybe config file have some wrong!!!\n";
401 }
402 }