Skip to content

Commit

Permalink
graph-genome#4 Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Toshiyuki Yokoyama committed Jun 19, 2019
1 parent 9c9a3aa commit bbb66c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
29 changes: 12 additions & 17 deletions gfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
import pickle
import subprocess
import io
import os
import tempfile

class GFA:
def __init__(self, gfa: gfapy.Gfa):
self.gfa = gfa

@classmethod
def load_from_pickle(cls, file: str):
#with open(file, 'rb') as pickle_file:
gfa = pickle.load(open(file, 'rb'))
graph = cls(gfa)
return graph
# @classmethod
# def load_from_pickle(cls, file: str):
# graph = pickle.load(open(file, 'rb'))
# return graph

@classmethod
def load_form_xg(cls, file: str, xg_bin: str):
#self.gfa = XGWrapper.load(file, xg_bin)
gfa = gfapy.Gfa()
# = subprocess.check_output([xg_bin, "-i", file, "--gfa-out"])
process = subprocess.Popen([xg_bin, "-i", file, "--gfa-out"], stdout=subprocess.PIPE)
with io.open(process.stdout.fileno(), closefd=False) as stream:
[gfa.add_line(line) for line in stream]
Expand All @@ -36,18 +33,16 @@ def load_from_gfa(cls, file: str):
graph = cls(gfa)
return graph

def save_as_pickle(self, outfile: str):
with open(outfile, 'wb') as pickle_file:
pickle.dump(self.gfa, pickle_file)
# pickle.dump(self.gfa, file)
# def save_as_pickle(self, outfile: str):
# with open(outfile, 'wb') as pickle_file:
# pickle.dump(self.gfa, pickle_file, protocol=2)

def save_as_xg(self, file: str, xg_bin:str):
with tempfile.NamedTemporaryFile(mode="w+") as f:
with tempfile.NamedTemporaryFile(mode="w+t", delete=False) as f:
f.write(self.gfa.to_gfa1_s())
process = subprocess.check_output([xg_bin, "-o", file, "-g", f.name])
#process = subprocess.Popen([xg_bin, "-o", file, "-g", "-"], stdin=subprocess.PIPE)
#output = process.communicate(self.gfa.to_gfa1_s())
#return output

process = subprocess.check_output([xg_bin, "-o", file, "-g", f.name])
os.remove(f.name)

def save_as_gfa(self, file:str):
self.gfa.to_file(file)
Expand Down
8 changes: 3 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ class GFATest(unittest.TestCase):

def test_gfa(self):
### Usage
self.maxDiff = None
location_of_xg = "test/xg"
graph = GFA.load_from_gfa("test/test.gfa")
graph.save_as_xg("test/test.xg", location_of_xg)
graph2 = GFA.load_form_xg("test/test.xg", location_of_xg)
graph2.save_as_pickle("test/test.pickle")
graph3 = GFA.load_from_pickle("test/test.pickle")
self.assertEqual(graph.gfa.to_gfa_1_s, graph3.to_gfa_1_s)


self.assertEqual(len(graph.gfa.to_gfa1_s().split("\n")), len(graph2.gfa.to_gfa1_s().split("\n")))

if __name__ == "__main__":
unittest.main()

Expand Down
37 changes: 37 additions & 0 deletions test/test.gfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
H VN:Z:1.0
P x 1+,3+,5+,6+,8+,9+,11+,12+,14+,15+ *,*,*,*,*,*,*,*,*
S 1 CAAATAAG
L 1 + 2 + 0M
L 1 + 3 + 0M
S 2 A
L 2 + 4 + 0M
L 2 + 5 + 0M
S 3 G
L 3 + 4 + 0M
L 3 + 5 + 0M
S 4 T
L 4 + 6 + 0M
S 5 C
L 5 + 6 + 0M
S 6 TTG
L 6 + 7 + 0M
L 6 + 8 + 0M
S 7 A
L 7 + 9 + 0M
S 8 G
L 8 + 9 + 0M
S 9 AAATTTTCTGGAGTTCTAT
L 9 + 10 + 0M
L 9 + 11 + 0M
S 10 A
L 10 + 12 + 0M
S 11 T
L 11 + 12 + 0M
S 12 ATAT
L 12 + 13 + 0M
L 12 + 14 + 0M
S 13 A
L 13 + 15 + 0M
S 14 T
L 14 + 15 + 0M
S 15 CCAACTCTCTG

0 comments on commit bbb66c7

Please sign in to comment.