<commit_before>#!/usr/bin/env python3

# Post-process GATK4's MuTect2 output. The main purpose is to split multi-allelic records into one variant record per line.

import sys, os, argparse, gzip
import regex as re

MY_DIR = os.path.dirname(os.path.realpath(__file__))
PRE_DIR = os.path.join(MY_DIR, os.pardir)
sys.path.append( PRE_DIR )

import genomic_file_handlers as genome

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# Variant Call Type, i.e., snp or indel
parser.add_argument('-infile', '--input-vcf',  type=str, help='Input VCF file', required=True)
parser.add_argument('-indel',  '--indel-out', type=str, help='Output VCF file', required=True)
parser.add_argument('-snv',    '--snv-out', type=str, help='Output VCF file', required=True)

# Parse the arguments:
args = parser.parse_args()

infile = args.input_vcf
indel_out = args.indel_out
snv_out = args.snv_out

info_to_split = 'NLOD', 'TLOD'
info_to_keep = 'STR', 'ECNT'

with genome.open_textfile(infile) as vcf_in, open(snv_out, 'w') as snv_out, open(indel_out, 'w') as indel_out:
    
    line_i = vcf_in.readline().rstrip()
    
    while line_i.startswith('##'):
        
        snv_out.write( line_i + '\n' )
        indel_out.write( line_i + '\n' )
        
        if line_i.startswith('##normal_sample='):
            normal_name = line_i.split('=')[1]
            
        if line_i.startswith('##tumor_sample='):
            tumor_name = line_i.split('=')[1]
            
        line_i = vcf_in.readline().rstrip()
        snv_out.write( line_i + '\n' )
        indel_out.write( line_i + '\n' )

    # This line will be #CHROM:
    header = line_i.split('\t')
    normal_index = header.index(normal_name) - 9
    tumor_index = header.index(tumor_name) - 9
    
    # This will be the first variant line:
    line_i = vcf_in.readline().rstrip()
    
    while line_i:
        
        vcf_i = genome.Vcf_line( line_i )
        
        if ',' not in vcf_i.altbase:
            
            if len(vcf_i.refbase) == 1 and len(vcf_i.altbase) == 1:
                snv_out.write( line_i + '\n' )
            else:
                indel_out.write( line_i + '\n' )
                
        else:
            alt_bases = vcf_i.altbase.split(',')
            measures = []
            still_measures = []
            
            for measure_i in info_to_split:
                try:
                    measures.append( vcf_i.get_info_value(measure_i).split(',') )
                except AttributeError:
                    measures.append( None )
                    
            for measure_i in info_to_keep:
                try:
                    still_measures.append( vcf_i.get_info_value(measure_i) )
                except AttributeError:
                    still_measures.append( None )
                
            for ith_base, altbase_i in enumerate(alt_bases):
                                
                split_infos = [ '{}={}'.format(info_variable, info_value[ith_base]) for info_variable, info_value in zip(info_to_split, measures) if info_value != None ]
                
                still_infos = [ '{}={}'.format(info_variable, info_value) for info_variable, info_value in zip(info_to_keep, still_measures) if info_value != False ]
                
                split_infos.extend(still_infos)
                
                info_string = ';'.join( split_infos )
                
                GT0 = vcf_i.get_sample_value('GT', idx=0)
                if GT0 != '0/0' or GT0 != '0/1':
                    sample_0 = re.sub(r'^0/[2-9]', '0/1', vcf_i.samples[0])
                
                GT1 = vcf_i.GT1('GT', idx=1)
                if GT1 != '0/0' or GT0 != '0/1':
                    sample_1 = re.sub(r'^0/[2-9]', '0/1', vcf_i.samples[1])
                
                
                new_line = '\t'.join(( vcf_i.chromosome, str(vcf_i.position), vcf_i.identifier, vcf_i.refbase, altbase_i, vcf_i.qual, vcf_i.filters, info_string, vcf_i.field, sample_0, sample_1 ))
                
                if len(vcf_i.refbase) == 1 and len(altbase_i) == 1:
                    snv_out.write( new_line + '\n' )
                else:
                    indel_out.write( new_line + '\n')
        
        line_i = vcf_in.readline().rstrip()
<commit_msg>mutect2.1<commit_after>#!/usr/bin/env python3

# Post-process GATK4's MuTect2 output. The main purpose is to split multi-allelic records into one variant record per line.

import sys, os, argparse, gzip
import regex as re

MY_DIR = os.path.dirname(os.path.realpath(__file__))
PRE_DIR = os.path.join(MY_DIR, os.pardir)
sys.path.append( PRE_DIR )

import genomic_file_handlers as genome

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# Variant Call Type, i.e., snp or indel
parser.add_argument('-infile', '--input-vcf',  type=str, help='Input VCF file', required=True)
parser.add_argument('-indel',  '--indel-out', type=str, help='Output VCF file', required=True)
parser.add_argument('-snv',    '--snv-out', type=str, help='Output VCF file', required=True)

# Parse the arguments:
args = parser.parse_args()

infile = args.input_vcf
indel_out = args.indel_out
snv_out = args.snv_out

info_to_split = 'NLOD', 'TLOD'
info_to_keep = 'STR', 'ECNT'

with genome.open_textfile(infile) as vcf_in, open(snv_out, 'w') as snv_out, open(indel_out, 'w') as indel_out:
    
    line_i = vcf_in.readline().rstrip()
    
    while line_i.startswith('##'):
        
        snv_out.write( line_i + '\n' )
        indel_out.write( line_i + '\n' )
        
        if line_i.startswith('##normal_sample='):
            normal_name = line_i.split('=')[1]
            
        if line_i.startswith('##tumor_sample='):
            tumor_name = line_i.split('=')[1]
            
        line_i = vcf_in.readline().rstrip()
        snv_out.write( line_i + '\n' )
        indel_out.write( line_i + '\n' )

    # This line will be #CHROM:
    header = line_i.split('\t')
    normal_index = header.index(normal_name) - 9
    tumor_index = header.index(tumor_name) - 9
    
    # This will be the first variant line:
    line_i = vcf_in.readline().rstrip()
    
    while line_i:
        
        vcf_i = genome.Vcf_line( line_i )
        
        if ',' not in vcf_i.altbase:
            
            if len(vcf_i.refbase) == 1 and len(vcf_i.altbase) == 1:
                snv_out.write( line_i + '\n' )
            else:
                indel_out.write( line_i + '\n' )
                
        else:
            alt_bases = vcf_i.altbase.split(',')
            measures = []
            still_measures = []
            
            for measure_i in info_to_split:
                try:
                    measures.append( vcf_i.get_info_value(measure_i).split(',') )
                except AttributeError:
                    measures.append( None )
                    
            for measure_i in info_to_keep:
                try:
                    still_measures.append( vcf_i.get_info_value(measure_i) )
                except AttributeError:
                    still_measures.append( None )
                
            for ith_base, altbase_i in enumerate(alt_bases):
                                
                split_infos = [ '{}={}'.format(info_variable, info_value[ith_base]) for info_variable, info_value in zip(info_to_split, measures) if info_value != None ]
                
                still_infos = [ '{}={}'.format(info_variable, info_value) for info_variable, info_value in zip(info_to_keep, still_measures) if info_value != False ]
                
                split_infos.extend(still_infos)
                
                info_string = ';'.join( split_infos )
                
                GT0 = vcf_i.get_sample_value('GT', idx=0)
                if GT0 != '0/0' or GT0 != '0/1':
                    sample_0 = re.sub(r'^0/[2-9]', '0/1', vcf_i.samples[0])
                
                GT1 = vcf_i.get_sample_value('GT', idx=1)
                if GT1 != '0/0' or GT0 != '0/1':
                    sample_1 = re.sub(r'^0/[2-9]', '0/1', vcf_i.samples[1])
                
                
                new_line = '\t'.join(( vcf_i.chromosome, str(vcf_i.position), vcf_i.identifier, vcf_i.refbase, altbase_i, vcf_i.qual, vcf_i.filters, info_string, vcf_i.field, sample_0, sample_1 ))
                
                if len(vcf_i.refbase) == 1 and len(altbase_i) == 1:
                    snv_out.write( new_line + '\n' )
                else:
                    indel_out.write( new_line + '\n')
        
        line_i = vcf_in.readline().rstrip()

{'fraction_non_alphanumeric': 0.12440579710144928, 'fraction_numerical': 0.010318840579710145, 'mean_word_length': 1.8525132275132274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1852251', 'n_tokens_mistral': 3018, 'n_tokens_neox': 2889, 'n_words': 566}