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;";
185 $seqcheck .= "if [ ! -e $data_dir/Libraries/$hpa2.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for sequencing.\"; fi;";
186 $seqcheck .= "if [ ! -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;";
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 $root_dir/reference_data/".$genome."_methylseq_regions $data_dir/Libraries/$hpa2.txt > \$@\n";
190 writeTask($mseq, $root, $outfile, $cmd, $seqcheck);
198 sub WriteProfileTasks {
205 if(exists($project->{qPCR})) {
206 for my $qpcr (@{$project->{qPCR}}) {
207 my $task = $qpcr->{TaskId};
208 my $lib = $qpcr->{Library};
209 my $genome = $qpcr->{Genome};
210 my $name = $qpcr->{Name}; $name =~ tr/ \(\)\./____/;
212 my $background = $QPCRBACKGROUND;
213 my $testdir = $QPCRTESTDIR;
214 my $outfile = "$name.qPCR";
216 my $seqcheck = "if [ ! -e $data_dir/Libraries/$lib.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for sequencing.\"; fi;";
217 my $cmd = "$outfile: $data_dir/Libraries/$lib.txt\n\t$QPCRDIR/qPCR \$< $background $testdir > \$@\n";
218 writeTask($qpcr, "qPCR", $outfile, $cmd,$seqcheck);
226 sub WriteProfileTasks {
229 if(exists($project->{ProfileReads})) {
230 for my $profile (@{$project->{ProfileReads}}) {
231 my $task = $profile->{TaskId};
232 my $genome = $profile->{Genome};
233 my $lib = $profile->{Library};
234 my $name = $profile->{Name};
236 $name =~ s/\s\(\)\./____/g;
238 my $outfile = "$lib.wig.gz $lib.profile.gif";
240 my $seqcheck = "if [ ! -e $data_dir/Libraries/$lib.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for sequencing.\"; fi;";
241 my $cmds .= "$lib.wig.gz: $data_dir/Libraries/$lib.txt\n";
242 $cmds .= "\t".$PROFILEDIR.'/profile_reads_wig '.$data_dir.'/Libraries/'.$lib.'.txt "'.$name.'" "'.$name.'" | gzip > '.$lib.'.wig.gz';
244 $cmds .= $lib.'.profile.gif: '.$data_dir.'/Libraries/'.$lib.'.txt '.$root_dir.'/reference_data/'.$genome.'_tx_start_sites'."\n";
245 $cmds .= "\t".$PROFILEDIR.'/profile_reads_against_features $^ | '.$root_dir.'/scripts/profile_to_svg.pm | /opt/local/bin/convert - $@'."\n";
248 writeTask($profile, "ProfileReads", $outfile, $cmds,$seqcheck);
255 sub WriteCompareLibTasks {
258 if(exists($project->{CompareLibraries})) {
259 for my $cmp (@{$project->{CompareLibraries}}) {
260 #Usage: /Users/ENCODE/EXPTRACK/QC/count_reads_in_peaks TF label1 fearure1 reads1 label2 feature2 reads2
262 my $task = $cmp->{TaskId};
264 print STDERR "Before: $tf\n"; $tf =~ s/\s/_/g; $tf =~ s/\(\)\./___/g; $tf =~ s/'//;
265 print STDERR "After: $tf\n";
266 my $genome = $cmp->{Genome};
267 my $features = "$root_dir/reference_data/".$genome."_upstream5k_downstream1k";
268 my $name1 = $cmp->{Library}->[0]->{Library};
269 my $name2 = $cmp->{Library}->[1]->{Library};
271 $name1 =~ s/\s\(\)\./____/g;
272 $name2 =~ s/\s\(\)\./____/g;
274 my $outfile = $name1."_".$name2.".compare ";
276 my $seqcheck = "if [ ! -e $data_dir/Libraries/$name1.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $name1 sequencing.\"; fi;";
277 $seqcheck .= "\n\tif [ ! -e $data_dir/Libraries/$name2.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $name2 sequencing.\"; fi;";
279 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";
281 writeTask($cmp, "CompareLibraries", $outfile, $cmd,$seqcheck);
289 sub WriteMotifFindingTasks {
292 if(exists($project->{MotifFinding})) {
293 for my $motiffind (@{$project->{MotifFinding}}) {
294 #my $caller = $motiffind->{Caller};
295 #my $name = $motiffind->{Genome};
296 #my $set = $motiffind->{Set};
297 #my $width = $motiffind->{Width};
298 #my $options = $motiffind->{Options};
301 #for my $i (0..scalar(@{$project->{PeakCalling}})-1) {
302 #if($project->{PeakCalling}->[$i]->{Name} eq $set && $project->{PeakCalling}->[$i]->{Caller} eq $caller) { $index = $i; }
305 #if($index < 0) { print STDERR "Error: Peak calling $set ($caller) not found in configuration.\n"; }
307 ##Usage: BioProspector -i fastafile
309 #my $outfile .= $set."-".$caller.".w".$width.".biop";
312 #if($caller eq "QuEST") { $input_file = "peak_caller.ChIP.out.fasta"; }
313 #elsif($caller eq "WingPeaks") { $input_file = "$set.peaks.fasta"; }
314 #else { print STDERR "Unable to compare peaks from $caller\n"; }
316 #my $set_fasta = $caller."_".$set."/".$input_file;
318 #my $cmd = "$outfile: $set_fasta\n";
319 #$cmd .= "\t$BIOP $options -W $width -i ".'$< -o $@'."\n";
321 #$file_list .= "$outfile ";
329 sub WritePeakCallingTasks {
332 if(exists($project->{PeakCalling})) {
333 for my $peakcall (@{$project->{PeakCalling}}) {
334 my $task = $peakcall->{TaskId};
335 my $name = $peakcall->{Name};
336 my $caller = $peakcall->{Caller};
338 my $signal = $peakcall->{Signal}->{Library};
339 my $bg = $peakcall->{Background}->{Library};
340 my $genome = $peakcall->{Genome};
342 my $seqcheck = "if [ ! -e $data_dir/Libraries/$signal.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $signal sequencing.\"; fi;";
343 $seqcheck .= "\n\tif [ ! -e $data_dir/Libraries/$bg.txt ]; then $root_dir/scripts/analys_track_main.py updsts $task \"Waiting for $bg sequencing.\"; fi;";
351 if($caller eq "QuEST") {
352 $outfile .= "peak_caller.ChIP.out peak_caller.ChIP.out.bedgraph peak_caller.ChIP.out.fasta";
354 $cmd .= "peak_caller.ChIP.out: $data_dir/Libraries/$signal.txt $data_dir/Libraries/$bg.txt\n";
355 $cmd .= "\trm -f *.align.txt\n";
356 $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";
357 $cmd .= "\t".$QUESTDIR.'/run_QuEST_with_param_file.pl -p QuEST.batch.pars >> '.$name.'.QuEST.log;'."\n";
358 $cmd .= "\t".'rm -rf scores;'."\n";
359 $cmd .= "\trm -f *.align.txt\n\n";
361 $cmd .= "peak_caller.ChIP.out.tab: peak_caller.ChIP.out\n";
362 $cmd .= "\t$root_dir/scripts/tabify_quest.sh \$< > \$@\n\n";
364 $cmd .= "peak_caller.ChIP.out.bedgraph: peak_caller.ChIP.out\n";
365 $cmd .= "\t$root_dir/scripts/QuEST_2_BED.pm \$< $name\n";
367 $cmd .= "peak_caller.ChIP.out.fasta: peak_caller.ChIP.out.tab\n";
368 $cmd .= "\t".'cat $< | '.$root_dir.'/scripts/extract_peaks.pm '.$root_dir.'/reference_data/hg18_chrom_list.txt > $@'."\n";
370 } elsif($caller eq "WingPeaks") {
371 $outfile .= "$name.peaks $name.peaks.fasta ";
373 $cmd .= "$name.peaks: $data_dir/Libraries/$signal.txt $data_dir/Libraries/$bg.txt\n";
374 $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";
376 $cmd .= "%.peaks.tab: %.peaks\n";
377 $cmd .= "\t".'cat $< | awk \'{ print $$1"\t"$$2"\t"$$3"\t"$$4"\t"$$7}\' > $@'."\n\n";
379 $cmd .= "%.peaks.fasta: %.peaks.tab\n";
380 $cmd .= "\t".'cat $< | '.$root_dir.'/scripts/extract_peaks.pm '.$root_dir.'/reference_data/hg18_chrom_list.txt > $@'."\n\n";
381 } elsif($caller eq "MACS") {
382 $outfile .= $name."_peaks.bed ".$name."_peaks.fasta ValidationPrimers.html ";
384 $cmd .= "\n.PRECIOUS: ".$name.'_peaks.xls '.$name.'_peaks.bed '.$name."_peaks.fasta\n\n";
386 $cmd .= "\n".$name."_peaks.xls: $data_dir/Libraries/$signal.txt $data_dir/Libraries/$bg.txt\n";
387 $cmd .= "\tcat $data_dir/Libraries/$signal.txt | $root_dir/scripts/align_to_bed.pm | grep -v 'contam' | grep -v 'humRibosomal' > $signal.bed\n";
388 $cmd .= "\tcat $data_dir/Libraries/$bg.txt | $root_dir/scripts/align_to_bed.pm | grep -v 'contam' | grep -v 'humRibosomal' > $bg.bed\n";
389 $cmd .= "\t$MACS -t $signal.bed -c ./$bg.bed --name=$name --pvalue=1e-10 --mfold=20 > $name.log 2> $name.err\n";
390 $cmd .= "\trm -f $signal.bed $bg.bed\n";
391 $cmd .= "\trm -f Background.ELAND.pos $name.ELAND.pos $name.R0.*.bar Background.ELAND.sorted $name.ELAND.sorted\n";
392 $cmd .= "\t".'exit `grep -c "^CRITICAL" '.$name.'.err`'."\n\n";
394 $cmd .= "\n".$name."_peaks.bed: ".$name."_peaks.xls\n";
395 $cmd .= "\t$root_dir".'/scripts/MACS_2_BED.sh $< '.$name.' > $@'."\n";
397 $cmd .= "\nValidationPrimers.html: ".$name."_peaks.bed\n";
398 $cmd .= "\t$root_dir".'/scripts/primer_design.pm $< '."$signal $root_dir $data_dir ".'> $@'."\n\n";
400 $cmd .= "\n".$name."_peaks.tab: ".$name."_peaks.bed\n";
401 $cmd .= "\t".'cat $< | awk \'{print NR"\t"$$1"\t"$$2"\t"$$3"\t1"}\' > $@'."\n\n";
403 $cmd .= "\n".$name."_peaks.fasta: ".$name."_peaks.tab\n";
404 $cmd .= "\t".'cat $< | '.$root_dir.'/scripts/extract_peaks.pm '.$root_dir.'/reference_data/hg18_chrom_list.txt > $@'."\n\n";
407 writeTask($peakcall, "PeakCalling", $outfile, $cmd,$seqcheck);
413 sub WriteComparePeakCallingTasks {
416 if(exists($project->{ComparePeakCalls})) {
417 for my $cmppeakcall (@{$project->{ComparePeakCalls}}) {
418 my $caller1 = $cmppeakcall->{Caller1};
419 my $caller2 = $cmppeakcall->{Caller2};
420 my $name = $cmppeakcall->{Genome};
421 my $set1 = $cmppeakcall->{Set1};
422 my $set2 = $cmppeakcall->{Set2};
427 #for my $i (0..scalar(@{$xml->{PeakCalling}})-1) {
428 #if($xml->{PeakCalling}->[$i]->{Name} eq $set1 && $xml->{PeakCalling}->[$i]->{Caller} eq $caller1) { $index1 = $i; }
429 #if($xml->{PeakCalling}->[$i]->{Name} eq $set2 && $xml->{PeakCalling}->[$i]->{Caller} eq $caller2) { $index2 = $i; }
432 #if($index1 < 0) { print STDERR "Error: Peak calling $set1 ($caller1) not found in configuration.\n"; }
433 #if($index2 < 0) { print STDERR "Error: Peak calling $set2 ($caller2) not found in configuration.\n"; }
434 #if($index1 >= 0 && $index2 >= 0) {
435 ##Usage: /Users/ENCODE/EXPTRACK/QC/count_reads_in_peaks TF label1 fearure1 reads1 label2 feature2 reads2
438 #my $outfile .= $set1."-".$caller1."_v_".$set2."-".$caller2.".compare";
441 #if($caller1 eq "QuEST") { $peak_file_1 = "peak_caller.ChIP.out.tab"; }
442 #elsif($caller1 eq "WingPeaks") { $peak_file_1 = "$set1.peaks.tab"; }
443 #else { print STDERR "Unable to compare peaks from $caller1\n"; }
446 #if($caller2 eq "QuEST") { $peak_file_2 = "peak_caller.ChIP.out.tab"; }
447 #elsif($caller2 eq "WingPeaks") { $peak_file_2 = "$set2.peaks.tab"; }
448 #else { print STDERR "Unable to compare peaks from $caller2\n"; }
450 #my $set1_feat = $caller1."_".$set1."/".$peak_file_1;
451 #my $set2_feat = $caller2."_".$set2."/".$peak_file_2;
453 #my $cmd = "$outfile: $set1_feat $set2_feat\n";
454 #$cmd .= "\t~/EXPTRACK/QC/count_reads_in_peaks NA $set1-$caller1 $set1_feat $data_dir/Libraries/".$xml->{PeakCalling}->[$index1]->{Signal}->{Library}.".txt ";
455 #$cmd .= "$set2-$caller2 $set2_feat $data_dir/Libraries/".$xml->{PeakCalling}->[$index2]->{Signal}->{Library}.".txt > \$@\n";
457 #$file_list .= "$outfile ";
467 my $dir= "$data_dir/Projects";
468 my $filename = "$dir/Projects.xml";
469 if(!defined($option)) { $option = ""; }
470 `$root_dir/scripts/analys_track_main.py getProjects $option $dir`;
471 `cat $filename | sed -e "s/\&/_and_/" > t; mv t $filename`;