Mercurial > repos > in_silico > cravat_annotate_mutations
changeset 17:a9cb0192d52d draft
Uploaded
author | in_silico |
---|---|
date | Tue, 12 Jun 2018 14:03:57 -0400 |
parents | efb15a586f5e |
children | dd9181024296 |
files | cravat_annotate/cravat_annotate.py cravat_annotate/cravat_annotate.xml cravat_convert/base_converter.py cravat_convert/cravat_convert.py cravat_convert/cravat_convert.xml cravat_convert/vcf_converter.py |
diffstat | 6 files changed, 362 insertions(+), 271 deletions(-) [+] |
line wrap: on
line diff
--- a/cravat_annotate/cravat_annotate.py Tue Jun 12 12:05:48 2018 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,246 +0,0 @@ -""" -A galaxy wrapper for the /rest/service/query API endpoint on Cravat. - - -Notes on Mapping: ------------------ -The CravatQuery class uses static method 'from_array' to interpret an array of values -into a query string for the /rest/service/query API service on the cravat server. -This involves using a mapping dictionary to know how to associate the array's index positions -in to query-ing attributes, such as the chromosome, position, etc. The CravatQuery -class contains a default value ('default_mapping'); however, this could also be -offered as a user-configurable option. -""" - - -import requests -import json -import sys -import re - - -class CravatQueryException(Exception): - - def __init__(self, message, errors=None): - super(CravatQueryException, self).__init__(message) - # Support for custom error codes - self.errors = errors - - -class CravatQuery(object): - """ - : A class for handling Cravat query strings. - : Args (all required): - : chr - Chromosome - : pos - Position - : strand - Strand - : ref - Reference Base - : alt - Alternate Base - """ - - # The endpoint that CravatQuerys are submitted to - endpoint = 'http://cravat.us/CRAVAT/rest/service/query' - - # The value delimiter used in the Cravat input file to delimit values - delimiter = "\t" - - # Defualt indices for intepretting a cravat file's row of data in to a CravatQuery - default_mapping = { - 'chromosome': 1, - 'position': 2, - 'strand': 3, - 'reference': 4, - 'alternate': 5 - } - - # Defualt values. Used as backup for CravatQuery to resolve query with incomplete information - default_values = { - 'strand': '+' - } - - # The neccessary attributes neeeded to submit a query. - query_keys = [ - 'chromosome', 'position', 'strand', 'reference', 'alternate' - ] - - # Expected response keys from server. Ordered in list so that galaxy output has uniform column ordering run-to-run. - # If cravat server returns additional keys, they are appended to and included in output. - response_keys = [ - "Chromosome", "Position", "Strand", "Reference base(s)", "Alternate base(s)", - "HUGO symbol", "S.O. transcript", "Sequence ontology protein change", "Sequence ontology", - "S.O. all transcripts", "gnomAD AF", "gnomAD AF (African)", "gnomAD AF (Amrican)", - "gnomAD AF (Ashkenazi Jewish)", "gnomAD AF (East Asian)", "gnomAD AF (Finnish)", - "gnomAD AF (Non-Finnish European)", "gnomAD AF (Other)", "gnomAD AF (South Asian)", - "1000 Genomes AF", "ESP6500 AF (average)", "ESP6500 AF (European American)", - "ESP6500 AF (African American)", "COSMIC transcript", "COSMIC protein change", - "COSMIC variant count [exact nucleotide change]", "cosmic_site_nt", "CGL driver class", - "TARGET", "dbSNP", "cgc_role", "cgc_inheritance", "cgc_tumor_type_somatic", - "cgc_tumor_type_germline", "ClinVar", "ClinVar disease identifier", "ClinVar XRef", - "GWAS Phenotype (GRASP)", "GWAS PMID (GRASP)", "Protein 3D variant" - ] - - - def __init__(self, _chr, pos, strand, ref, alt): - # '_chr' used to avoid naming confliction with python built-in 'chr' - self.chromosome = CravatQuery.format_chromosome(_chr) - self.position = pos - self.strand = strand - self.reference = ref - self.alternate = alt - self.values = [self.chromosome, self.position, self.strand, self.reference, self.alternate] - - - def __str__(self): - """ : Represent the CravatQuery as a valid query string for call to Cravat server """ - return "_".join(map(lambda x: str(x), self.values)) - - - def as_query_string(self): - return str(self) - - - @staticmethod - def from_dictionary(d): - """ - : Instantiate a CravatQuery from a dictionary representation. - : Args: - : d <dictionary>: A dictionary representing a CravatQuery, containing keys: [{}] - """.format(CravatQuery.query_keys) - - for key in CravatQuery.query_keys: - if key not in d: - raise CravatQueryException("CravatQuery.from_dictionary requires keys: [{}], however key: '{}' was not provided " - .format(CravatQuery.query_keys, key)) - return CravatQuery(d["chromosome"], d["position"], d["strand"], d["reference"], d["alternate"]) - - - @staticmethod - def from_array(array, mapping=None): - """ - : Instantiate a CravatQuery from an array of values. Useful when translating read lines from a file. - : Args: - : fmt <str> - Either 'cr' or 'vcf', describing input format - : array <list> - The values to instantiate the CravatQuery from - : mapping <dict> - Optional. A dictionary associating cravat parameters to indicies in the array. - Valid values are: 'chromosome', 'position', 'strand', 'reference', 'alternate' - """ - - # Set the mapping value. Either recieved from user, or obtained via defualt associated to 'fmt' - if mapping == None: - mapping = CravatQuery.default_mapping - - # Build a dict of cravat querying keys to values. - d = {} - for key in CravatQuery.query_keys: - # Try to get index position from mapping by the key, and value from array by the index - if key in mapping: - index = mapping[key] - d[key] = array[index] - # If index not provided in mapping, check if there is a defualt value - elif key in CravatQuery.default_values: - d[key] = CravatQuery.default_values[key] - # Unable to get value for querying key, meaning can't construct the minimum requirements for query - else: - raise CravatQueryException("CravatQuery.from_array requires a mapping index for key: '{}', however value was not provided".format(key)) - return CravatQuery.from_dictionary(d) - - - - @staticmethod - def format_chromosome(_chr): - """ - : Format a chromosome for use as query parameter. '_chr' name used to avoid python built-in name confliction. - : Args: - : _chr - Either an interger [1,23], or 'x'/'X', or 'y'/'Y', or a string of the form - : 'chr<z>' where '<z>' is one of the previously described values - """ - inRange = lambda x: 1 <= x and x <= 23 - _chr = _chr.lower() - _chr = _chr.strip('chr') - # Handler interger chromosomes 1 to 23 - try: - _chr = int(_chr) - if inRange(_chr): - return 'chr' + str(_chr) - else: - raise CravatQueryException("Chromsomme of '{}' was out of range [1,23]".format(_chr)) - except: - pass - # Handle chromosomes chromosomes x and y - if _chr == 'x' or _chr == 'y': - return 'chr' + _chr - raise CravatQueryException("Unable to resolve input: '{}' into a valid chromosome representation".format(_chr)) - - - @staticmethod - def jump_header(in_file, out_file, headerlines=0): - """ - : Jumps over a header space of line number 'headerlines'. Sets up in_file so that - : the next execution of in_file.readline() will return the first non-header line. - """ - in_file.seek(0) - for line in range(headerlines): - in_file.readline() - - -def main(in_path, out_path, pre_callback=None, user_mapping=None): - """ - : Read the file line by line and use data to query cravat server. - : Args: - : fmt <str>: 'cr' or 'vcf'. The input format - : in_path <str>: Path to input file - : in_path <str>: Path to output file - : header_callback <function>: A function to handle the header space. Executed - before main loop. Recieves in_file, out_file, and fmt as argumnets - """ - - with open(in_path, 'r') as in_file, \ - open(out_path, 'w') as out_file: - - # Perform any pre-processing steps, such as jumping a header space - if pre_callback: - pre_callback(in_file, out_file, fmt) - - # main loop - for line in in_file: - - # Create query from line of input data - line = line.strip().split('\t') - query = CravatQuery.from_array(line, user_mapping) - # Make request, and write respone data - call = requests.get(CravatQuery.endpoint, params={ 'mutation': query.as_query_string }) - try: - if call.status_code != 200 or call.text == "": - raise CravatQueryException("Bad Server Response. Respone code: '{}', Response Text: '{}'".format(call.status_code, call.text)) - json_response = json.loads(call.text) - wrote = False - for key, val in json_response.items(): - # Set numeric values to uniform format - try: - val = float(val) - val = format(val, ".4f") - except: - pass - if wrote: - out_file.write("\t") - out_file.write(val) - wrote = True - out_file.write("\n") - except CravatQueryException as e: - print(e) - - - - -if __name__ == "__main__": - - # Input and output file paths, obtained form command line - in_path = sys.argv[1] - out_path = sys.argv[2] - - # Possibly allow user mapping configuration thourgh here. Not fully implemented - if len(sys.argv) > 2: - user_mapping = sys.argv[3] - - # Run the main operation - main(in_path, out_path) \ No newline at end of file
--- a/cravat_annotate/cravat_annotate.xml Tue Jun 12 12:05:48 2018 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -<tool id="cravat_query" name="CRAVAT Query" version="1.0.0"> - <description>Queries CRAVAT for cancer annotation</description> - <command interpreter="python">cravat_annotate.py $input $output</command> - - <inputs> - <param format="tabular" name="input" type="data" label="Source file"/> - </inputs> - - <outputs> - <data format="tabular" name="output" /> - </outputs> - - <tests> - <test> - <param name="input" value="input_call.txt"/> - <output name="output" file="Galaxy23-[CRAVAT_Query_on_data_22].tabular"/> - </test> - </tests> - - <help> - This tool queries CRAVAT for cancer annotation. - </help> - -</tool> -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cravat_convert/base_converter.py Tue Jun 12 14:03:57 2018 -0400 @@ -0,0 +1,22 @@ +class BaseConverter(object): + def __init__(self): + self.format_name = None + def check_format(self,*args,**kwargs): + err_msg = 'Converter for %s format has no method check_format' %\ + self.format_name + raise NotImplementedError(err_msg) + def setup(self,*args,**kwargs): + err_msg = 'Converter for %s format has no method setup' %\ + self.format_name + raise NotImplementedError(err_msg) + def convert_line(self,*args,**kwargs): + err_msg = 'Converter for %s format has no method convert_line' %\ + self.format_name + raise NotImplementedError(err_msg) + + +class BadFormatError(Exception): + def __init__(self, message, errors=None): + super(BadFormatError, self).__init__(message) + # Support for custom error codes, if added later + self.errors = errors \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cravat_convert/cravat_convert.py Tue Jun 12 14:03:57 2018 -0400 @@ -0,0 +1,77 @@ +''' +Convert a VCF format file to Cravat format file +''' + +import os +import argparse +from vcf_converter import CravatConverter + +# File read/write configuration variables +vcf_sep = '\t' +cr_sep = '\t' +cr_newline = '\n' + +# VCF Headers mapped to their index position in a row of VCF values +vcf_mapping = { + 'CHROM': 0, + 'POS': 1, + 'ID': 2, + 'REF': 3, + 'ALT': 4, + 'QUAL': 5, + 'FILTER': 6, + 'INFO': 7, + 'FORMAT': 8, + 'NA00001': 9, + 'NA00002': 10, + 'NA00003': 11 +} + + +def get_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--input', + '-i', + required = True, + help='Input path to a VCF file for conversion',) + parser.add_argument('--output', + '-o', + default = os.path.join(os.getcwd(), "cravat_converted.txt"), + help = 'Output path to write the cravat file to') + return parser.parse_args() + + +def convert(in_path, out_path=None): + if not out_path: + base, _ = os.path.split(in_path) + out_path = os.path.join(base, "cravat_converted.txt") + + with open(in_path, 'r') as in_file, \ + open(out_path, 'w') as out_file: + + # cr_count will be used to generate the 'TR' field of the cravat rows (first header) + cr_count = 0 + # VCF lines are always assumed to be '+' strand, as VCF doesn't specify that attribute + strand = '+' + # VCF converter. Adjusts position, reference, and alternate for Cravat formatting. + converter = CravatConverter() + + for line in in_file: + if line.startswith("#"): + continue + line = line.strip().split(vcf_sep) + # row is dict of VCF headers mapped to corresponding values of this line + row = { header: line[index] for header, index in vcf_mapping.items() } + for alt in row["ALT"].split(","): + new_pos, new_ref, new_alt = converter.extract_vcf_variant(strand, row["POS"], row["REF"], alt) + new_pos, new_ref, new_alt = str(new_pos), str(new_ref), str(new_alt) + cr_line = cr_sep.join([ + 'TR' + str(cr_count), row['CHROM'], new_pos, strand, new_ref, new_alt, row['ID'] + ]) + out_file.write(cr_line + cr_newline) + cr_count += 1 + + +if __name__ == "__main__": + cli_args = get_args() + convert(cli_args.input, cli_args.output)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cravat_convert/cravat_convert.xml Tue Jun 12 14:03:57 2018 -0400 @@ -0,0 +1,20 @@ +<tool id="cravat_convert" name="CRAVAT Convert" version="1.0.0"> + <description>Converts a VCF format file to a Cravat format file</description> + <command interpreter="python">cravat_convert.py -i $input -o $output</command> + + <inputs> + <param format="tabular" name="input" type="data" label="Source file"/> + </inputs> + + <outputs> + <data format="tabular" name="output" /> + </outputs> + + <!-- <tests></tests> --> + + <help> + Converts a VCF format file to a Cravat format file + </help> + +</tool> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cravat_convert/vcf_converter.py Tue Jun 12 14:03:57 2018 -0400 @@ -0,0 +1,243 @@ +""" +A module originally obtained from the cravat package. Modified to use in the vcf +converter galaxy tool. + + +Register of changes made (Chris Jacoby): + 1) Changed imports as galaxy tool won't have access to complete cravat python package + 2) Defined BadFormatError in BaseConverted file, as I didn't have the BadFormatError module +""" + +from base_converter import BaseConverter, BadFormatError +import re + +class CravatConverter(BaseConverter): + + def __init__(self): + self.format_name = 'vcf' + self.samples = [] + self.var_counter = 0 + self.addl_cols = [{'name':'phred', + 'title':'Phred', + 'type':'string'}, + {'name':'filter', + 'title':'VCF filter', + 'type':'string'}, + {'name':'zygosity', + 'title':'Zygosity', + 'type':'string'}, + {'name':'alt_reads', + 'title':'Alternate reads', + 'type':'int'}, + {'name':'tot_reads', + 'title':'Total reads', + 'type':'int'}, + {'name':'af', + 'title':'Variant allele frequency', + 'type':'float'}] + + def check_format(self, f): + return f.readline().startswith('##fileformat=VCF') + + def setup(self, f): + + vcf_line_no = 0 + for line in f: + vcf_line_no += 1 + if len(line) < 6: + continue + if line[:6] == '#CHROM': + toks = re.split('\s+', line.rstrip()) + if len(toks) > 8: + self.samples = toks[9:] + break + + def convert_line(self, l): + if l.startswith('#'): return None + self.var_counter += 1 + toks = l.strip('\r\n').split('\t') + all_wdicts = [] + if len(toks) < 8: + raise BadFormatError('Wrong VCF format') + [chrom, pos, tag, ref, alts, qual, filter, info] = toks[:8] + if tag == '': + raise BadFormatError('ID column is blank') + elif tag == '.': + tag = 'VAR' + str(self.var_counter) + if chrom[:3] != 'chr': + chrom = 'chr' + chrom + alts = alts.split(',') + len_alts = len(alts) + if len(toks) == 8: + for altno in range(len_alts): + wdict = None + alt = alts[altno] + newpos, newref, newalt = self.extract_vcf_variant('+', pos, ref, alt) + wdict = {'tags':tag, + 'chrom':chrom, + 'pos':newpos, + 'ref_base':newref, + 'alt_base':newalt, + 'sample_id':'no_sample', + 'phred': qual, + 'filter': filter} + all_wdicts.append(wdict) + elif len(toks) > 8: + sample_datas = toks[9:] + genotype_fields = {} + genotype_field_no = 0 + for genotype_field in toks[8].split(':'): + genotype_fields[genotype_field] = genotype_field_no + genotype_field_no += 1 + if not ('GT' in genotype_fields): + raise BadFormatError('No GT Field') + gt_field_no = genotype_fields['GT'] + for sample_no in range(len(sample_datas)): + sample = self.samples[sample_no] + sample_data = sample_datas[sample_no].split(':') + gts = {} + for gt in sample_data[gt_field_no].replace('/', '|').split('|'): + if gt == '.': + continue + else: + gts[int(gt)] = True + for gt in sorted(gts.keys()): + wdict = None + if gt == 0: + continue + else: + alt = alts[gt - 1] + newpos, newref, newalt = self.extract_vcf_variant('+', pos, ref, alt) + zyg = self.homo_hetro(sample_data[gt_field_no]) + depth, alt_reads, af = self.extract_read_info(sample_data, gt, gts, genotype_fields) + + wdict = {'tags':tag, + 'chrom':chrom, + 'pos':newpos, + 'ref_base':newref, + 'alt_base':newalt, + 'sample_id':sample, + 'phred': qual, + 'filter': filter, + 'zygosity': zyg, + 'tot_reads': depth, + 'alt_reads': alt_reads, + 'af': af, + } + all_wdicts.append(wdict) + return all_wdicts + + #The vcf genotype string has a call for each allele separated by '\' or '/' + #If the call is the same for all allels, return 'hom' otherwise 'het' + def homo_hetro(self, gt_str): + if '.' in gt_str: + return ''; + + gts = gt_str.strip().replace('/', '|').split('|') + for gt in gts: + if gt != gts[0]: + return 'het' + return 'hom' + + #Extract read depth, allele count, and allele frequency from optional VCR information + def extract_read_info (self, sample_data, gt, gts, genotype_fields): + depth = '' + alt_reads = '' + ref_reads = '' + af = '' + + #AD contains 2 values usually ref count and alt count unless there are + #multiple alts then it will have alt 1 then alt 2. + if 'AD' in genotype_fields and genotype_fields['AD'] <= len(sample_data): + if 0 in gts.keys(): + #if part of the genotype is reference, then AD will have #ref reads, #alt reads + ref_reads = sample_data[genotype_fields['AD']].split(',')[0] + alt_reads = sample_data[genotype_fields['AD']].split(',')[1] + elif gt == max(gts.keys()): + #if geontype has multiple alt bases, then AD will have #alt1 reads, #alt2 reads + alt_reads = sample_data[genotype_fields['AD']].split(',')[1] + else: + alt_reads = sample_data[genotype_fields['AD']].split(',')[0] + + if 'DP' in genotype_fields and genotype_fields['DP'] <= len(sample_data): + depth = sample_data[genotype_fields['DP']] + elif alt_reads != '' and ref_reads != '': + #if DP is not present but we have alt and ref reads count, dp = ref+alt + depth = int(alt_reads) + int(ref_reads) + + if 'AF' in genotype_fields and genotype_fields['AF'] <= len(sample_data): + af = float(sample_data[genotype_fields['AF']] ) + elif depth != '' and alt_reads != '': + #if AF not specified, calc it from alt and ref reads + af = float(alt_reads) / float(depth) + + return depth, alt_reads, af + + def extract_vcf_variant (self, strand, pos, ref, alt): + + reflen = len(ref) + altlen = len(alt) + + # Returns without change if same single nucleotide for ref and alt. + if reflen == 1 and altlen == 1 and ref == alt: + return pos, ref, alt + + # Trimming from the start and then the end of the sequence + # where the sequences overlap with the same nucleotides + new_ref2, new_alt2, new_pos = \ + self.trimming_vcf_input(ref, alt, pos, strand) + + if new_ref2 == '': + new_ref2 = '-' + if new_alt2 == '': + new_alt2 = '-' + + return new_pos, new_ref2, new_alt2 + + # This function looks at the ref and alt sequences and removes + # where the overlapping sequences contain the same nucleotide. + # This trims from the end first but does not remove the first nucleotide + # because based on the format of VCF input the + # first nucleotide of the ref and alt sequence occur + # at the position specified. + # End removed first, not the first nucleotide + # Front removed and position changed + def trimming_vcf_input(self, ref, alt, pos, strand): + pos = int(pos) + reflen = len(ref) + altlen = len(alt) + minlen = min(reflen, altlen) + new_ref = ref + new_alt = alt + new_pos = pos + # Trims from the end. Except don't remove the first nucleotide. + # 1:6530968 CTCA -> GTCTCA becomes C -> GTC. + for nt_pos in range(0, minlen - 1): + if ref[reflen - nt_pos - 1] == alt[altlen - nt_pos - 1]: + new_ref = ref[:reflen - nt_pos - 1] + new_alt = alt[:altlen - nt_pos - 1] + else: + break + new_ref_len = len(new_ref) + new_alt_len = len(new_alt) + minlen = min(new_ref_len, new_alt_len) + new_ref2 = new_ref + new_alt2 = new_alt + # Trims from the start. 1:6530968 G -> GT becomes 1:6530969 - -> T. + for nt_pos in range(0, minlen): + if new_ref[nt_pos] == new_alt[nt_pos]: + if strand == '+': + new_pos += 1 + elif strand == '-': + new_pos -= 1 + new_ref2 = new_ref[nt_pos + 1:] + new_alt2 = new_alt[nt_pos + 1:] + else: + new_ref2 = new_ref[nt_pos:] + new_alt2 = new_alt[nt_pos:] + break + return new_ref2, new_alt2, new_pos + + +if __name__ == "__main__": + c = CravatConverter() \ No newline at end of file