view commons/launcher/LaunchLastZ.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
line wrap: on
line source

#! /usr/bin/env python

# Copyright INRA (Institut National de la Recherche Agronomique)
# http://www.inra.fr
# http://urgi.versailles.inra.fr
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software.  You can  use, 
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info". 
#
# As a counterpart to the access to the source code and  rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty  and the software's author,  the holder of the
# economic rights,  and the successive licensors  have only  limited
# liability. 
#
# In this respect, the user's attention is drawn to the risks associated
# with loading,  using,  modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean  that it is complicated to manipulate,  and  that  also
# therefore means  that it is reserved for developers  and  experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or 
# data to be ensured and,  more generally, to use and operate it in the 
# same conditions as regards security. 
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.

import sys
import os
import optparse
from commons.core.checker.CheckerUtils import CheckerUtils
from commons.core.utils.FileUtils import FileUtils
from subprocess import Popen
from commons.core.utils.RepetOptionParser import RepetOptionParser
import subprocess
import tempfile
  
class LaunchLastZ(object):

    def __init__(self,queryFileName="", refFileName ="", outputFileName=None,outputFileFormat="axt",noTransition=True, ambiguous=None, step=1,gfextend=False, chain=False, verbosity=1 ):
        self.queryFileName = queryFileName
        self.refFileName = refFileName
        self.outputFileName = outputFileName
        self.outputFileFormat = outputFileFormat
        self.noTransition = noTransition
        self.step = step
        self.ambiguous = ambiguous
        self.gfextend = gfextend
        self.chain = chain
        self.verbosity = verbosity
        
    def setAttributesFromCmdLine(self):
        description = "LaunchLastZ runs the LastZ program ."    
        parser = RepetOptionParser(description = description)
        parser.add_option("-q", "--query", dest="queryFileName", default = None,  action="store", type="string", help="input query file [compulsory] [format: fasta]")
        parser.add_option("-r", "--ref", dest="refFileName", default = None,  action="store", type="string", help="input ref file [compulsory] [format: fasta]")
        parser.add_option("-o", "--output", dest="outputFileName", default = None,  action="store", type="string", help="output file [compulsory] ")
        parser.add_option("-f", "--format", dest="outputFileFormat", default = "axt",  action="store", type="string", help="output file format[optional] ")
        parser.add_option("-n", "--notransition", dest="noTransition", action="store_false", default=True, help="noTransition (default True) [optional] ")
        parser.add_option("-a", "--ambiguous", dest="ambiguous", action="store",  type="string", help="ambiguous [optional] ")
        parser.add_option("-s", "--step", dest="step", default = 1,  action="store", type="int", help="stepsize (default 1) [optional] ")
        parser.add_option("-g", "--gfextend", dest="gfextend",  action="store_true",  help="extend gf (default false)[optional] ")
        parser.add_option("-c", "--chain", dest="chain",  action="store_true", help="chain (default false)[optional] ")
        parser.add_option("-v", "--verbosity", dest="verbosity", default = 1,  action="store", type="int", help="verbosity [optional] ")
        (self._options, args) = parser.parse_args()
        self._setAttributesFromOptions(self._options)

    def _setAttributesFromOptions(self, options):
        self.queryFileName = options.queryFileName
        self.refFileName = options.refFileName
        self.outputFileName = options.outputFileName
        self.outputFileFormat = options.outputFileFormat
	self.ambiguous = options.ambiguous
        self.noTransition = options.noTransition
        self.step = options.step
        self.gfextend = options.gfextend
        self.chain = options.chain
        self.verbosity = options.verbosity
      
    def checkOptions(self):       
        if self.queryFileName != "":
            if not FileUtils.isRessourceExists(self.queryFileName):
                raise Exception("ERROR: Query file does not exist!")
        else:
            raise Exception("ERROR: No specified --query option!")
        if self.refFileName != "":
            if not FileUtils.isRessourceExists(self.refFileName):
                raise Exception("ERROR: Ref file does not exist!")
        else:
            raise Exception("ERROR: No specified --ref option!")
        if self.outputFileName == None:
            self.outputFileName = "%s_%s.axt" % (os.path.basename(self.queryFileName), os.path.basename(self.refFileName))
                                
    def run(self):
        if not CheckerUtils.isExecutableInUserPath("lastz") :
            print ("ERROR: LastZ must be in your path")
        else:
            self.checkOptions()
            
            transition = "" 
            if self.noTransition:
                transition = "--notransition"
            ambiguous = ""    
            if self.ambiguous is not None:
                ambiguous =  "--ambiguous=%s" % self.ambiguous
                
            gfextend = ""
            if self.gfextend:
                gfextend = "--gfextend"
                
            chain = ""
            if self.chain:
                chain = "--chain"
                
            cmd = "lastz %s[format=fasta] %s[format=fasta] --output=%s --format=%s %s %s --step=%i %s %s" % (self.refFileName, self.queryFileName, self.outputFileName
                                                                           , self.outputFileFormat, ambiguous, transition,self.step, gfextend, chain)
            if self.verbosity>0:
                print("Running LastZ with following commands : %s" %cmd)
            sys.stdout.flush()
            cmd = cmd.split()
            process = subprocess.Popen(cmd)
            process.wait()
            return process.returncode

if __name__ == "__main__":
    iLaunchLastZ = LaunchLastZ()
    iLaunchLastZ.setAttributesFromCmdLine()
    iLaunchLastZ.run()