def main(seq_a, seq_b, is_global, figure): D = score_matrix(seq_a, seq_b, is_global=False) path = backtrack(D, is_global) f = format_matrix_tikz(D, path, seq_a, seq_b) A, B = get_alignment(seq_a, seq_b, path) block = tikz_matrix("block", body([A, B]), "below=of m") start = tikz_matrix("seqs", body([seq_a, seq_b]), "above=of m") figure.write(f + "\n" + block + "\n" + start) # open("needlemanwunch.tex", "w").write(HEADER + f + "\n" + block + FOOTER) " TODO: Add hlines and vlines to denote which letters are in the block "
def create_fm_figure(reference, query): ref_mat = [list(reference), [str(i) for i in range(len(reference))]] ref_fig = tikz_matrix("ref", body(ref_mat)) reference = reference + "$" idxs, suffices = sort_suffices(reference) bwt = [s[-1] for s in suffices] occ = get_occurance_matrix(bwt) counts = get_char_counts([s[0] for s in suffices]) + 1 matrix = matrix1(idxs, suffices, occ, counts).replace("#", "\$") query_fig = tikz_matrix("query", body([query]), "below=of fm") Figure("fma", "fm").write("\n".join((ref_fig, matrix, query_fig)))
def matrix1(idxs, suffices, occ, counts): # counts = np.concatenate(([0], counts)) suffices = [row.replace("$", "#") for row in suffices] occ = np.asanyarray(occ, dtype="int") F = [ "$%s_%s$" % (suffix[0], i - counts[lookup[suffix[0]]]) if suffix[0] != "#" else "#" for i, suffix in enumerate(suffices) ] L = [ "$%s_%s$" % (suffix[-1], o[lookup[suffix[-1]]]) if suffix[-1] != "#" else "#" for o, suffix in zip(occ, suffices) ] elements = [[str(i), f, suffix[1:-1], l] + [str(o) for o in row] for i, f, suffix, l, row in zip(idxs, F, suffices, L, occ)] print(elements) return tikz_matrix("fm", body(elements), "below=of ref") rows = [ " & ".join([str(i)] + [f] + [suffix[1:-1]]) + [l] for i, f, suffix, l in zip(idxs, F, suffices, L) ] # rows = [str(i)+ "&".join(row[0], row[1:-1], row[-1]) for i, row in zip(idxs, suffices)] # " & " + " & ".join(str(c) for c in row) for i, row in zip(idxs, suffices)] occ_rows = ["&&" + "&".join(str(n) for n in row) + "\\\\" for row in occ] rows = [row + occ_row for row, occ_row in zip(rows, occ_rows)] rows = [ row + "\n \\hline" if i in counts else row for i, row in enumerate(rows) ]
def generate_tikz(self, name, args=""): self.name = name prev_a_id = None prev_b_id = None del_a = False del_b = False for x, (c_a, c_b) in enumerate(zip(self._row_a, self._row_b)): y = 0 if c_a != "-": a_id = self.node(c_a, x, y) y += 1 if prev_a_id is not None: if del_a: self.del_edge(prev_a_id, a_id) else: self.edge(prev_a_id, a_id) prev_a_id = a_id del_a = False else: del_a = True if c_b != "-": if c_a != c_b: b_id = self.node(c_b, x, y) else: b_id = a_id if prev_b_id is not None: if del_b: self.del_edge(prev_b_id, b_id) else: self.edge(prev_b_id, b_id) prev_b_id = b_id del_b = False else: del_b = True return tikz_matrix(name, body(self._matrix), args) + "\n" + "\n".join(self.lines)
def format_matrix_tikz(D, path, seq_a, seq_b): path_set = set(path) lines = [] path_color = "red!25" c_text = r"\textcolor{red}{%s}" b_text = r"\textcolor{black!25}{%s}" color = r"\cellcolor{blue!25}" elements = [[ c_text % c if (i, j) in path_set else b_text % c for j, c in enumerate(row) ] for i, row in enumerate(D)] m = tikz_matrix("m", body(elements)) seq_template = r"\path[-stealth] (m-%s-1) edge [draw=none] node [left=0.5em] {%s} (m-%s-1);" seq_template_b = r"\path[-stealth] (m-%s-%s) edge [draw=none] node [above=0.5em] {%s} (m-%s-%s);" seq_a_text = "\n".join(seq_template % (i + 1, c, i + 2) for i, c in enumerate(seq_a)) seq_b_text = "\n".join(seq_template_b % (1, i + 1, c, 1, i + 2) for i, c in enumerate(seq_b)) edge_template = r"\path[-stealth] (m-%s-%s) edge[color=red] (m-%s-%s);" edges = "\n".join([ edge_template % (i + 1, j + 1, k + 1, l + 1) for (i, j), (k, l) in zip(path[:-1], path[1:]) ]) return "\n".join((m, seq_a_text, seq_b_text, edges))
def to_tikz(self, horizontal=True): lines = [] labels = [self._labels] if horizontal else [[c] for c in self._labels] lines.append(tikz_matrix("m", body(labels))) lines.extend( self.get_edge(i, j, horizontal) for i, js in self._edges.iteritems() for j in js) return "\n".join(lines)
def to_file(self, filename): f = open(filename, "w") f.write(self.header + "\n") # f.write(self._row_a + "\n") f.write(tikz_matrix("seq", body(self._matrix))+"\n") for line in self.lines: f.write(line+"\n") f.write(self.footer) f.close() subprocess.call(["pdflatex", filename])
def create_figure(seq, interval_dict, query): seq_fig = tikz_matrix("seq", body([seq, "".join(rev_c[c] for c in seq)])) query_fig = tikz_matrix("q", body([query]), "below=of seq") rec_1 = r"\node[color=%s, draw, dashed, rounded corners, fit=(seq-1-%s) (seq-1-%s)] {};" rec_2 = r"\node[color=%s, draw, dashed, rounded corners, fit=(seq-2-%s) (seq-2-%s)] {};" rec_q = r"\node[color=%s, draw, dashed, rounded corners, fit=(q-1-%s) (q-1-%s)] {};" lines = [] N = len(seq) i = 0 for (s, e), (intervals, reverse_intervals) in interval_dict.iteritems(): lines.append(rec_q % (colors[i], s + 1, e)) for (start, end) in intervals: lines.append(rec_1 % (colors[i], start + 1, end)) for start, end in reverse_intervals: lines.append(rec_2 % (colors[i], N - end + 1, N - start)) i += 1 content = seq_fig + "\n" + query_fig + "\n" + "\n".join(lines) content = content.replace("$", r"\$") return content
path = backtrack_g(m, seq_graph, seq_graph2) print path figure = Figure("seqgraph_a", "seqalign_figs") c_text = r"\textcolor{red}{%s}" b_text = r"\textcolor{black!25}{%s}" m = [[ c_text % c if (i, j) in path else b_text % c for j, c in enumerate(row) ] for i, row in enumerate(m)] elems = [list("." + labels)] + [[c] + row for c, row in zip(labels2, m)] edges2 = seq_graph2.get_tikz_edges() edges = seq_graph.get_tikz_edges(False) path_edge_template = r"\path[-stealth] (m-%s-%s) edge[color=red] (m-%s-%s);" path_edges = "\n".join([ path_edge_template % (i + 2, j + 2, k + 2, l + 2) for (i, j), (k, l) in zip(path[:-1], path[1:]) ]) A, B = get_alignment(seq_graph._labels, seq_graph2._labels, path) alignment_matrix = tikz_matrix("am", body([A, B]), "below=of m") init = align2.generate_tikz("ga", "") init2 = align1.generate_tikz("gb", "below=of ga,") content = "\n".join([ init, init2, tikz_matrix("m", body(elems), "below=of gb,"), edges, edges2, path_edges, alignment_matrix ]) content = content.replace("#", r"\#") figure.write(content) # figure.write(seq_graph.to_tikz(False)) # print(edges)