forked from graph-genome/graph_summarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfa.py
68 lines (54 loc) · 1.65 KB
/
gfa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import List, NamedTuple
import gfapy
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):
# graph = pickle.load(open(file, 'rb'))
# return graph
@classmethod
def load_form_xg(cls, file: str, xg_bin: str):
gfa = gfapy.Gfa()
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]
process.wait()
if process.returncode != 0:
raise OSError()
graph = cls(gfa)
return graph
@classmethod
def load_from_gfa(cls, file: str):
gfa = gfapy.Gfa.from_file(file)
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, protocol=2)
def save_as_xg(self, file: str, xg_bin:str):
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])
os.remove(f.name)
def save_as_gfa(self, file:str):
self.gfa.to_file(file)
'''
class XGWrapper:
@staticmethod
def save(gfa):
pass
@staticmethod
def load(gfa):
pass
class GraphStack:
def __init__(graphs: List[Graph]):
self.graphs = graphs
'''
if __name__ == "__main__":
location_of_xg = sys.argv[0]