annotate macs21_wrapper.py @ 5:3c435705aca5 draft default tip

New version 2.1.2-galaxy1 (updates UCSC dependencies)
author pjbriggs
date Tue, 27 Jun 2023 07:54:55 +0000
parents 00d73c812399
children
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
2
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
64 $ bedSort treat.clipped treat.clipped.sorted
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
65 $ bedGraphToBigWig treat.clipped.sorted mm9.chrom.sizes treat.bw
0
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
66
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
67 Get the binaries from
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
68 http://hgdownload.cse.ucsc.edu/admin/exe/linux.x86_64/
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
69
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
70 We skip the fetchChromSizes step if the 'chrom_sizes'
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
71 argument supplied a valid file with the chromosome sizes
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
72 for the genome build in question.
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 """
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
75 print "Generating bigWig from bedGraph..."
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
76 # Check for chromosome sizes
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
77 if not os.path.exists(chrom_sizes):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
78 # Determine genome build
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
79 chrom_sizes = os.path.basename(chrom_sizes)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
80 genome_build = chrom_sizes.split('.')[0]
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
81 if genome_build == '?':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
82 # No genome build set
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
83 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
84 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
85 sys.exit(1)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
86 print "Missing chrom sizes file, attempting to fetch for '%s'" % genome_build
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
87 # Run fetchChromSizes
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
88 chrom_sizes = os.path.join(working_dir,chrom_sizes)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
89 stderr_file = os.path.join(working_dir,"fetchChromSizes.stderr")
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
90 cmd = "fetchChromSizes %s" % genome_build
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
91 print "Running %s" % cmd
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
92 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
93 stdout=open(chrom_sizes,'wb'),
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
94 stderr=open(stderr_file,'wb'))
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
95 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
96 # Copy stderr from fetchChromSizes for information only
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
97 for line in open(stderr_file,'r'):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
98 print line.strip()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
99 os.remove(stderr_file)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
100 # Check that the sizes file was downloaded
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
101 if not os.path.exists(chrom_sizes):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
102 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
103 sys.exit(1)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
104 # Run bedClip
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
105 treat_clipped = "%s.clipped" % os.path.basename(bedgraph_file)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
106 cmd = "bedClip %s %s %s" % (bedgraph_file,chrom_sizes,treat_clipped)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
107 print "Running %s" % cmd
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
108 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
109 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
110 # Check that clipped file exists
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
111 treat_clipped = os.path.join(working_dir,treat_clipped)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
112 if not os.path.exists(treat_clipped):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
113 sys.stderr.write("Failed to create clipped bed file\n")
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
114 sys.exit(1)
2
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
115 # Run bedSort
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
116 treat_clipped_sorted = "%s.sorted" % os.path.basename(treat_clipped)
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
117 cmd = "bedSort %s %s" % (treat_clipped,treat_clipped_sorted)
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
118 print "Running %s" % cmd
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
119 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir)
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
120 proc.wait()
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
121 # Check that sorted file exists
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
122 treat_clipped_sorted = os.path.join(working_dir,treat_clipped_sorted)
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
123 if not os.path.exists(treat_clipped_sorted):
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
124 sys.stderr.write("Failed to create sorted clipped bed file\n")
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
125 sys.exit(1)
0
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
126 # Run bedGraphToBigWig
2
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
127 cmd = "bedGraphToBigWig %s %s %s" % (treat_clipped_sorted,
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
128 chrom_sizes,
0
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
129 bigwig_file)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
130 print "Running %s" % cmd
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
131 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
132 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
133 # Clean up temporary chrom length file
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
134 if os.path.dirname(chrom_sizes) == working_dir:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
135 print "Removing temporary chrom sizes file"
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
136 os.remove(chrom_sizes)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
137
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
138 if __name__ == "__main__":
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
139
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
140 # Echo the command line
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
141 print ' '.join(sys.argv)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
142
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
143 # Initialise output files - values are set by reading from
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
144 # the command line supplied by the Galaxy wrapper
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
145 output_extra_html = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
146 output_extra_path = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
147 output_broadpeaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
148 output_gappedpeaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
149 output_narrowpeaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
150 output_treat_pileup = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
151 output_lambda_bedgraph = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
152 output_bigwig = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
153 output_xls_to_interval_peaks_file = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
154 output_peaks = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
155 output_bdgcmp = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
156
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
157 # Other initialisations
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
158 chrom_sizes_file = None
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
159
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
160 # Build the MACS 2.1 command line
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
161 # Initial arguments are always the same: command & input ChIP-seq file name
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
162 cmdline = ["macs2 %s -t %s" % (sys.argv[1],sys.argv[2])]
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
163
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
164 # Process remaining args
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
165 for arg in sys.argv[3:]:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
166 if arg.startswith('--format='):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
167 # Convert format to uppercase
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
168 format_ = arg.split('=')[1].upper()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
169 cmdline.append("--format=%s" % format_)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
170 elif arg.startswith('--name='):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
171 # Replace whitespace in name with underscores
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
172 experiment_name = '_'.join(arg.split('=')[1].split())
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
173 cmdline.append("--name=%s" % experiment_name)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
174 elif arg.startswith('--length='):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
175 # Extract chromosome size file
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
176 chrom_sizes_file = arg.split('=')[1]
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
177 elif arg.startswith('--output-'):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
178 # Handle destinations for output files
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
179 arg0,filen = arg.split('=')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
180 if arg0 == '--output-summits':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
181 output_summits = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
182 elif arg0 == '--output-extra-files':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
183 output_extra_html = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
184 elif arg0 == '--output-extra-files-path':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
185 output_extra_path = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
186 elif arg0 == '--output-broadpeaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
187 output_broadpeaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
188 elif arg0 == '--output-gappedpeaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
189 output_gappedpeaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
190 elif arg0 == '--output-narrowpeaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
191 output_narrowpeaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
192 elif arg0 == '--output-pileup':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
193 output_treat_pileup = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
194 elif arg0 == '--output-lambda-bedgraph':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
195 output_lambda_bedgraph = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
196 elif arg0 == '--output-bigwig':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
197 output_bigwig = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
198 elif arg0 == '--output-xls-to-interval':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
199 output_xls_to_interval_peaks_file = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
200 elif arg0 == '--output-peaks':
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
201 output_peaks = filen
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
202 else:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
203 # Pass remaining args directly to MACS
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
204 # command line
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
205 cmdline.append(arg)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
206
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
207 cmdline = ' '.join(cmdline)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
208 print "Generated command line:\n%s" % cmdline
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 # Execute MACS2
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
211 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
212 # Make a working directory
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
213 working_dir = tempfile.mkdtemp()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
214 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
215 # Collect stderr in a file for reporting later
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
216 stderr_filen = tempfile.NamedTemporaryFile().name
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
217 #
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
218 # Run MACS2
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
219 proc = subprocess.Popen(args=cmdline,shell=True,cwd=working_dir,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
220 stderr=open(stderr_filen,'wb'))
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
221 proc.wait()
2
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
222 exit_code = proc.returncode
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
223 if exit_code != 0:
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
224 sys.stderr.write(open(stderr_filen,'rb').read())
00d73c812399 Version 2.1.0-6: add sorting step in bigWig generation, and explicitly terminate tool on error from MACS2.
pjbriggs
parents: 0
diff changeset
225 sys.exit(exit_code)
0
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
226
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
227 # Run R script to create PDF from model script
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
228 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
229 cmdline = 'R --vanilla --slave < "%s_model.r" > "%s_model.r.log"' % \
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
230 (experiment_name, experiment_name)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
231 proc = subprocess.Popen(args=cmdline,shell=True,cwd=working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
232 proc.wait()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
233
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
234 # Convert XLS to interval, if requested
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
235 if output_xls_to_interval_peaks_file is not None:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
236 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
237 if os.path.exists(peaks_xls_file):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
238 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
239 header='peaks file')
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 # Create bigWig from bedGraph, if requested
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
242 if output_bigwig is not None:
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
243 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
244 if os.path.exists(treat_bedgraph_file):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
245 make_bigwig_from_bedgraph(treat_bedgraph_file,output_bigwig,
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
246 chrom_sizes_file,working_dir)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
247
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
248 # Move MACS2 output files from working dir to their final destinations
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
249 move_file(working_dir,"%s_summits.bed" % experiment_name,output_summits)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
250 move_file(working_dir,"%s_peaks.xls" % experiment_name,output_peaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
251 move_file(working_dir,"%s_peaks.narrowPeak" % experiment_name,output_narrowpeaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
252 move_file(working_dir,"%s_peaks.broadPeak" % experiment_name,output_broadpeaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
253 move_file(working_dir,"%s_peaks.gappedPeak" % experiment_name,output_gappedpeaks)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
254 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
255 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
256 move_file(working_dir,"bdgcmp_out.bdg",output_bdgcmp)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
257
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
258 # 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
259 # file to allow user to access them from within Galaxy
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
260 html_file = open(output_extra_html,'wb')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
261 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
262 # Make the 'extra files' directory
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
263 os.mkdir(output_extra_path)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
264 # Move the files
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
265 for filen in sorted(os.listdir(working_dir)):
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
266 shutil.move(os.path.join(working_dir,filen),
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
267 os.path.join(output_extra_path,filen))
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
268 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
269 # All files moved, close out HTML
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
270 html_file.write( '</ul></p>\n' )
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
271 # Append any stderr output
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
272 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
273 open(stderr_filen,'rb').read())
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
274 html_file.write('</body></html>\n')
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
275 html_file.close()
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
276
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
277 # Clean up the working directory and files
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
278 os.unlink(stderr_filen)
06cb587a5e87 Uploaded initial version 2.1.0-4.
pjbriggs
parents:
diff changeset
279 os.rmdir(working_dir)