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