Mercurial > repos > devteam > fastqc
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 |
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 |
19 """ | |
1 | 20 |
0 | 21 import re |
22 import os | |
1 | 23 import shutil |
0 | 24 import subprocess |
25 import optparse | |
26 import tempfile | |
1 | 27 import glob |
28 import gzip | |
29 import bz2 | |
0 | 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 | 32 |
1 | 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 | 35 def __init__(self,opts=None): |
1 | 36 ''' |
37 Initializes an object to run FastQC in Galaxy. To start the process, use the function run_fastqc() | |
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 | 40 # Check whether the options are specified and saves them into the object |
41 assert opts != None | |
42 self.opts = opts | |
0 | 43 |
1 | 44 def prepare_command_line(self): |
45 ''' | |
46 Develops the Commandline to run FastQC in Galaxy | |
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 | 49 # Check whether a given file compression format is valid |
50 # This prevents uncompression of already uncompressed files | |
0 | 51 infname = self.opts.inputfilename |
52 linf = infname.lower() | |
53 trimext = False | |
54 # decompression at upload currently does NOT remove this now bogus ending - fastqc will barf | |
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 | 58 f = gzip.open(self.opts.input) |
59 try: | |
1 | 60 f.readline() |
0 | 61 except: |
62 trimext = True | |
63 f.close() | |
64 elif linf.endswith('bz2'): | |
65 f = bz2.open(self.opts.input,'rb') | |
66 try: | |
67 f.readline() | |
68 except: | |
69 trimext = True | |
70 f.close() | |
71 elif linf.endswith('.zip'): | |
72 if not zipfile.is_zipfile(self.opts.input): | |
73 trimext = True | |
74 if trimext: | |
1 | 75 f = open(self.opts.input) |
76 try: | |
77 f.readline() | |
78 except: | |
79 raise Exception("Input file corruption, could not identify the filetype") | |
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 | 82 # Replace unwanted or problematic charaters in the input file name |
83 self.fastqinfilename = re.sub(ur'[^a-zA-Z0-9_\-\.]', '_', os.path.basename(infname)) | |
4 | 84 # check that the symbolic link gets a proper ending, fastqc seems to ignore the given format otherwise |
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 | 87 # accepted formats.. |
88 opts.informat = 'fastq' | |
89 elif not self.fastqinfilename.endswith(opts.informat): | |
90 self.fastqinfilename += '.%s' % opts.informat | |
91 | |
1 | 92 # Build the Commandline from the given parameters |
93 command_line = [opts.executable, '--outdir %s' % opts.outputdir] | |
94 if opts.contaminants != None: | |
95 command_line.append('--contaminants %s' % opts.contaminants) | |
96 if opts.limits != None: | |
97 command_line.append('--limits %s' % opts.limits) | |
98 command_line.append('--quiet') | |
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 | 104 command_line.append(self.fastqinfilename) |
105 self.command_line = ' '.join(command_line) | |
106 | |
107 def copy_output_file_to_dataset(self): | |
108 ''' | |
109 Retrieves the output html and text files from the output directory and copies them to the Galaxy output files | |
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 | 112 # retrieve html file |
113 result_file = glob.glob(opts.outputdir + '/*html') | |
114 with open(result_file[0], 'rb') as fsrc: | |
115 with open(self.opts.htmloutput, 'wb') as fdest: | |
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 | 118 # retrieve text file |
119 text_file = glob.glob(opts.outputdir + '/*/fastqc_data.txt') | |
120 with open(text_file[0], 'rb') as fsrc: | |
121 with open(self.opts.textoutput, 'wb') as fdest: | |
122 shutil.copyfileobj(fsrc, fdest) | |
123 | |
124 def run_fastqc(self): | |
125 ''' | |
126 Executes FastQC. Make sure the mandatory import parameters input, inputfilename, outputdir and htmloutput have been specified in the options (opts) | |
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 | 129 # Create a log file |
130 dummy,tlog = tempfile.mkstemp(prefix='rgFastQC',suffix=".log",dir=self.opts.outputdir) | |
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 | 133 self.prepare_command_line() |
134 sout.write(self.command_line) | |
135 sout.write('\n') | |
136 sout.write("Creating symlink\n") # between the input (.dat) file and the given input file name | |
137 os.symlink(self.opts.input, self.fastqinfilename) | |
138 sout.write("check_call\n") | |
139 subprocess.check_call(self.command_line, shell=True) | |
140 sout.write("Copying working %s file to %s \n" % (self.fastqinfilename, self.opts.htmloutput)) | |
141 self.copy_output_file_to_dataset() | |
142 sout.write("Finished") | |
0 | 143 sout.close() |
144 | |
145 if __name__ == '__main__': | |
146 op = optparse.OptionParser() | |
147 op.add_option('-i', '--input', default=None) | |
1 | 148 op.add_option('-j', '--inputfilename', default=None) |
0 | 149 op.add_option('-o', '--htmloutput', default=None) |
1 | 150 op.add_option('-t', '--textoutput', default=None) |
0 | 151 op.add_option('-d', '--outputdir', default="/tmp/shortread") |
152 op.add_option('-f', '--informat', default='fastq') | |
153 op.add_option('-n', '--namejob', default='rgFastQC') | |
154 op.add_option('-c', '--contaminants', default=None) | |
1 | 155 op.add_option('-l', '--limits', default=None) |
0 | 156 op.add_option('-e', '--executable', default='fastqc') |
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 | 159 assert opts.input != None |
160 assert opts.inputfilename != None | |
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 | 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 | 165 fastqc_runner = FastQCRunner(opts) |
3 | 166 fastqc_runner.run_fastqc() |