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