1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#!/usr/bin/env python
# Created: Sun Oct 15 16:16:20 2000
# Last changed: Time-stamp: <01/02/15 09:01:27 thomas>
# thomas@cbs.dtu.dk, http://www.cbs.dtu.dk/thomas
# File: getgene.py
"""Example indexing plain text SwissProt files.
Example code to index a non-reduntant protein database of
SwissProt + TrEMBL for fast lookup and retrieval.
To build the database and index it::
cd /opt/bio/data/
wget -N -nd -r -l1 -A'.dat.Z' ftp://expasy.cbr.nrc.ca/databases/sp_tr_nrdb/
zcat *.dat.Z > nr.dat
./getgene.py --index nr.dat
setenv PYPHY '/opt/bio/data'
To retrieve entries from the command line::
./getgene.py EFTU_ECOLI
To use from a python script::
from getgene import DB_Index
db_index = DB_Index()
# retrieve a complete entry:
db_index.Get('EFTU_ECOLI')
# get organism, lineage and gene
db_index.Get_OS_OC_GN('EFTU_ECOLI')
"""
from __future__ import print_function
import os
import re
import string
import sys
try:
import gdbm # Python 2
except ImportError:
from dbm import gnu as gdbm # Python 3
class DB_Index(object):
def __init__(self, open=1):
if open:
self.Open()
def Create(self, infile, outfile):
db = gdbm.open(outfile, 'n')
with open(infile) as fid:
db['datafile'] = os.path.abspath(infile)
while True:
line = fid.readline()
if not line or not len(line):
break
if line[:3] == 'ID ':
id = string.split(line)[1]
start = fid.tell() - len(line)
elif line[:3] == 'AC ':
acc = string.split(line)[1]
if acc[-1] == ';':
acc = acc[:-1]
elif line[:2] == '//':
stop = fid.tell()
try:
value = '%d %d' % (start, stop)
db[id] = value
db[acc] = value
id, acc, start, stop = None, None, None, None
except:
print("AARRGGGG %d %d %s %s" %
(start, stop, type(start), type(stop)))
print("%s %s" % (id, acc))
db.close()
def Open(self, indexfile=None):
if not indexfile:
indexfile = os.path.join(os.environ['PYPHY'], 'nr.dat.indexed')
self.db = gdbm.open(indexfile)
self.datafile = self.db['datafile']
self.fid = open(self.datafile)
def Close(self):
self.db.close()
def Get(self, id):
try:
values = self.db[id]
except:
return None
start, stop = [int(x) for x in values.split()]
self.fid.seek(start)
txt = self.fid.read(stop - start)
return txt
def Get_Organism(self, id):
entry = self.Get(id)
if not entry:
return None
for line in string.split(entry, '\n'):
if line[0:5] == 'OS ':
OS = string.strip(line[5:])
if OS[-1] == ".":
OS = OS[0:-1]
return OS
if line[0:2] == "//":
break
return None
def FixOS(self, os):
os = string.split(os, ',')[0]
os = string.split(os, '(')[0]
return string.strip(os)
def Get_Taxonomy(self, id):
entry = self.Get(id)
if not entry:
return None
OC = ""
for line in string.split(entry, '\n'):
if line[0:5] == 'OC ':
OC = OC + string.strip(line[5:])
if OC[-1] == ".":
OC = OC[0:-1]
if line[0:2] == "//":
break
return OC
def Get_Kingdom(self, id):
res = self.Get_Taxonomy(id)
# print("%s %s" % (id, res))
if not res:
return "U"
kd = string.strip(string.split(res, ";")[0])
if kd == "Eubacteria" or kd == "Prokaryota" or kd == "Bacteria":
return "B"
elif kd == "Eukaryota" or kd == "Eukaryotae":
return "E"
elif kd == "Archaebacteria" or kd == "Archaea":
return "A"
elif kd == "Viridae" or kd == "Viruses":
return "V"
else:
print("%s UNKNOWN" % kd)
return "U"
def Get_Gene(self, id):
entry = self.Get(id)
if not entry:
return None
GN = ''
for line in string.split(entry, '\n'):
if line[0:5] == 'GN ':
GN = string.strip(line[5:])
if GN[-1] == ".":
GN = GN[0:-1]
return GN
if line[0:2] == "//":
break
return GN
def Get_OS_OC_GN(self, id):
entry = self.Get(id)
if not entry:
return None, None, None
OS, OC, GN = "", "", ""
for line in string.split(entry, '\n'):
if line[0:5] == 'OS ':
OS = string.strip(line[5:])
if OS[-1] == ".":
OS = OS[0:-1]
if line[0:5] == 'OC ':
OC = OC + string.strip(line[5:])
if OC[-1] == ".":
OC = OC[0:-1]
if line[0:5] == 'GN ':
GN = string.strip(line[5:])
if GN[-1] == ".":
GN = GN[0:-1]
if line[0:2] == "//":
break
return OS, OC, GN
def Get_OS_OC_OG(self, id):
entry = self.Get(id)
if not entry:
return None, None, None
OS, OC, OG = "", "", ""
for line in string.split(entry, '\n'):
if line[0:5] == 'OS ':
OS = string.strip(line[5:])
if OS[-1] == ".":
OS = OS[0:-1]
if line[0:5] == 'OC ':
OC = OC + string.strip(line[5:])
if OC[-1] == ".":
OC = OC[0:-1]
if line[0:5] == 'OG ':
OG = string.strip(line[5:])
if OG[-1] == ".":
OG = OG[0:-1]
if line[0:2] == "//":
break
return OS, OC, OG
def Get_SQ(self, id, fasta=1):
entry = self.Get(id)
if not entry:
return ""
SQ = ""
record = 0
for line in string.split(entry, '\n'):
if record:
SQ = SQ + string.strip(line[5:])
if line[0:5] == 'SQ ':
record = 1
if line[0:2] == "//":
break
SQ = re.sub('[ \n]', '', SQ)
if fasta:
SQ = '>%s\n%s' % (id, re.sub('(.{60})', '\\1\n', SQ))
return SQ
def Get_XX(self, id, xx):
entry = self.Get(id)
if not entry:
return ""
XX = ""
for line in string.split(entry, '\n'):
if line[0:5] == '%s ' % xx:
XX = XX + string.strip(line[5:])
if XX[-1] == ".":
XX = XX[0:-1]
if line[0:2] == "//":
break
return XX
def Get_Keywords(self, id):
entry = self.Get(id)
if not entry:
return []
keywords = []
for line in string.split(entry, '\n'):
if line[0:5] == 'KW ':
for i in string.split(string.strip(line[5:]), ';'):
kw = string.strip(i)
if len(kw) < 2:
continue
if kw[-1] == '.':
kw = kw[:-1]
keywords.append(kw)
if line[0:2] == "//":
break
return keywords
def usage(exit=0):
name = os.path.basename(sys.argv[0])
print('Usage: %s <db> <gene ID>' % name)
print(' or %s --index <db.dat>' % name)
if exit:
sys.exit(0)
if __name__ == '__main__':
pyphy_home = os.environ.get('PYPHY')
if len(sys.argv) == 1:
usage(exit=1)
db_index = DB_Index(open=0)
func = db_index.Get
for arg in sys.argv[1:]:
if arg == '--index':
sys.argv.remove(arg)
infile = sys.argv[1]
outfile = os.path.basename(infile) + '.indexed'
db_index.Create(infile, outfile)
sys.exit(0)
elif arg[:4] == '-Get':
sys.argv.remove(arg)
func = getattr(db_index, arg[1:])
elif arg == '-h' or arg == '--help':
usage(exit=1)
db = 'nr.dat'
if len(sys.argv) == 2:
# shortcut, mostly we use nr.dat so dont bother to name it
ids = sys.argv[1:]
else:
try:
db = sys.argv[1]
ids = sys.argv[2:]
except:
usage(exit=1)
dbfile = os.path.join(pyphy_home, db + '.indexed')
db_index.Open(dbfile)
for id in ids:
# print(db_index.Get(id))
print(func(id))
|