-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
158 lines (136 loc) · 4.66 KB
/
Snakefile
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import pathlib
import os
import shutil
import dill
import pandas as pd
from prairiedog.profiler import Profiler
from prairiedog.kmers import Kmers
from prairiedog.networkx_graph import NetworkXGraph
from prairiedog.graph_ref import GraphRef
from prairiedog.subgraph_ref import SubgraphRef
from prairiedog.lemon_graph import LGGraph, DB_PATH
from prairiedog.dgraph import DgraphBulk, port
from prairiedog.dgraph_bundled_helper import DgraphBundledHelper
from dgraph.bulk import run_dgraph_bulk
configfile: "config.yaml"
K = config["k"]
INPUTS = [os.path.splitext(f)[0] for f in os.listdir(config["samples_dir"])
if f.endswith(('.fna', '.fasta', '.fa'))
]
MIC_CSV = config["graph_labels"]
if os.path.isfile(MIC_CSV):
MIC_COLUMNS = set(pd.read_csv(MIC_CSV).columns)
MIC_COLUMNS.remove('run')
samples_dir = config['samples_dir']
outputs_dir = config['outputs_dir']
print("Snakemake will run with samples dir {} and output dir {}".format(
samples_dir, outputs_dir
))
###################
# Graphing steps
###################
rule all:
input:
os.path.join(outputs_dir, 'pangenome.g')
rule kmers:
input:
os.path.join(samples_dir, '{sample}.fasta')
output:
os.path.join(outputs_dir, 'kmers/{sample}.pkl')
run:
pathlib.Path(outputs_dir, 'kmers').mkdir(parents=True, exist_ok=True)
km = Kmers(input[0],K)
dill.dump(km, open(output[0],'wb'))
rule pangenome:
input:
os.path.join(outputs_dir, 'kmers/{input}.pkl')
output:
os.path.join(outputs_dir, 'pangenome_{input}.g')
run:
# Setup graph backend
gr = GraphRef(MIC_CSV)
if config['backend'] == 'networkx':
print("Using NetworkX as graph backend")
sg = SubgraphRef(NetworkXGraph())
elif config['backend'] == 'lemongraph':
print("Using LemonGraph as graph backend")
sg = SubgraphRef(LGGraph())
elif config['backend'] == 'dgraph':
print("Using Dgraph as graph backend")
sg = SubgraphRef(DgraphBulk())
else:
raise Exception("No graph backend found")
# Setup pyinstrument profiler
if config['pyinstrument'] is True:
print("Setting up pyinstrument profiler...")
profiler = Profiler()
profiler.start()
else:
profiler = None
# Main graphing step
km = dill.load(open(input[0],'rb'))
gr.index_kmers(km)
sg.update_graph(km, gr)
if config['backend'] in ('lemongraph', 'dgraph'):
sg.save(output[0])
# It seems the km object is being kept in memory for too long
del km
# Stop pyinstrument profiler
if config['pyinstrument'] is True:
profiler.stop()
print(profiler.output_text(unicode=True, color=True))
print("rule 'pangenome' found max_num_nodes to be {}".format(
gr.max_num_nodes))
dill.dump(
gr,
open(
os.path.join(outputs_dir, 'graphref.pkl'),'wb'),
protocol=4)
if config['backend'] == 'lemongraph':
shutil.copy2(DB_PATH, output[0])
elif config['backend'] == 'dgraph':
open(output[0], 'w').close()
else:
sg.save(output[0])
rule done:
input:
expand(
os.path.join(outputs_dir, 'pangenome_{input}.g'),
input=INPUTS)
output:
os.path.join(outputs_dir, 'pangenome.g')
run:
open(output[0], 'w').close()
###########
# Dgraph specific rules
###########
rule preload:
output:
os.path.join(outputs_dir, 'samples/kmers.rdf')
run:
dg = DgraphBulk()
print("Trying to create rdf for all possible k-mers...")
dg.preload(K)
print("Done creating rdf for all possible k-mers.")
print("Trying to save rdf to file {}".format(output[0]))
dg.save(output[0])
print("Saved rdf as {}".format(output[0]))
rule dgraph:
input:
os.path.join(outputs_dir, 'pangenome.g'),
os.path.join(outputs_dir, 'samples/kmers.rdf')
output:
os.path.join(outputs_dir, 'dgraph.done')
run:
dgraph_output = pathlib.Path(outputs_dir, 'dgraph/').resolve()
rdfs = pathlib.Path(outputs_dir, 'samples/').resolve()
# Create a reference to a running Dgraph instance
# DgraphBundledHelper will resolve output directory to ./dgraph/
dgh = DgraphBundledHelper(out_dir=outputs_dir)
# Execute dgraph bulk
dgh.load(rdf_dir=rdfs, delete_after=False)
# Create the done file
open(output[0], 'w').close()
rule clean:
shell:
"rm -rf {}".format(outputs_dir)