promb 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- promb/__init__.py +7 -0
- promb/__version__.py +1 -0
- promb/cli.py +218 -0
- promb/db.py +504 -0
- promb/formatting.py +59 -0
- promb/netmhc.py +139 -0
- promb/parsing.py +111 -0
- promb/resources/OASis_9mers_v1_10perc_subjects.txt.gz +0 -0
- promb/resources/uniprot_sprot_human.fasta.gz +0 -0
- promb/resources/uniprotkb_proteome_UP000005640_2025_04_21.fasta.gz +0 -0
- promb-1.0.0.dist-info/METADATA +208 -0
- promb-1.0.0.dist-info/RECORD +17 -0
- promb-1.0.0.dist-info/WHEEL +5 -0
- promb-1.0.0.dist-info/entry_points.txt +2 -0
- promb-1.0.0.dist-info/licenses/LICENSE +21 -0
- promb-1.0.0.dist-info/licenses/LICENSES_THIRD_PARTY +12 -0
- promb-1.0.0.dist-info/top_level.txt +1 -0
promb/__init__.py
ADDED
promb/__version__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '1.0.0'
|
promb/cli.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import argparse
|
|
3
|
+
import sys
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
from promb import init_db
|
|
7
|
+
from tqdm import tqdm
|
|
8
|
+
|
|
9
|
+
from promb.netmhc import predict_netmhc_top_ranks
|
|
10
|
+
from promb.parsing import iterate_sequences
|
|
11
|
+
|
|
12
|
+
def run_promb(
|
|
13
|
+
db,
|
|
14
|
+
input_paths,
|
|
15
|
+
netmhc_alleles: list[str] = None,
|
|
16
|
+
netmhc_peptide_lengths=None,
|
|
17
|
+
mode='mutations',
|
|
18
|
+
chains: str = None,
|
|
19
|
+
):
|
|
20
|
+
if netmhc_peptide_lengths is None:
|
|
21
|
+
netmhc_peptide_lengths = [12, 13, 14, 15, 16, 17]
|
|
22
|
+
tuples = iterate_sequences(input_paths, chains=chains)
|
|
23
|
+
skipped_ids = []
|
|
24
|
+
dfs = []
|
|
25
|
+
for filename, sequence_id, sequence in tqdm(tuples):
|
|
26
|
+
if len(sequence) < db.peptide_length:
|
|
27
|
+
skipped_ids.append(sequence_id)
|
|
28
|
+
continue
|
|
29
|
+
peptides = db.chop_seq_peptides(sequence)
|
|
30
|
+
df = pd.DataFrame([{
|
|
31
|
+
'File': filename,
|
|
32
|
+
'ID': sequence_id,
|
|
33
|
+
'Position': position,
|
|
34
|
+
'Peptide': peptide,
|
|
35
|
+
} for position, peptide in enumerate(peptides, start=1)])
|
|
36
|
+
if mode == 'nearest':
|
|
37
|
+
nearest = db.find_nearest_peptides(peptides, n=1)
|
|
38
|
+
df['Nearest'] = [n[0] for n in nearest]
|
|
39
|
+
df['Mutations'] = [sum(aa != bb for aa, bb in zip(peptide, n[0])) for peptide, n in zip(peptides, nearest)]
|
|
40
|
+
elif mode == 'exact':
|
|
41
|
+
df['Found'] = [db.contains(peptide) for peptide in peptides]
|
|
42
|
+
elif mode == 'content':
|
|
43
|
+
df['Content'] = [db.contains(peptide) for peptide in peptides]
|
|
44
|
+
df = df.groupby(['File', 'ID'], as_index=False).agg({'Content': 'mean'})
|
|
45
|
+
print(df)
|
|
46
|
+
else:
|
|
47
|
+
raise ValueError(f'Unknown mode: {mode}')
|
|
48
|
+
if netmhc_alleles:
|
|
49
|
+
assert db.peptide_length == 9, 'DB needs to contain 9-mers, current implementation matches peptides to the predicted netMHCpan 9-mer core'
|
|
50
|
+
core_ranks, core_binders = predict_netmhc_top_ranks(sequence, k=db.peptide_length, alleles=netmhc_alleles,
|
|
51
|
+
peptide_lengths=netmhc_peptide_lengths)
|
|
52
|
+
df['Top_Rank'] = core_ranks.min(axis=1).values
|
|
53
|
+
assert (df['Peptide'] == core_ranks.index).all(), 'Expected the same order of peptides in df and core_ranks'
|
|
54
|
+
df = pd.concat([
|
|
55
|
+
df.reset_index(drop=True),
|
|
56
|
+
core_ranks.reset_index(drop=True),
|
|
57
|
+
core_binders.reset_index(drop=True)
|
|
58
|
+
], axis=1)
|
|
59
|
+
dfs.append(df)
|
|
60
|
+
if not dfs:
|
|
61
|
+
raise ValueError(f'No input sequences found in: {input_paths}')
|
|
62
|
+
result = pd.concat(dfs)
|
|
63
|
+
return result, skipped_ids
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def run_and_save(db_path: str, peptide_length=None, output_path=None, **kwargs):
|
|
67
|
+
db = init_db(db_path, peptide_length=peptide_length)
|
|
68
|
+
result, skipped_ids = run_promb(db=db, **kwargs)
|
|
69
|
+
|
|
70
|
+
if output_path and output_path != '-':
|
|
71
|
+
if output_path.endswith(('.csv', '.csv.gz')):
|
|
72
|
+
result.to_csv(output_path, index=False)
|
|
73
|
+
elif output_path.endswith('.xlsx'):
|
|
74
|
+
with pd.ExcelWriter(output_path, engine='xlsxwriter') as writer:
|
|
75
|
+
result.to_excel(writer, sheet_name='Peptides', index=False)
|
|
76
|
+
sheet = writer.sheets['Peptides']
|
|
77
|
+
header = result.columns.tolist()
|
|
78
|
+
id_col = header.index('ID')
|
|
79
|
+
sheet.set_column(id_col, id_col, 20)
|
|
80
|
+
peptide_col = header.index('Peptide')
|
|
81
|
+
sheet.set_column(peptide_col, peptide_col, db.peptide_length + 2)
|
|
82
|
+
# format Mutations from white (0) to red (3)
|
|
83
|
+
if 'Mutations' in header:
|
|
84
|
+
mutations_col = header.index('Mutations')
|
|
85
|
+
sheet.conditional_format(1, mutations_col, len(result), mutations_col, {
|
|
86
|
+
'type': '2_color_scale',
|
|
87
|
+
'min_type': 'num',
|
|
88
|
+
'min_value': 0,
|
|
89
|
+
'max_type': 'num',
|
|
90
|
+
'max_value': 4,
|
|
91
|
+
'min_color': '#FFFFFF',
|
|
92
|
+
'max_color': '#ff7b91'
|
|
93
|
+
})
|
|
94
|
+
# format Rank columns from Dark purple (1.0) to purple (5.0) to white (20.0)
|
|
95
|
+
if 'Top_Rank' in header:
|
|
96
|
+
rank_from = header.index('Top_Rank')
|
|
97
|
+
rank_to = [(i, col) for i, col in enumerate(header) if col.endswith('_Rank')][-1][0]
|
|
98
|
+
sheet.conditional_format(1, rank_from, len(result), rank_to, {
|
|
99
|
+
'type': '3_color_scale',
|
|
100
|
+
'min_type': 'num',
|
|
101
|
+
'min_value': 1,
|
|
102
|
+
'mid_type': 'num',
|
|
103
|
+
'mid_value': 5,
|
|
104
|
+
'max_type': 'num',
|
|
105
|
+
'max_value': 20,
|
|
106
|
+
'min_color': '#c362ff',
|
|
107
|
+
'mid_color': '#cb98ff',
|
|
108
|
+
'max_color': '#FFFFFF'
|
|
109
|
+
})
|
|
110
|
+
elif output_path == '/dev/null':
|
|
111
|
+
pass
|
|
112
|
+
else:
|
|
113
|
+
raise NotImplementedError(f'Unsupported output format, expected .csv or .xlsx, got: {output_path}')
|
|
114
|
+
else:
|
|
115
|
+
result.to_csv(sys.stdout, index=False)
|
|
116
|
+
|
|
117
|
+
numeric_columns = [c for c in result.select_dtypes(include=['number', 'bool']).columns if c not in ['Position']]
|
|
118
|
+
for column in numeric_columns:
|
|
119
|
+
print(f"{column}: Statistics across sequences", file=sys.stderr)
|
|
120
|
+
print(result.groupby(['File', 'ID'])[column].mean().describe().T, file=sys.stderr)
|
|
121
|
+
|
|
122
|
+
if skipped_ids:
|
|
123
|
+
print(f"NOTE: Skipped {len(skipped_ids)} sequences because they were shorter "
|
|
124
|
+
f"than the peptide length ({db.peptide_length})", file=sys.stderr)
|
|
125
|
+
if output_path:
|
|
126
|
+
print("Saved to:", output_path, file=sys.stderr)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def run_oasis(input_paths, output_path=None, chains=None):
|
|
130
|
+
db = init_db('human-oas')
|
|
131
|
+
tuples = iterate_sequences(input_paths, chains=chains)
|
|
132
|
+
|
|
133
|
+
result = []
|
|
134
|
+
skipped_ids = []
|
|
135
|
+
for filename, sequence_id, sequence in tqdm(tuples):
|
|
136
|
+
result.append({
|
|
137
|
+
'File': filename,
|
|
138
|
+
'ID': sequence_id,
|
|
139
|
+
'Identity': db.compute_peptide_content(sequence),
|
|
140
|
+
})
|
|
141
|
+
result = pd.DataFrame(result)
|
|
142
|
+
|
|
143
|
+
if output_path and output_path != '-':
|
|
144
|
+
if output_path.endswith(('.csv', '.csv.gz')):
|
|
145
|
+
result.to_csv(output_path, index=False)
|
|
146
|
+
elif output_path.endswith('.xlsx'):
|
|
147
|
+
with pd.ExcelWriter(output_path, engine='xlsxwriter') as writer:
|
|
148
|
+
result.to_excel(writer, sheet_name='Scores', index=False)
|
|
149
|
+
sheet = writer.sheets['Scores']
|
|
150
|
+
header = result.columns.tolist()
|
|
151
|
+
id_col = header.index('ID')
|
|
152
|
+
sheet.set_column(id_col, id_col, 20)
|
|
153
|
+
identity_col = header.index('Identity')
|
|
154
|
+
sheet.conditional_format(1, identity_col, len(result), identity_col, {
|
|
155
|
+
'type': '2_color_scale',
|
|
156
|
+
'min_type': 'num',
|
|
157
|
+
'min_value': 0,
|
|
158
|
+
'max_type': 'num',
|
|
159
|
+
'max_value': 1,
|
|
160
|
+
'min_color': '#ff7b91',
|
|
161
|
+
'max_color': '#FFFFFF'
|
|
162
|
+
})
|
|
163
|
+
elif output_path == '/dev/null':
|
|
164
|
+
pass
|
|
165
|
+
else:
|
|
166
|
+
raise NotImplementedError(f'Unsupported output format, expected .csv or .xlsx, got: {output_path}')
|
|
167
|
+
else:
|
|
168
|
+
result.to_csv(sys.stdout, index=False)
|
|
169
|
+
|
|
170
|
+
if skipped_ids:
|
|
171
|
+
print(f"NOTE: Skipped {len(skipped_ids)} sequences because they were shorter "
|
|
172
|
+
f"than the peptide length ({db.peptide_length})", file=sys.stderr)
|
|
173
|
+
if output_path:
|
|
174
|
+
print("Saved to:", output_path, file=sys.stderr)
|
|
175
|
+
|
|
176
|
+
def main(args=None):
|
|
177
|
+
parser = argparse.ArgumentParser()
|
|
178
|
+
subparsers = parser.add_subparsers(dest='command')
|
|
179
|
+
|
|
180
|
+
nearest_parser = subparsers.add_parser('nearest')
|
|
181
|
+
exact_parser = subparsers.add_parser('exact')
|
|
182
|
+
oasis_parser = subparsers.add_parser('oasis')
|
|
183
|
+
|
|
184
|
+
for p in [nearest_parser, exact_parser, oasis_parser]:
|
|
185
|
+
p.add_argument('input', nargs='+', help='Input file(s) - FASTA, .txt/.txt.gz (with one peptide per line), .pdb file, or a directory of such files')
|
|
186
|
+
p.add_argument('-c', '--chains', help='Chains to parse when processing PDB files (comma-separated)')
|
|
187
|
+
p.add_argument('-o', '--output', help='Output CSV file, will print to stdout if not provided')
|
|
188
|
+
|
|
189
|
+
for p in [nearest_parser, exact_parser]:
|
|
190
|
+
p.add_argument('-d', '--db', required=True, help='Input database name (oas-human, oas-swissprot) or path to .txt.gz file (one peptide per line, same length) or .fasta.gz file (whole sequences in fasta format)')
|
|
191
|
+
p.add_argument('-l', '--peptide-length', type=int, help='Length of peptide (when using oas-swissprot or other fasta database)')
|
|
192
|
+
p.add_argument('-a', '--netmhc-alleles', help='Run netMHCIIpan for given alleles (comma-separated, for example DRB1_0101,DRB1_0102). '
|
|
193
|
+
'requires netMHCIIpan license and netMHCIIpan to be installed and on PATH')
|
|
194
|
+
|
|
195
|
+
args = parser.parse_args(args)
|
|
196
|
+
|
|
197
|
+
if args.command is None:
|
|
198
|
+
parser.print_help()
|
|
199
|
+
if args.command == 'oasis':
|
|
200
|
+
run_oasis(
|
|
201
|
+
input_paths=args.input,
|
|
202
|
+
chains=args.chains,
|
|
203
|
+
output_path=args.output,
|
|
204
|
+
)
|
|
205
|
+
else:
|
|
206
|
+
run_and_save(
|
|
207
|
+
db_path=args.db,
|
|
208
|
+
input_paths=args.input,
|
|
209
|
+
peptide_length=args.peptide_length,
|
|
210
|
+
chains=args.chains,
|
|
211
|
+
netmhc_alleles=args.netmhc_alleles.split(',') if args.netmhc_alleles else None,
|
|
212
|
+
mode=args.command,
|
|
213
|
+
output_path=args.output,
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
if __name__ == '__main__':
|
|
218
|
+
main()
|