From e5c617260e480d180bfa93bd3b2a2bdbe7319cca Mon Sep 17 00:00:00 2001
From: Toshiyuki Yokoyama <yokoyama@no.reply>
Date: Tue, 2 Jul 2019 23:07:05 +0900
Subject: [PATCH] #4: Add paths to graph class

---
 src/gfa.py     |  3 +--
 src/graph.py   | 45 ++++++++++++++++++++++++---------------------
 src/test.py    |  2 +-
 test/test2.gfa |  2 +-
 4 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/src/gfa.py b/src/gfa.py
index e54d9c1..f4e64ba 100644
--- a/src/gfa.py
+++ b/src/gfa.py
@@ -117,7 +117,7 @@ def from_graph(cls, graph: Graph):
                     path_list[path].append(segment_id)
         for path_key in path_list:
             path_values = [str(x) for x in path_list[path_key]]
-            gfa.add_line('\t'.join(['P', path_key, "+,".join(path_values)+"+", ",".join(['*' for x in path_values])]))
+            gfa.add_line('\t'.join(['P', path_key, "+,".join(path_values)+"+", ",".join(['*' for _ in path_values])]))
         return cls(gfa)
 
     @property
@@ -128,7 +128,6 @@ def to_graph(self):
 
         # Extract all paths into graph
         for path in self.gfa.paths:
-            ### TODO: Replace it as a path class object.
             for node in path.segment_names:
                 path_dict[node.name + node.orient].append(path.name)
             for node_pair in pairwise(path.segment_names):
diff --git a/src/graph.py b/src/graph.py
index f406623..68d5001 100644
--- a/src/graph.py
+++ b/src/graph.py
@@ -107,10 +107,33 @@ def smallest(self):
     version = 1.0
 
 
+class NodeIndex(NamedTuple):
+    node: Node
+    strand: str
+
+class Path:
+    """TODO: Paths have not been implemented yet."""
+    def __init__(self, name: str, nodes: List[NodeIndex]):
+        self.name = name
+        self.nodes = nodes
+        self.position_checkpoints = {}
+
+    def __getitem__(self, i):
+        return self.nodes[i]
+
+    def __repr__(self):
+        """Warning: the representation strings are very sensitive to whitespace"""
+        return self.nodes.__repr__()
+
+    def to_gfa(self):
+        return '\t'.join(['P', self.name, "+,".join([x.node.name + x.strand for x in self.nodes])+"+", ",".join(['*' for x in self.nodes])])
+
+
 class Graph:
-    def __init__(self, cmd: List):
+    def __init__(self, cmd: List, paths: List[Path] = []):
         """Factory for generating graphs from a representation"""
         self.slices = []
+        self.paths = paths
         if isinstance(cmd, str):
             cmd = eval(cmd)
         for sl in cmd:
@@ -159,26 +182,6 @@ def save_as_pickle(self, file):
     def save_as_xg(self):
         raise NotImplementedError()
 
-class NodeIndex(NamedTuple):
-    node: Node
-    strand: str
-
-class Path:
-    """TODO: Paths have not been implemented yet."""
-    def __init__(self, name: str, nodes: List[NodeIndex]):
-        self.name = name
-        self.nodes = nodes
-        self.position_checkpoints = {}
-
-    def __getitem__(self, i):
-        return self.nodes[i]
-
-    def __repr__(self):
-        """Warning: the representation strings are very sensitive to whitespace"""
-        return self.nodes.__repr__()
-
-    def to_gfa(self):
-        return '\t'.join(['P', self.name, "+,".join([x.node.name + x.strand for x in self.nodes])+"+", ",".join(['*' for x in self.nodes])])
 
 if __name__ == "__main__":
     location_of_xg = sys.argv[0]
diff --git a/src/test.py b/src/test.py
index cf2e819..e740131 100644
--- a/src/test.py
+++ b/src/test.py
@@ -121,7 +121,7 @@ def is_different(gfa1, gfa2):
                 if l.line.from_segment.name != l2.line.from_segment.name:
                     print(l, l2)
                     different = True
-                if l.line.to_segment.name != l.line.to_segment.name:
+                if l.line.to_segment.name != l2.line.to_segment.name:
                     print(l, l2)
                     different = True
         for s in gfa1.edges:
diff --git a/test/test2.gfa b/test/test2.gfa
index 5a01b3e..52164c1 100644
--- a/test/test2.gfa
+++ b/test/test2.gfa
@@ -1,7 +1,7 @@
 H	VN:Z:1.0
 P	x	1+,3+,5+,6+,8+,9+,11+,12+,14+,15+	*,*,*,*,*,*,*,*,*
 P	y	1+,2+,5+,6+,8+,9+,11+,12+,14+,15+	*,*,*,*,*,*,*,*,*
-P	z	1+,2+,5+,6+,7+,9+,11+,12+,14+,15+	*,*,*,*,*,*,*,*,*
+P	z	1+,2+,4+,6+,7+,9+,10+,12+,14+,15+	*,*,*,*,*,*,*,*,*
 P	a	12+,13+,15+	*,*
 S	1	CAAATAAG
 L	1	+	2	+	0M