15 # Number of threads to run simultaneously.
18 my $CmdQueue = Thread::Queue->new;
24 if(!defined($root_dir) || !defined($data_dir)) {
25 print "Usage: ./ConfigureTasks.pm [root_dir] [data_dir] {option} \n";
31 my $BIOP = "$root_dir/bin/BioProspector.mac";
32 my $QUESTDIR = "$root_dir/bin/QuEST";
33 my $MACS = $ENV{'MACS'} || `which macs | perl -e '\$l = <>; chomp \$l; print \$l;'` || die "MACS not found in PATH. Install and put in path";
34 my $WINGPEAKSDIR = "$root_dir/bin";
35 my $WINGPEAKSGENOMEDIR = "$root_dir/reference_data";
36 my $PROFILEDIR = "$root_dir/bin";
37 my $GENOMEDIR = "/Volumes/Genomes";
38 my $QPCRDIR = "$root_dir/bin";
39 my $METHYLSEQDIR = "$root_dir/bin";
40 my $QPCRTESTDIR = "$root_dir/reference_data/qPCR_Tests";
41 my $QPCRBACKGROUND = "$root_dir/reference_data/GenericBackground";
43 my $SUMMARIZE_PROJECT="$root_dir/scripts/SummarizeProject2.pm";
46 ### Check directory for project and task folders. Make them if necessary
47 ### May not have permissions for this. These should be moved to an install script
48 `if [ ! -e $data_dir/Tasks ]; then mkdir $data_dir/Tasks; fi;`;
49 `if [ ! -e $data_dir/Projects ]; then mkdir $data_dir/Projects; fi;`;
53 ### Important: get the xml file form the server.
55 my $projects_file = getProjectsXML($parm);
57 my $xmldoc = XML::Simple->new();
59 my $xmlfile = $projects_file;
60 my $xml = $xmldoc->XMLin($xmlfile, ForceArray => ['Project','Methylseq', 'ComparePeakCalls','CompareLibraries','PeakCalling','ProfileReads','qPCR','MotifFinding'], KeepRoot=>1);
66 if(exists($xml->{Projects}->{Project})) {
67 for my $project (@{$xml->{Projects}->{Project}}) {
68 push @tasks, @{writeProject($project)};
70 my $projectname = $project->{Name};
71 my $projectId = $project->{ProjectId};
73 $projects .= "$projectId ";
77 print STDERR "Projects all complete\n";
79 my %saw; my $unique_tasks = ""; my $unique_task_dirs = "";
80 for(grep(!$saw{$_}++, @tasks)) {
81 $unique_tasks .= "$_ ";
82 $unique_task_dirs .= "../Tasks/$_ ";
85 print STDERR "writing tasks\n";
87 open(MAKE,">$data_dir/Projects/TaskMakefile");
88 print MAKE "all: $unique_task_dirs\n\n.PHONY: $unique_task_dirs\n\n";
89 for(split(/ /,$unique_tasks)) { print MAKE "../Tasks/$_:\n\t".'if ! $(MAKE) -C $@; then '.$root_dir.'/scripts/analys_track_main.py updsts '.$_.' "Error"; fi;'."\n\n"; }
92 print STDERR "Task makefile complete\n";
95 for(split(/ /,$projects)) { $index_list .= "$_/index.html "; }
97 open(MAKE,">$data_dir/Projects/ProjectMakefile");
98 print MAKE "all: $index_list | .start\n\n.PHONY: $index_list .start\n\n.start:\n\ttouch .start; echo \"Projects updated at `date`\";\n\n$index_list:\n\t".'cd `dirname $@` && '.$SUMMARIZE_PROJECT.' '.$root_dir.' '.$data_dir.'/LibraryInfo.xml > `basename $@`'."\n";
101 print STDERR "Project makefile compete. Starting to build tasks\n";
103 `cd $data_dir/Projects && make -j 4 -f TaskMakefile > make_tasks.log 2> make_tasks.err`;
107 my $projectname = $project->{Name}; $projectname =~ s/\s/_/g;
108 my $projectid = $project->{ProjectId};
110 my $projectdir = $projectid;
113 $tasks .= WriteMethylseqTasks($project);
114 $tasks .= WriteQPCRTasks($project);
115 $tasks .= WriteProfileTasks($project);
116 $tasks .= WriteCompareLibTasks($project);
117 $tasks .= WriteMotifFindingTasks($project);
118 $tasks .= WritePeakCallingTasks($project);
120 print STDERR "Wrote tasks for project '$projectname' with tasks ($tasks)\n";
122 my @tasks = split(/ /,$tasks);
124 if(! (-e $projectdir && -d $projectdir)) {
125 `mkdir $data_dir/Projects/$projectdir`;
128 print STDERR "Created directory $projectdir\n";
130 $xmldoc->XMLout($project, OutputFile=>"$data_dir/Projects/$projectid/Project.xml", RootName=>"Project", XMLDecl=>1);
132 print STDERR "Wrote configureation for project $projectdir\n";
139 `$root_dir/scripts/analys_track_main.py updsts $taskid`;
140 print STDERR "Task $taskid registered.\n";
148 my $seqcheck = shift;
150 my $taskid = $task->{TaskId};
151 my $taskdir = "$data_dir/Tasks/".$taskid;
153 if(! (-e $taskdir && -d $taskdir)) {
157 $xmldoc->XMLout($task, OutputFile=>"$taskdir/Task.xml", RootName=>$root, XMLDecl=>1);
159 open(MAKEFILE, ">$taskdir/Makefile");
160 print MAKEFILE "all: .notify $outfile | .start\n\n.PHONY: .notify .start\n\n";
161 print MAKEFILE ".start:\n\t$root_dir/scripts/analys_track_main.py updsts $taskid \"Processing\"\n\t$seqcheck\n\ttouch .start\n\n";
163 print MAKEFILE ".notify: | .start $outfile .start\n\techo \"Complete\"\n\t$root_dir/scripts/analys_track_main.py updsts $taskid \"Complete\"\n\ttouch .notify\n\n";
164 print MAKEFILE "$cmd";
166 registerTask($taskid);
169 sub WriteMethylseqTasks {
171 my $root = "Methylseq";
173 if(exists($project->{$root})) {
174 for my $mseq (@{$project->{$root}}) {
175 my $task = $mseq->{TaskId};
176 my $genome = $mseq->{Genome};
177 my $name = $mseq->{Name}; $name =~ tr/ \(\)\./____/;
179 my $msp1 = $mseq->{Msp1}->{Library};
180 my $hpa2 = $mseq->{Hpa2}->{Library};
182 my $outfile = "methylseq_summary";
184 my $seqcheck = "if [ ! -e $data_dir/Libraries/$msp1.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for sequencing.\"; fi;\n";
185 $seqcheck .= "\tif [ ! -e $data_dir/Libraries/$hpa2.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for sequencing.\"; fi;\n";
186 $seqcheck .= "\tif [ ! -e $root_dir/reference_data/".$genome."_methylseq_regions ]; then $root_dir/scripts/analys_track_main.py updsts $task \"No methylseq regions available for $genome.\"; fi;\n";
188 my $cmd = "$outfile: $data_dir/Libraries/$msp1.txt $data_dir/Libraries/$hpa2.txt\n";
189 $cmd .= "\t$METHYLSEQDIR/Methylseq_Analysis $data_dir/Libraries/$msp1.txt $data_dir/Libraries/$hpa2.txt $root_dir/reference_data/".$genome."_methylseq_regions $name > \$@\n";
190 writeTask($mseq, $root, $outfile, $cmd, $seqcheck);
201 if(exists($project->{qPCR})) {
202 for my $qpcr (@{$project->{qPCR}}) {
203 my $task = $qpcr->{TaskId};
204 my $lib = $qpcr->{Library};
205 my $genome = $qpcr->{Genome};
206 my $name = $qpcr->{Name}; $name =~ tr/ \(\)\./____/;
208 my $background = $QPCRBACKGROUND;
209 my $testdir = $QPCRTESTDIR;
210 my $outfile = "$name.qPCR";
212 my $seqcheck = "if [ ! -e $data_dir/Libraries/$lib.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for sequencing.\"; fi;";
213 my $cmd = "$outfile: $data_dir/Libraries/$lib.txt\n\t$QPCRDIR/qPCR \$< $background $testdir > \$@\n";
214 writeTask($qpcr, "qPCR", $outfile, $cmd,$seqcheck);
222 sub WriteProfileTasks {
225 if(exists($project->{ProfileReads})) {
226 for my $profile (@{$project->{ProfileReads}}) {
227 my $task = $profile->{TaskId};
228 my $genome = $profile->{Genome};
229 my $lib = $profile->{Library};
230 my $name = $profile->{Name};
232 $name =~ s/\s\(\)\./____/g;
234 my $outfile = "$lib.wig.gz $lib.profile.gif";
236 my $seqcheck = "if [ ! -e $data_dir/Libraries/$lib.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for sequencing.\"; fi;";
237 my $cmds .= "$lib.wig.gz: $data_dir/Libraries/$lib.txt\n";
238 $cmds .= "\t".$PROFILEDIR.'/profile_reads_bedgraph '.$data_dir.'/Libraries/'.$lib.'.txt "'.$name.'" "'.$name.'" | grep -v contam | grep -v Ribo | grep -v splice | gzip > '.$lib.'.wig.gz';
240 $cmds .= $lib.'.profile.gif: '.$data_dir.'/Libraries/'.$lib.'.txt '.$root_dir.'/reference_data/'.$genome.'_tx_start_sites'."\n";
241 $cmds .= "\t".$PROFILEDIR.'/profile_reads_against_features $^ | '.$root_dir.'/scripts/profile_to_svg.pm | /opt/local/bin/convert - $@'."\n";
244 writeTask($profile, "ProfileReads", $outfile, $cmds,$seqcheck);
251 sub WriteCompareLibTasks {
254 if(exists($project->{CompareLibraries})) {
255 for my $cmp (@{$project->{CompareLibraries}}) {
256 #Usage: /Users/ENCODE/EXPTRACK/QC/count_reads_in_peaks TF label1 fearure1 reads1 label2 feature2 reads2
258 my $task = $cmp->{TaskId};
260 print STDERR "Before: $tf\n"; $tf =~ s/\s/_/g; $tf =~ s/\(\)\./___/g; $tf =~ s/'//;
261 print STDERR "After: $tf\n";
262 my $genome = $cmp->{Genome};
263 my $features = "$root_dir/reference_data/".$genome."_upstream5k_downstream1k";
264 my $name1 = $cmp->{Library}->[0]->{Library};
265 my $name2 = $cmp->{Library}->[1]->{Library};
267 $name1 =~ s/\s\(\)\./____/g;
268 $name2 =~ s/\s\(\)\./____/g;
270 my $outfile = $name1."_".$name2.".compare ";
272 my $seqcheck = "if [ ! -e $data_dir/Libraries/$name1.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $name1 sequencing.\"; fi;";
273 $seqcheck .= "\n\tif [ ! -e $data_dir/Libraries/$name2.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $name2 sequencing.\"; fi;";
275 my $cmd = "$outfile: $data_dir/Libraries/$name1.txt $data_dir/Libraries/$name2.txt\n\t$root_dir/bin/count_reads_in_peaks $tf $name1 $features $data_dir/Libraries/$name1.txt $name2 $features $data_dir/Libraries/$name2.txt > \$@\n";
277 writeTask($cmp, "CompareLibraries", $outfile, $cmd,$seqcheck);
285 sub WriteMotifFindingTasks {
288 if(exists($project->{MotifFinding})) {
289 for my $motiffind (@{$project->{MotifFinding}}) {
290 #my $caller = $motiffind->{Caller};
291 #my $name = $motiffind->{Genome};
292 #my $set = $motiffind->{Set};
293 #my $width = $motiffind->{Width};
294 #my $options = $motiffind->{Options};
297 #for my $i (0..scalar(@{$project->{PeakCalling}})-1) {
298 #if($project->{PeakCalling}->[$i]->{Name} eq $set && $project->{PeakCalling}->[$i]->{Caller} eq $caller) { $index = $i; }
301 #if($index < 0) { print STDERR "Error: Peak calling $set ($caller) not found in configuration.\n"; }
303 ##Usage: BioProspector -i fastafile
305 #my $outfile .= $set."-".$caller.".w".$width.".biop";
308 #if($caller eq "QuEST") { $input_file = "peak_caller.ChIP.out.fasta"; }
309 #elsif($caller eq "WingPeaks") { $input_file = "$set.peaks.fasta"; }
310 #else { print STDERR "Unable to compare peaks from $caller\n"; }
312 #my $set_fasta = $caller."_".$set."/".$input_file;
314 #my $cmd = "$outfile: $set_fasta\n";
315 #$cmd .= "\t$BIOP $options -W $width -i ".'$< -o $@'."\n";
317 #$file_list .= "$outfile ";
325 sub WritePeakCallingTasks {
328 if(exists($project->{PeakCalling})) {
329 for my $peakcall (@{$project->{PeakCalling}}) {
330 my $task = $peakcall->{TaskId};
331 my $name = $peakcall->{Name};
332 my $caller = $peakcall->{Caller};
334 my $signal = $peakcall->{Signal}->{Library};
335 my $bg = $peakcall->{Background}->{Library};
336 my $genome = $peakcall->{Genome};
338 my $seqcheck = "if [ ! -e $data_dir/Libraries/$signal.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $signal sequencing.\"; fi;";
339 $seqcheck .= "\n\tif [ ! -e $data_dir/Libraries/$bg.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $bg sequencing.\"; fi;";
347 if($caller eq "QuEST") {
348 $outfile .= "peak_caller.ChIP.out peak_caller.ChIP.out.bedgraph peak_caller.ChIP.out.fasta";
350 $cmd .= "peak_caller.ChIP.out: $data_dir/Libraries/$signal.txt $data_dir/Libraries/$bg.txt\n";
351 $cmd .= "\trm -f *.align.txt\n";
352 $cmd .= "\t".$QUESTDIR.'/generate_QuEST_parameters.pl -rp '.$GENOMEDIR.'/QuEST_'.$genome.' -solexa_align_ChIP '.$data_dir.'/Libraries/'.$signal.'.txt -solexa_align_RX_noIP '.$data_dir.'/Libraries/'.$bg.'.txt -ap '.$data_dir.'/Tasks/'.$task.' -silent > '.$name.'.QuEST.log;'."\n";
353 $cmd .= "\t".$QUESTDIR.'/run_QuEST_with_param_file.pl -p QuEST.batch.pars >> '.$name.'.QuEST.log;'."\n";
354 $cmd .= "\t".'rm -rf scores;'."\n";
355 $cmd .= "\trm -f *.align.txt\n\n";
357 $cmd .= "peak_caller.ChIP.out.tab: peak_caller.ChIP.out\n";
358 $cmd .= "\t$root_dir/scripts/tabify_quest.sh \$< > \$@\n\n";
360 $cmd .= "peak_caller.ChIP.out.bedgraph: peak_caller.ChIP.out\n";
361 $cmd .= "\t$root_dir/scripts/QuEST_2_BED.pm \$< $name\n";
363 $cmd .= "peak_caller.ChIP.out.fasta: peak_caller.ChIP.out.tab\n";
364 $cmd .= "\t".'cat $< | '.$root_dir.'/scripts/extract_peaks.pm '.$root_dir.'/reference_data/hg18_chrom_list.txt > $@'."\n";
366 } elsif($caller eq "WingPeaks") {
367 $outfile .= "$name.peaks $name.peaks.fasta ";
369 $cmd .= "$name.peaks: $data_dir/Libraries/$signal.txt $data_dir/Libraries/$bg.txt\n";
370 $cmd .= "\t".$WINGPEAKSDIR.'/ChIPSeq_PeakCaller_ENCODE -gn '.$WINGPEAKSGENOMEDIR.'/'.$genome.'_chrlist.cod -it '.$name.' -in 1 -if '.$data_dir.'/Libraries/'.$signal.'.txt -ct Background -cn 1 -cf '.$data_dir.'/Libraries/'.$bg.'.txt -ot '.$name.' > '.$name.'.log;'."\n";
372 $cmd .= "%.peaks.tab: %.peaks\n";
373 $cmd .= "\t".'cat $< | awk \'{ print $$1"\t"$$2"\t"$$3"\t"$$4"\t"$$7}\' > $@'."\n\n";
375 $cmd .= "%.peaks.fasta: %.peaks.tab\n";
376 $cmd .= "\t".'cat $< | '.$root_dir.'/scripts/extract_peaks.pm '.$root_dir.'/reference_data/hg18_chrom_list.txt > $@'."\n\n";
377 } elsif($caller eq "MACS") {
378 $outfile .= $name."_peaks.bed ".$name."_peaks.fasta ValidationPrimers.html ";
380 $cmd .= "\n.PRECIOUS: ".$name.'_peaks.xls '.$name.'_peaks.bed '.$name."_peaks.fasta\n\n";
382 $cmd .= "\n".$name."_peaks.xls: $data_dir/Libraries/$signal.txt $data_dir/Libraries/$bg.txt\n";
383 $cmd .= "\tcat $data_dir/Libraries/$signal.txt | $root_dir/scripts/align_to_bed.pm | grep -v 'splice' | grep -v 'contam' | grep -v 'humRibosomal' > $signal.bed\n";
384 $cmd .= "\tcat $data_dir/Libraries/$bg.txt | $root_dir/scripts/align_to_bed.pm | grep -v 'splice' | grep -v 'contam' | grep -v 'humRibosomal' > $bg.bed\n";
385 $cmd .= "\t$MACS -t $signal.bed -c ./$bg.bed --name=$name --pvalue=1e-10 --mfold=20 > $name.log 2> $name.err\n";
386 $cmd .= "\trm -f $signal.bed $bg.bed\n";
387 $cmd .= "\trm -f Background.ELAND.pos $name.ELAND.pos $name.R0.*.bar Background.ELAND.sorted $name.ELAND.sorted\n";
388 $cmd .= "\t".'exit `grep -c "^CRITICAL" '.$name.'.err`'."\n\n";
390 $cmd .= "\n".$name."_peaks.bed: ".$name."_peaks.xls\n";
391 $cmd .= "\t$root_dir".'/scripts/MACS_2_BED.sh $< '.$name.' > $@'."\n";
393 $cmd .= "\nValidationPrimers.html: ".$name."_peaks.bed\n";
394 $cmd .= "\t$root_dir".'/scripts/primer_design.pm $< '."$signal $root_dir $data_dir ".'> $@'."\n\n";
396 $cmd .= "\n".$name."_peaks.tab: ".$name."_peaks.bed\n";
397 $cmd .= "\t".'cat $< | awk \'{print NR"\t"$$1"\t"$$2"\t"$$3"\t1"}\' > $@'."\n\n";
399 $cmd .= "\n".$name."_peaks.fasta: ".$name."_peaks.tab\n";
400 $cmd .= "\t".'cat $< | '.$root_dir.'/scripts/extract_peaks.pm '.$root_dir.'/reference_data/hg18_chrom_list.txt > $@'."\n\n";
403 writeTask($peakcall, "PeakCalling", $outfile, $cmd,$seqcheck);
409 sub WriteComparePeakCallingTasks {
412 if(exists($project->{ComparePeakCalls})) {
413 for my $cmppeakcall (@{$project->{ComparePeakCalls}}) {
414 my $caller1 = $cmppeakcall->{Caller1};
415 my $caller2 = $cmppeakcall->{Caller2};
416 my $name = $cmppeakcall->{Genome};
417 my $set1 = $cmppeakcall->{Set1};
418 my $set2 = $cmppeakcall->{Set2};
423 #for my $i (0..scalar(@{$xml->{PeakCalling}})-1) {
424 #if($xml->{PeakCalling}->[$i]->{Name} eq $set1 && $xml->{PeakCalling}->[$i]->{Caller} eq $caller1) { $index1 = $i; }
425 #if($xml->{PeakCalling}->[$i]->{Name} eq $set2 && $xml->{PeakCalling}->[$i]->{Caller} eq $caller2) { $index2 = $i; }
428 #if($index1 < 0) { print STDERR "Error: Peak calling $set1 ($caller1) not found in configuration.\n"; }
429 #if($index2 < 0) { print STDERR "Error: Peak calling $set2 ($caller2) not found in configuration.\n"; }
430 #if($index1 >= 0 && $index2 >= 0) {
431 ##Usage: /Users/ENCODE/EXPTRACK/QC/count_reads_in_peaks TF label1 fearure1 reads1 label2 feature2 reads2
434 #my $outfile .= $set1."-".$caller1."_v_".$set2."-".$caller2.".compare";
437 #if($caller1 eq "QuEST") { $peak_file_1 = "peak_caller.ChIP.out.tab"; }
438 #elsif($caller1 eq "WingPeaks") { $peak_file_1 = "$set1.peaks.tab"; }
439 #else { print STDERR "Unable to compare peaks from $caller1\n"; }
442 #if($caller2 eq "QuEST") { $peak_file_2 = "peak_caller.ChIP.out.tab"; }
443 #elsif($caller2 eq "WingPeaks") { $peak_file_2 = "$set2.peaks.tab"; }
444 #else { print STDERR "Unable to compare peaks from $caller2\n"; }
446 #my $set1_feat = $caller1."_".$set1."/".$peak_file_1;
447 #my $set2_feat = $caller2."_".$set2."/".$peak_file_2;
449 #my $cmd = "$outfile: $set1_feat $set2_feat\n";
450 #$cmd .= "\t~/EXPTRACK/QC/count_reads_in_peaks NA $set1-$caller1 $set1_feat $data_dir/Libraries/".$xml->{PeakCalling}->[$index1]->{Signal}->{Library}.".txt ";
451 #$cmd .= "$set2-$caller2 $set2_feat $data_dir/Libraries/".$xml->{PeakCalling}->[$index2]->{Signal}->{Library}.".txt > \$@\n";
453 #$file_list .= "$outfile ";
463 my $dir= "$data_dir/Projects";
464 my $filename = "$dir/Projects.xml";
465 if(!defined($option)) { $option = ""; }
466 `$root_dir/scripts/analys_track_main.py getProjects $option $dir`;
467 `cat $filename | sed -e "s/\&/_and_/" > t; mv t $filename`;