comparison commons/tools/RetrieveInitHeaders.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 optparse import OptionParser
34 from commons.core.sql.DbMySql import DbMySql
35
36 class RetrieveInitHeaders(object):
37
38 def __init__(self, inTableName = "", linkFileName = "", outTableName = "", isQueryHeaders = True, clean = False, verbose = 0):
39 self._inTableName = inTableName
40 self._linkFileName = linkFileName
41 if outTableName == "":
42 self._outTableName = self._inTableName
43 else:
44 self._outTableName = outTableName
45 self._isQueryHeaders = isQueryHeaders
46 self._clean = clean
47 self._verbose = verbose
48 self._iDb = None
49 self._tmpTableName = "%s_tmp" % self._inTableName
50
51 #TODO: can handle config file
52 #TODO: description, help...
53 def setAttributesFromCmdLine(self):
54 description = ""
55 parser = OptionParser(description = description)
56 parser.add_option("-i", "--input", dest = "inTableName", type = "string", help = "", default = "")
57 parser.add_option("-l", "--link", dest = "linkFileName", type = "string", help = "", default = "")
58 parser.add_option("-o", "--output", dest = "outTableName", type = "string", help = "(default = input table name)", default = "")
59 parser.add_option("-s", "--subject", dest = "isQueryHeaders", action = "store_false", help = "change subject name and not query name", default = True)
60 parser.add_option("-c", "--clean", dest = "clean", action = "store_true", help = "drop input table", default = False)
61 parser.add_option("-v", "--verbose", dest = "verbose", type = "int", help = "0 or 1", default = 0)
62 options, args = parser.parse_args()
63 self.setAttributesFromOptions(options)
64
65 def setAttributesFromOptions(self, options):
66 self.setInTableName(options.inTableName)
67 self.setLinkFileName(options.linkFileName)
68 self.setOutTableName(options.outTableName)
69 self.setIsQueryHeaders(options.isQueryHeaders)
70 self.setClean(options.clean)
71 self.setVerbose(options.verbose)
72
73 def setInTableName(self, inTableName):
74 self._inTableName = inTableName
75 self._tmpTableName = "%s_tmp" % self._inTableName
76
77 def setLinkFileName(self, linkFileName):
78 self._linkFileName = linkFileName
79
80 def setOutTableName(self, outTableName):
81 if outTableName == "":
82 self._outTableName = self._inTableName
83 else:
84 self._outTableName = outTableName
85
86 def setIsQueryHeaders(self, isQueryHeaders):
87 self._isQueryHeaders = isQueryHeaders
88
89 def setClean(self, clean):
90 self._clean = clean
91
92 def setVerbose(self, verbose):
93 self._verbose = verbose
94
95 #TODO: checkOptions
96 def checkOptions(self):
97 pass
98
99 def run(self):
100 if self._verbose > 0:
101 print "START RetrieveInitHeaders.py"
102 self.checkOptions()
103
104 if self._verbose > 0:
105 print "copy '%s' table to '%s' table" % (self._inTableName, self._tmpTableName)
106 self._iDb = DbMySql()
107 self._iDb.copyTable(self._inTableName, self._tmpTableName)
108
109 if self._verbose > 0:
110 print "read '%s' file" % self._linkFileName
111 f = open(self._linkFileName)
112 line = f.readline()
113 count = 0
114 while line:
115 oldHeader = line.split()[0]
116 newHeader = line.split()[1]
117 if self._isQueryHeaders:
118 self._updateQueryName(oldHeader, newHeader)
119 else:
120 self._updateSubjectName(oldHeader, newHeader)
121 count += 1
122 line = f.readline()
123 f.close()
124
125 if self._verbose > 0:
126 print "nb of relationships: %i" % count
127 if self._clean:
128 self._iDb.dropTable(self._inTableName)
129 if self._verbose > 0:
130 print "drop '%s' table" % self._inTableName
131 if self._verbose > 0:
132 print "rename '%s' table to '%s' table" % (self._tmpTableName, self._outTableName)
133 self._iDb.renameTable(self._tmpTableName, self._outTableName)
134 self._iDb.close()
135 if self._verbose > 0:
136 print "END RetrieveInitHeaders.py"
137
138 #TODO: methods must be in TablePathAdaptator ?
139 def _updateQueryName(self, oldH, newH):
140 sqlCmd = "UPDATE %s SET query_name = '%s' WHERE query_name = '%s'" % (self._tmpTableName, newH, oldH)
141 self._iDb.execute(sqlCmd)
142
143 def _updateSubjectName(self, oldH, newH):
144 sqlCmd = "UPDATE %s SET subject_name = '%s' WHERE subject_name = '%s'" % (self._tmpTableName, newH, oldH)
145 self._iDb.execute(sqlCmd)
146
147 if __name__ == "__main__":
148 iRIH = RetrieveInitHeaders()
149 iRIH.setAttributesFromCmdLine()
150 iRIH.run()