Previous changeset 10:152227fa7851 (2018-06-12) Next changeset 12:4b860b1f92fa (2018-06-12) |
Commit message:
Deleted selected files |
removed:
cravat_annotate/cravat_annotate.py |
b |
diff -r 152227fa7851 -r a9299c08850e cravat_annotate/cravat_annotate.py --- a/cravat_annotate/cravat_annotate.py Tue Jun 12 11:04:25 2018 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 |
[ |
b'@@ -1,261 +0,0 @@\n-"""\n-A galaxy wrapper for the /rest/service/query API endpoint on Cravat.\n-\n-\n-Notes on Mapping:\n------------------\n-The CravatQuery class uses static method \'from_array\' to interpret an array of values\n-into a query string for the /rest/service/query API service on the cravat server.\n-This involves using a mapping dictionary to know how to associate the array\'s index positions\n-in to query-ing attributes, such as the chromosome, position, etc. The CravatQuery\n-class contains a default value (\'default_mapping\'); however, this could also be\n-offered as a user-configurable option.\n-\n-\n-Remaining Items (including possible expansion features):\n------------------\n-TODO: Possibly provide user-configurability of CravatQuery array index mapping\n-TODO: Possibly provide user-configurability of delimiter value\n-TODO: Check if chromosomes are 0 or 1 based indexing\n-TODO: Port \'write headers\' option and include in user prompts in galaxy xml\n-TODO: Try-catch structure on the query call to cravat so if one bad query doesn\'t get back a response,\n-\t\tthe rest of the run can still execute. Report this to user.\n-"""\n-\n-\n-import requests\n-import json\n-import sys\n-import re\n-###\n-import ipdb\n-\n-\n-class CravatQueryException(Exception):\n-\n-\tdef __init__(self, message, errors=None):\t \n-\t\tsuper(CravatQueryException, self).__init__(message)\n-\t\t# Support for custom error codes\n-\t\tself.errors = errors\n-\n-\n-class CravatQuery(object):\n-\t"""\n-\t: A class for handling Cravat query strings.\n-\t: Args (all required):\n-\t:\tchr - Chromosome\n-\t:\tpos - Position\n-\t:\tstrand - Strand\n-\t:\tref - Reference Base\n-\t:\talt - Alternate Base\n-\t"""\n-\n-\t# The endpoint that CravatQuerys are submitted to\n-\tendpoint = \'http://cravat.us/CRAVAT/rest/service/query\'\n-\n-\t# The value delimiter used in the Cravat input file to delimit values\n-\tdelimiter = "\\t"\n-\n-\t# Defualt indices for intepretting a cravat file\'s row of data in to a CravatQuery\n-\tdefault_mapping = {\n-\t\t\'chromosome\': 1,\n-\t\t\'position\': 2,\n-\t\t\'strand\': 3,\n-\t\t\'reference\': 4,\n-\t\t\'alternate\': 5\n-\t}\n-\n-\t# Defualt values. Used as backup for CravatQuery to resolve query with incomplete information\n-\tdefault_values = {\n-\t\t\'strand\': \'+\'\n-\t}\n-\n-\t# The neccessary attributes neeeded to submit a query.\n-\tquery_keys = [\n-\t\t\'chromosome\', \'position\', \'strand\', \'reference\', \'alternate\'\n-\t]\n-\n-\t# Expected response keys from server. Ordered in list so that galaxy output has uniform column ordering run-to-run.\n-\t# If cravat server returns additional keys, they are appended to and included in output.\n-\tresponse_keys = [\n-\t\t"Chromosome", "Position", "Strand", "Reference base(s)", "Alternate base(s)",\n-\t \t"HUGO symbol", "S.O. transcript", "Sequence ontology protein change", "Sequence ontology",\n-\t\t"S.O. all transcripts", "gnomAD AF", "gnomAD AF (African)", "gnomAD AF (Amrican)",\n-\t\t"gnomAD AF (Ashkenazi Jewish)", "gnomAD AF (East Asian)", "gnomAD AF (Finnish)",\n-\t\t"gnomAD AF (Non-Finnish European)", "gnomAD AF (Other)", "gnomAD AF (South Asian)",\n-\t\t"1000 Genomes AF", "ESP6500 AF (average)", "ESP6500 AF (European American)",\n-\t\t"ESP6500 AF (African American)", "COSMIC transcript", "COSMIC protein change", \n-\t\t"COSMIC variant count [exact nucleotide change]", "cosmic_site_nt", "CGL driver class",\n-\t\t"TARGET", "dbSNP", "cgc_role", "cgc_inheritance", "cgc_tumor_type_somatic",\n-\t\t"cgc_tumor_type_germline", "ClinVar", "ClinVar disease identifier", "ClinVar XRef",\n-\t\t"GWAS Phenotype (GRASP)", "GWAS PMID (GRASP)", "Protein 3D variant"\n-\t]\n-\n-\n-\tdef __init__(self, _chr, pos, strand, ref, alt):\n-\t\t# \'_chr\' used to avoid naming confliction with python built-in \'chr\'\n-\t\tself.chromosome = CravatQuery.format_chromosome(_chr)\n-\t\tself.position = pos\n-\t\tself.strand = strand\n-\t\tself.reference = ref\n-\t\tself.alternate = alt\n-\t\tself.values = [self.chromosome, self.position, self.strand, self.reference, self.alternate]\n-\n-\n-\tdef __str__(self):\n-\t\t"""\n-\t\t: Represent the CravatQuery as a valid query string for call to Cravat server\n-\t\t"""\n-\t\treturn "_'..b'\tif mapping == None:\n-\t\t\tmapping = CravatQuery.default_mapping\n-\t\t\t\n-\t\t# Build a dict of cravat querying keys to values.\n-\t\td = {}\n-\t\tfor key in CravatQuery.query_keys:\n-\t\t\t# Try to get index position from mapping by the key, and value from array by the index\n-\t\t\tif key in mapping:\n-\t\t\t\tindex = mapping[key]\n-\t\t\t\td[key] = array[index]\n-\t\t\t# If index not provided in mapping, check if there is a defualt value\n-\t\t\telif key in CravatQuery.default_values:\n-\t\t\t\td[key] = CravatQuery.default_values[key]\n-\t\t\t# Unable to get value for querying key, meaning can\'t construct the minimum requirements for query\n-\t\t\telse:\n-\t\t\t\traise CravatQueryException("CravatQuery.from_array requires a mapping index for key: \'{}\', however value was not provided".format(key))\n-\t\treturn CravatQuery.from_dictionary(d)\n-\n-\n-\n-\t@staticmethod\n-\tdef format_chromosome(_chr):\n-\t\t"""\n-\t\t: Format a chromosome for use as query parameter. \'_chr\' name used to avoid python built-in name confliction.\n-\t\t: Args:\n-\t\t:\t_chr - Either an interger [1,23], or \'x\'/\'X\', or \'y\'/\'Y\', or a string of the form\n-\t\t:\t\t\t\'chr<z>\' where \'<z>\' is one of the previously described values \n-\t\t"""\n-\t\tinRange = lambda x: 1 <= x and x <= 23\n-\t\t_chr = _chr.lower()\n-\t\t_chr = _chr.strip(\'chr\')\n-\t\t# Handler interger chromosomes 1 to 23\n-\t\ttry:\n-\t\t\t_chr = int(_chr)\n-\t\t\tif inRange(_chr):\n-\t\t\t\treturn \'chr\' + str(_chr)\n-\t\t\telse:\n-\t\t\t\traise CravatQueryException("Chromsomme of \'{}\' was out of range [1,23]".format(_chr))\n-\t\texcept:\n-\t\t\tpass\n-\t\t# Handle chromosomes chromosomes x and y\n-\t\tif _chr == \'x\' or _chr == \'y\':\n-\t\t\treturn \'chr\' + _chr\n-\t\traise CravatQueryException("Unable to resolve input: \'{}\' into a valid chromosome representation".format(_chr))\n-\n-\n-\t@staticmethod\n-\tdef jump_header(in_file, out_file, headerlines=0):\n-\t\t"""\n-\t\t: Jumps over a header space of line number \'headerlines\'. Sets up in_file so that\n-\t\t: the next execution of in_file.readline() will return the first non-header line.\n-\t\t"""\n-\t\tin_file.seek(0)\n-\t\tfor line in range(headerlines):\n-\t\t\tin_file.readline()\n-\n-\n-def main(in_path, out_path, pre_callback=None, user_mapping=None):\n-\t"""\n-\t: Read the file line by line and use data to query cravat server.\n-\t: Args:\n-\t:\t- fmt <str>: \'cr\' or \'vcf\'. The input format\n-\t:\t- in_path <str>: Path to input file\n-\t:\t- in_path <str>: Path to output file\n-\t:\t- header_callback <function>: A function to handle the header space. Executed\n-\t\t\tbefore main loop. Recieves in_file, out_file, and fmt as argumnets\n-\t"""\n-\n-\twith open(in_path, \'r\') as in_file, \\\n-\topen(out_path, \'w\') as out_file:\n-\n-\t\t# Perform any pre-processing steps, such as jumping a header space\n-\t\tif pre_callback:\n-\t\t\tpre_callback(in_file, out_file, fmt)\n-\n-\t\t# main loop\n-\t\tfor line in in_file:\n-\n-\t\t\t# Create query from line of input data\n-\t\t\tline = line.strip().split(\'\\t\')\n-\t\t\tquery = CravatQuery.from_array(line, user_mapping)\n-\t\t\t# Make request, and write respone data\n-\t\t\tcall = requests.get(CravatQuery.endpoint, params={ \'mutation\': query.as_query_string })\n-\t\t\tipdb.set_trace()\n-\t\t\ttry:\n-\t\t\t\tif call.status_code != 200 or call.text == "":\n-\t\t\t\t\traise CravatQueryException("Bad Server Response. Respone code: \'{}\', Response Text: \'{}\'".format(call.status_code, call.text))\n-\t\t\t\tjson_response = json.loads(call.text)\n-\t\t\t\twrote = False\n-\t\t\t\tfor key, val in json_response.items():\n-\t\t\t\t\t# Set numeric values to uniform format\n-\t\t\t\t\ttry:\n-\t\t\t\t\t\tval = float(val)\n-\t\t\t\t\t\tval = format(val, ".4f")\n-\t\t\t\t\texcept:\n-\t\t\t\t\t\tpass\n-\t\t\t\t\tif wrote:\n-\t\t\t\t\t\tout_file.write("\\t")\n-\t\t\t\t\tout_file.write(val)\n-\t\t\t\t\twrote = True\n-\t\t\t\tout_file.write("\\n")\n-\t\t\texcept CravatQueryException as e:\n-\t\t\t\tprint(e)\n-\t\t\t\t\n-\t\t\n-\n-\n-if __name__ == "__main__":\n-\n-\t# Input and output file paths, obtained form command line\n-\tin_path = sys.argv[1]\n-\tout_path = sys.argv[2]\n-\n-\t# Possibly allow user mapping configuration thourgh here. Not fully implemented\n-\tif len(sys.argv) > 2:\n-\t\tuser_mapping = sys.argv[3]\n-\n-\t# Run the main operation\n-\tmain(in_path, out_path)\n\\ No newline at end of file\n' |