Mercurial > repos > in_silico > cravat_vcf_convert
changeset 1:152cc8feff8a draft
Uploaded
author | in_silico |
---|---|
date | Tue, 12 Jun 2018 11:05:27 -0400 |
parents | 399f41a4bad6 |
children | c042835a7163 |
files | cravat_convert/cravat_convert.py cravat_convert/cravat_convert.xml cravat_submit/cravat_submit.py cravat_submit/cravat_submit.xml |
diffstat | 4 files changed, 100 insertions(+), 170 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cravat_convert/cravat_convert.py Tue Jun 12 11:05:27 2018 -0400 @@ -0,0 +1,80 @@ +''' +Convert a VCF format file to Cravat format file +''' + +import os +import argparse +from vcf_converter import CravatConverter +### +import ipdb + + +# 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 11:05:27 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> +
--- a/cravat_submit/cravat_submit.py Tue Jun 12 11:05:20 2018 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,136 +0,0 @@ -import requests -import json -import time -import urllib -import sys -import csv -import pdb - -input_filename = sys.argv[1] -input_select_bar = sys.argv[2] -output_filename = sys.argv[3] - -#in_file = open('input_call.txt', "r") -#out_file = open('output_call.txt', "w") - -write_header = True - -#plugs in params to given URL -submit = requests.post('http://staging.cravat.us/CRAVAT/rest/service/submit', files={'inputfile':open(input_filename)}, data={'email':'znylund@insilico.us.com', 'analyses': input_select_bar}) -#,'analysis':input_select_bar,'functionalannotation': "on"}) -#Makes the data a json dictionary, takes out only the job ID -jobid = json.loads(submit.text)['jobid'] -#out_file.write(jobid) -submitted = json.loads(submit.text)['status'] -#out_file.write('\t' + submitted) - -#loops until we find a status equal to Success, then breaks -while True: - check = requests.get('http://staging.cravat.us/CRAVAT/rest/service/status', params={'jobid': jobid}) - status = json.loads(check.text)['status'] - resultfileurl = json.loads(check.text)['resultfileurl'] - #out_file.write(str(status) + ', ') - pdb.set_trace() - if status == 'Success': - #out_file.write('\t' + resultfileurl) - break - else: - time.sleep(2) - -#out_file.write('\n') - -#creates three files -file_1 = time.strftime("%H:%M") + '_Z_Variant_Result.tsv' -file_2 = time.strftime("%H:%M") + '_Z_Additional_Details.tsv' -file_3 = time.strftime("%H:%M") + 'Combined_Variant_Results.tsv' - - -#Download the two results -urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant.Result.tsv", file_1) -urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant_Additional_Details.Result.tsv", file_2) - -headers = [] -duplicates = [] - -#opens the Variant Result file and the Variant Additional Details file as csv readers, then opens the output file (galaxy) as a writer -with open(file_1) as tsvin_1, open(file_2) as tsvin_2, open(output_filename, 'wb') as tsvout: - tsvreader_1 = csv.reader(tsvin_1, delimiter='\t') - tsvreader_2 = csv.reader(tsvin_2, delimiter='\t') - tsvout = csv.writer(tsvout, delimiter='\t') - -#loops through each row in the Variant Additional Details file - for row in tsvreader_2: - #sets row_2 equal to the same row in Variant Result file - row_2 = tsvreader_1.next() - #checks if row is empty or if the first term contains '#' - if row == [] or row[0][0] == '#': - continue - #checks if the row begins with input line - if row[0] == 'Input line': - #Goes through each value in the headers list in VAD - for value in row: - #Adds each value into headers - headers.append(value) - #Loops through the Keys in VR - for value in row_2: - #Checks if the value is already in headers - if value in headers: - continue - #else adds the header to headers - else: - headers.append(value) - - print headers - tsvout.writerow(headers) - - - else: - - cells = [] - #Goes through each value in the next list - for value in row: - #adds it to cells - cells.append(value) - #Goes through each value from the VR file after position 11 (After it is done repeating from VAD file) - for value in row_2[11:]: - #adds in the rest of the values to cells - cells.append(value) - - print cells - tsvout.writerow(cells) - - - - - - - - - - - - - - - - - - - - - - - -#a = 'col1\tcol2\tcol3' -#header_list = a.split('\t') - -#loop through the two results, when you first hit header you print out the headers in tabular form -#Print out each header only once -#Combine both headers into one output file -#loop through the rest of the data and assign each value to its assigned header -#combine this all into one output file - - - - -
--- a/cravat_submit/cravat_submit.xml Tue Jun 12 11:05:20 2018 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -<tool id="cravat_submit" name="CRAVAT Submit, Check, and Retrieve" version="0.1.0"> - <description>Submits, checks for, and retrieves data for cancer annotation</description> - <command interpreter="python">cravat_submit.py $input $dropdown $output</command> - - - <inputs> - - <param format="tabular" name="input" type="data" label="Source file"> </param> - <param format="tabular" name="dropdown" type="select" label="Analysis Program"> - <option value="">None</option> - <option value="VEST">VEST</option> - <option value="CHASM">CHASM</option> - <option value="VEST;CHASM">VEST and CHASM</option> - </param> - - - </inputs> - - <outputs> - <data format="tabular" name="output" /> - </outputs> - - <tests> - <test> - <param name="input" value="fa_gc_content_input.fa"/> - <output name="out_file1" file="fa_gc_content_output.txt"/> - </test> - </tests> - - <help> - This tool submits, checks for, and retrieves data for cancer annotation. - </help> - -</tool>