annotate commons/launcher/LaunchMummerPlot.py @ 19:9bcfa7936eec

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