Mercurial > repos > devteam > fastqc
annotate rgFastQC.py @ 11:db2dc6bc8f05 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
author | iuc |
---|---|
date | Thu, 20 Apr 2017 11:06:17 -0400 |
parents | a00a6402d09a |
children | f2e8552cf1d0 |
rev | line source |
---|---|
0 | 1 """ |
1 | 2 Rewrite of rgFastQC.py for Version 0.11.2 of FastQC. |
3 | |
4 Changes implemented from tmcgowan at | |
5 https://testtoolshed.g2.bx.psu.edu/view/tmcgowan/fastqc | |
6 and iuc at https://toolshed.g2.bx.psu.edu/view/iuc/fastqc | |
7 with minor changes and bug fixes | |
0 | 8 |
1 | 9 SYNOPSIS |
10 | |
11 rgFastQC.py -i input_file -j input_file.name -o output_html_file [-d output_directory] | |
12 [-f fastq|bam|sam] [-n job_name] [-c contaminant_file] [-e fastqc_executable] | |
0 | 13 |
1 | 14 EXAMPLE (generated by Galaxy) |
0 | 15 |
1 | 16 rgFastQC.py -i path/dataset_1.dat -j 1000gsample.fastq -o path/dataset_3.dat -d path/job_working_directory/subfolder |
17 -f fastq -n FastQC -c path/dataset_2.dat -e fastqc | |
0 | 18 """ |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
19 import bz2 |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
20 import glob |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
21 import gzip |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
22 import mimetypes |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
23 import optparse |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
24 import os |
0 | 25 import re |
1 | 26 import shutil |
0 | 27 import subprocess |
28 import tempfile | |
29 import zipfile | |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
30 |
0 | 31 |
1 | 32 class FastQCRunner(object): |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
33 def __init__(self, opts=None): |
1 | 34 ''' |
35 Initializes an object to run FastQC in Galaxy. To start the process, use the function run_fastqc() | |
36 ''' | |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
37 |
1 | 38 # Check whether the options are specified and saves them into the object |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
39 assert opts is not None |
1 | 40 self.opts = opts |
0 | 41 |
1 | 42 def prepare_command_line(self): |
43 ''' | |
44 Develops the Commandline to run FastQC in Galaxy | |
45 ''' | |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
46 |
1 | 47 # Check whether a given file compression format is valid |
48 # This prevents uncompression of already uncompressed files | |
0 | 49 infname = self.opts.inputfilename |
50 linf = infname.lower() | |
11
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
51 informat = self.opts.informat |
0 | 52 trimext = False |
53 # decompression at upload currently does NOT remove this now bogus ending - fastqc will barf | |
54 # patched may 29 2013 until this is fixed properly | |
11
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
55 ftype = mimetypes.guess_type(self.opts.input) |
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
56 if linf.endswith('.gz') or linf.endswith('.gzip') or ftype[-1] == "gzip" or informat.endswith('.gz'): |
0 | 57 f = gzip.open(self.opts.input) |
58 try: | |
1 | 59 f.readline() |
11
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
60 ftype = ['gzip'] |
0 | 61 except: |
62 trimext = True | |
63 f.close() | |
11
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
64 elif linf.endswith('bz2') or informat.endswith('.bz2'): |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
65 f = bz2.BZ2File(self.opts.input, 'r') |
0 | 66 try: |
11
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
67 ftype = ['bzip2'] |
0 | 68 f.readline() |
69 except: | |
70 trimext = True | |
71 f.close() | |
72 elif linf.endswith('.zip'): | |
73 if not zipfile.is_zipfile(self.opts.input): | |
74 trimext = True | |
75 if trimext: | |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
76 f = open(self.opts.input) |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
77 try: |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
78 f.readline() |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
79 except: |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
80 raise Exception("Input file corruption, could not identify the filetype") |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
81 infname = os.path.splitext(infname)[0] |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
82 |
1 | 83 # Replace unwanted or problematic charaters in the input file name |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
84 self.fastqinfilename = re.sub(r'[^a-zA-Z0-9_\-\.]', '_', os.path.basename(infname)) |
4 | 85 # check that the symbolic link gets a proper ending, fastqc seems to ignore the given format otherwise |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
86 if 'fastq' in self.opts.informat: |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
87 # with fastq the .ext is ignored, but when a format is actually passed it must comply with fastqc's |
4 | 88 # accepted formats.. |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
89 self.opts.informat = 'fastq' |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
90 elif not self.fastqinfilename.endswith(self.opts.informat): |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
91 self.fastqinfilename += '.%s' % self.opts.informat |
4 | 92 |
1 | 93 # Build the Commandline from the given parameters |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
94 command_line = [opts.executable, '--outdir %s' % self.opts.outputdir] |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
95 if self.opts.contaminants is not None: |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
96 command_line.append('--contaminants %s' % self.opts.contaminants) |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
97 if self.opts.limits is not None: |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
98 command_line.append('--limits %s' % self.opts.limits) |
1 | 99 command_line.append('--quiet') |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
100 command_line.append('--extract') # to access the output text file |
11
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
101 if ftype[-1] == 'gzip': |
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
102 self.fastqinfilename += '.gz' |
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
103 elif ftype[-1] == 'bzip2': |
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
104 self.fastqinfilename += '.bz2' |
db2dc6bc8f05
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 168f7aeb0f9f38de6dec873f3cbf0c30014fdd72
iuc
parents:
10
diff
changeset
|
105 else: |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
106 command_line.append('-f %s' % self.opts.informat) |
1 | 107 command_line.append(self.fastqinfilename) |
108 self.command_line = ' '.join(command_line) | |
109 | |
110 def copy_output_file_to_dataset(self): | |
111 ''' | |
112 Retrieves the output html and text files from the output directory and copies them to the Galaxy output files | |
113 ''' | |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
114 |
1 | 115 # retrieve html file |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
116 result_file = glob.glob(self.opts.outputdir + '/*html') |
1 | 117 with open(result_file[0], 'rb') as fsrc: |
118 with open(self.opts.htmloutput, 'wb') as fdest: | |
119 shutil.copyfileobj(fsrc, fdest) | |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
120 |
1 | 121 # retrieve text file |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
122 text_file = glob.glob(self.opts.outputdir + '/*/fastqc_data.txt') |
1 | 123 with open(text_file[0], 'rb') as fsrc: |
124 with open(self.opts.textoutput, 'wb') as fdest: | |
125 shutil.copyfileobj(fsrc, fdest) | |
126 | |
127 def run_fastqc(self): | |
128 ''' | |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
129 Executes FastQC. Make sure the mandatory import parameters input, inputfilename, outputdir and htmloutput have been specified in the options |
1 | 130 ''' |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
131 |
1 | 132 # Create a log file |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
133 dummy, tlog = tempfile.mkstemp(prefix='rgFastQC', suffix=".log", dir=self.opts.outputdir) |
1 | 134 sout = open(tlog, 'w') |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
135 |
1 | 136 self.prepare_command_line() |
137 sout.write(self.command_line) | |
138 sout.write('\n') | |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
139 sout.write("Creating symlink\n") # between the input (.dat) file and the given input file name |
1 | 140 os.symlink(self.opts.input, self.fastqinfilename) |
141 sout.write("check_call\n") | |
142 subprocess.check_call(self.command_line, shell=True) | |
143 sout.write("Copying working %s file to %s \n" % (self.fastqinfilename, self.opts.htmloutput)) | |
144 self.copy_output_file_to_dataset() | |
145 sout.write("Finished") | |
0 | 146 sout.close() |
147 | |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
148 |
0 | 149 if __name__ == '__main__': |
150 op = optparse.OptionParser() | |
151 op.add_option('-i', '--input', default=None) | |
1 | 152 op.add_option('-j', '--inputfilename', default=None) |
0 | 153 op.add_option('-o', '--htmloutput', default=None) |
1 | 154 op.add_option('-t', '--textoutput', default=None) |
0 | 155 op.add_option('-d', '--outputdir', default="/tmp/shortread") |
156 op.add_option('-f', '--informat', default='fastq') | |
157 op.add_option('-n', '--namejob', default='rgFastQC') | |
158 op.add_option('-c', '--contaminants', default=None) | |
1 | 159 op.add_option('-l', '--limits', default=None) |
0 | 160 op.add_option('-e', '--executable', default='fastqc') |
161 opts, args = op.parse_args() | |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
162 |
10
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
163 assert opts.input is not None |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
164 assert opts.inputfilename is not None |
a00a6402d09a
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fastqc commit 2bfbb5ae6b801e43355fdc3f964a5111fe3fe3a1
iuc
parents:
9
diff
changeset
|
165 assert opts.htmloutput is not None |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
166 if not os.path.exists(opts.outputdir): |
0 | 167 os.makedirs(opts.outputdir) |
8
06819360a9e2
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit df4c0b0c6372e2984966e220fa42ecd8a3d370e8
devteam
parents:
7
diff
changeset
|
168 |
1 | 169 fastqc_runner = FastQCRunner(opts) |
3 | 170 fastqc_runner.run_fastqc() |