mhcflow 0.2.0__tar.gz → 0.2.2__tar.gz

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.
mhcflow-0.2.2/PKG-INFO ADDED
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.3
2
+ Name: mhcflow
3
+ Version: 0.2.2
4
+ Summary: MHC Class I and II workflow with fisher, realigner and typer.
5
+ Project-URL: Source, https://github.com/svm-zhang/mhcflow
6
+ Project-URL: Documentation, https://svm-zhang.github.io/mhcflow/
7
+ Project-URL: Issues, https://github.com/svm-zhang/mhcflow/issues
8
+ Author-email: simo <svm.zhang@gmail.com>
9
+ Maintainer-email: Simo Zhang <svm.zhang@gmail.com>
10
+ License: MIT
11
+ Keywords: bioinformatics, genomics, sequencing, HLA typing
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
22
+ Classifier: Topic :: Software Development :: Libraries
23
+ Requires-Python: >=3.10
24
+ Requires-Dist: mhctyper>=0.1.7
25
+ Requires-Dist: pyahocorasick>=2.1.0
26
+ Requires-Dist: pyfaidx>=0.8.1.3
27
+ Requires-Dist: tinyscibio>=0.4.3
28
+ Description-Content-Type: text/markdown
29
+
30
+ # mhcflow
31
+
32
+ [![PyPI version](https://img.shields.io/pypi/v/mhcflow)](https://pypi.org/project/mhcflow/)
33
+ ![Python versions](https://img.shields.io/pypi/pyversions/mhcflow)
34
+ [![PyPI Downloads](https://img.shields.io/pypi/dm/mhcflow)](https://pypistats.org/packages/mhcflow)
35
+ ![License](https://img.shields.io/pypi/l/mhcflow)
36
+
37
+ MHC class I and II typing workflow including fishing, realigning, and typing that
38
+ generates results ready for detection of HLA loss of heterozygosity (LOH) and
39
+ peptide binding prediction.
40
+
41
+ ## Installation
42
+
43
+ Starting from `v0.2.0`, `mhcflow` can be installed from PyPI:
44
+
45
+ ```bash
46
+ pip install mhcflow
47
+ ```
48
+
49
+ If you prefer to use the shell script implementation, please check out the
50
+ `v0.1.0` branch of this repo. For details on how to use the `v0.1.0` version,
51
+ please refer to the [documentation](https://svm-zhang.github.io/mhcflow).
52
+
53
+ ## Quick start
54
+
55
+ Please refer to [documentation](https://svm-zhang.github.io/mhcflow) for more details.
@@ -0,0 +1,26 @@
1
+ # mhcflow
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/mhcflow)](https://pypi.org/project/mhcflow/)
4
+ ![Python versions](https://img.shields.io/pypi/pyversions/mhcflow)
5
+ [![PyPI Downloads](https://img.shields.io/pypi/dm/mhcflow)](https://pypistats.org/packages/mhcflow)
6
+ ![License](https://img.shields.io/pypi/l/mhcflow)
7
+
8
+ MHC class I and II typing workflow including fishing, realigning, and typing that
9
+ generates results ready for detection of HLA loss of heterozygosity (LOH) and
10
+ peptide binding prediction.
11
+
12
+ ## Installation
13
+
14
+ Starting from `v0.2.0`, `mhcflow` can be installed from PyPI:
15
+
16
+ ```bash
17
+ pip install mhcflow
18
+ ```
19
+
20
+ If you prefer to use the shell script implementation, please check out the
21
+ `v0.1.0` branch of this repo. For details on how to use the `v0.1.0` version,
22
+ please refer to the [documentation](https://svm-zhang.github.io/mhcflow).
23
+
24
+ ## Quick start
25
+
26
+ Please refer to [documentation](https://svm-zhang.github.io/mhcflow) for more details.
@@ -17,5 +17,5 @@ __version__: str
17
17
  __version_tuple__: VERSION_TUPLE
18
18
  version_tuple: VERSION_TUPLE
19
19
 
20
- __version__ = version = '0.2.0'
21
- __version_tuple__ = version_tuple = (0, 2, 0)
20
+ __version__ = version = '0.2.2'
21
+ __version_tuple__ = version_tuple = (0, 2, 2)
@@ -54,6 +54,11 @@ def parse_cmd() -> argparse.ArgumentParser:
54
54
  default=999,
55
55
  help="specify minimum # of mm events (999).",
56
56
  )
57
+ parser.add_argument(
58
+ "--realn-only",
59
+ action="store_true",
60
+ help="specify to use realn-only mode.",
61
+ )
57
62
  parser.add_argument(
58
63
  "--nproc",
59
64
  metavar="INT",
@@ -1,4 +1,5 @@
1
1
  import multiprocessing as mp
2
+ import sys
2
3
  import time
3
4
  from functools import partial
4
5
 
@@ -248,9 +249,19 @@ def _run_fisher(
248
249
  bam_fspath, prebuilt_tag, unplaced_qname_out
249
250
  )
250
251
 
251
- merged_qnames = pl.concat(
252
- [hla_bed_qnames, chr6_qnames, unplaced_qnames]
253
- ).unique()
252
+ # make sure to not concat empty qnames
253
+ # https://github.com/svm-zhang/mhcflow/issues/2
254
+ fished_qnames: list[pl.DataFrame] = [
255
+ qnames
256
+ for qnames in [hla_bed_qnames, chr6_qnames, unplaced_qnames]
257
+ if qnames.shape[0] > 0
258
+ ]
259
+ # if no qname fished, terminate.
260
+ if not fished_qnames:
261
+ logger.info("Zero HLA-related reads fished. Cannot continue.")
262
+ sys.exit(0)
263
+
264
+ merged_qnames = pl.concat(fished_qnames).unique()
254
265
  fisher_idx_out = outdir / f"{sm}.fisher.idx.final.tsv"
255
266
  merged_qnames.write_csv(fisher_idx_out, separator="\t")
256
267
 
@@ -37,6 +37,11 @@ def run_mhcflow() -> int:
37
37
  realigner_fm = _run_realigner(
38
38
  args.bam, args.ref, fisher_fm_json, out_realn_dir, args.nproc
39
39
  )
40
+ if args.realn_only:
41
+ logger.info(
42
+ "Realgnment-only mode specified. Finished running mhcflow."
43
+ )
44
+ return 0
40
45
 
41
46
  realn_bam = realigner_fm.outputs.get("realn_bam", "")
42
47
  assert isinstance(realn_bam, str)
mhcflow-0.2.0/PKG-INFO DELETED
@@ -1,342 +0,0 @@
1
- Metadata-Version: 2.3
2
- Name: mhcflow
3
- Version: 0.2.0
4
- Summary: MHC Class I and II workflow with fisher, realigner and typer.
5
- Project-URL: Source, https://github.com/svm-zhang/mhcflow
6
- Project-URL: Documentation, https://svm-zhang.github.io/mhcflow/
7
- Project-URL: Issues, https://github.com/svm-zhang/mhcflow/issues
8
- Author-email: simo <svm.zhang@gmail.com>
9
- Maintainer-email: Simo Zhang <svm.zhang@gmail.com>
10
- License: MIT
11
- Keywords: bioinformatics, genomics, sequencing, HLA typing
12
- Classifier: Development Status :: 4 - Beta
13
- Classifier: Intended Audience :: Developers
14
- Classifier: License :: OSI Approved :: MIT License
15
- Classifier: Operating System :: OS Independent
16
- Classifier: Programming Language :: Python :: 3 :: Only
17
- Classifier: Programming Language :: Python :: 3.10
18
- Classifier: Programming Language :: Python :: 3.11
19
- Classifier: Programming Language :: Python :: 3.12
20
- Classifier: Programming Language :: Python :: 3.13
21
- Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
22
- Classifier: Topic :: Software Development :: Libraries
23
- Requires-Python: >=3.10
24
- Requires-Dist: mhctyper>=0.1.7
25
- Requires-Dist: pyahocorasick>=2.1.0
26
- Requires-Dist: pyfaidx>=0.8.1.3
27
- Requires-Dist: tinyscibio>=0.4.3
28
- Description-Content-Type: text/markdown
29
-
30
- # mhcflow
31
-
32
- <!-- toc -->
33
-
34
- - [Introduction](#introduction)
35
- - [Features](#features)
36
- - [Installation](#installation)
37
- - [Quick Start](#quick-start)
38
- - [Explain Output](#explain-output)
39
- - [Step by Step](#step-by-step)
40
- * [Fisherman: fishing HLA-relevant reads](#fisherman-fishing-hla-relevant-reads)
41
- * [Realigner: realigning fished reads to HLA reference](#realigner-realigning-fished-reads-to-hla-reference)
42
- * [Typer: typing HLA class I genotype](#typer-typing-hla-class-i-genotype)
43
- - [Realigner: generating analysis-ready HLA typing result](#realigner-generating-analysis-ready-hla-typing-result)
44
- - [Extend to Class II typing](#extend-to-class-ii-typing)
45
- - [Scenario: detecting LOH from paired tumor and normal samples](#scenario-detecting-loh-from-paired-tumor-and-normal-samples)
46
- - [License](#license)
47
- - [Disclaimer](#disclaimer)
48
- - [Citation](#citation)
49
-
50
- <!-- tocstop -->
51
-
52
- ## Introduction
53
-
54
- `mhcflow` is the original
55
- [polysolver](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4747795/) HLA
56
- typing algorithm re-engineered in modern style. It offers almost all aspects
57
- of the original algorithm, adds new features, runs faster, and is friendly to
58
- pipeline integration.
59
-
60
- ## Features
61
-
62
- - Supports both class I and
63
- [II](https://github.com/svm-zhang/mhcflow?tab=readme-ov-file#extend-to-class-ii-typing)
64
- typing with good
65
- [accuracy](https://github.com/svm-zhang/hla_benchmark?tab=readme-ov-file)
66
- - Generates analysis-ready HLA alignments for HLALOH detection
67
- - Re-engineered in modern style with
68
- - Modular design
69
- - Faster runtime with internal optimization
70
- - Minimum I/O operations
71
- - Minimum hard-coded code
72
- - Easy integration to pipeline with packaging and better CLI
73
-
74
- ## Installation
75
-
76
- Please refer to [INSATLL](INSTALL.md) for details.
77
-
78
- ## Quick Start
79
-
80
- `mhcflow` requires
81
-
82
- - Sorted genomic alignment in BAM format with index
83
- - HLA reference sequence in Fasta and Nix (novoalign index)
84
- - BED file with region of each HLA allele
85
- - HLA kmer tags
86
- - HLA 4-digit supertype frequency table
87
-
88
- Let us type class 1 alleles for `NA12046` sample provided by the 1000
89
- genome project (`NA12046` will be used as example throughout this doc):
90
-
91
- ```bash
92
- polysolvermod --bam NA12046.so.bam \
93
- --hla_ref abc_complete.fasta \
94
- --bed class1.bed \
95
- --tag abc_v14.uniq \
96
- --freq HLA_FREQ.txt \
97
- --outdir "$PWD/NA12046_class1 \
98
- --sample NA12046
99
- ```
100
-
101
- The command above generates HLA typing results in the designated output
102
- directory specified by `--outdir` option. A quick peek into the result
103
- folder looks like:
104
-
105
- ```text
106
- -- NA12046_class1
107
- -- finalizer
108
- -- fisher
109
- -- realigner
110
- -- typer
111
- ```
112
-
113
- ## Explain Output
114
-
115
- The `finalizer` folder provides the sample-level HLA reference sequence that
116
- can be directly used as reference for realigning tumor data in a paired tumor
117
- and normal setting. There is also an alignment result in BAM against this
118
- sample-level reference with suffix `hla.realn.ready.bam`. In context of oncology
119
- or immuno-oncology research, the BAM file can be directly ported to program to
120
- detect HLA loss of heterozygosity.
121
-
122
- The typing result can be found within the `typer` folder, with suffix
123
- `hlatyping.res.tsv`. The result table should look like the following:
124
-
125
- ```text
126
- allele gene tot_scores sample
127
- hla_a_01_01_29 hla_a 2452923.4298 NA12046
128
- hla_a_02_05_01 hla_a 1766396.924 NA12046
129
- hla_b_50_01_01 hla_b 1332194.9171 NA12046
130
- hla_b_57_01_08 hla_b 1134814.4428 NA12046
131
- hla_c_06_02_01_01 hla_c 3020505.4303 NA12046
132
- hla_c_06_02_01_02 hla_c 1519636.0349 NA12046
133
- ```
134
-
135
- ## Step by Step
136
-
137
- `mhcflow` is re-engineered with a modular design. It generally consists of
138
- 4 steps: `fishing`, `realigning`, `typing`, and `realigning` (again). Each
139
- module implements basic break-and-continue mechanism, meaning that module
140
- finished previously will be automatically skipped. Also it is more friendly
141
- to integrate with pipeline/workflow.
142
-
143
- The `hlapolysolver.sh` script and `mhcflow` binary (after building the package)
144
- demonstrates each following step, if you are interested.
145
-
146
- ### Fisherman: fishing HLA-relevant reads
147
-
148
- The original `polysolver` algorithm fishes HLA-related reads via matching
149
- pre-built kmer (tag) sequence and extracting alignments mapped to regions where
150
- HLA class I allele located. `mhcflow` follows the same strategy and speeds
151
- it up.
152
-
153
- ```bash
154
- fisher --mode faster \
155
- --tag abc_v14.uniq \
156
- --bed class1.bed \
157
- --bam NA12046.so.bam \
158
- --sample NA12045 \
159
- --out "$PWD/NA12046_class1/fisher/NA12046.fqs.list.txt
160
- ```
161
-
162
- The result is plain text file with two lines of fished fastq files
163
- (paired-end reads).
164
-
165
- It is important to note there are other approches to fish HLA-relevant reads.
166
- For instance, `Optitype` aligns trimmed reads against the HLA reference using
167
- `razerS3`. From my experience, direct alignment finds more reads and these
168
- reads tend to align better. However, `razerS3` is not quite memory-efficient,
169
- which in my opinion limits its utility, especially your computing platform
170
- is not unlimited. The approach that the original `polysolver` uses provides
171
- decent fishing result.
172
-
173
- ### Realigner: realigning fished reads to HLA reference
174
-
175
- Next the realigner module aligns the fished reads against the provided
176
- class I HLA reference sequence using `novoalign`, same as the original
177
- `polysolver` program. The difference is the realigner module achieves the
178
- reaignment process in parallel to speed things up a bit.
179
-
180
- ```bash
181
- realigner --hla_ref abc_complete.fasta \
182
- --fqs "$PWD/NA12046_class1/fisher/NA12046.fqs.list.txt \
183
- --sample NA12046 \
184
- --out "$PWD/NA12046_class1/realigner/NA12046.hla.realn.so.bam
185
- ```
186
-
187
- Because the academia version of `novoalign` does not support gzipped fastq
188
- file, this step can take up some disk space depending on the sample
189
- sequencing depth that is HLA-related.
190
-
191
- ### Typer: typing HLA class I genotype
192
-
193
- The typer module is a complete overhaul of the origial perl scripts
194
- `first_allele_calculations.pl` and `second_allele_calcuations.pl`. The original
195
- `polysolver` algorithm types first and second alleles at a locus in two
196
- separated processes. For each HLA class I allele defined in the HLA reference
197
- sequence, it outputs a plain text file with scores. This generates thousands of
198
- files that creates I/O pressure and make the typing process I/O bound. Also,
199
- it takes about 3-4 script calls to type the first allele and makes it hard to
200
- track when error happens.
201
-
202
- The `pyhlatyper` written in this repo tires to improves on all aspects:
203
-
204
- 1. Typing two alleles with one program call
205
- 2. Making typing CPU-bound powered by `polars` and `pysam`
206
- 3. Processing alignments to calculate scores in parallel
207
- 4. Enabling possibility of typing alleles class II alleles
208
- 5. Capturing errors in proper way
209
- 6. Free of hard-coded code
210
-
211
- ```bash
212
- pyhlatyper --freq HLA_FREQ.txt \
213
- --bam "$PWD/NA12046_class1/realigner/NA12046.hla.realn.so.bam \
214
- --out "$PWD/NA12046_class1/typer/NA12046.hla_typing.res.tsv
215
- ```
216
-
217
- One important difference of `pyhlatyper` from the original typing scripts is
218
- that it does not actaully use frequency as prior to calculate posterior scores.
219
- This means the `--race` is always `Unknown`. The choice was made because the race
220
- is usually not a known factor when dealing with real-world data.
221
- I probably will remove the `--race` option from CLI for good in the future.
222
-
223
- ## Realigner: generating analysis-ready HLA typing result
224
-
225
- The original `polysolver` finishes after typing is done. `mhcflow` goes
226
- beyond by providing
227
-
228
- 1. HLA reference sequence specific to the typed sample
229
- 2. Alignment against the sample HLA reference
230
-
231
- The reason to have this additional step is to get analysis-ready result.
232
- In oncology and/or immuno-oncology research, one of the questions
233
- people has is to know if there is loss of heterozygosity (LOH) occurring in a tumor
234
- sample. [LOHHLA](https://bitbucket.org/mcgranahanlab/lohhla/src/master/) is the
235
- common go-to algorithm to answer the question. However, `lohhla`, before detecting
236
- any LOH event, goes through realigning both normal and tumor samples, despite typing
237
- has been done for the normal sample. Also realignment, in my opinion, belongs to
238
- pipeline. LOH detection algorithm should be simplified to serve what it is designed
239
- for. To have a clearer picture of what I mean, please refer to [tumor and
240
- normal
241
- scenario](https://github.com/svm-zhang/mhcflow?tab=readme-ov-file#scenario-detecting-loh-from-paired-tumor-and-normal-samples)
242
- below.
243
-
244
- The final realignment process splits into two steps.
245
- First to extract and index sample-level HLA reference.
246
-
247
- ```bash
248
- extractor --hla_ref abc_complete.fasta \
249
- --sample NA12046 \
250
- --typeres "$PWD/NA12046_class1/typer/NA12046.hla_typing.res.tsv
251
- --out "$PWD/NA12046_class1/finalizer/NA12046.hla.fasta
252
-
253
- ```
254
-
255
- Then do the realignment against this new reference.
256
-
257
- ```bash
258
- realigner \
259
- --hla_ref "$PWD/NA12046_class1/finalizer/NA12046.hla.fasta
260
- --fqs "$PWD/NA12046_class1/fisher/NA12046.fqs.list.txt \
261
- --sample NA12046 \
262
- --mdup \
263
- --out "$PWD/NA12046_class1/finalizer/NA12046.hla.realn.ready.bam
264
- ```
265
-
266
- The `--mdup` option marks PCR duplicates so that when counting coverage during
267
- LOH detection, duplicated reads do not get included. If you want to keep duplicates,
268
- simply not using this option.
269
-
270
- ## Extend to Class II typing
271
-
272
- The original `polysolver` algorithm has been well-known for genotyping Class
273
- I alleles. However, in theory it should also be able to apply to the Class II
274
- case, with certain modification as well as a set of Class II references.
275
-
276
- To type the Class II alleles, you only need to swap in the new reference
277
- data, and the CLI is the same as we have shown for the Class I case.
278
-
279
- I have done some preliminary benchmark on Class II typing using some samples from
280
- 1000 genome project. The result is suprisingly not too shady and can be found
281
- [here](https://github.com/svm-zhang/hla_benchmark).
282
-
283
- You can also find all Class II-related reference data within the `reference`
284
- folder in this repo.
285
-
286
- ## Scenario: detecting LOH from paired tumor and normal samples
287
-
288
- Detecting LOH event within the HLA region has been one of the popular subjects
289
- scientists want to look into, especially in a clinical cohort where patients
290
- receive immune checkpoint inhibitor treatment. Homozygous HLA genotypes
291
- decrease the diversity of antigen/neo-antigen the immune system can capture.
292
-
293
- `mhcflow` can generate LOH analysis-ready inputs for both tumor and paired
294
- normal samples. Here, I only show how to prepare for the tumor data. You can refer
295
- to upstairs for getting the normal data ready.
296
-
297
- Again, let us pretend we have a hypothetical tumor data for sample `NA12046`
298
- (apologize for "giving" this sample a tumor, no harmful damage intented):
299
-
300
- ```bash
301
- polysolvermod --bam NA12046.tumor.so.bam \
302
- --hla_ref "$PWD/NA12046_class1/finalizer/NA12046.hla.fasta
303
- --bed class1.bed \
304
- --tag abc_v14.uniq \
305
- --realn_only \
306
- --outdir "$PWD/NA12046_tumor \
307
- --sample NA12046.tumor
308
- ```
309
-
310
- The `--realn_only` tells `mhcflow` to run only the fishing and realigning
311
- steps using the sample-specific HLA reference obtained in earlier example.
312
-
313
- Now you can skip the mapping step (`--skip-map`) in `lohhla`, and directly detect
314
- LOH events.
315
-
316
- ## License
317
-
318
- - `mhcflow` respects all LICENSE requirement imposed by the original
319
- `Polysolver` software, and is licensed under GPL-3.
320
-
321
- ## Disclaimer
322
-
323
- - I, by no means, intend to overtake the origianl idea and implementation
324
- of `Polysolver` algorithm.
325
- - This repo does not distribute `Polysolver` software, as well as all
326
- its dependencies such as `novoalign` and `novoindex` under commercial licenses.
327
- - `mhcflow` re-engineered only the HLA typing algorithm. All other
328
- tools in the `Polysolver` suite was not modified and included in this repo.
329
- - `mhcflow` does not necessarily produce identical result as
330
- `Polysolver` on typing HLA class I alleles.
331
- - Please interpret result at your own discretion when using
332
- `mhcflow`.
333
- [`hla_benchmark`](https://github.com/svm-zhang/hla_benchmark) repo provides
334
- fundamental assessment of `mhcflow` using 1000 genome data on HLA-A,
335
- HLA-B, HLA-C, HLA-DQB1, and HLA-DRB1.
336
-
337
- ## Citation
338
-
339
- Please cite the original
340
- [Polysolver](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4747795/) paper.
341
-
342
- If you use `mhcflow`, please cite this github repo as well.
mhcflow-0.2.0/README.md DELETED
@@ -1,313 +0,0 @@
1
- # mhcflow
2
-
3
- <!-- toc -->
4
-
5
- - [Introduction](#introduction)
6
- - [Features](#features)
7
- - [Installation](#installation)
8
- - [Quick Start](#quick-start)
9
- - [Explain Output](#explain-output)
10
- - [Step by Step](#step-by-step)
11
- * [Fisherman: fishing HLA-relevant reads](#fisherman-fishing-hla-relevant-reads)
12
- * [Realigner: realigning fished reads to HLA reference](#realigner-realigning-fished-reads-to-hla-reference)
13
- * [Typer: typing HLA class I genotype](#typer-typing-hla-class-i-genotype)
14
- - [Realigner: generating analysis-ready HLA typing result](#realigner-generating-analysis-ready-hla-typing-result)
15
- - [Extend to Class II typing](#extend-to-class-ii-typing)
16
- - [Scenario: detecting LOH from paired tumor and normal samples](#scenario-detecting-loh-from-paired-tumor-and-normal-samples)
17
- - [License](#license)
18
- - [Disclaimer](#disclaimer)
19
- - [Citation](#citation)
20
-
21
- <!-- tocstop -->
22
-
23
- ## Introduction
24
-
25
- `mhcflow` is the original
26
- [polysolver](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4747795/) HLA
27
- typing algorithm re-engineered in modern style. It offers almost all aspects
28
- of the original algorithm, adds new features, runs faster, and is friendly to
29
- pipeline integration.
30
-
31
- ## Features
32
-
33
- - Supports both class I and
34
- [II](https://github.com/svm-zhang/mhcflow?tab=readme-ov-file#extend-to-class-ii-typing)
35
- typing with good
36
- [accuracy](https://github.com/svm-zhang/hla_benchmark?tab=readme-ov-file)
37
- - Generates analysis-ready HLA alignments for HLALOH detection
38
- - Re-engineered in modern style with
39
- - Modular design
40
- - Faster runtime with internal optimization
41
- - Minimum I/O operations
42
- - Minimum hard-coded code
43
- - Easy integration to pipeline with packaging and better CLI
44
-
45
- ## Installation
46
-
47
- Please refer to [INSATLL](INSTALL.md) for details.
48
-
49
- ## Quick Start
50
-
51
- `mhcflow` requires
52
-
53
- - Sorted genomic alignment in BAM format with index
54
- - HLA reference sequence in Fasta and Nix (novoalign index)
55
- - BED file with region of each HLA allele
56
- - HLA kmer tags
57
- - HLA 4-digit supertype frequency table
58
-
59
- Let us type class 1 alleles for `NA12046` sample provided by the 1000
60
- genome project (`NA12046` will be used as example throughout this doc):
61
-
62
- ```bash
63
- polysolvermod --bam NA12046.so.bam \
64
- --hla_ref abc_complete.fasta \
65
- --bed class1.bed \
66
- --tag abc_v14.uniq \
67
- --freq HLA_FREQ.txt \
68
- --outdir "$PWD/NA12046_class1 \
69
- --sample NA12046
70
- ```
71
-
72
- The command above generates HLA typing results in the designated output
73
- directory specified by `--outdir` option. A quick peek into the result
74
- folder looks like:
75
-
76
- ```text
77
- -- NA12046_class1
78
- -- finalizer
79
- -- fisher
80
- -- realigner
81
- -- typer
82
- ```
83
-
84
- ## Explain Output
85
-
86
- The `finalizer` folder provides the sample-level HLA reference sequence that
87
- can be directly used as reference for realigning tumor data in a paired tumor
88
- and normal setting. There is also an alignment result in BAM against this
89
- sample-level reference with suffix `hla.realn.ready.bam`. In context of oncology
90
- or immuno-oncology research, the BAM file can be directly ported to program to
91
- detect HLA loss of heterozygosity.
92
-
93
- The typing result can be found within the `typer` folder, with suffix
94
- `hlatyping.res.tsv`. The result table should look like the following:
95
-
96
- ```text
97
- allele gene tot_scores sample
98
- hla_a_01_01_29 hla_a 2452923.4298 NA12046
99
- hla_a_02_05_01 hla_a 1766396.924 NA12046
100
- hla_b_50_01_01 hla_b 1332194.9171 NA12046
101
- hla_b_57_01_08 hla_b 1134814.4428 NA12046
102
- hla_c_06_02_01_01 hla_c 3020505.4303 NA12046
103
- hla_c_06_02_01_02 hla_c 1519636.0349 NA12046
104
- ```
105
-
106
- ## Step by Step
107
-
108
- `mhcflow` is re-engineered with a modular design. It generally consists of
109
- 4 steps: `fishing`, `realigning`, `typing`, and `realigning` (again). Each
110
- module implements basic break-and-continue mechanism, meaning that module
111
- finished previously will be automatically skipped. Also it is more friendly
112
- to integrate with pipeline/workflow.
113
-
114
- The `hlapolysolver.sh` script and `mhcflow` binary (after building the package)
115
- demonstrates each following step, if you are interested.
116
-
117
- ### Fisherman: fishing HLA-relevant reads
118
-
119
- The original `polysolver` algorithm fishes HLA-related reads via matching
120
- pre-built kmer (tag) sequence and extracting alignments mapped to regions where
121
- HLA class I allele located. `mhcflow` follows the same strategy and speeds
122
- it up.
123
-
124
- ```bash
125
- fisher --mode faster \
126
- --tag abc_v14.uniq \
127
- --bed class1.bed \
128
- --bam NA12046.so.bam \
129
- --sample NA12045 \
130
- --out "$PWD/NA12046_class1/fisher/NA12046.fqs.list.txt
131
- ```
132
-
133
- The result is plain text file with two lines of fished fastq files
134
- (paired-end reads).
135
-
136
- It is important to note there are other approches to fish HLA-relevant reads.
137
- For instance, `Optitype` aligns trimmed reads against the HLA reference using
138
- `razerS3`. From my experience, direct alignment finds more reads and these
139
- reads tend to align better. However, `razerS3` is not quite memory-efficient,
140
- which in my opinion limits its utility, especially your computing platform
141
- is not unlimited. The approach that the original `polysolver` uses provides
142
- decent fishing result.
143
-
144
- ### Realigner: realigning fished reads to HLA reference
145
-
146
- Next the realigner module aligns the fished reads against the provided
147
- class I HLA reference sequence using `novoalign`, same as the original
148
- `polysolver` program. The difference is the realigner module achieves the
149
- reaignment process in parallel to speed things up a bit.
150
-
151
- ```bash
152
- realigner --hla_ref abc_complete.fasta \
153
- --fqs "$PWD/NA12046_class1/fisher/NA12046.fqs.list.txt \
154
- --sample NA12046 \
155
- --out "$PWD/NA12046_class1/realigner/NA12046.hla.realn.so.bam
156
- ```
157
-
158
- Because the academia version of `novoalign` does not support gzipped fastq
159
- file, this step can take up some disk space depending on the sample
160
- sequencing depth that is HLA-related.
161
-
162
- ### Typer: typing HLA class I genotype
163
-
164
- The typer module is a complete overhaul of the origial perl scripts
165
- `first_allele_calculations.pl` and `second_allele_calcuations.pl`. The original
166
- `polysolver` algorithm types first and second alleles at a locus in two
167
- separated processes. For each HLA class I allele defined in the HLA reference
168
- sequence, it outputs a plain text file with scores. This generates thousands of
169
- files that creates I/O pressure and make the typing process I/O bound. Also,
170
- it takes about 3-4 script calls to type the first allele and makes it hard to
171
- track when error happens.
172
-
173
- The `pyhlatyper` written in this repo tires to improves on all aspects:
174
-
175
- 1. Typing two alleles with one program call
176
- 2. Making typing CPU-bound powered by `polars` and `pysam`
177
- 3. Processing alignments to calculate scores in parallel
178
- 4. Enabling possibility of typing alleles class II alleles
179
- 5. Capturing errors in proper way
180
- 6. Free of hard-coded code
181
-
182
- ```bash
183
- pyhlatyper --freq HLA_FREQ.txt \
184
- --bam "$PWD/NA12046_class1/realigner/NA12046.hla.realn.so.bam \
185
- --out "$PWD/NA12046_class1/typer/NA12046.hla_typing.res.tsv
186
- ```
187
-
188
- One important difference of `pyhlatyper` from the original typing scripts is
189
- that it does not actaully use frequency as prior to calculate posterior scores.
190
- This means the `--race` is always `Unknown`. The choice was made because the race
191
- is usually not a known factor when dealing with real-world data.
192
- I probably will remove the `--race` option from CLI for good in the future.
193
-
194
- ## Realigner: generating analysis-ready HLA typing result
195
-
196
- The original `polysolver` finishes after typing is done. `mhcflow` goes
197
- beyond by providing
198
-
199
- 1. HLA reference sequence specific to the typed sample
200
- 2. Alignment against the sample HLA reference
201
-
202
- The reason to have this additional step is to get analysis-ready result.
203
- In oncology and/or immuno-oncology research, one of the questions
204
- people has is to know if there is loss of heterozygosity (LOH) occurring in a tumor
205
- sample. [LOHHLA](https://bitbucket.org/mcgranahanlab/lohhla/src/master/) is the
206
- common go-to algorithm to answer the question. However, `lohhla`, before detecting
207
- any LOH event, goes through realigning both normal and tumor samples, despite typing
208
- has been done for the normal sample. Also realignment, in my opinion, belongs to
209
- pipeline. LOH detection algorithm should be simplified to serve what it is designed
210
- for. To have a clearer picture of what I mean, please refer to [tumor and
211
- normal
212
- scenario](https://github.com/svm-zhang/mhcflow?tab=readme-ov-file#scenario-detecting-loh-from-paired-tumor-and-normal-samples)
213
- below.
214
-
215
- The final realignment process splits into two steps.
216
- First to extract and index sample-level HLA reference.
217
-
218
- ```bash
219
- extractor --hla_ref abc_complete.fasta \
220
- --sample NA12046 \
221
- --typeres "$PWD/NA12046_class1/typer/NA12046.hla_typing.res.tsv
222
- --out "$PWD/NA12046_class1/finalizer/NA12046.hla.fasta
223
-
224
- ```
225
-
226
- Then do the realignment against this new reference.
227
-
228
- ```bash
229
- realigner \
230
- --hla_ref "$PWD/NA12046_class1/finalizer/NA12046.hla.fasta
231
- --fqs "$PWD/NA12046_class1/fisher/NA12046.fqs.list.txt \
232
- --sample NA12046 \
233
- --mdup \
234
- --out "$PWD/NA12046_class1/finalizer/NA12046.hla.realn.ready.bam
235
- ```
236
-
237
- The `--mdup` option marks PCR duplicates so that when counting coverage during
238
- LOH detection, duplicated reads do not get included. If you want to keep duplicates,
239
- simply not using this option.
240
-
241
- ## Extend to Class II typing
242
-
243
- The original `polysolver` algorithm has been well-known for genotyping Class
244
- I alleles. However, in theory it should also be able to apply to the Class II
245
- case, with certain modification as well as a set of Class II references.
246
-
247
- To type the Class II alleles, you only need to swap in the new reference
248
- data, and the CLI is the same as we have shown for the Class I case.
249
-
250
- I have done some preliminary benchmark on Class II typing using some samples from
251
- 1000 genome project. The result is suprisingly not too shady and can be found
252
- [here](https://github.com/svm-zhang/hla_benchmark).
253
-
254
- You can also find all Class II-related reference data within the `reference`
255
- folder in this repo.
256
-
257
- ## Scenario: detecting LOH from paired tumor and normal samples
258
-
259
- Detecting LOH event within the HLA region has been one of the popular subjects
260
- scientists want to look into, especially in a clinical cohort where patients
261
- receive immune checkpoint inhibitor treatment. Homozygous HLA genotypes
262
- decrease the diversity of antigen/neo-antigen the immune system can capture.
263
-
264
- `mhcflow` can generate LOH analysis-ready inputs for both tumor and paired
265
- normal samples. Here, I only show how to prepare for the tumor data. You can refer
266
- to upstairs for getting the normal data ready.
267
-
268
- Again, let us pretend we have a hypothetical tumor data for sample `NA12046`
269
- (apologize for "giving" this sample a tumor, no harmful damage intented):
270
-
271
- ```bash
272
- polysolvermod --bam NA12046.tumor.so.bam \
273
- --hla_ref "$PWD/NA12046_class1/finalizer/NA12046.hla.fasta
274
- --bed class1.bed \
275
- --tag abc_v14.uniq \
276
- --realn_only \
277
- --outdir "$PWD/NA12046_tumor \
278
- --sample NA12046.tumor
279
- ```
280
-
281
- The `--realn_only` tells `mhcflow` to run only the fishing and realigning
282
- steps using the sample-specific HLA reference obtained in earlier example.
283
-
284
- Now you can skip the mapping step (`--skip-map`) in `lohhla`, and directly detect
285
- LOH events.
286
-
287
- ## License
288
-
289
- - `mhcflow` respects all LICENSE requirement imposed by the original
290
- `Polysolver` software, and is licensed under GPL-3.
291
-
292
- ## Disclaimer
293
-
294
- - I, by no means, intend to overtake the origianl idea and implementation
295
- of `Polysolver` algorithm.
296
- - This repo does not distribute `Polysolver` software, as well as all
297
- its dependencies such as `novoalign` and `novoindex` under commercial licenses.
298
- - `mhcflow` re-engineered only the HLA typing algorithm. All other
299
- tools in the `Polysolver` suite was not modified and included in this repo.
300
- - `mhcflow` does not necessarily produce identical result as
301
- `Polysolver` on typing HLA class I alleles.
302
- - Please interpret result at your own discretion when using
303
- `mhcflow`.
304
- [`hla_benchmark`](https://github.com/svm-zhang/hla_benchmark) repo provides
305
- fundamental assessment of `mhcflow` using 1000 genome data on HLA-A,
306
- HLA-B, HLA-C, HLA-DQB1, and HLA-DRB1.
307
-
308
- ## Citation
309
-
310
- Please cite the original
311
- [Polysolver](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4747795/) paper.
312
-
313
- If you use `mhcflow`, please cite this github repo as well.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes