-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_ont_stats.py
More file actions
executable file
·201 lines (157 loc) · 6.61 KB
/
get_ont_stats.py
File metadata and controls
executable file
·201 lines (157 loc) · 6.61 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
"""
Usage: ./get_ont_stats.py --sample CHM1 --cohort pop --prefix /path/to/LRA
Author: Mei Wu, https://github.com/projectoriented
Modified: Youngjun Kwon
"""
import os
import glob
import sys
import logging
import argparse
from collections import defaultdict
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
ont_obj = FindONT(prefix=prefix, sample=sample, cohort=cohort)
ont_obj.apply_regex()
ont_obj.make_directory()
fastq_dict = ont_obj.get_fastqs()
for k in fastq_dict.keys():
final_df = pd.DataFrame()
working_df = fastq_dict[k]
if isinstance(working_df, pd.Series):
working_df = working_df.to_frame().T
for idx, row in working_df.iterrows():
final_df = pd.concat(
[
final_df,
CalculateStats(filepath=[row.filepath], cell_name=row.run_id).stats
]
)
# Add a total row
fastqs = working_df.filepath.tolist()
total_df = CalculateStats(filepath=fastqs, cell_name="total").stats
final_df = pd.concat([final_df, total_df])
final_df.to_csv(k, sep='\t', index=False, header=True)
LOG.info(f"Wrote: {k}")
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/compteam_tools/blob/main/ont_stats"""
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:
"""copy/pasta https://github.com/EichlerLab/compteam_tools/blob/main/ont_stats"""
working_df = self.df
len_list = pd.Series(working_df[1].copy())
len_list.sort_values(ascending=False, inplace=True)
len_list_k = pd.Series(working_df.loc[working_df[1] >= 100000][1].copy())
coverage = np.sum(len_list) / (self.genome * 1000000000)
coverage_k = np.sum(len_list_k) / (self.genome * 1000000000)
return pd.DataFrame(data={
'CELL': [self.cell_name],
'COVERAGE_X': ["{:.2f}".format(coverage)],
'COVERAGE_X_100_Kbp': ["{:.2f}".format(coverage_k)],
'READS': [len(len_list)],
'N50_Kbp': ["{:.2f}".format(self.get_n50(len_list))],
'FILES': ','.join(self.filepath)
})
class FindONT:
regex = r'(?P<common_dir>.*nanopore)/(?P<library>[A-Za-z]+)/fastq/(?P<run_id>.+)/(?P<basecaller>.+)/(?P<version>\d+.\d+.\d+)/(?P<model>.+)/(?P<filename>.*_pass.*fastq.gz)'
def __init__(self, prefix, sample, cohort):
self.prefix = prefix
self.sample = sample
self.cohort = cohort
# added a condition that fastq have filtered and has .filt. in the name.
glob_list_tmp = glob.glob(
f"{os.path.join(prefix, cohort, sample)}/raw_data/nanopore/*/fastq/*/*/*/*/*_pass*fastq.gz")
groups = {}
for file in glob_list_tmp:
if "HERRO" in file:
continue
run_id = file.split("raw_data/nanopore")[1].split("/")[3]
groups.setdefault(run_id, {})
if file.endswith("pass.filt.fastq.gz"):
groups[run_id]["filt"] = file
else:
groups[run_id]["raw"] = file
glob_list = []
for run_id in groups.keys():
try:
glob_list.append(groups[run_id]["filt"])
except KeyError:
glob_list.append(groups[run_id]["raw"])
self.df = pd.DataFrame(data=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 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)
# Just take the major version number
self.df["version"] = self.df["version"].str.split(".", n=1, expand=True)[0]
self.df["directory_make"] = self.df.apply(
lambda row: os.path.join(row.common_dir, row.library, "quick_stats"),
axis=1)
self.df["n50_filename"] = self.df.apply(lambda row: f"n50_{row.basecaller}_{row.model}_v{row.version}.txt",
axis=1)
self.df.set_index(["version", "model", "library", "directory_make", "n50_filename"], inplace=True, drop=False)
self.df.sort_index(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.")
def get_fastqs(self):
fastq_dict = {}
for v in self.unique_indicies:
dict_key = os.path.join(v[3], v[-1]) # directory_make + n50_filename
fastq_dict[dict_key] = self.df.loc[v, ["filepath", "run_id"]]
return fastq_dict
if __name__ == "__main__":
sys.exit(main())