view velvet_optimiser.py @ 0:50ae1360fbbe default tip

Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author konradpaszkiewicz
date Tue, 07 Jun 2011 18:07:56 -0400
parents
children
line wrap: on
line source

#!/usr/bin/env python

"""
VelvetOptimiser Wrapper
Adapted from velveth and velvetg tools in Galaxy
Konrad Paszkiewicz	University of Exeter, UK.

"""
import pkg_resources;
import logging, os, string, sys, tempfile, glob, shutil, types, urllib
import shlex, subprocess
from optparse import OptionParser, OptionGroup
from stat import *


log = logging.getLogger( __name__ )

assert sys.version_info[:2] >= ( 2, 4 )

def stop_err( msg ):
    sys.stderr.write( "%s\n" % msg )
    sys.exit()

def __main__():
    #Parse Command Line
    s = 'velvetg_optimiser.py:  argv = %s\n' % (sys.argv)
    #print >> sys.stderr, s # so will appear as blurb for file
    argcnt = len(sys.argv)
    starthash = sys.argv[1]
    endhash = sys.argv[2]
    inputs = sys.argv[3]
    threads = sys.argv[4]
    afgFile = sys.argv[5]
    kmeropt = sys.argv[6]
    covopt = sys.argv[7]
    contigs = sys.argv[8]
    LastGraph = sys.argv[9]
    velvet_asm = sys.argv[10]
    unusedReadsFile = sys.argv[11]
    stats = sys.argv[12]
    othervelvetgoptions = sys.argv[13]
    working_dir = ''
    
    cmdline = '/usr/local/velvet/contrib/VelvetOptimiser-2.1.7/VelvetOptimiser.pl -s %s -e %s -f \' %s \' -t %s -a 1 -k %s -c %s -o \'-unused_reads yes %s\' 2&1>/dev/null' % (starthash, endhash, inputs, threads, kmeropt, covopt, othervelvetgoptions)
    #print >> sys.stderr, cmdline # so will appear as blurb for file
    try:
        proc = subprocess.Popen( args=cmdline, shell=True, stderr=subprocess.PIPE )
        returncode = proc.wait()
        # get stderr, allowing for case where it's very large
        stderr = ''
        buffsize = 1048576
        try:
            while True:
                stderr += proc.stderr.read( buffsize )
                if not stderr or len( stderr ) % buffsize != 0:
                    break
        except OverflowError:
            pass
        if returncode != 0:
            raise Exception, stderr
    except Exception, e:
        stop_err( 'Error running velvet_optimiser.py' + str( e ) )
    
    out = open(contigs,'w')
    contigs_path = os.path.join(working_dir,'contigs.fa')
    #print >> sys.stderr, contigs_path
    for line in open(contigs_path ):
        out.write( "%s" % (line) )
    out.close()
    out = open(stats,'w')
    stats_path = os.path.join(working_dir,'stats.txt')
    for line in open( stats_path ):
        out.write( "%s" % (line) )
    out.close()
    if LastGraph != 'None':
        out = open(LastGraph,'w')
        LastGraph_path = os.path.join(working_dir,'LastGraph')
        for line in open( LastGraph_path ):
            out.write( "%s" % (line) )
        out.close()
    if afgFile != 'None':
        out = open(afgFile,'w')
        afgFile_path = os.path.join(working_dir,'velvet_asm.afg')
        try:
            for line in open( afgFile_path ):
                out.write( "%s" % (line) )
        except:
            logging.warn( 'error reading %s' %(afgFile_path))
            pass
        out.close()
    if unusedReadsFile != 'None':
        out = open(unusedReadsFile,'w')
        unusedReadsFile_path = os.path.join(working_dir,'UnusedReads.fa')
        try:
            for line in open( unusedReadsFile_path ):
                out.write( "%s" % (line) )
        except:
            logging.info( 'error reading %s' %(unusedReadsFile_path))
            pass
        out.close()

if __name__ == "__main__": __main__()