-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_pb_stats.py
executable file
·183 lines (139 loc) · 5.68 KB
/
get_pb_stats.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
"""
Usage: ./get_pb_stats.py --sample CHM1 --cohort pop --prefix /path/to/LRA
Author: Mei Wu, https://github.com/projectoriented
This is a script tuned for CCS/HiFi or Revio generated .fastq.gz files.
"""
import os
import glob
import sys
import logging
import argparse
import pandas as pd
import numpy as np
LOG = logging.getLogger()
logging.basicConfig(stream=sys.stdout, level="INFO", format='%(asctime)s - %(levelname)s - %(message)s')
def get_parser():
"""Get options"""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__
)
required = parser.add_argument_group('required')
required.add_argument('--sample', required=True, help='Sample')
required.add_argument('--cohort', required=True, help='Cohort, e.g. clinical, pop')
required.add_argument('--prefix', required=True, help='Common directory path of all cohorts in the LRA')
return parser
def main():
parser = get_parser()
args = parser.parse_args()
sample = args.sample
cohort = args.cohort
prefix = args.prefix
pb_obj = FindPB(prefix=prefix, sample=sample, cohort=cohort)
pb_obj.apply_regex()
pb_obj.make_directory()
outpath = pb_obj.outpath
final_df = pd.DataFrame()
for idx, row in pb_obj.df.iterrows():
final_df = pd.concat(
[
final_df,
CalculateStats(filepath=[row.filepath], cell_name=row.filename).stats
]
)
# Add a total row
fastqs = pb_obj.df.filepath.tolist()
total_df = CalculateStats(filepath=fastqs, cell_name="total").stats
final_df = pd.concat([final_df, total_df])
del pb_obj
final_df.to_csv(outpath, sep='\t', index=False, header=True)
LOG.info(f"Wrote: {outpath}")
class CalculateStats:
def __init__(self, filepath: list, cell_name: str):
self.filepath = filepath
self.cell_name = cell_name
self.genome = 3.1
@staticmethod
def get_n50(vals):
"""copy/pasta: https://github.com/EichlerLab/ccs_stats/blob/master/rules/ccs_stats.snakefile"""
vals = vals.sort_values(ascending=False)
vals_csum = np.cumsum(vals)
return vals.iloc[np.sum(vals_csum <= (vals_csum.iloc[-1] // 2))] / 1000
@property
def df(self):
working_df = pd.concat(
[
pd.read_csv(file + '.fai', sep='\t', header=None, usecols=[0, 1]) for file in self.filepath
]
)
return working_df
@property
def stats(self) -> pd.DataFrame:
working_df = self.df
len_list = pd.Series(working_df[1].copy())
len_list.sort_values(ascending=False, inplace=True)
len_list_k = {
"15k": pd.Series(working_df.loc[working_df[1] >= 15000][1].copy()),
"18k": pd.Series(working_df.loc[working_df[1] >= 18000][1].copy())
}
coverage = np.sum(len_list) / (self.genome * 1000000000)
coverage_k = {
"15k": np.sum(len_list_k["15k"]) / (self.genome * 1000000000),
"18k": np.sum(len_list_k["18k"]) / (self.genome * 1000000000),
}
return pd.DataFrame(data={
'CELL': [self.cell_name],
'COVERAGE_X': ["{:.2f}".format(coverage)],
'COVERAGE_X_15_Kbp': ["{:.2f}".format(coverage_k["15k"])],
'COVERAGE_X_18_Kbp': ["{:.2f}".format(coverage_k["18k"])],
'READS': [len(len_list)],
'N50_Kbp': ["{:.2f}".format(self.get_n50(len_list))],
'FILES': ','.join(self.filepath)
})
class FindPB:
regex = r'(?P<common_dir>.*PacBio_HiFi)/(?P<filename>.*fastq.gz)'
def __init__(self, prefix, sample, cohort):
self.prefix = prefix
self.sample = sample
self.cohort = cohort
self.glob_list = glob.glob(
f"{os.path.join(prefix, cohort, sample)}/raw_data/PacBio_HiFi/*.fastq.gz")
self.df = pd.DataFrame(data=self.glob_list, columns=["filepath"])
@property
def unique_indicies(self):
return self.df.index.unique().tolist()
def apply_regex(self):
# Check if the DF is empty.
if self.df.empty:
raise OSError(f"Cannot find any PacBio files for {os.path.join(self.prefix, self.cohort, self.sample)}")
regex_df = self.df.filepath.str.extract(self.__class__.regex, expand=True)
self.df = pd.concat([self.df, regex_df], axis=1)
self.df["directory_make"] = self.df.apply(lambda row: os.path.join(row.common_dir, "quick_stats"), axis=1)
self.df["n50_filename"] = self.df.apply(lambda row: f"n50_{self.sample}.tsv", axis=1)
# re-do the filename
self.df["filename"] = self.df["filename"].apply(lambda x: x.split(".")[0])
self.df.set_index("n50_filename", inplace=True)
return self.df
def make_directory(self):
list_of_dirs = self.df.directory_make.unique().tolist()
for v in list_of_dirs:
# Check if we can write in the directory
one_level_up = os.path.dirname(v)
if not os.access(one_level_up, os.W_OK):
raise RuntimeError(f"Cannot write in {v}")
if not os.path.exists(v):
LOG.info(f"Making {v} directory")
os.makedirs(v)
os.chmod(v, 0o775)
else:
LOG.info(f"{v} exists, skipping.")
@property
def outpath(self):
if len(self.unique_indicies) > 1:
raise OSError(f"There are multiple output names: {self.unique_indicies}")
n50_filename = self.unique_indicies.pop()
n50_abs_path = os.path.join(self.df.directory_make[0], n50_filename)
return n50_abs_path
if __name__ == "__main__":
sys.exit(main())