-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhatshap_stats_plotter.py
More file actions
executable file
·109 lines (89 loc) · 3.85 KB
/
Copy pathwhatshap_stats_plotter.py
File metadata and controls
executable file
·109 lines (89 loc) · 3.85 KB
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
#!/usr/bin/env python3
import argparse
import glob
import sys
import numpy as np
import matplotlib.pyplot as plt
import pysam
import os
import math
FILE_NAME = "file_name"
BLOCK_N50 = "block_n50"
ALL_SWITCH_RATE = "all_switch_rate"
ALL_HAMMING_RATE = "blockwise_hamming_rate"
def parse_args(args = None):
parser = argparse.ArgumentParser("Plots information from tpesout's whatshap wdl workflow")
parser.add_argument('--input', '-i', dest='input', default=None, required=True, type=str,
help='TSV file from whatshap wdl workflow with stats and pairwise info')
parser.add_argument('--figure_name', '-f', dest='figure_name', default=None, required=False, type=str,
help='Figure name (will save if set)')
parser.add_argument('--figure_name_input', '-F', dest='figure_name_input', default=False, required=False, action='store_true',
help='Figure name should be based off of input file (-f overrides this)')
return parser.parse_args() if args is None else parser.parse_args(args)
def plot_two_stats(stats, key_x_fn, key_y_fn, label_x=None, label_y=None, figure_name=None):
for filename in stats.keys():
file_stats = stats[filename]
# get marker
if "UL" in filename:
marker = '*'
elif "75x" in filename:
marker = 'p'
elif "50x" in filename:
marker = 's'
elif "25x" in filename:
marker = '^'
else:
marker = "o"
if "whatshap_phased" in filename:
color="blue"
elif "margin_phased" in filename:
color="red"
else:
color="grey"
label = filename.replace("HG001_", "")\
.replace("_2_GRCh37_pepper_margin_deepvariant.", " ")\
.replace("_guppy422_GRCh37_pepper_margin_deepvariant.", " ")\
.replace("_phased.vcf", "")
# print("{}: {}".format(label_x, list(key_x_fn(file_stats))))
# print("{}: {}".format(label_y, list(key_y_fn(file_stats))))
plt.scatter(key_x_fn(file_stats), key_y_fn(file_stats), marker=marker, color=color, label=label, alpha=.3)
plt.xlim(0, max(map(key_x_fn, stats.values())) * 1.1)
plt.ylim(0, max(map(key_y_fn, stats.values())) * 1.1)
if label_x is not None: plt.xlabel(label_x)
if label_y is not None: plt.ylabel(label_y)
plt.ticklabel_format(style='plain')
plt.legend()
plt.tight_layout()
if figure_name is not None:
if not figure_name.endswith(".png"):
figure_name += ".png"
plt.savefig(figure_name)
plt.show()
plt.close()
def main():
args = parse_args()
raw_list_info = list()
stats_dict = dict()
header = None
with open(args.input) as f:
for line in f:
if header is None:
header = line.strip().lstrip("#").split("\t")
continue
if len(line.strip()) == 0: continue
parts = line.strip().split("\t")
assert(len(parts) == len(header))
sample_stats = dict()
for i, key in enumerate(header):
sample_stats[key] = parts[i]
stats_dict[sample_stats[FILE_NAME]] = sample_stats
print("Got {} records from {}".format(len(stats_dict), args.input))
fig_name = args.figure_name
if fig_name is None and args.figure_name_input:
fig_name = ".".join(os.path.basename(args.input).split(".")[:-1])
plot_two_stats(stats_dict, lambda x: int(x[BLOCK_N50]), lambda x: float(x[ALL_SWITCH_RATE]), "N50", "Switch Rate",
figure_name=None if fig_name is None else fig_name+".switch_rate")
plot_two_stats(stats_dict, lambda x: int(x[BLOCK_N50]), lambda x: float(x[ALL_HAMMING_RATE]), "N50", "Hamming Rate",
figure_name=None if fig_name is None else fig_name+".hamming_rate")
if __name__ == "__main__":
main()