annotate commons/tools/GetSpecificTELibAccordingToAnnotation.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
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
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 import os
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 import sys
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 from commons.core.sql.DbMySql import DbMySql
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 from commons.core.utils.RepetOptionParser import RepetOptionParser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 from commons.core.utils.FileUtils import FileUtils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 from commons.core.sql.TableSeqAdaptator import TableSeqAdaptator
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 from commons.core.LoggerFactory import LoggerFactory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 LOG_DEPTH = "repet.tools"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 LOG_FORMAT = "%(message)s"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 #TODO: use configuration file
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 ## Get 3 annotation files, using output from TEannot:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 #- consensus with one or more full length copy,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 #- consensus with one or more full length fragment,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 #- consensus without copy
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 class GetSpecificTELibAccordingToAnnotation(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 def __init__(self, inInfoFileName = "", tableName = "", verbose = 0):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 self._inInfoFileName = inInfoFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 self._tableName = tableName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 self._verbose = verbose
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbose, LOG_FORMAT)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 def setAttributesFromCmdLine(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 desc = "Splits a GiveInfoTEannot \"statsPerTE.txt\" file in 3 subfiles containing consensus which have at least one copy, one full length fragment or one full length copy. "
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 desc += "A TEs library is built according to each category. Connection to the database parameters are retrieved from the environment"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 examples = "\nExample : with a project called \"MyTEannotAnalysis\":\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 examples += "\t$ python GetSpecificTELibAccordingToAnnotation.py -i MyTEannotAnalysis_chr_allTEs_nr_noSSR_join_path_statsPerTE.txt -t MyTEannotAnalysis_refTEs_seq"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 examples += "\n\t"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 examples += "\n\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 parser = RepetOptionParser(description = desc, epilog = examples)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 parser.add_option("-i", "--file", dest = "inInfoFileName", action = "store", type = "string", help = "input file (mandatory) = output file from GiveInfoTEannot.py (e.g. <project_name>_chr_allTEs_nr_noSSR_join_path_statsPerTE.txt)", default = "")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 parser.add_option("-t", "--table", dest = "tableName", action = "store", type = "string", help = "table name of TEs sequences (mandatory, seq format, e.g. <project_name>_refTEs_seq)", default = "")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 parser.add_option("-v", "--verbose", dest = "verbose", action = "store", type = "int", help = "verbosity level (default=0, else 1)", default = 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 (options, args) = parser.parse_args()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 self._setAttributesFromOptions(options)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 def _setAttributesFromOptions(self, options):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 self.setInInfoFileName(options.inInfoFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 self.setTableName(options.tableName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 self.setVerbose(options.verbose)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 def setTableName(self, tableName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 self._tableName = tableName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 def setInInfoFileName(self, inInfoFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 self._inInfoFileName = inInfoFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 def setVerbose(self, verbose):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 self._verbose = verbose
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 def checkOptions(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 if self._inInfoFileName != "":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 if not FileUtils.isRessourceExists(self._inClassifFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 self._logAndRaise("ERROR: Input GiveInfoTEannot.txt output file does not exist!")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 self._logAndRaise("ERROR: No specified -i option!")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 if self._tableName != "":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 iDb = DbMySql()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 if not iDb.doesTableExist(self._tableName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 self._logAndRaise("ERROR: table does not exist!")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 iDb.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 self._logAndRaise("ERROR: No specified -t option!")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 def _logAndRaise(self, errorMsg):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 self._log.error(errorMsg)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 raise Exception(errorMsg)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 def writeFastaFileFromGiveInfoTEAnnot(self, fileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 fFileHandler = open(fileName,"r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 lineHeader = fFileHandler.readline()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 line = fFileHandler.readline()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 lConsensusName = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 while line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 lConsensusName.append(line.split()[0])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 line = fFileHandler.readline()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 fFileHandler.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 iDb = DbMySql()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 iTSA = TableSeqAdaptator(iDb, self._tableName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 outPutFileName = "%s.fa" % os.path.splitext(fileName)[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 iTSA.saveAccessionsListInFastaFile(lConsensusName, outPutFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122 iDb.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 LoggerFactory.setLevel(self._log, self._verbose)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 outInfoFileNameFullCopy = "%s_FullLengthCopy.txt" % os.path.splitext(os.path.basename(self._inInfoFileName))[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 outInfoFileNameCopy = "%s_OneCopyAndMore.txt" % os.path.splitext(os.path.basename(self._inInfoFileName))[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 outInfoFileNameFullFrag = "%s_FullLengthFrag.txt" % os.path.splitext(os.path.basename(self._inInfoFileName))[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131 outInfoFileFullCopy = open(outInfoFileNameFullCopy, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 outInfoFileCopy = open(outInfoFileNameCopy, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133 outInfoFileFullFrag = open(outInfoFileNameFullFrag, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 self._log.info("START GetSpecificTELibAccordingToAnnotation\n input info file: %s" % self._inInfoFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 inFileFh = open(self._inInfoFileName, "r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 line = inFileFh.readline()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 lHeaders = line.split()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140 if "fullLgthCopies" not in lHeaders:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141 self._logAndRaise("ERROR: No headers in %s!" % self._inInfoFileName )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143 outInfoFileFullCopy.write(line)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 outInfoFileCopy.write(line)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 outInfoFileFullFrag.write(line)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 line = inFileFh.readline()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148 while line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149 dTokens = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150 for index, token in enumerate(line.split()):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 dTokens[lHeaders[index]] = token
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 if int(dTokens["fullLgthCopies"]) > 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154 outInfoFileFullCopy.write(line)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155 if int(dTokens["copies"]) > 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156 outInfoFileCopy.write(line)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157 if int(dTokens["fullLgthFrags"]) > 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 outInfoFileFullFrag.write(line)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159 line = inFileFh.readline()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 inFileFh.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162 outInfoFileFullCopy.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163 outInfoFileCopy.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164 outInfoFileFullFrag.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
165
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
166 self.writeFastaFileFromGiveInfoTEAnnot(outInfoFileNameFullCopy)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
167 self.writeFastaFileFromGiveInfoTEAnnot(outInfoFileNameCopy)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
168 self.writeFastaFileFromGiveInfoTEAnnot(outInfoFileNameFullFrag)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
169
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
170 self._log.info("END GetSpecificTELibAccordingToAnnotation\n" )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
171
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
172 return 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
173
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
174 if __name__ == '__main__':
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
175 iGetTELib = GetSpecificTELibAccordingToAnnotation()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
176 iGetTELib.setAttributesFromCmdLine()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
177 iGetTELib.run()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
178