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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
Author: Andreas Tille <tille@debian.org>
Last-Update: Wed, 24 Jun 2015 19:24:02 +0200
Description: 2to3 conversion to Python3
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
import struct
import sys
@@ -78,7 +78,7 @@ class Filter(object):
file = self.__universalOpen(file)
(recordCount,) = struct.unpack('> I',file.read(4))
- for i in xrange(recordCount):
+ for i in range(recordCount):
(recordSize,)=struct.unpack('>I',file.read(4))
record = file.read(recordSize)
yield record
@@ -236,12 +236,12 @@ class ColumnFile(object):
elif hasattr(stream,'next'):
self._stream = stream
else:
- raise ValueError,'stream must be string or an iterator'
+ raise ValueError('stream must be string or an iterator')
self._delimiter=sep
self._strip=strip
if types:
self._types=[x for x in types]
- for i in xrange(len(self._types)):
+ for i in range(len(self._types)):
if self._types[i] is bool:
self._types[i]=ColumnFile.str2bool
else:
@@ -257,16 +257,16 @@ class ColumnFile(object):
def __iter__(self):
return self
- def next(self):
- ligne = self._stream.next()
+ def __next__(self):
+ ligne = next(self._stream)
while ligne[0] == self._skip:
- ligne = self._stream.next()
+ ligne = next(self._stream)
data = ligne.split(self._delimiter)
if self._strip or self._types:
data = [x.strip() for x in data]
if self._types:
it = self.endLessIterator(self._types)
- data = [x[1](x[0]) for x in ((y,it.next()) for y in data)]
+ data = [x[1](x[0]) for x in ((y,next(it)) for y in data)]
return data
def endLessIterator(self,endedlist):
@@ -286,18 +286,19 @@ class Table(list):
def printTable(self):
for h in self.headers:
- print "\t%s\t|" % h,
- print "\n"
+ print("\t%s\t|" % h, end=' ')
+ print("\n")
for l in self.lines:
for c in l:
- print "\t%s\t|" % c
- print "\n"
+ print("\t%s\t|" % c)
+ print("\n")
def getColumn(self,n):
- print "\t%s\n" % self.header[n]
+ print("\t%s\n" % self.header[n])
for i in range(len(self.lines)):
- print "\t%s\n" % i[n]
+ print("\t%s\n" % i[n])
+
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.7
+#!/usr/bin/python3
import re
import gzip
@@ -80,12 +80,12 @@ class ColumnFile(object):
elif hasattr(stream,'next'):
self._stream = stream
else:
- raise ValueError,'stream must be string or an iterator'
+ raise ValueError('stream must be string or an iterator')
self._delimiter=sep
self._strip=strip
if types:
self._types=[x for x in types]
- for i in xrange(len(self._types)):
+ for i in range(len(self._types)):
if self._types[i] is bool:
self._types[i]=ColumnFile.str2bool
else:
@@ -100,14 +100,14 @@ class ColumnFile(object):
def __iter__(self):
return self
- def next(self):
- ligne = self._stream.next()
+ def __next__(self):
+ ligne = next(self._stream)
data = ligne.split(self._delimiter)
if self._strip or self._types:
data = [x.strip() for x in data]
if self._types:
it = endLessIterator(self._types)
- data = [x[1](x[0]) for x in ((y,it.next()) for y in data)]
+ data = [x[1](x[0]) for x in ((y,next(it)) for y in data)]
return data
def taxonCmp(t1,t2):
@@ -149,22 +149,22 @@ def readNodeTable(file):
str,str,bool,
int,bool,int,
bool,bool,bool,str))
- print >>sys.stderr,"Reading taxonomy dump file..."
+ print("Reading taxonomy dump file...", file=sys.stderr)
taxonomy=[[n[0],n[2],n[1]] for n in nodes]
- print >>sys.stderr,"List all taxonomy rank..."
+ print("List all taxonomy rank...", file=sys.stderr)
ranks =list(set(x[1] for x in taxonomy))
ranks.sort()
- ranks = dict(map(None,ranks,xrange(len(ranks))))
+ ranks = dict(map(None,ranks,range(len(ranks))))
- print >>sys.stderr,"Sorting taxons..."
+ print("Sorting taxons...", file=sys.stderr)
taxonomy.sort(taxonCmp)
- print >>sys.stderr,"Indexing taxonomy..."
+ print("Indexing taxonomy...", file=sys.stderr)
index = {}
for t in taxonomy:
index[t[0]]=bsearchTaxon(taxonomy, t[0])
- print >>sys.stderr,"Indexing parent and rank..."
+ print("Indexing parent and rank...", file=sys.stderr)
for t in taxonomy:
t[1]=ranks[t[1]]
t[2]=index[t[2]]
@@ -200,7 +200,7 @@ def deletedNodeIterator(file):
def readTaxonomyDump(taxdir):
taxonomy,ranks,index = readNodeTable('%s/nodes.dmp' % taxdir)
- print >>sys.stderr,"Adding scientific name..."
+ print("Adding scientific name...", file=sys.stderr)
alternativeName=[]
for taxid,name,classname in nameIterator('%s/names.dmp' % taxdir):
@@ -208,11 +208,11 @@ def readTaxonomyDump(taxdir):
if classname == 'scientific name':
taxonomy[index[taxid]].append(name)
- print >>sys.stderr,"Adding taxid alias..."
+ print("Adding taxid alias...", file=sys.stderr)
for taxid,current in mergedNodeIterator('%s/merged.dmp' % taxdir):
index[taxid]=index[current]
- print >>sys.stderr,"Adding deleted taxid..."
+ print("Adding deleted taxid...", file=sys.stderr)
for taxid in deletedNodeIterator('%s/delnodes.dmp' % taxdir):
index[taxid]=None
@@ -453,11 +453,11 @@ def ecoSeqWriter(file,input,taxindex,par
skipped.append(entry['id'])
where = universalTell(input)
progressBar(where, inputsize)
- print >>sys.stderr," Readed sequences : %d " % seqcount,
+ print(" Readed sequences : %d " % seqcount, end=' ', file=sys.stderr)
else:
skipped.append(entry['id'])
- print >>sys.stderr
+ print(file=sys.stderr)
output.seek(0,0)
output.write(struct.pack('> I',seqcount))
@@ -478,7 +478,7 @@ def ecoRankWriter(file,ranks):
output = open(file,'wb')
output.write(struct.pack('> I',len(ranks)))
- rankNames = ranks.keys()
+ rankNames = list(ranks.keys())
rankNames.sort()
for rank in rankNames:
@@ -521,8 +521,8 @@ def ecoDBWriter(prefix,taxonomy,seqFileN
taxonomy[3],
parser)
if sk:
- print >>sys.stderr,"Skipped entry :"
- print >>sys.stderr,sk
+ print("Skipped entry :", file=sys.stderr)
+ print(sk, file=sys.stderr)
def ecoParseOptions(arguments):
opt = {
@@ -562,25 +562,25 @@ def ecoParseOptions(arguments):
opt['parser']=sequenceIteratorFactory(emblEntryParser,
entryIterator)
else:
- raise ValueError,'Unknown option %s' % name
+ raise ValueError('Unknown option %s' % name)
return opt,filenames
def printHelp():
- print "-----------------------------------"
- print " ecoPCRFormat.py"
- print "-----------------------------------"
- print "ecoPCRFormat.py [option] <argument>"
- print "-----------------------------------"
- print "-e --embl :[E]mbl format"
- print "-f --fasta :[F]asta format"
- print "-g --genbank :[G]enbank format"
- print "-h --help :[H]elp - print this help"
- print "-n --name :[N]ame of the new database created"
- print "-t --taxonomy :[T]axonomy - path to the taxonomy database"
- print " :bcp-like dump from GenBank taxonomy database."
- print "-----------------------------------"
+ print("-----------------------------------")
+ print(" ecoPCRFormat.py")
+ print("-----------------------------------")
+ print("ecoPCRFormat.py [option] <argument>")
+ print("-----------------------------------")
+ print("-e --embl :[E]mbl format")
+ print("-f --fasta :[F]asta format")
+ print("-g --genbank :[G]enbank format")
+ print("-h --help :[H]elp - print this help")
+ print("-n --name :[N]ame of the new database created")
+ print("-t --taxonomy :[T]axonomy - path to the taxonomy database")
+ print(" :bcp-like dump from GenBank taxonomy database.")
+ print("-----------------------------------")
if __name__ == '__main__':
@@ -590,3 +590,4 @@ if __name__ == '__main__':
ecoDBWriter(opt['prefix'], taxonomy, filenames, opt['parser'])
+
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
import struct
import sys
@@ -98,7 +98,7 @@ class Filter(object):
file = self.__universalOpen(file)
(recordCount,) = struct.unpack('> I',file.read(4))
- for i in xrange(recordCount):
+ for i in range(recordCount):
(recordSize,)=struct.unpack('>I',file.read(4))
record = file.read(recordSize)
yield record
@@ -283,12 +283,12 @@ class ColumnFile(object):
elif hasattr(stream,'next'):
self._stream = stream
else:
- raise ValueError,'stream must be string or an iterator'
+ raise ValueError('stream must be string or an iterator')
self._delimiter=sep
self._strip=strip
if types:
self._types=[x for x in types]
- for i in xrange(len(self._types)):
+ for i in range(len(self._types)):
if self._types[i] is bool:
self._types[i]=ColumnFile.str2bool
else:
@@ -305,16 +305,16 @@ class ColumnFile(object):
def __iter__(self):
return self
- def next(self):
- ligne = self._stream.next()
+ def __next__(self):
+ ligne = next(self._stream)
while ligne[0] == self._skip:
- ligne = self._stream.next()
+ ligne = next(self._stream)
data = ligne.split(self._delimiter)
if self._strip or self._types:
data = [x.strip() for x in data]
if self._types:
it = self.endLessIterator(self._types)
- data = [x[1](x[0]) for x in ((y,it.next()) for y in data)]
+ data = [x[1](x[0]) for x in ((y,next(it)) for y in data)]
return data
def endLessIterator(self,endedlist):
@@ -396,7 +396,7 @@ def _parseOligoResult(filter,file,strand
for s in filter.ecoPCRResultIterator(file):
o = s[key]
taxid = s['taxid']
- if not seq.has_key(o):
+ if o not in seq:
seq[o] = [1,taxid]
else:
seq[o][0] = seq[o][0] + 1
@@ -410,7 +410,7 @@ def _parseTaxonomyResult(table):
taxid = l[2]
scName = l[3]
count = l[1]
- if not tax.has_key(taxid):
+ if taxid not in tax:
tax[taxid] = [1,scName,count]
else:
tax[taxid][0] = tax[taxid][0] + 1
@@ -464,12 +464,12 @@ def customSort(table,x,y):
tmp = {}
for l in table:
- if tmp.has_key(l[x]):
+ if l[x] in tmp:
tmp[l[x]] = tmp[l[x]] + l[y]
else:
tmp[l[x]] = l[y]
- for k,v in tmp.items():
+ for k,v in list(tmp.items()):
cTable.append([k,v])
return cTable
@@ -484,12 +484,12 @@ def countColumnOccurrence(table,x):
tmp = {}
for l in table:
- if tmp.has_key(l[x]):
+ if l[x] in tmp:
tmp[l[x]] = tmp[l[x]] + 1
else:
tmp[l[x]] = 1
- for k,v in tmp.items():
+ for k,v in list(tmp.items()):
cTable.append([k,v])
return cTable
@@ -502,15 +502,15 @@ def buildSpecificityTable(table):
tmp = {}
for l in table:
- if not tmp.has_key(l[5]):
+ if l[5] not in tmp:
tmp[l[5]] = {}
- if not tmp[l[5]].has_key(l[3]):
+ if l[3] not in tmp[l[5]]:
tmp[l[5]][l[3]] = l[1]
else:
tmp[l[5]][l[3]] = tmp[l[5]][l[3]] + l[1]
- for mismatch in tmp.items():
- for taxon,count in mismatch[1].items():
+ for mismatch in list(tmp.items()):
+ for taxon,count in list(mismatch[1].items()):
speTable.append([mismatch[0],taxon,count])
return speTable
@@ -531,7 +531,7 @@ def buildOligoTable(table, file, filter,
seq = _parseOligoResult(filter, file, strand)
i = 0
- for oligo, info in seq.items():
+ for oligo, info in list(seq.items()):
table.append(0)
count, lctTaxid = info[0], info[1]
scName = filter.findTaxonByTaxid(info[1])[3]
@@ -554,7 +554,7 @@ def buildTaxonomicTable(table):
tax = _parseTaxonomyResult(table)
i = 0
- for taxid, info in tax.items():
+ for taxid, info in list(tax.items()):
taxTable.append(0)
numOfOligo, scName, numOfAmpl = info[0], info[1], info[2]
taxTable[i]=[scName,numOfOligo,numOfAmpl,taxid]
@@ -578,9 +578,9 @@ def _parseSequenceResult(filter,file,id)
for s in filter.ecoPCRResultIterator(file):
seq = s['sq_des']
id = s[key]
- if not idIndex.has_key(id):
+ if id not in idIndex:
idIndex[id] = []
- if not sequences.has_key(seq):
+ if seq not in sequences:
sequences[seq] = [id]
else:
sequences[seq].append(id)
@@ -598,7 +598,7 @@ def _sortSequences(file,filter):
sequences, idIndex = _parseSequenceResult(filter,file,'species')
- for s,id in sequences.items():
+ for s,id in list(sequences.items()):
if len(id) == 1 or _sameValuesInList(id):
idIndex[id[0]].append(1)
else:
@@ -606,7 +606,7 @@ def _sortSequences(file,filter):
idIndex[e].append(0)
- for id,values in idIndex.items():
+ for id,values in list(idIndex.items()):
idIndex[id] = float(values.count(1)) / float(len(values)) * 100
@@ -622,15 +622,15 @@ def getIntraSpeciesDiversity(table,file,
seq, idIndex = _sortSequences(file,filter)
- for id,percent in idIndex.items():
+ for id,percent in list(idIndex.items()):
if percent == 100:
intraDiv[id] = [0,[]]
- for seq,idList in sequences.items():
+ for seq,idList in list(sequences.items()):
if id in idList:
intraDiv[id][0] = intraDiv[id][0] + 1
intraDiv[id][1].append(seq)
- for id, values in intraDiv.items():
+ for id, values in list(intraDiv.items()):
table.append(id,values[0],values[1])
@@ -649,10 +649,10 @@ def printTable(table):
"""
format = ("%20s | " * len(table.headers))[:-3]
- print format % tuple([str(e) for e in table.headers ]) +"\n" + "-"*23*len(table.headers)
+ print(format % tuple([str(e) for e in table.headers ]) +"\n" + "-"*23*len(table.headers))
for l in table:
- print format % tuple([str(e) for e in l ])
- print "# %d results" % len(table)
+ print(format % tuple([str(e) for e in l ]))
+ print("# %d results" % len(table))
def saveAsCSV(table,path):
@@ -809,3 +809,4 @@ def start():
+
|