Skip to content

Commit

Permalink
Merge pull request #2 from mradamczyk/ma/fix-draw
Browse files Browse the repository at this point in the history
fix draw
  • Loading branch information
mradamczyk authored Sep 17, 2017
2 parents 4277093 + c519b16 commit fe364df
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 20 deletions.
57 changes: 38 additions & 19 deletions drawDecomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,34 @@ def dnaComplementarity(x):
'T': 'A'
}.get(x, x)

def check(f, word, i, j, delta, positions):
def check(f, word, i, j, delta, positions, mode):
if delta < 0:
return None
if i > j:
deletion, swap = False, False
return [(word[l], any(p == l for p in positions), any(p == l+len(word) for p in positions))
for l in xrange(len(word))]
end = None

if word[i] == f(word[j]):
end = check(f, word, i+1, j-1, delta, positions)
if end is not None:
return end
end = check(f, word, i+1, j, delta-1, positions + [i])
end = check(f, word, i+1, j-1, delta, positions, mode)
if end is not None:
return end
end = check(f, word, i, j-1, delta-1, positions + [j])
if end is not None:
return end
end = check(f, word, i+1, j-1, delta-1, positions + [i+len(word), j+len(word)])

if mode == 'edit':
end = check(f, word, i+1, j, delta-1, positions + [i], mode)
if end is not None:
return end

end = check(f, word, i, j-1, delta-1, positions + [j], mode)
if end is not None:
return end

end = check(f, word, i+1, j-1, delta-1, positions + [i+len(word), j+len(word)], mode)
return end

def word2nodes(f, word, delta):
letters = check(f, word, 0, len(word)-1, delta, [])
def word2nodes(f, word, delta, mode):
letters = check(f, word, 0, len(word)-1, delta, [], mode)
nodes = [('#', False, [])]
last = nodes[-1]
for (let, deletion, swap) in letters:
Expand All @@ -44,14 +49,16 @@ def word2nodes(f, word, delta):
nodes.append(('#', False, []))
return nodes

def decomposition2graph(f, decomposition, delta):
def decomposition2graph(f, decomposition, delta, mode):
g = nx.Graph()
words = decomposition.split(' ')
#words = ['a','b','c']
cnt = 1
g.add_node(0, label='#', pos=[0, 0], color="white")
last_x, last_y = 0, 0
for w in words:
if len(w) == 0:
continue
if w[0] == '[':
if len(w) == 2:
continue
Expand All @@ -61,8 +68,9 @@ def decomposition2graph(f, decomposition, delta):
cnt += 1
last_x += 1 + (int(len(w)/5))
else:
nodes = word2nodes(f, w, delta)
nodes = word2nodes(f, w, delta, mode)
k = len(nodes) / 2
odd = len(nodes) % 2
temp = []
last_x += 1
for i in xrange(k):
Expand Down Expand Up @@ -96,10 +104,19 @@ def decomposition2graph(f, decomposition, delta):
last_x -= 1 if i == k-1 else (0 if i == 0 else -1)
last_y = initial_y + mx * 2 - (0 if i != 0 or deletions1 or deletions2 else 2)

last_x += 2
if odd == 0:
last_x += 2
else:
last_x +=1
last_y += 1 if last_y != 0 else 0
g.add_node(cnt, label=nodes[k][0], pos=[last_x, last_y], color='black')
g.add_edge(cnt-1, cnt, style='bold')
cnt += 1
last_y -= 1 if last_y != 1 else 0
last_x += 1

for i in xrange(k):
(let2, swap2, deletions2) = nodes[i+k]
(let2, swap2, deletions2) = nodes[i+odd+k]
(r_id, r_y) = temp.pop()

if i == k-1 and len(nodes[-2][2]) >= len(nodes[0][2]):
Expand Down Expand Up @@ -147,23 +164,25 @@ def drawGraph(g, fileName):
G.render(fileName + '.gv')

def main(argv):
dna, delta, fileName = False, 0, 'decomposition'
dna, delta, fileName, mode = False, 0, 'decomposition', 'edit'
try:
opts, args = getopt.getopt(argv,"dE:f:")
opts, args = getopt.getopt(argv,"dHE:f:")
except getopt.GetoptError:
print ('drawDecomposition.py [-d] [-E N] [-f fileName]')
print ('drawDecomposition.py [-d] [-H] [-E N] [-f fileName]')
sys.exit(2)
for opt, arg in opts:
if opt == '-d':
dna = True
elif opt == '-H':
mode = 'hamming'
elif opt == '-E':
delta = int(arg)
elif opt == '-f':
fileName = arg

sys.stdin.readline()
decomp = sys.stdin.readline().strip()
g = decomposition2graph(dnaComplementarity if dna else (lambda x: x), decomp, delta)
g = decomposition2graph(dnaComplementarity if dna else (lambda x: x), decomp, delta, mode)
drawGraph(g, fileName)

if __name__ == "__main__":
Expand Down
46 changes: 46 additions & 0 deletions examples/eertree-ex1.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
digraph G {
size="6,6" ;
splines=true ;
0:sw -> bb:nw [label=" b "] ;

-1 -> a [label=" a "] ;
-1 -> b [label=" b "] ;
-1 -> c [label=" c "] ;

b -> cbc [label=" c "] ;
c -> bcb [label=" b "] ;
cbc -> bcbcb [label=" b "] ;
bcbcb -> bbcbcbb [label=" b "] ;
bbcbcbb-> abbcbcbba [label=" a "] ;

0 -> -1 [style="dashed", color="blue", constraint=false] ;
-1:ne -> -1:nw [style="dashed", color="blue", constraint=false] ;
a -> 0 [style="dashed", color="blue", constraint=false] ;
b -> 0 [style="dashed", color="blue", constraint=false] ;
c -> 0 [style="dashed", color="blue", constraint=false] ;
bb -> b [style="dashed", color="blue", constraint=false] ;
bcb -> b [style="dashed", color="blue", constraint=false] ;
cbc -> c [style="dashed", color="blue", constraint=false] ;
bcbcb -> bcb [style="dashed", color="blue", constraint=false] ;
bbcbcbb -> bb [style="dashed", color="blue", constraint=false] ;
abbcbcbba -> a [style="dashed", color="blue", constraint=false] ;

bb -> 0 [style="dotted", color="red", dir=forward] ;
bcbcb:nw -> b [style="dotted", color="red"] ;


-1 [ pos = "2,6!"] ;
0 [ pos = "0,6!"] ;
a [ pos = "1,5!"] ;
b [ pos = "2,5!"] ;
c [ pos = "3,5!"] ;
bb [ pos = "0,4!"] ;
cbc [ pos = "2,3!"] ;
bcb [ pos = "3,3!"] ;
bcbcb [ pos = "2,2!"] ;
bbcbcbb [ pos = "2,1!"] ;
abbcbcbba [ pos = "2,0!"] ;

sep=0.3 ;
nodesep=0.3 ;
}
44 changes: 44 additions & 0 deletions examples/eertree-ex2.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
digraph G {
splines=true ;

0 -> GC [label=" C "]
0 -> CG [label=" G "]
0 -> TA [label=" A "]
GC -> CGCG [label=" G "]
CGCG -> ACGCGT [label=" T "]
TA -> GTAC [label=" C "]
TA -> CTAG [label=" G "]
CTAG -> ACTAGT [label=" T "]


GC:ne -> 0 [style="dashed", color="blue", constraint=false] ;
CG:n -> 0 [style="dashed", color="blue", constraint=false] ;
TA:n -> 0 [style="dashed", color="blue", constraint=false] ;
GTAC:n -> 0 [style="dashed", color="blue", constraint=false] ;
CTAG -> 0 [style="dashed", color="blue", constraint=false] ;
ACGCGT:ne -> 0 [style="dashed", color="blue", constraint=false] ;
ACTAGT -> 0 [style="dashed", color="blue", constraint=false] ;
CGCG -> CG [style="dashed", color="blue", constraint=false] ;


0 -> -1 [style="dashed", color="blue", constraint=false] ;
-1:ne -> -1:nw [style="dashed", color="blue", constraint=false] ;

CGCG:nw -> 0 [style="dotted", color="red", constraint=false] ;


0 [ pos="1,4!" ];
-1 [ pos="2,4!" ];
CG [ pos="0,2!" ];
GC [ pos="1,2!" ];
TA [ pos="4,2!" ];
CGCG [ pos="1,1!" ];
GTAC [ pos="3,1!" ];
CTAG [ pos="5,1!" ];
ACGCGT [ pos="1,0!" ];
ACTAGT [ pos="4,0!" ];

sep=0.3 ;
nodesep=0.3 ;

}
5 changes: 4 additions & 1 deletion gen_figures.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
./max_pal_decomp_gaps_errors.x -d -G 4 -E 3 -L 14 -H -p < examples/ab220944.in | python drawDecomposition.py -d -E 3 -f max-pal-ham
./max_pal_decomp_gaps_errors.x -d -G 4 -E 3 -L 14 -H -p < examples/ab220944.in | python drawDecomposition.py -d -H -E 3 -f max-pal-ham
./max_pal_decomp_gaps_errors.x -d -G 4 -E 3 -L 14 -p < examples/ab220944.in | python drawDecomposition.py -d -E 3 -f max-pal-edit-1
./max_pal_decomp_gaps_errors.x -d -G 4 -E 4 -L 20 -p < examples/ab220944.in | python drawDecomposition.py -d -E 4 -f max-pal-edit-2

./max_pal_decomp_gaps_errors.x -d -L 5 -G 2 -E 2 -p < examples/ab286863.in | python drawDecomposition.py -d -E 2 -f not-maximal-max
./all_pal_decomp_gaps_errors.x -d -L 5 -G 2 -E 2 -p < examples/ab286863.in | python drawDecomposition.py -d -E 2 -f not-maximal-all

python ./drawDecomposition.py -d -E 1 < examples/vis-ex.in -f vis-ex

cat examples/eertree-ex2.dot | neato -Tpdf > eertree-ex2.pdf
cat examples/eertree-ex1.dot | neato -Tpdf > eertree-ex1.pdf

0 comments on commit fe364df

Please sign in to comment.