<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""
run_genmod.py

Script for annotating genetic models in variant files.

Created by <NAME> on 2014-01-21.
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
"""

import sys
import os
import argparse
from multiprocessing import JoinableQueue, Manager, cpu_count
from codecs import open
from datetime import datetime
from tempfile import mkdtemp
import shutil
import pkg_resources

from pysam import tabix_index, tabix_compress

from ped_parser import parser

from genmod.utils import variant_consumer, variant_sorter, annotation_parser, variant_printer
from genmod.vcf import vcf_header, vcf_parser

def get_family(args):
    """Return the family"""
    family_type = 'ped'
    family_file = args.family_file[0]
    
    my_family_parser = parser.FamilyParser(family_file, family_type)
    # Stupid thing but for now when we only look at one family
    return my_family_parser.families.popitem()[1]

def get_header(variant_file):
    """Return a fixed header parser"""
    head = vcf_header.VCFParser(variant_file)
    head.parse()
    return head

def add_metadata(head, args):
    """Add metadata for the information added by this script."""
    head.metadataparser.add_info('ANN', '.', 'String', 'Annotates what feature(s) this variant belongs to.')
    head.metadataparser.add_info('Comp', '.', 'String', "':'-separated list of compound pairs for this variant.")
    head.metadataparser.add_info('GM', '.', 'String', "':'-separated list of genetic models for this variant.")
    head.metadataparser.add_info('MS', '1', 'Integer', "PHRED score for genotype models.")
    if args.cadd_file[0]:
        head.metadataparser.add_info('CADD', '1', 'Float', "The CADD relative score for this alternative.")
    return

def print_headers(args, header_object):
    """Print the headers to a results file."""
    if args.outfile[0]:
        with open(args.outfile[0], 'w', encoding='utf-8') as f: 
            for head_count in header_object.print_header():
                f.write(head_count+'\n')
    else:
        if not args.silent:
            for line in header_object.print_header():
                print(line)
    return

def main():
    
    info_string = """Individuals that are not present in ped file will not be considered in the analysis."""
    
    parser = argparse.ArgumentParser(description="Annotate genetic models in variant files..")
    
    parser.add_argument('family_file', 
        type=str, nargs=1, 
        help='A pedigree file in .ped format.'
    )
    parser.add_argument('variant_file', 
        type=str, nargs=1, 
        help='A variant file. Default is vcf format.'
    )

    parser.add_argument('annotation_file', 
        type=str, nargs=1, 
        help='A annotations file. Default is ref_gene format.'
    )
    
    parser.add_argument('-at', '--annotation_type',  
        type=str, nargs=1, choices=['bed', 'ccds', 'gtf', 'gene_pred'], 
        help='Specify the format of the annotation file. gene_pred is default (this is the format of the refgene files.)'
    )    
    
    parser.add_argument('--version', 
        action="version", 
        version=pkg_resources.require("genmod")[0].version
    )
    
    parser.add_argument('-v', '--verbose', 
        action="store_true", 
        help='Increase output verbosity.'
    )

    parser.add_argument('-chr', '--chr_prefix', 
        action="store_true", 
        help='If chr prefix is used in vcf.'
    )
    
    parser.add_argument('-s', '--silent', 
        action="store_true", 
        help='Do not print the variants.'
    )
    
    parser.add_argument('-phased', '--phased', 
        action="store_true", 
        help='If data is phased use this flag.'
    )
    
    parser.add_argument('-o', '--outfile', 
        type=str, nargs=1, default=[None],
        help='Specify the path to a file where results should be stored.'
    )
    
    parser.add_argument('-cadd', '--cadd_file', 
        type=str, nargs=1, default=[None],
        help='Specify the path to a bgzipped cadd file with variant scores.\
            If no index is present it will be created.'
    )    
    
    args = parser.parse_args()
    var_file = args.variant_file[0]
    file_name, file_extension = os.path.splitext(var_file)
    anno_file = args.annotation_file[0]
        
    start_time_analysis = datetime.now()
    
            
    # Start by parsing at the pedigree file:

    my_family = get_family(args)
    
    # Parse the header of the vcf:
    
    head = get_header(var_file)
    add_metadata(head, args)
    # Parse the annotation file and make annotation trees:

    if args.verbose:
        print('Parsing annotation ...')
        print('')
        start_time_annotation = datetime.now()
    
    anno_file_name, anno_file_extension = os.path.splitext(anno_file)
    anno_zipped = False
    annotation = 'gene_pred'
    
    if anno_file_extension == '.gz':
        anno_zipped = True
        anno_file_name, anno_file_extension = os.path.splitext(anno_file_name)
    
    if args.annotation_type:
        annotation = annotation_type[0]<commit_msg>fixed typo<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""
run_genmod.py

Script for annotating genetic models in variant files.

Created by <NAME> on 2014-01-21.
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
"""

import sys
import os
import argparse
from multiprocessing import JoinableQueue, Manager, cpu_count
from codecs import open
from datetime import datetime
from tempfile import mkdtemp
import shutil
import pkg_resources

from pysam import tabix_index, tabix_compress

from ped_parser import parser

from genmod.utils import variant_consumer, variant_sorter, annotation_parser, variant_printer
from genmod.vcf import vcf_header, vcf_parser

def get_family(args):
    """Return the family"""
    family_type = 'ped'
    family_file = args.family_file[0]
    
    my_family_parser = parser.FamilyParser(family_file, family_type)
    # Stupid thing but for now when we only look at one family
    return my_family_parser.families.popitem()[1]

def get_header(variant_file):
    """Return a fixed header parser"""
    head = vcf_header.VCFParser(variant_file)
    head.parse()
    return head

def add_metadata(head, args):
    """Add metadata for the information added by this script."""
    head.metadataparser.add_info('ANN', '.', 'String', 'Annotates what feature(s) this variant belongs to.')
    head.metadataparser.add_info('Comp', '.', 'String', "':'-separated list of compound pairs for this variant.")
    head.metadataparser.add_info('GM', '.', 'String', "':'-separated list of genetic models for this variant.")
    head.metadataparser.add_info('MS', '1', 'Integer', "PHRED score for genotype models.")
    if args.cadd_file[0]:
        head.metadataparser.add_info('CADD', '1', 'Float', "The CADD relative score for this alternative.")
    return

def print_headers(args, header_object):
    """Print the headers to a results file."""
    if args.outfile[0]:
        with open(args.outfile[0], 'w', encoding='utf-8') as f: 
            for head_count in header_object.print_header():
                f.write(head_count+'\n')
    else:
        if not args.silent:
            for line in header_object.print_header():
                print(line)
    return

def main():
    
    info_string = """Individuals that are not present in ped file will not be considered in the analysis."""
    
    parser = argparse.ArgumentParser(description="Annotate genetic models in variant files..")
    
    parser.add_argument('family_file', 
        type=str, nargs=1, 
        help='A pedigree file in .ped format.'
    )
    parser.add_argument('variant_file', 
        type=str, nargs=1, 
        help='A variant file. Default is vcf format.'
    )

    parser.add_argument('annotation_file', 
        type=str, nargs=1, 
        help='A annotations file. Default is ref_gene format.'
    )
    
    parser.add_argument('-at', '--annotation_type',  
        type=str, nargs=1, choices=['bed', 'ccds', 'gtf', 'gene_pred'], 
        help='Specify the format of the annotation file. gene_pred is default (this is the format of the refgene files.)'
    )    
    
    parser.add_argument('--version', 
        action="version", 
        version=pkg_resources.require("genmod")[0].version
    )
    
    parser.add_argument('-v', '--verbose', 
        action="store_true", 
        help='Increase output verbosity.'
    )

    parser.add_argument('-chr', '--chr_prefix', 
        action="store_true", 
        help='If chr prefix is used in vcf.'
    )
    
    parser.add_argument('-s', '--silent', 
        action="store_true", 
        help='Do not print the variants.'
    )
    
    parser.add_argument('-phased', '--phased', 
        action="store_true", 
        help='If data is phased use this flag.'
    )
    
    parser.add_argument('-o', '--outfile', 
        type=str, nargs=1, default=[None],
        help='Specify the path to a file where results should be stored.'
    )
    
    parser.add_argument('-cadd', '--cadd_file', 
        type=str, nargs=1, default=[None],
        help='Specify the path to a bgzipped cadd file with variant scores.\
            If no index is present it will be created.'
    )    
    
    args = parser.parse_args()
    var_file = args.variant_file[0]
    file_name, file_extension = os.path.splitext(var_file)
    anno_file = args.annotation_file[0]
        
    start_time_analysis = datetime.now()
    
            
    # Start by parsing at the pedigree file:

    my_family = get_family(args)
    
    # Parse the header of the vcf:
    
    head = get_header(var_file)
    add_metadata(head, args)
    # Parse the annotation file and make annotation trees:

    if args.verbose:
        print('Parsing annotation ...')
        print('')
        start_time_annotation = datetime.now()
    
    anno_file_name, anno_file_extension = os.path.splitext(anno_file)
    anno_zipped = False
    annotation = 'gene_pred'
    
    if anno_file_extension == '.gz':
        anno_zipped = True
        anno_file_name, anno_file_extension = os.path.splitext(anno_file_name)
    
    if args.annotation_type:
        annotation = args.annotation_type[0]
{'fraction_non_alphanumeric': 0.11229476153244722, 'fraction_numerical': 0.006059421422986709, 'mean_word_length': 2.886441321686289, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4514996', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3107, 'n_words': 858}