annotate commons/core/sql/TableMapAdaptator.py @ 14:c79b9ae3f65f

Deleted selected files
author m-zytnicki
date Fri, 19 Apr 2013 10:13:11 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.sql.TableAdaptator import TableAdaptator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.sql.ITableMapAdaptator import ITableMapAdaptator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.coord.Map import Map
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.coord.MapUtils import MapUtils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 ## Adaptator for Map table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 class TableMapAdaptator( TableAdaptator, ITableMapAdaptator ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 ## Give a list of Map instances having a given seq name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 # @param seqName string seq name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 # @return lMap list of instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 def getListFromSeqName( self, seqName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 sqlCmd = "SELECT * FROM %s" % (self._table)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 colum2Get, type2Get, attr2Get = self._getTypeColumAttr2Get(seqName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 sqlCmd += " WHERE " + colum2Get
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 sqlCmd += " = "
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 sqlCmd = sqlCmd + type2Get
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 sqlCmd = sqlCmd % "'" + attr2Get + "'"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 return self._iDb.getObjectListWithSQLCmd( sqlCmd, self._getInstanceToAdapt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 ## Give a list of Map instances overlapping a given region
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 # @param query string query name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 # @param start integer start coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 # @param end integer end coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 # @return list map instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 def getListOverlappingCoord(self, query, start, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 sqlCmd = 'select * from %s where chr="%s" and ((start between least(%d,%d) and greatest(%d,%d) or end between least(%d,%d) and greatest(%d,%d)) or (least(start,end)<=least(%d,%d) and greatest(start,end)>=greatest(%d,%d))) ;' % (self._table, query, start, end, start, end, start, end, start, end, start, end, start, end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 return self._iDb.getObjectListWithSQLCmd( sqlCmd, self._getInstanceToAdapt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 ## Give a list of Map instances having a given sequence name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 # @param seqName string sequence name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 # @return lMap list of instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 def getMapListFromSeqName(self, seqName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 lMap = self.getListFromSeqName( seqName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 return lMap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 #TODO: Check getListFromSeqName method: uses name instead of seqname
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 # ## Give a list of Map instances having a given sequence name from list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 # #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 # # @param lSeqName string sequence name list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 # # @return lMap list of instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 # #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 # def getMapListFromSeqNameList(self, lSeqName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 # lMap = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 # [lMap.extend(self.getListFromSeqName(seqName)) for seqName in lSeqName]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 # return lMap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 ## Give a list of Map instances having a given chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 # @param chr string chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 # @return lMap list of instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 def getMapListFromChr(self, chr):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 sqlCmd = "SELECT * FROM %s WHERE chr='%s'" % (self._table, chr)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 lMap = self._iDb.getObjectListWithSQLCmd( sqlCmd, self._getInstanceToAdapt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 return lMap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 ## Give a list of the distinct seqName/chr present in the table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 # @return lDistinctContigNames string list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 def getSeqNameList(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 sqlCmd = "SELECT DISTINCT chr FROM %s" % ( self._table )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 lDistinctContigNames = self._iDb.getStringListWithSQLCmd(sqlCmd)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 return lDistinctContigNames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 ## Return a list of Set instances from a given sequence name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 # @param seqName string sequence name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 # @return lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 def getSetListFromSeqName( self, seqName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 lMaps = self.getListFromSeqName( seqName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 lSets = MapUtils.mapList2SetList( lMaps )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 return lSets
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 ## Give a map instances list overlapping a given region
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 # @param seqName string seq name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 # @param start integer start coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 # @param end integer end coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 # @return lMap list of map instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 def getMapListOverlappingCoord(self, seqName, start, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 lMap = self.getListOverlappingCoord(seqName, start, end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 return lMap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 ## Return a list of Set instances overlapping a given sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 # @param seqName string sequence name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 # @param start integer start coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 # @param end integer end coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 # @return lSet list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 def getSetListOverlappingCoord( self, seqName, start, end ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 lMaps = self.getListOverlappingCoord( seqName, start, end )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 lSets = MapUtils.mapList2SetList( lMaps )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 return lSets
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 ## Give a dictionary which keys are Map names and values the corresponding Map instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 # @return dName2Maps dict which keys are Map names and values the corresponding Map instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 def getDictPerName( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 dName2Maps = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 lMaps = self.getListOfAllMaps()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 for iMap in lMaps:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 if dName2Maps.has_key( iMap.name ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 if iMap == dName2Maps[ iMap.name ]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 msg = "ERROR: in table '%s' two different Map instances have the same name '%s'" % ( self._table, iMap.name )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 sys.stderr.write( "%s\n" % ( msg ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 dName2Maps[ iMap.name ] = iMap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 return dName2Maps
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 ## Return a list of Map instances with all the data contained in the table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 # @return lMaps list of Map instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 def getListOfAllMaps( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 sqlCmd = "SELECT * FROM %s" % ( self._table )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 lMaps = self._iDb.getObjectListWithSQLCmd( sqlCmd, self._getInstanceToAdapt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 return lMaps
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 ## Give the end of map as integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 # @return end integer the end of map
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 def getEndFromSeqName(self, seqName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 sqlCmd = "SELECT end FROM %s WHERE chr = '%s'" % (self._table, seqName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 end = self._iDb.getIntegerWithSQLCmd(sqlCmd)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 return end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 def _getInstanceToAdapt(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 iMap = Map()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 return iMap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 def _getTypeColumAttr2Get(self, name):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 colum2Get = 'name'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 type2Get = '%s'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 attr2Get = name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 return colum2Get, type2Get, attr2Get
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 def _getTypeAndAttr2Insert(self, map):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 type2Insert = ("'%s'","'%s'","'%d'","'%d'")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 attr2Insert = (map.name, map.seqname, map.start, map.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 return type2Insert, attr2Insert
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 def _escapeAntislash(self, obj):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 obj.name = obj.name.replace("\\", "\\\\")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 obj.seqname = obj.seqname.replace("\\", "\\\\")