view commons/launcher/LaunchMummerPlot.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
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
from commons.core.LoggerFactory import LoggerFactory
import os
import shutil
  
LOG_DEPTH = "repet.tools"  
  
class LaunchMummerPlot(object):

    def __init__(self, inputFileName="", queryFileName="", refFileName ="", prefix = None, fat=False, filter=False,clean=False, verbosity=0):
        self._inputFileName = inputFileName
        self._queryFileName = queryFileName
        self._refFileName = refFileName
        self._prefix = prefix
        self._fat = fat
        self._filter = filter
        self.doClean = clean
        self.verbosity = verbosity
        self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self.verbosity)
             
    def setAttributesFromCmdLine(self):
        description = "LaunchMummerPlot runs the MummerPlot program (part of the mummer package) ."    
        parser = RepetOptionParser(description = description)
        parser.add_option("-i", "--input", dest="inputFileName", default = None,  action="store", type="string", help="input file[mandatory] [format: delta]")      
        parser.add_option("-q", "--Qfile", dest="queryFileName", default = None,  action="store", type="string", help="Plot an ordered set of reference sequences from Qfile [optional] [format: fasta]")
        parser.add_option("-r", "--Rfile", dest="refFileName", default = None,  action="store", type="string", help="Plot an ordered set of reference sequences from Rfile [optional] [format: fasta]")
        parser.add_option("-p", "--prefix", dest="prefix", default = None,  action="store", type="string", help="prefix name [mandatory]")
        parser.add_option("-o","--fat", dest="fat",action="store_true", help="Layout sequences using fattest alignment only[optional] ")
        parser.add_option("-s","--filter", dest="filter",action="store_true", help="Only display .delta alignments which represent the 'best' hit [optional] ")      
        parser.add_option("-c", "--clean", dest = "clean", help = "clean temporary files", default = False, action="store_true")  
        parser.add_option("-v", "--verbosity", dest="verbosity", default = 0,  action="store", type="int", help="verbosity [optional] ")
        
        (self._options, args) = parser.parse_args()
        self._setAttributesFromOptions(self._options)

    def _setAttributesFromOptions(self, options):
        self._inputFileName = options.inputFileName
        self._queryFileName = options.queryFileName
        self._refFileName = options.refFileName
        self._prefix = options.prefix
        self._fat = options.fat
        self._filter = options.filter
        self.verbosity = options.verbosity
        
    def _logAndRaise(self, errorMsg):
        self._log.error(errorMsg)
        raise Exception(errorMsg)
            
    def checkOptions(self):  
        if self._inputFileName != "":
            if not FileUtils.isRessourceExists(self._inputFileName):
                self._logAndRaise("ERROR: Query file: %s does not exist!" % self._inputFileName)
        else:
            self._logAndRaise("ERROR: No specified --query option!")     
        
        if self._queryFileName != "":
            if not FileUtils.isRessourceExists(self._queryFileName):
                self._logAndRaise("ERROR: Query file: %s does not exist!" % self._queryFileName)
        
        if self._refFileName != "":
            if not FileUtils.isRessourceExists(self._refFileName):
                self._logAndRaise("ERROR: Ref file does not exist!"% self._refFileName)
                
    def clean(self):
        try:
            os.remove("%s.filter" % self._prefix)
        except Exception as inst: 
            self._log.error(inst)
        try:
            os.remove("%s.fplot" % self._prefix)
        except Exception as inst: 
            self._log.error(inst)
        try:
            os.remove("%s.rplot" % self._prefix)
        except Exception as inst: 
            self._log.error(inst)
                                
    def run(self):
        if not CheckerUtils.isExecutableInUserPath("mummerplot") :
            self._logAndRaise("ERROR: mummerplot must be in your path")
        self.checkOptions()
        
        ref=""
        if self._refFileName != "":
            ref = "-R %s"  % self._refFileName
           
        query=""
        if self._queryFileName != "":
            query = "-Q %s"  % self._queryFileName
           
        fat = ""
        if self._fat:
            fat =  "--fat"
            
        filter = ""
        if self._filter:
            filter = "-f" 
            
        prefix = ""
        if self._prefix is not None:
            prefix = "--prefix=%s" %(self._prefix)
            
        cmd = "mummerplot %s %s %s %s %s %s --png" % (self._inputFileName, prefix, ref, query, fat, filter)
        self._log.debug("Running mummerplot with following commands : %s" %cmd)
        cmd = cmd.split()         
        process = subprocess.Popen(cmd)
        process.wait()
        
        self.clean()
        return process.returncode
            
if __name__ == "__main__":
    iLaunchNucmer = LaunchMummerPlot()
    iLaunchNucmer.setAttributesFromCmdLine()
    iLaunchNucmer.run()