annotate rgFastQC.py @ 9:3a458e268066 draft

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