comparison commons/launcher/LaunchMummerPlot.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
comparison
equal deleted inserted replaced
17:b0e8584489e6 18:94ab73e8a190
1 #! /usr/bin/env python
2
3 # Copyright INRA (Institut National de la Recherche Agronomique)
4 # http://www.inra.fr
5 # http://urgi.versailles.inra.fr
6 #
7 # This software is governed by the CeCILL license under French law and
8 # abiding by the rules of distribution of free software. You can use,
9 # modify and/ or redistribute the software under the terms of the CeCILL
10 # license as circulated by CEA, CNRS and INRIA at the following URL
11 # "http://www.cecill.info".
12 #
13 # As a counterpart to the access to the source code and rights to copy,
14 # modify and redistribute granted by the license, users are provided only
15 # with a limited warranty and the software's author, the holder of the
16 # economic rights, and the successive licensors have only limited
17 # liability.
18 #
19 # In this respect, the user's attention is drawn to the risks associated
20 # with loading, using, modifying and/or developing or reproducing the
21 # software by the user in light of its specific status of free software,
22 # that may mean that it is complicated to manipulate, and that also
23 # therefore means that it is reserved for developers and experienced
24 # professionals having in-depth computer knowledge. Users are therefore
25 # encouraged to load and test the software's suitability as regards their
26 # requirements in conditions enabling the security of their systems and/or
27 # data to be ensured and, more generally, to use and operate it in the
28 # same conditions as regards security.
29 #
30 # The fact that you are presently reading this means that you have had
31 # knowledge of the CeCILL license and that you accept its terms.
32
33 from commons.core.checker.CheckerUtils import CheckerUtils
34 from commons.core.utils.FileUtils import FileUtils
35 from commons.core.utils.RepetOptionParser import RepetOptionParser
36 import subprocess
37 from commons.core.LoggerFactory import LoggerFactory
38 import os
39 import shutil
40
41 LOG_DEPTH = "repet.tools"
42
43 class LaunchMummerPlot(object):
44
45 def __init__(self, inputFileName="", queryFileName="", refFileName ="", prefix = None, fat=False, filter=False,clean=False, verbosity=0):
46 self._inputFileName = inputFileName
47 self._queryFileName = queryFileName
48 self._refFileName = refFileName
49 self._prefix = prefix
50 self._fat = fat
51 self._filter = filter
52 self.doClean = clean
53 self.verbosity = verbosity
54 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self.verbosity)
55
56 def setAttributesFromCmdLine(self):
57 description = "LaunchMummerPlot runs the MummerPlot program (part of the mummer package) ."
58 parser = RepetOptionParser(description = description)
59 parser.add_option("-i", "--input", dest="inputFileName", default = None, action="store", type="string", help="input file[mandatory] [format: delta]")
60 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]")
61 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]")
62 parser.add_option("-p", "--prefix", dest="prefix", default = None, action="store", type="string", help="prefix name [mandatory]")
63 parser.add_option("-o","--fat", dest="fat",action="store_true", help="Layout sequences using fattest alignment only[optional] ")
64 parser.add_option("-s","--filter", dest="filter",action="store_true", help="Only display .delta alignments which represent the 'best' hit [optional] ")
65 parser.add_option("-c", "--clean", dest = "clean", help = "clean temporary files", default = False, action="store_true")
66 parser.add_option("-v", "--verbosity", dest="verbosity", default = 0, action="store", type="int", help="verbosity [optional] ")
67
68 (self._options, args) = parser.parse_args()
69 self._setAttributesFromOptions(self._options)
70
71 def _setAttributesFromOptions(self, options):
72 self._inputFileName = options.inputFileName
73 self._queryFileName = options.queryFileName
74 self._refFileName = options.refFileName
75 self._prefix = options.prefix
76 self._fat = options.fat
77 self._filter = options.filter
78 self.verbosity = options.verbosity
79
80 def _logAndRaise(self, errorMsg):
81 self._log.error(errorMsg)
82 raise Exception(errorMsg)
83
84 def checkOptions(self):
85 if self._inputFileName != "":
86 if not FileUtils.isRessourceExists(self._inputFileName):
87 self._logAndRaise("ERROR: Query file: %s does not exist!" % self._inputFileName)
88 else:
89 self._logAndRaise("ERROR: No specified --query option!")
90
91 if self._queryFileName != "":
92 if not FileUtils.isRessourceExists(self._queryFileName):
93 self._logAndRaise("ERROR: Query file: %s does not exist!" % self._queryFileName)
94
95 if self._refFileName != "":
96 if not FileUtils.isRessourceExists(self._refFileName):
97 self._logAndRaise("ERROR: Ref file does not exist!"% self._refFileName)
98
99 def clean(self):
100 try:
101 os.remove("%s.filter" % self._prefix)
102 except Exception as inst:
103 self._log.error(inst)
104 try:
105 os.remove("%s.fplot" % self._prefix)
106 except Exception as inst:
107 self._log.error(inst)
108 try:
109 os.remove("%s.rplot" % self._prefix)
110 except Exception as inst:
111 self._log.error(inst)
112
113 def run(self):
114 if not CheckerUtils.isExecutableInUserPath("mummerplot") :
115 self._logAndRaise("ERROR: mummerplot must be in your path")
116 self.checkOptions()
117
118 ref=""
119 if self._refFileName != "":
120 ref = "-R %s" % self._refFileName
121
122 query=""
123 if self._queryFileName != "":
124 query = "-Q %s" % self._queryFileName
125
126 fat = ""
127 if self._fat:
128 fat = "--fat"
129
130 filter = ""
131 if self._filter:
132 filter = "-f"
133
134 prefix = ""
135 if self._prefix is not None:
136 prefix = "--prefix=%s" %(self._prefix)
137
138 cmd = "mummerplot %s %s %s %s %s %s --png" % (self._inputFileName, prefix, ref, query, fat, filter)
139 self._log.debug("Running mummerplot with following commands : %s" %cmd)
140 cmd = cmd.split()
141 process = subprocess.Popen(cmd)
142 process.wait()
143
144 self.clean()
145 return process.returncode
146
147 if __name__ == "__main__":
148 iLaunchNucmer = LaunchMummerPlot()
149 iLaunchNucmer.setAttributesFromCmdLine()
150 iLaunchNucmer.run()