Package obitools :: Package ecopcr :: Module annotation
[hide private]
[frames] | no frames]

Source Code for Module obitools.ecopcr.annotation

  1  import struct 
  2   
3 -class EcoPCRDBAnnotationWriter(object):
4 ''' 5 Class used to write Annotation description in EcoPCRDB format. 6 7 EcoPCRDBAnnotationWriter is oftenly called through the EcoPCRDBSequenceWriter class 8 9 @see: L{ecopcr.sequence.EcoPCRDBSequenceWriter} 10 ''' 11
12 - def __init__(self,dbname,id,fileidx=1,type=('CDS'),definition=None):
13 ''' 14 class constructor 15 16 @param dbname: name of ecoPCR database 17 @type dbname: C{str} 18 @param id: name of the qualifier used as feature id 19 @type id: C{str} 20 @param fileidx: 21 @type fileidx: C{int} 22 @param type: 23 @type type: C{list} or C{tuple} 24 @param definition: 25 @type definition: C{str} 26 ''' 27 self._type = type 28 self._definition = definition 29 self._id = id 30 self._filename="%s_%03d.adx" % (dbname,fileidx) 31 self._file = open(self._filename,'wb') 32 self._sequenceIdx=0 33 34 35 ftname ="%s.fdx" % (dbname) 36 ft = open(ftname,'wb') 37 38 self._fttypeidx=dict(map(None,type,xrange(len(type)))) 39 40 ft.write(struct.pack('> I',len(type))) 41 42 for t in type: 43 ft.write(self._ecoFtTypePacker(t)) 44 45 ft.close() 46 47 self._annotationCount=0 48 self._file.write(struct.pack('> I',self._annotationCount))
49 50
51 - def _ecoFtTypePacker(self,type):
52 totalSize = len(type) 53 packed = struct.pack('> I %ds' % totalSize,totalSize,type) 54 55 assert len(packed) == totalSize+4, "error in feature type packing" 56 57 return packed
58
59 - def _ecoAnnotationPacker(self,feature,seqidx):
60 begin = feature.begin-1 61 end = feature.end 62 type = self._fttypeidx[feature.ftType] 63 strand = feature.isDirect() 64 id = feature[self._id][0] 65 if self._definition in feature: 66 definition = feature[self._definition][0] 67 else: 68 definition = '' 69 70 assert strand is not None,"Only strand defined features can be stored" 71 72 deflength = len(definition) 73 74 totalSize = 4 + 4 + 4 + 4 + 4 + 20 + 4 + deflength 75 76 packed = struct.pack('> I I I I I 20s I %ds' % (deflength), 77 totalSize, 78 seqidx, 79 begin, 80 end, 81 type, 82 int(strand), 83 id, 84 deflength, 85 definition) 86 87 assert len(packed) == totalSize+4, "error in annotation packing" 88 89 return packed
90 91
92 - def put(self,sequence,seqidx=None):
93 if seqidx is None: 94 seqidx = self._sequenceIdx 95 self._sequenceIdx+=1 96 for feature in sequence.getFeatureTable(): 97 if feature.ftType in self._type: 98 self._annotationCount+=1 99 self._file.write(self._ecoAnnotationPacker(feature,seqidx))
100
101 - def __del__(self):
102 self._file.seek(0,0) 103 self._file.write(struct.pack('> I',self._annotationCount)) 104 self._file.close()
105