view commons/launcher/LaunchRepeatMasker.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.

from commons.core.checker.CheckerUtils import CheckerUtils
from commons.core.utils.FileUtils import FileUtils
from commons.core.utils.RepetOptionParser import RepetOptionParser
import subprocess
  
class LaunchRepeatMasker(object):

    def __init__(self,queryFileName="", libFileName ="",sensitivity="", engine="wu", cutOff=225, outputDir = ".",verbosity=0):
        self._queryFileName = queryFileName
        self._libFileName = libFileName
        self._engine = engine
        self._sensitivity = sensitivity
        self._cutOff = cutOff
        self._outputDir = outputDir
        self._verbosity = verbosity
         
    def setAttributesFromCmdLine(self):
        description = "LaunchRepeatMasker runs the RepeatMasker 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("-l", "--libFileName", dest="libFileName", default = None,  action="store", type="string", help="custom library [optional]")  
        parser.add_option("-n", "--outputDir", dest="outputDir", default=".", action="store", type="string", help="outputDir (default : current directory) [optional] ")  
        parser.add_option("-c", "--cutOff", dest="cutOff", default=225, action="store", type="int", help="Sets cutoff score for masking repeats when using -lib (default 225) [optional] ")
        parser.add_option("-e", "--engine", dest="engine", default = "wu",  action="store", type="string", help="engine  [optional] ")    
        parser.add_option("-u", "--sensitivity", dest="sensitivity", default = "",  action="store", type="string", help="sensitivity  can be s, q, qq[optional] ")    
        parser.add_option("-v", "--verbosity", dest="verbosity", default = 0,  action="store", type="int", help="verbosity [optional] ")    

        (options, args) = parser.parse_args()
        self._setAttributesFromOptions(options)

    def _setAttributesFromOptions(self, options):
        self._queryFileName = options.queryFileName
        self._libFileName = options.libFileName
        self._outputDir = options.outputDir
        self._engine = options.engine
        self._sensitivity = options.sensitivity
        self._cutOff = options.cutOff
        self._verbosity = options.verbosity
        
    def checkOptions(self):       
        if self._queryFileName != "":
            if not FileUtils.isRessourceExists(self._queryFileName):
                raise Exception("ERROR: Query file: %s does not exist!" % self._queryFileName)
        else:
            raise Exception("ERROR: No specified --query option!")
                                
    def run(self):
        if not CheckerUtils.isExecutableInUserPath("RepeatMasker") :
            print "ERROR: RepeatMasker must be in your path"
        else:
            self.checkOptions()
            
            engine = ""
            if self._engine == "wu":
                engine = "-e wublast"
            elif self._engine == "cm":
                engine = "-e crossmatch"
            sensitivity = ""
            if self._sensitivity:
                sensitivity = "-%s" % self._sensitivity     
            libFileName = ""
            if self._libFileName != "":
                libFileName = "-lib %s" % self._libFileName

            cmd = "RepeatMasker %s -dir %s -pa 1 -gccalc -no_is -nolow %s %s %s" % (self._queryFileName,self._outputDir,engine,sensitivity,libFileName)
            cmd = cmd.split() 
            
            if self._verbosity>0:
                print("Running RepeatMasker with following commands : %s" %cmd)
            
            process = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr = subprocess.PIPE)
#            process.wait()
            output= process.communicate()
            if self._verbosity>0:
                print("".join(output))
            return process.returncode
            
if __name__ == "__main__":
    iLaunchRepeatMasker = LaunchRepeatMasker()
    iLaunchRepeatMasker.setAttributesFromCmdLine()
    iLaunchRepeatMasker.run()