def assign_orientation(self): signs = defaultdict(list) scaffolds = self.scaffolds for mlg in self.linkage_groups: mapname = mlg.mapname series = mlg.series if mapname == self.pivot: pivot_oo = mlg.oo pivot_nmarkers = mlg.nmarkers for i, j in combinations(range(len(scaffolds)), 2): si, sj = scaffolds[i], scaffolds[j] si, sj = series.get(si, []), series.get(sj, []) d = self.get_orientation(si, sj) if not d: continue signs[i, j].append((d, mapname)) for e, v in signs.items(): signs[e] = self.weighted_mean(v) signs_edges = sorted((a, b, w) for (a, b), w in signs.items()) signs = determine_signs(scaffolds, signs_edges) # Finally flip this according to pivot map, then weight by #_markers pivot_oo = [pivot_oo.get(x, 0) for x in scaffolds] nmarkers = [pivot_nmarkers.get(x, 0) for x in scaffolds] flipr = signs * np.sign(np.array(pivot_oo)) * nmarkers if sum(flipr) < 0: signs = -signs return signs
def assign_orientation(self): signs = defaultdict(list) scaffolds = self.scaffolds for mlg in self.linkage_groups: mapname = mlg.mapname series = mlg.series if mapname == self.pivot: pivot_oo = mlg.oo pivot_nmarkers = mlg.nmarkers for i, j in combinations(range(len(scaffolds)), 2): si, sj = scaffolds[i], scaffolds[j] si, sj = series.get(si, []), series.get(sj, []) d = self.get_orientation(si, sj) if not d: continue signs[i, j].append((d, mapname)) for e, v in signs.items(): signs[e] = self.weighted_mean(v) signs_edges = sorted((a, b, w) for (a, b), w in signs.items()) signs = determine_signs(scaffolds, signs_edges) # Finally flip this according to pivot map, then weight by #_markers pivot_oo = [pivot_oo.get(x, 0) for x in scaffolds] nmarkers = [pivot_nmarkers.get(x, 0) for x in scaffolds] flipr = signs * np.sign(np.array(pivot_oo)) * nmarkers if sum(flipr) < 0: signs = - signs return signs
def test_determine_signs(nodes, edges, expected): from jcvi.algorithms.matrix import determine_signs assert np.allclose(determine_signs(nodes, edges), expected)
def solve_component(h, sizes, fwlog): """ Solve the component first by orientations, then by positions. """ from jcvi.algorithms.matrix import determine_signs, determine_positions from jcvi.assembly.base import orientationflips nodes, edges = h.nodes(), h.edges(data=True) nodes = sorted(nodes) inodes = dict((x, i) for i, x in enumerate(nodes)) # Solve signs ledges = [] for a, b, c in edges: orientation = c["orientation"] orientation = '+' if orientation[0] == orientation[1] else '-' a, b = inodes[a], inodes[b] if a > b: a, b = b, a ledges.append((a, b, orientation)) N = len(nodes) print >> fwlog, N, ", ".join(nodes) signs = determine_signs(nodes, ledges) print >> fwlog, signs # Solve positions dedges = [] for a, b, c in edges: orientation = c["orientation"] distance = c["distance"] a, b = inodes[a], inodes[b] if a > b: a, b = b, a ta = '+' if signs[a] > 0 else '-' tb = '+' if signs[b] > 0 else '-' pair = ta + tb if orientationflips[orientation] == pair: distance = - distance elif orientation != pair: continue dedges.append((a, b, distance)) positions = determine_positions(nodes, dedges) print >> fwlog, positions bed = [] for node, sign, position in zip(nodes, signs, positions): size = sizes[node] if sign < 0: start = position - size end = position orientation = "-" else: start = position end = position + size orientation = "+" bed.append((node, start, end, orientation)) key = lambda x: x[1] offset = key(min(bed, key=key)) bed.sort(key=key) for node, start, end, orientation in bed: start -= offset end -= offset print >> fwlog, "\t".join(str(x) for x in \ (node, start, end, orientation)) return bed
def solve_component(h, sizes, fwlog): """ Solve the component first by orientations, then by positions. """ from jcvi.algorithms.matrix import determine_signs, determine_positions from jcvi.assembly.base import orientationflips nodes, edges = h.nodes(), h.edges(data=True) nodes = sorted(nodes) inodes = dict((x, i) for i, x in enumerate(nodes)) # Solve signs ledges = [] for a, b, c in edges: orientation = c["orientation"] orientation = '+' if orientation[0] == orientation[1] else '-' a, b = inodes[a], inodes[b] if a > b: a, b = b, a ledges.append((a, b, orientation)) N = len(nodes) print >> fwlog, N, ", ".join(nodes) signs = determine_signs(nodes, ledges) print >> fwlog, signs # Solve positions dedges = [] for a, b, c in edges: orientation = c["orientation"] distance = c["distance"] a, b = inodes[a], inodes[b] if a > b: a, b = b, a ta = '+' if signs[a] > 0 else '-' tb = '+' if signs[b] > 0 else '-' pair = ta + tb if orientationflips[orientation] == pair: distance = -distance elif orientation != pair: continue dedges.append((a, b, distance)) positions = determine_positions(nodes, dedges) print >> fwlog, positions bed = [] for node, sign, position in zip(nodes, signs, positions): size = sizes[node] if sign < 0: start = position - size end = position orientation = "-" else: start = position end = position + size orientation = "+" bed.append((node, start, end, orientation)) key = lambda x: x[1] offset = key(min(bed, key=key)) bed.sort(key=key) for node, start, end, orientation in bed: start -= offset end -= offset print >> fwlog, "\t".join(str(x) for x in \ (node, start, end, orientation)) return bed