annotate rgFastQC.py @ 6:28d39af2dd06 draft

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fastqc commit 8328b3d01e1cb49c5dcbb6578ec42784ad6beaa8
author devteam
date Mon, 18 Jan 2016 09:33:58 -0500
parents 8c650f7f76e9
children 3fdc1a74d866
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
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
31
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
32 class FastQCRunner(object):
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
33
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
34 def __init__(self,opts=None):
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
35 '''
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
36 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
37 '''
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
38
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
39 # Check whether the options are specified and saves them into the object
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
40 assert opts != None
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
41 self.opts = opts
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
42
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
43 def prepare_command_line(self):
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
44 '''
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
45 Develops the Commandline to run FastQC in Galaxy
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
46 '''
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
47
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
48 # Check whether a given file compression format is valid
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
49 # This prevents uncompression of already uncompressed files
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
50 infname = self.opts.inputfilename
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
51 linf = infname.lower()
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
52 trimext = False
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
53 # decompression at upload currently does NOT remove this now bogus ending - fastqc will barf
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
54 # patched may 29 2013 until this is fixed properly
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
55 if ( linf.endswith('.gz') or linf.endswith('.gzip') ):
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
56 f = gzip.open(self.opts.input)
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
57 try:
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
58 f.readline()
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
59 except:
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
60 trimext = True
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
61 f.close()
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
62 elif linf.endswith('bz2'):
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
63 f = bz2.open(self.opts.input,'rb')
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
64 try:
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
65 f.readline()
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
66 except:
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
67 trimext = True
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
68 f.close()
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
69 elif linf.endswith('.zip'):
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
70 if not zipfile.is_zipfile(self.opts.input):
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
71 trimext = True
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
72 if trimext:
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
73 f = open(self.opts.input)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
74 try:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
75 f.readline()
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
76 except:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
77 raise Exception("Input file corruption, could not identify the filetype")
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
78 infname = os.path.splitext(infname)[0]
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
79
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
80 # Replace unwanted or problematic charaters in the input file name
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
81 self.fastqinfilename = re.sub(ur'[^a-zA-Z0-9_\-\.]', '_', os.path.basename(infname))
4
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
82 # check that the symbolic link gets a proper ending, fastqc seems to ignore the given format otherwise
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
83 if 'fastq' in opts.informat:
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
84 # with fastq the .ext is ignored, but when a format is actually passed it must comply with fastqc's
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
85 # accepted formats..
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
86 opts.informat = 'fastq'
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
87 elif not self.fastqinfilename.endswith(opts.informat):
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
88 self.fastqinfilename += '.%s' % opts.informat
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
89
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
90 # Build the Commandline from the given parameters
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
91 command_line = [opts.executable, '--outdir %s' % opts.outputdir]
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
92 if opts.contaminants != None:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
93 command_line.append('--contaminants %s' % opts.contaminants)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
94 if opts.limits != None:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
95 command_line.append('--limits %s' % opts.limits)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
96 command_line.append('--quiet')
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
97 command_line.append('--extract') # to access the output text file
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
98 command_line.append(self.fastqinfilename)
4
8c650f7f76e9 Uploaded
devteam
parents: 3
diff changeset
99 command_line.append('-f %s' % opts.informat)
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
100 self.command_line = ' '.join(command_line)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
101
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
102 def copy_output_file_to_dataset(self):
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
103 '''
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
104 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
105 '''
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
106
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
107 # retrieve html file
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
108 result_file = glob.glob(opts.outputdir + '/*html')
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
109 with open(result_file[0], 'rb') as fsrc:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
110 with open(self.opts.htmloutput, 'wb') as fdest:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
111 shutil.copyfileobj(fsrc, fdest)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
112
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
113 # retrieve text file
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
114 text_file = glob.glob(opts.outputdir + '/*/fastqc_data.txt')
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
115 with open(text_file[0], 'rb') as fsrc:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
116 with open(self.opts.textoutput, 'wb') as fdest:
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
117 shutil.copyfileobj(fsrc, fdest)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
118
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
119 def run_fastqc(self):
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
120 '''
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
121 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
122 '''
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
123
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
124 # Create a log file
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
125 dummy,tlog = tempfile.mkstemp(prefix='rgFastQC',suffix=".log",dir=self.opts.outputdir)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
126 sout = open(tlog, 'w')
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
127
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
128 self.prepare_command_line()
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
129 sout.write(self.command_line)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
130 sout.write('\n')
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
131 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
132 os.symlink(self.opts.input, self.fastqinfilename)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
133 sout.write("check_call\n")
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
134 subprocess.check_call(self.command_line, shell=True)
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
135 sout.write("Copying working %s file to %s \n" % (self.fastqinfilename, self.opts.htmloutput))
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
136 self.copy_output_file_to_dataset()
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
137 sout.write("Finished")
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
138 sout.close()
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
139
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
140 if __name__ == '__main__':
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
141 op = optparse.OptionParser()
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
142 op.add_option('-i', '--input', default=None)
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
143 op.add_option('-j', '--inputfilename', default=None)
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
144 op.add_option('-o', '--htmloutput', default=None)
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
145 op.add_option('-t', '--textoutput', default=None)
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
146 op.add_option('-d', '--outputdir', default="/tmp/shortread")
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
147 op.add_option('-f', '--informat', default='fastq')
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
148 op.add_option('-n', '--namejob', default='rgFastQC')
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
149 op.add_option('-c', '--contaminants', default=None)
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
150 op.add_option('-l', '--limits', default=None)
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
151 op.add_option('-e', '--executable', default='fastqc')
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
152 opts, args = op.parse_args()
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
153
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
154 assert opts.input != None
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
155 assert opts.inputfilename != None
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
156 assert opts.htmloutput != None
0
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
157 assert os.path.isfile(opts.executable),'##rgFastQC.py error - cannot find executable %s' % opts.executable
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
158 if not os.path.exists(opts.outputdir):
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
159 os.makedirs(opts.outputdir)
e28c965eeed4 Imported from capsule None
devteam
parents:
diff changeset
160
1
8fae48caaf06 Uploaded form GH
devteam
parents: 0
diff changeset
161 fastqc_runner = FastQCRunner(opts)
3
0b201de108b9 Uploaded
devteam
parents: 1
diff changeset
162 fastqc_runner.run_fastqc()