diff ssake.py @ 1:386166019772 draft

Uploaded
author crs4
date Tue, 07 Jan 2014 04:48:50 -0500
parents 0ec408bcfc80
children
line wrap: on
line diff
--- a/ssake.py	Wed Sep 11 12:51:21 2013 -0400
+++ b/ssake.py	Tue Jan 07 04:48:50 2014 -0500
@@ -35,90 +35,12 @@
     return result
 
 
-class SSAKE:
-    def __init__(self, logger, options):
-        self.logger = logger
-        self.executables = ('SSAKE', 'makePairedOutput2EQUALfiles.pl', 'makePairedOutput2UNEQUALfiles.pl')
-        self.logger.debug(which(self.executables[0]))
-        self.logger.debug(which(self.executables[1]))
-        self.logger.debug(which(self.executables[2]))
-        self.logger.debug('Creating temp dir')
-        self.wd = tempfile.mkdtemp()
-        
-        self.kind_of_reads = int(options.kind_of_reads)
-        if not (self.kind_of_reads):
-            self.infile = options.if_unpaired
-            self.paired = 0
-        else:
-            self.infile_r1 = options.if_paired_r1
-            self.infile_r2 = options.if_paired_r2
-            self.paired = 1
-            self.insert_size = options.insert_size
-            self.minnumlinks = options.minnumlinks
-            self.error = options.error
-            self.maxlinkratio = options.maxlinkratio
-            self.minoverlap = options.minoverlap
-        self.mindepthofcoverage = options.mindepthofcoverage
-        self.minoverlappingbases = options.minoverlappingbases
-        self.mincall = options.mincall
-        self.baseratio = options.baseratio
-        self.ignore_header = options.ignore_header
-        self.prefix = options.prefix
-        self.contigs = options.contigs
-        self.log = options.logfile
-        self.short = options.short
-        self.singlets = options.singlets
-        if options.seeds_file:
-            self.seeds_file = options.seeds_file
-
-    def run(self):
-        """ """
-        os.chdir(self.wd)
-        seeds = ''
-        if hasattr(self, 'seeds_file'):
-            seeds = " -s %s" % self.seeds_file
-        if self.kind_of_reads == 1:
-            cmd = "%s %s %s %d" % (
-                self.executables[1], self.infile_r1, self.infile_r2,
-                self.insert_size)
-            self.logger.info("Preparing data")
-            execute(cmd)
-            paired_file = "%s/paired.fa" % self.wd
-            command = "%s -f %s -k %d -e %s -a %s -x %d" % (self.executables[0], paired_file, self.minnumlinks, self.error, self.maxlinkratio, self.minoverlap)
-        elif self.kind_of_reads == 2:
-            cmd = "%s %s %s %d" % (
-                self.executables[2], self.infile_r1, self.infile_r2,
-                self.insert_size)
-            self.logger.info("Preparing data")
-            execute(cmd)
-            paired_file = "%s/paired.fa" % self.wd
-            unpaired_file = "%s/unpaired.fa" % self.wd
-            command = "%s -f %s -g %s -k %d -e %s -a %s -x %d" % (self.executables[0], paired_file, unpaired_file, self.minnumlinks, self.error, self.maxlinkratio, self.minoverlap)
-        else:
-            command = "%s -f %s" % (self.executables[0], self.infile)
-        command += " %s -w %d -m %d -o %d -r %s -h %s -b %s -p %s" % (seeds, self.mindepthofcoverage, self.minoverlappingbases, self.mincall, self.baseratio, self.ignore_header, self.prefix, self.paired)
-        self.logger.debug(command)
-        self.logger.info("Executing SSAKE")
-        execute(command)
-
-        with open("%s.log" % os.path.join(self.wd, self.prefix), 'rb') as ssake_log_file:
-            self.logger.info("\n".join(["Log from SSAKE", ssake_log_file.read()]))
-        self.logger.info("Moving result files")
-        shutil.move("%s.contigs" % os.path.join(self.wd, self.prefix), self.contigs)
-        shutil.move("%s.short" % os.path.join(self.wd, self.prefix), self.short)
-        shutil.move("%s.singlets" % os.path.join(self.wd, self.prefix), self.singlets)
-
-    def __del__(self):
-        shutil.rmtree(self.wd)
-
-
 LOG_FORMAT = '%(asctime)s|%(levelname)-8s|%(message)s'
 LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'
 LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
 
 
 def __main__():
-    """ main function """
     parser = optparse.OptionParser()
     parser.add_option('--if_unpaired', dest='if_unpaired', help='Unpaired FASTA input file name')
     parser.add_option('--if_paired_r1', dest='if_paired_r1', help='Paired FASTA reads 1 input file name')
@@ -154,9 +76,70 @@
         logging.basicConfig(**kwargs)
         logger = logging.getLogger('SSAKE scaffold assembly')
 
-    S = SSAKE(logger, options)
-    S.run()
-    return
+    executables = ('SSAKE', 'makePairedOutput2EQUALfiles.pl', 'makePairedOutput2UNEQUALfiles.pl')
+    logger.debug(which(executables[0]))
+    logger.debug(which(executables[1]))
+    logger.debug(which(executables[2]))
+    logger.debug('Creating temp dir')
+    kind_of_reads = int(options.kind_of_reads)
+    if not (kind_of_reads):
+        infile = options.if_unpaired
+        paired = 0
+    else:
+        infile_r1 = options.if_paired_r1
+        infile_r2 = options.if_paired_r2
+        paired = 1
+        insert_size = options.insert_size
+        minnumlinks = options.minnumlinks
+        error = options.error
+        maxlinkratio = options.maxlinkratio
+        minoverlap = options.minoverlap
+    mindepthofcoverage = options.mindepthofcoverage
+    minoverlappingbases = options.minoverlappingbases
+    mincall = options.mincall
+    baseratio = options.baseratio
+    ignore_header = options.ignore_header
+    prefix = options.prefix
+    contigs = options.contigs
+    short = options.short
+    singlets = options.singlets
+    seeds = " -s %s" % options.seeds_file if options.seeds_file else ''
+    wd = tempfile.mkdtemp()
+    try:
+        os.chdir(wd)
+        if kind_of_reads == 1:
+            cmd = "%s %s %s %d" % (
+                executables[1], infile_r1, infile_r2,
+                insert_size)
+            logger.info("Preparing data")
+            execute(cmd)
+            paired_file = "%s/paired.fa" % wd
+            command = "%s -f %s -k %d -e %s -a %s -x %d" % (executables[0], paired_file, minnumlinks, error, maxlinkratio, minoverlap)
+        elif kind_of_reads == 2:
+            cmd = "%s %s %s %d" % (
+                executables[2], infile_r1, infile_r2,
+                insert_size)
+            logger.info("Preparing data")
+            execute(cmd)
+            paired_file = "%s/paired.fa" % wd
+            unpaired_file = "%s/unpaired.fa" % wd
+            command = "%s -f %s -g %s -k %d -e %s -a %s -x %d" % (executables[0], paired_file, unpaired_file, minnumlinks, error, maxlinkratio, minoverlap)
+        else:
+            command = "%s -f %s" % (executables[0], infile)
+        command += " %s -w %d -m %d -o %d -r %s -h %s -b %s -p %s" % (seeds, mindepthofcoverage, minoverlappingbases, mincall, baseratio, ignore_header, prefix, paired)
+        logger.debug(command)
+        logger.info("Executing SSAKE")
+        execute(command)
+
+        with open("%s.log" % os.path.join(wd, prefix), 'rb') as ssake_log_file:
+            logger.info("\n".join(["Log from SSAKE", ssake_log_file.read()]))
+        logger.info("Moving result files")
+        shutil.move("%s.contigs" % os.path.join(wd, prefix), contigs)
+        shutil.move("%s.short" % os.path.join(wd, prefix), short)
+        shutil.move("%s.singlets" % os.path.join(wd, prefix), singlets)
+    finally:
+        shutil.rmtree(wd)
+
 
 if __name__ == "__main__":
     __main__()