Mercurial > repos > urgi-team > mdust
comparison mdust_wrapper.py @ 0:cc6db1ee2d48 draft
Uploaded
| author | urgi-team |
|---|---|
| date | Mon, 05 Oct 2015 11:05:21 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:cc6db1ee2d48 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 import subprocess | |
| 4 import tempfile | |
| 5 import sys | |
| 6 import os | |
| 7 import re | |
| 8 from optparse import OptionParser | |
| 9 | |
| 10 | |
| 11 class MdustWrapper(object): | |
| 12 | |
| 13 def __init__(self): | |
| 14 self._options = None | |
| 15 | |
| 16 def stop_err(self, msg): | |
| 17 sys.stderr.write("%s\n" % msg) | |
| 18 sys.exit() | |
| 19 | |
| 20 def setAttributesFromCmdLine(self): | |
| 21 description = "mdust_wrapper" | |
| 22 description += "\nWrapper for mdust\n" | |
| 23 description += "example: mdust_wrapper.py -i seq.fasta -v 27\n" | |
| 24 parser = OptionParser(description = description, version = "0.1") | |
| 25 parser.add_option("-i", "--input", dest = "FastaFile", action = "store", type = "string", help = "Input Fasta File name [compulsory] [format: Fasta]", default = "") | |
| 26 parser.add_option("-o", "--output", dest = "outFile", action = "store", type = "string", help = "output File name [compulsory] [format: fasta,tab or bed]", default = "") | |
| 27 parser.add_option("-v", "--cutoff", dest = "cutoff", action = "store", type = "int", help = "cutoff", default = 28) | |
| 28 parser.add_option("-w", "--wsize", dest = "wsize", action = "store", type = "int", help = "window size", default = 3) | |
| 29 parser.add_option("-m", "--maskingletter", dest = "maskingletter", action = "store", type = "string", help = "masking letter", default = "N") | |
| 30 parser.add_option("-f", "--format", dest = "format", action = "store", type = "string", help = "format", default = "default") | |
| 31 options = parser.parse_args()[0] | |
| 32 self._setAttributesFromOptions(options) | |
| 33 | |
| 34 | |
| 35 def _setAttributesFromOptions(self, options): | |
| 36 self._options = options | |
| 37 | |
| 38 if self._options.FastaFile == "": | |
| 39 raise Exception("Missing input file, please provide fasta file with -i file !") | |
| 40 if self._options.outFile == "": | |
| 41 raise Exception("Missing output file, please provide output file with -o file !") | |
| 42 | |
| 43 | |
| 44 def run(self): | |
| 45 | |
| 46 prg = "mdust" | |
| 47 args = "" | |
| 48 args += " %s" % self._options.FastaFile | |
| 49 args += " -v %d" % self._options.cutoff | |
| 50 args += " -w %d" % self._options.wsize | |
| 51 args += " -m %s" % self._options.maskingletter | |
| 52 if self._options.format == "tab" or self._options.format == "bed": | |
| 53 args += " -c " | |
| 54 cmd = "%s %s" %(prg, args) | |
| 55 | |
| 56 try: | |
| 57 tmp_err = tempfile.NamedTemporaryFile().name | |
| 58 tmp_out = "outfile" | |
| 59 tmp_stderr = open( tmp_err, 'wb' ) | |
| 60 tmp_stdout = open( tmp_out, 'wb' ) | |
| 61 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stdout=tmp_stdout, stderr=tmp_stderr ) | |
| 62 returncode = proc.wait() | |
| 63 tmp_stderr.close() | |
| 64 # get stderr, allowing for case where it's very large | |
| 65 tmp_stderr = open( tmp_err, 'rb' ) | |
| 66 tmp_stdout = open( tmp_out, 'rb' ) | |
| 67 | |
| 68 stderr = '' | |
| 69 stdout = '' | |
| 70 buffsize = 1048576 | |
| 71 try: | |
| 72 while True: | |
| 73 stdout += tmp_stdout.read( buffsize ) | |
| 74 if not stdout or len( stdout ) % buffsize != 0: | |
| 75 break | |
| 76 except OverflowError: | |
| 77 pass | |
| 78 tmp_stdout.close() | |
| 79 | |
| 80 try: | |
| 81 while True: | |
| 82 stderr += tmp_stderr.read( buffsize ) | |
| 83 if not stderr or len( stderr ) % buffsize != 0: | |
| 84 break | |
| 85 except OverflowError: | |
| 86 pass | |
| 87 tmp_stderr.close() | |
| 88 if stderr: | |
| 89 raise Exception, stderr | |
| 90 except Exception, e: | |
| 91 self.stop_err( 'Error with mdust :\n' + str( e ) ) | |
| 92 | |
| 93 if self._options.format == 'bed': | |
| 94 with open(tmp_out,"r") as fin: | |
| 95 with open(self._options.outFile, "w") as fout: | |
| 96 lineNumber = 0 | |
| 97 for line in fin: | |
| 98 lineNumber += 1 | |
| 99 m = re.search(r"^(\S+)\t(\d+)\t(\d+)\t(\d+)$", line) | |
| 100 if m is not None: | |
| 101 fout.write("%s\t%d\t%d\n" % (m.group(1), int(m.group(3))-1, int(m.group(4)))) | |
| 102 if m is None: | |
| 103 raise Exception("\nLine %d '%s' does not has a mdust format." % (lineNumber, line)) | |
| 104 else: | |
| 105 os.rename(tmp_out,self._options.outFile) | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 if __name__ == "__main__": | |
| 111 iWrapper = MdustWrapper() | |
| 112 iWrapper.setAttributesFromCmdLine() | |
| 113 iWrapper.run() |
