-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from NCI-GDC/feature/binf-687-modify-sample
[BINF-687] Add modify vcf sample script
- Loading branch information
Showing
9 changed files
with
164 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Modify GATK4.1.2 Mutect2 VCF sample header to "TUMOR", "NORMAL" | ||
@author: Shenglai Li | ||
""" | ||
import logging | ||
from typing import Optional | ||
|
||
import click | ||
import pysam | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
tumor_bamT = str | ||
normal_bamT = Optional[str] | ||
vcfT = str | ||
outputT = str | ||
|
||
|
||
def get_sample_name(bam: str) -> str: | ||
''' | ||
Get sample name from BAM file | ||
''' | ||
sample: str | ||
b = pysam.AlignmentFile(bam, 'rb') | ||
sample = b.header['RG'][0]['SM'] # type: ignore | ||
return sample | ||
|
||
|
||
def modify_vcf_sample( | ||
tumor_bam: tumor_bamT, normal_bam: normal_bamT, vcf: vcfT, output: outputT | ||
) -> None: | ||
''' | ||
Modify VCF sample in the header | ||
''' | ||
out_vcf = output | ||
reader = pysam.BGZFile(vcf, mode='rb') # type: ignore | ||
writer = pysam.BGZFile(out_vcf, mode='wb') # type: ignore | ||
try: | ||
for line in reader: | ||
new_line: str | ||
new_line = line.decode('utf-8') | ||
if new_line.startswith('#CHROM'): | ||
if normal_bam: | ||
assert ( | ||
normal_bam in new_line | ||
), f'Unable to find normal sample tag in the vcf file. {normal_bam}' | ||
new_line = new_line.replace(f'{normal_bam}', 'NORMAL') | ||
assert ( | ||
tumor_bam in new_line | ||
), f'Unable to find tumor sample tag in the vcf file. {tumor_bam}' | ||
new_line = new_line.replace(f'{tumor_bam}', 'TUMOR') | ||
writer.write(str.encode(f"{new_line}\n", encoding='utf-8')) | ||
else: | ||
new_line = new_line + '\n' | ||
writer.write(new_line.encode('utf-8')) | ||
except AssertionError as e: | ||
logger.exception(e) | ||
finally: | ||
writer.close() | ||
reader.close() | ||
pysam.tabix_index(out_vcf, preset='vcf', force=True) | ||
|
||
|
||
@click.command() | ||
@click.option('--tumor_bam', required=True) | ||
@click.option('--vcf', required=True) | ||
@click.option('--output', required=True) | ||
@click.option('--normal_bam', required=False) | ||
def main( | ||
tumor_bam: tumor_bamT, | ||
vcf: vcfT, | ||
output: outputT, | ||
normal_bam: normal_bamT = None, | ||
) -> None: | ||
''' | ||
main | ||
''' | ||
tumor_sample = get_sample_name(tumor_bam) | ||
logger.info(f'{tumor_sample=}') | ||
normal_sample = None | ||
if normal_bam: | ||
normal_sample = get_sample_name(normal_bam) | ||
logger.info(f'{normal_sample=}') | ||
modify_vcf_sample(tumor_sample, normal_sample, vcf, output) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
# __END__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import unittest | ||
|
||
from click.testing import CliRunner | ||
|
||
from gatk4_mutect2_tool import modify_vcf_sample as MOD | ||
|
||
|
||
class ThisTestCase(unittest.TestCase): | ||
def setUp(self) -> None: | ||
super().setUp() | ||
self.cli_args_dict = { | ||
"--normal_bam": "normal.bam", | ||
"--tumor_bam": "tumor.bam", | ||
"--vcf": "test.vcf", | ||
"--output": "output.vcf", | ||
} | ||
self.cli_args_list = [] | ||
for k, v in self.cli_args_dict.items(): | ||
self.cli_args_list.extend([k, v]) | ||
|
||
def cli_args(self): | ||
runner = CliRunner() | ||
result = runner.invoke(MOD.main, self.cli_args_list) | ||
self.assertEqual(result.exit_code, 0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
|
||
# __END__ |