def realign(self, x, dx, y, dy, ignore=set(), positionGenerator=None): """Realign part of sequences.""" for processor in self.posterior_processors: self.posteriorTable = processor(self, self.posteriorTable) jcpoint( lambda: self.posteriorTable, 'processed', self.io_files, ) if positionGenerator == None: if self.positionGenerator != None: positionGenerator = self.positionGenerator else: positionGenerator = AlignmentFullGenerator([self.X, self.Y]) D = self.computeBacktrackTable(x, dx, y, positionGenerator, ignore) aln = self.backtrack(dx, dy, D) return self.generateAnnotationAndAlignment( aln, x, y, positionGenerator, ignore, )
def backtrack(self, dx, dy, D): # backtrack _x = dx _y = dy aln = [] jcpoint(lambda: D, 'D', self.io_files) while _x > 0 or _y > 0: (_, (fr, _dx, _dy)) = D[_x][_y] aln.append((fr, _dx, _dy)) # print _x, _y, _dx, _dy assert(_dx >= 0 and _dy >= 0) _x -= _dx _y -= _dy aln = list(reversed(aln)) return aln
def backtrack(self, dx, dy, D): # backtrack _x = dx _y = dy aln = [] jcpoint(lambda: D, 'D', self.io_files) while _x > 0 or _y > 0: (_, (fr, _dx, _dy)) = D[_x][_y] aln.append((fr, _dx, _dy)) # print _x, _y, _dx, _dy assert (_dx >= 0 and _dy >= 0) _x -= _dx _y -= _dy aln = list(reversed(aln)) return aln
def computePosterior(self): self.posteriorTable = jcpoint( lambda: divide(*self.model.getPosteriorTable( self.X, 0, len(self.X), self.Y, 0, len(self.Y), positionGenerator=self.positionGenerator, )), 'posterior', self.io_files, self.mathType, )
def marginalize_gaps_processor(realigner, table): gapdict = defaultdict(lambda *_: realigner.mathType(0.0)) for i in range(len(table)): for j, D in table[i].iteritems(): for (s, x, y), p in D.iteritems(): if x != 0 and y != 0: continue if x == 0 and y == 0: continue if x == 0: y = j if y == 0: x = i gapdict[(s, x, y)] += p jcpoint( lambda: gapdict, 'gapdict', realigner.io_files, ) # xxx = defaultdict(list) for i in range(len(table)): for j, D in table[i].iteritems(): for (s, x, y), p in D.iteritems(): if x != 0 and y != 0: continue if x == 0 and y == 0: continue nx = x ny = y if x == 0: ny = j if y == 0: nx = i # xxx[(s, nx, ny)].append(str(((i, j), (s, x, y)))) table[i][j][(s, x, y)] = gapdict[(s, nx, ny)] # jcpoint( # lambda: xxx, # 'gaplist', # realigner.io_files, # ) return table
def getTable(self, X, x, dx, Y, y, dy, tables, forwardTable=None, backwardTable=None, positionGenerator=None): # Najskor chcem taku, co predpoklada ze obe tabulky su velke # Potom chcem taku, ktora si bude doratavat chybajuce # Potom pridat switch, ktory mi umozni robit optimalizacie. # Ale potom bude treba vediet, ci ist od predu, alebo od zadu # Fetch tables if they are not provided if positionGenerator != None: positionGenerator = list(positionGenerator) perf.push() forwardTable = jcpoint( lambda: forwardTable if forwardTable != None else self.getForwardTableGenerator(X, x, dx, Y, y, dy, positionGenerator=positionGenerator), 'forward_table', self.io_files, self.mathType, ) perf.msg('Forward table was computed in {time} seconds.') perf.replace() backwardTable = jcpoint( lambda: backwardTable if backwardTable != None else self.getBackwardTable(X, x, dx, Y, y, dy, positionGenerator=positionGenerator), 'backward_table', self.io_files, self.mathType, ) perf.msg('Backward table was computed in {time} seconds.') perf.replace() # Sort tables by first element (just in case) sorted(backwardTable,key=lambda (x,_) : x) perf.msg('Tables were sorted in {time} seconds.') perf.replace() # Convert forward table into list #ft = [dict() for _ in range(dx + 1)] #for (i, _x) in forwardTable: # ft[i - x] = _x # Convert backward table into list bt = [dict() for _ in range(dx + 1)] for (i, B) in backwardTable: bt[i] = B perf.msg('Backward table was flattened in {time} seconds.') perf.replace() States = [table(dx, self) for table in tables] index = 0 for i, row in forwardTable: #slice position generator while (index < len(positionGenerator) and positionGenerator[index][0] < i): index += 1 start = index while (index < len(positionGenerator) and positionGenerator[index][0] <= i): index += 1 for table in States: table.processRow(X, x, dx, Y, y, dy, i, row, bt, positionGenerator[start:index]) ret = [table.getData() for table in States] # ret = [table(X, x, dx, Y, y, dy, ft, bt, positionGenerator) # for table in tables] perf.msg('Posterior table was computed in {time} seconds.') perf.pop() return ret
def getTable(self, X, x, dx, Y, y, dy, tables, forwardTable=None, backwardTable=None, positionGenerator=None): # Najskor chcem taku, co predpoklada ze obe tabulky su velke # Potom chcem taku, ktora si bude doratavat chybajuce # Potom pridat switch, ktory mi umozni robit optimalizacie. # Ale potom bude treba vediet, ci ist od predu, alebo od zadu # Fetch tables if they are not provided if positionGenerator != None: positionGenerator = list(positionGenerator) perf.push() forwardTable = jcpoint( lambda: forwardTable if forwardTable != None else self.getForwardTableGenerator( X, x, dx, Y, y, dy, positionGenerator=positionGenerator), 'forward_table', self.io_files, self.mathType, ) perf.msg('Forward table was computed in {time} seconds.') perf.replace() backwardTable = jcpoint( lambda: backwardTable if backwardTable != None else self.getBackwardTable( X, x, dx, Y, y, dy, positionGenerator=positionGenerator), 'backward_table', self.io_files, self.mathType, ) perf.msg('Backward table was computed in {time} seconds.') perf.replace() # Sort tables by first element (just in case) sorted(backwardTable, key=lambda (x, _): x) perf.msg('Tables were sorted in {time} seconds.') perf.replace() # Convert forward table into list #ft = [dict() for _ in range(dx + 1)] #for (i, _x) in forwardTable: # ft[i - x] = _x # Convert backward table into list bt = [dict() for _ in range(dx + 1)] for (i, B) in backwardTable: bt[i] = B perf.msg('Backward table was flattened in {time} seconds.') perf.replace() States = [table(dx, self) for table in tables] index = 0 for i, row in forwardTable: #slice position generator while (index < len(positionGenerator) and positionGenerator[index][0] < i): index += 1 start = index while (index < len(positionGenerator) and positionGenerator[index][0] <= i): index += 1 for table in States: table.processRow(X, x, dx, Y, y, dy, i, row, bt, positionGenerator[start:index]) ret = [table.getData() for table in States] # ret = [table(X, x, dx, Y, y, dy, ft, bt, positionGenerator) # for table in tables] perf.msg('Posterior table was computed in {time} seconds.') perf.pop() return ret