annotate macs21_wrapper.py @ 0:06cb587a5e87 draft

Uploaded initial version 2.1.0-4.
author pjbriggs
date Tue, 30 Jun 2015 08:16:18 -0400
parents
children 00d73c812399
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
1 #!/bin/env python
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
2 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
3 # Galaxy wrapper to run MACS 2.1
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
4 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
5 # Completely rewritten from the original macs2 wrapped by Ziru Zhou
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
6 # taken from http://toolshed.g2.bx.psu.edu/view/modencode-dcc/macs2
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
7
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
8 import sys
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
9 import os
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
10 import subprocess
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
11 import tempfile
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
12 import shutil
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
13
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
14 def move_file(working_dir,name,destination):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
15 """Move a file 'name' from 'working_dir' to 'destination'
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
16
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
17 """
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
18 if destination is None:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
19 # Nothing to do
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
20 return
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
21 source = os.path.join(working_dir,name)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
22 if os.path.exists(source):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
23 shutil.move(source,destination)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
24
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
25 def convert_xls_to_interval(xls_file,interval_file,header=None):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
26 """Convert MACS XLS file to interval
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
27
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
28 From the MACS readme: "Coordinates in XLS is 1-based which is different with
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
29 BED format."
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
30
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
31 However this function no longer performs any coordinate conversions, it
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
32 simply ensures that any blank or non-data lines are commented out
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
33
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
34 """
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
35 fp = open(interval_file,'wb')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
36 if header:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
37 fp.write('#%s\n' % header)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
38 for line in open(xls_file):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
39 # Keep all existing comment lines
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
40 if line.startswith('#'):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
41 fp.write(line)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
42 else:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
43 # Split line into fields and test to see if
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
44 # the 'start' field is actually an integer
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
45 fields = line.split('\t')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
46 if len(fields) > 1:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
47 try:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
48 int(fields[1])
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
49 except ValueError:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
50 # Integer conversion failed so comment out
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
51 # "bad" line instead
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
52 fields[0] = "#%s" % fields[0]
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
53 fp.write( '\t'.join( fields ) )
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
54 fp.close()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
55
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
56 def make_bigwig_from_bedgraph(bedgraph_file,bigwig_file,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
57 chrom_sizes,working_dir=None):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
58 """Make bigWig file from a bedGraph
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
59
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
60 The protocol is:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
61
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
62 $ fetchChromSizes.sh mm9 > mm9.chrom.sizes
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
63 $ bedClip treat.bedgraph mm9.chrom.sizes treat.clipped
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
64 $ bedGraphToBigWig treat.clipped mm9.chrom.sizes treat.bw
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
65
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
66 Get the binaries from
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
67 http://hgdownload.cse.ucsc.edu/admin/exe/linux.x86_64/
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
68
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
69 We skip the fetchChromSizes step if the 'chrom_sizes'
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
70 argument supplied a valid file with the chromosome sizes
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
71 for the genome build in question.
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
72
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
73 """
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
74 print "Generating bigWig from bedGraph..."
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
75 # Check for chromosome sizes
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
76 if not os.path.exists(chrom_sizes):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
77 # Determine genome build
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
78 chrom_sizes = os.path.basename(chrom_sizes)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
79 genome_build = chrom_sizes.split('.')[0]
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
80 if genome_build == '?':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
81 # No genome build set
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
82 sys.stderr.write("ERROR genome build not set, cannot get sizes for '?'\n")
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
83 sys.stderr.write("Assign a genome build to your input dataset and rerun\n")
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
84 sys.exit(1)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
85 print "Missing chrom sizes file, attempting to fetch for '%s'" % genome_build
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
86 # Run fetchChromSizes
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
87 chrom_sizes = os.path.join(working_dir,chrom_sizes)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
88 stderr_file = os.path.join(working_dir,"fetchChromSizes.stderr")
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
89 cmd = "fetchChromSizes %s" % genome_build
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
90 print "Running %s" % cmd
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
91 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
92 stdout=open(chrom_sizes,'wb'),
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
93 stderr=open(stderr_file,'wb'))
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
94 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
95 # Copy stderr from fetchChromSizes for information only
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
96 for line in open(stderr_file,'r'):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
97 print line.strip()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
98 os.remove(stderr_file)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
99 # Check that the sizes file was downloaded
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
100 if not os.path.exists(chrom_sizes):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
101 sys.stderr.write("Failed to download chrom sizes for '%s'\n" % genome_build)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
102 sys.exit(1)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
103 # Run bedClip
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
104 treat_clipped = "%s.clipped" % os.path.basename(bedgraph_file)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
105 cmd = "bedClip %s %s %s" % (bedgraph_file,chrom_sizes,treat_clipped)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
106 print "Running %s" % cmd
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
107 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
108 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
109 # Check that clipped file exists
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
110 treat_clipped = os.path.join(working_dir,treat_clipped)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
111 if not os.path.exists(treat_clipped):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
112 sys.stderr.write("Failed to create clipped bed file\n")
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
113 sys.exit(1)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
114 # Run bedGraphToBigWig
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
115 cmd = "bedGraphToBigWig %s %s %s" % (treat_clipped,chrom_sizes,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
116 bigwig_file)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
117 print "Running %s" % cmd
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
118 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
119 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
120 # Clean up temporary chrom length file
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
121 if os.path.dirname(chrom_sizes) == working_dir:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
122 print "Removing temporary chrom sizes file"
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
123 os.remove(chrom_sizes)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
124
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
125 if __name__ == "__main__":
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
126
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
127 # Echo the command line
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
128 print ' '.join(sys.argv)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
129
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
130 # Initialise output files - values are set by reading from
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
131 # the command line supplied by the Galaxy wrapper
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
132 output_extra_html = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
133 output_extra_path = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
134 output_broadpeaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
135 output_gappedpeaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
136 output_narrowpeaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
137 output_treat_pileup = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
138 output_lambda_bedgraph = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
139 output_bigwig = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
140 output_xls_to_interval_peaks_file = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
141 output_peaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
142 output_bdgcmp = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
143
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
144 # Other initialisations
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
145 chrom_sizes_file = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
146
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
147 # Build the MACS 2.1 command line
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
148 # Initial arguments are always the same: command & input ChIP-seq file name
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
149 cmdline = ["macs2 %s -t %s" % (sys.argv[1],sys.argv[2])]
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
150
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
151 # Process remaining args
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
152 for arg in sys.argv[3:]:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
153 if arg.startswith('--format='):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
154 # Convert format to uppercase
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
155 format_ = arg.split('=')[1].upper()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
156 cmdline.append("--format=%s" % format_)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
157 elif arg.startswith('--name='):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
158 # Replace whitespace in name with underscores
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
159 experiment_name = '_'.join(arg.split('=')[1].split())
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
160 cmdline.append("--name=%s" % experiment_name)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
161 elif arg.startswith('--length='):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
162 # Extract chromosome size file
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
163 chrom_sizes_file = arg.split('=')[1]
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
164 elif arg.startswith('--output-'):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
165 # Handle destinations for output files
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
166 arg0,filen = arg.split('=')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
167 if arg0 == '--output-summits':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
168 output_summits = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
169 elif arg0 == '--output-extra-files':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
170 output_extra_html = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
171 elif arg0 == '--output-extra-files-path':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
172 output_extra_path = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
173 elif arg0 == '--output-broadpeaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
174 output_broadpeaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
175 elif arg0 == '--output-gappedpeaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
176 output_gappedpeaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
177 elif arg0 == '--output-narrowpeaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
178 output_narrowpeaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
179 elif arg0 == '--output-pileup':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
180 output_treat_pileup = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
181 elif arg0 == '--output-lambda-bedgraph':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
182 output_lambda_bedgraph = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
183 elif arg0 == '--output-bigwig':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
184 output_bigwig = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
185 elif arg0 == '--output-xls-to-interval':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
186 output_xls_to_interval_peaks_file = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
187 elif arg0 == '--output-peaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
188 output_peaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
189 else:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
190 # Pass remaining args directly to MACS
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
191 # command line
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
192 cmdline.append(arg)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
193
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
194 cmdline = ' '.join(cmdline)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
195 print "Generated command line:\n%s" % cmdline
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
196
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
197 # Execute MACS2
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
198 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
199 # Make a working directory
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
200 working_dir = tempfile.mkdtemp()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
201 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
202 # Collect stderr in a file for reporting later
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
203 stderr_filen = tempfile.NamedTemporaryFile().name
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
204 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
205 # Run MACS2
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
206 proc = subprocess.Popen(args=cmdline,shell=True,cwd=working_dir,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
207 stderr=open(stderr_filen,'wb'))
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
208 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
209
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
210 # Run R script to create PDF from model script
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
211 if os.path.exists(os.path.join(working_dir,"%s_model.r" % experiment_name)):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
212 cmdline = 'R --vanilla --slave < "%s_model.r" > "%s_model.r.log"' % \
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
213 (experiment_name, experiment_name)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
214 proc = subprocess.Popen(args=cmdline,shell=True,cwd=working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
215 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
216
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
217 # Convert XLS to interval, if requested
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
218 if output_xls_to_interval_peaks_file is not None:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
219 peaks_xls_file = os.path.join(working_dir,'%s_peaks.xls' % experiment_name )
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
220 if os.path.exists(peaks_xls_file):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
221 convert_xls_to_interval(peaks_xls_file,output_xls_to_interval_peaks_file,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
222 header='peaks file')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
223
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
224 # Create bigWig from bedGraph, if requested
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
225 if output_bigwig is not None:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
226 treat_bedgraph_file = os.path.join(working_dir,'%s_treat_pileup.bdg' % experiment_name)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
227 if os.path.exists(treat_bedgraph_file):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
228 make_bigwig_from_bedgraph(treat_bedgraph_file,output_bigwig,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
229 chrom_sizes_file,working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
230
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
231 # Move MACS2 output files from working dir to their final destinations
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
232 move_file(working_dir,"%s_summits.bed" % experiment_name,output_summits)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
233 move_file(working_dir,"%s_peaks.xls" % experiment_name,output_peaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
234 move_file(working_dir,"%s_peaks.narrowPeak" % experiment_name,output_narrowpeaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
235 move_file(working_dir,"%s_peaks.broadPeak" % experiment_name,output_broadpeaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
236 move_file(working_dir,"%s_peaks.gappedPeak" % experiment_name,output_gappedpeaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
237 move_file(working_dir,"%s_treat_pileup.bdg" % experiment_name,output_treat_pileup)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
238 move_file(working_dir,"%s_control_lambda.bdg" % experiment_name,output_lambda_bedgraph)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
239 move_file(working_dir,"bdgcmp_out.bdg",output_bdgcmp)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
240
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
241 # Move remaining file to the 'extra files' path and link from the HTML
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
242 # file to allow user to access them from within Galaxy
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
243 html_file = open(output_extra_html,'wb')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
244 html_file.write('<html><head><title>Additional output created by MACS (%s)</title></head><body><h3>Additional Files:</h3><p><ul>\n' % experiment_name)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
245 # Make the 'extra files' directory
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
246 os.mkdir(output_extra_path)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
247 # Move the files
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
248 for filen in sorted(os.listdir(working_dir)):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
249 shutil.move(os.path.join(working_dir,filen),
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
250 os.path.join(output_extra_path,filen))
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
251 html_file.write( '<li><a href="%s">%s</a></li>\n' % (filen,filen))
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
252 # All files moved, close out HTML
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
253 html_file.write( '</ul></p>\n' )
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
254 # Append any stderr output
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
255 html_file.write('<h3>Messages from MACS:</h3>\n<p><pre>%s</pre></p>\n' %
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
256 open(stderr_filen,'rb').read())
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
257 html_file.write('</body></html>\n')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
258 html_file.close()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
259
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
260 # Clean up the working directory and files
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
261 os.unlink(stderr_filen)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
262 os.rmdir(working_dir)