diff --git a/gfa.py b/gfa.py index f240df0..c5dfe36 100644 --- a/gfa.py +++ b/gfa.py @@ -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] @@ -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) diff --git a/test.py b/test.py index 1598ed6..0386258 100644 --- a/test.py +++ b/test.py @@ -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() diff --git a/test/test.gfa b/test/test.gfa new file mode 100644 index 0000000..c6f1c9f --- /dev/null +++ b/test/test.gfa @@ -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