annotate pyrocleaner_galaxy_tool_V1.2/pyrocleaner_wrapper.py @ 0:ef5dd11c01e6 default tip

pyrocleaner v1.2
author g2cmnty@test-web1.g2.bx.psu.edu
date Thu, 09 Jun 2011 06:09:09 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
1 #
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
2 # Pyrocleaner wrapper for galaxy
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
3 # Copyright (C) 2009 INRA
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
4 #
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
5 # This program is free software: you can redistribute it and/or modify
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
8 # (at your option) any later version.
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
9 #
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful,
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
13 # GNU General Public License for more details.
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
14 #
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
17 #
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
18 #
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
19
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
20 __author__ = 'Plateforme bioinformatique Midi Pyrenees'
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
21 __copyright__ = 'Copyright (C) 2009 INRA'
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
22 __license__ = 'GNU General Public License'
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
23 __version__ = '1.2'
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
24 __email__ = 'support.genopole@toulouse.inra.fr'
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
25 __status__ = 'beta'
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
26
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
27 import optparse, os, shutil, subprocess, sys, tempfile, re, string
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
28
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
29 if __name__ == "__main__":
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
30
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
31 parser = optparse.OptionParser()
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
32
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
33 parser.add_option( '-s', '--options', dest='options', help='The pyrocleaner options' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
34 parser.add_option( '-l', '--log', dest='log', help='Path to the pyrocleaner log' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
35 parser.add_option( '-f', '--format', dest='format', help='The input file format (sff|fastq)' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
36 parser.add_option( '-o', '--output', dest='output', help='The output file path' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
37 parser.add_option( '-a', '--out-pe-qual', dest='out_pe_qual', help='The output pairends qual file path' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
38 parser.add_option( '-b', '--out-pe-fasta', dest='out_pe_fasta', help='The output pairends fasta file path' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
39 parser.add_option( '-d', '--out-dir', dest='out_dir', help='The output dir where to process the data' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
40
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
41 (options, args) = parser.parse_args()
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
42
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
43 buffsize = 1048576
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
44 tmp_dir = tempfile.mkdtemp()
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
45
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
46 try:
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
47
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
48 if options.format == "sff": format = "sff"
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
49 else : format = "fastq"
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
50
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
51 tmp_stderr_name = tempfile.NamedTemporaryFile( dir=tmp_dir,suffix='.err' ).name
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
52 tmp_stderr = open( tmp_stderr_name, 'wb' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
53 tmp_stdout_name = tempfile.NamedTemporaryFile( dir=tmp_dir,suffix='.out' ).name
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
54 tmp_stdout = open( tmp_stdout_name, 'wb' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
55 cmd = 'pyrocleaner --format %s %s --out %s' % (format, options.options, options.out_dir)
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
56 proc = subprocess.Popen( args=cmd, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno() )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
57 returncode = proc.wait()
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
58 tmp_stderr.close()
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
59 # get stderr, allowing for case where it's very large
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
60 tmp_stderr = open( tmp_stderr_name, 'rb' )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
61 stderr = ''
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
62 try:
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
63 while True:
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
64 stderr += tmp_stderr.read( buffsize )
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
65 if not stderr or len( stderr ) % buffsize != 0:
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
66 break
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
67 except OverflowError:
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
68 pass
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
69 tmp_stderr.close()
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
70 if returncode != 0:
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
71 raise Exception, stderr
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
72
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
73 # Move the log file to the right place
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
74 shutil.move(os.path.join(options.out_dir, "pyrocleaner.log"), options.log)
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
75
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
76 for file in os.listdir(options.out_dir):
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
77 # If the file has the input file format and is not a shotgun file
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
78 if file.endswith(format):
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
79 shutil.move(os.path.join(options.out_dir, file), options.output)
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
80 # If it's a quality file from shotgun
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
81 if file.endswith(".qual") :
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
82 shutil.move(os.path.join(options.out_dir, file), options.out_pe_qual)
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
83 # If it's a fasta file from shotgun
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
84 elif file.endswith(".fasta") :
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
85 shutil.move(os.path.join(options.out_dir, file), options.out_pe_fasta)
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
86
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
87 except Exception:
ef5dd11c01e6 pyrocleaner v1.2
g2cmnty@test-web1.g2.bx.psu.edu
parents:
diff changeset
88 raise Exception, 'Error executing pyrocleaner.'