{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Working with the Taxonomy Information**","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-27T20:10:55.079076Z","iopub.execute_input":"2023-04-27T20:10:55.080240Z","iopub.status.idle":"2023-04-27T20:11:06.930755Z","shell.execute_reply.started":"2023-04-27T20:10:55.080194Z","shell.execute_reply":"2023-04-27T20:11:06.929316Z"}}},{"cell_type":"code","source":"!pip install taxoniq\nimport numpy as np \nimport pandas as pd \nimport os\nfrom Bio import SeqIO\n# a python package for taxonomy\nimport taxoniq","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:11:12.299525Z","iopub.execute_input":"2023-04-27T20:11:12.299998Z","iopub.status.idle":"2023-04-27T20:11:24.168456Z","shell.execute_reply.started":"2023-04-27T20:11:12.299954Z","shell.execute_reply":"2023-04-27T20:11:24.167240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define a LCA function hopefully useful","metadata":{}},{"cell_type":"code","source":"# find the LCA of two species using taxoniq\ndef lca(t0, t1):\n    parents_t0 = set()\n    t = t0\n    while t != taxoniq.Taxon(1):\n        parents_t0.add(t.tax_id)\n        t = t.parent\n    parents_t0.add(t.tax_id)\n    t = t1\n    while t != taxoniq.Taxon(1):\n        if t.tax_id in parents_t0:\n            return t\n        t = t.parent","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:02:18.433578Z","iopub.execute_input":"2023-04-27T20:02:18.433969Z","iopub.status.idle":"2023-04-27T20:02:18.441509Z","shell.execute_reply.started":"2023-04-27T20:02:18.433917Z","shell.execute_reply":"2023-04-27T20:02:18.440474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the training and test taxonomy information","metadata":{}},{"cell_type":"code","source":"train_taxonomy = pd.read_csv(\"/kaggle/input/cafa-5-protein-function-prediction/Train/train_taxonomy.tsv\",sep=\"\\t\")\ntest_taxonomy_id_list = pd.read_csv(\"/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset-taxon-list.tsv\",sep=\"\\t\", encoding = 'unicode_escape')\n\nfn = '/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset.fasta'\nsequences = SeqIO.parse(fn, \"fasta\")\ndict_id_species = {}\nset_test_taxonomy_id = set()\nfor m in sequences:\n    dict_id_species[m.id] = int(m.description.split('\\t')[1])\n    set_test_taxonomy_id.add(int(m.description.split('\\t')[1]))\n\n# check whether all test species are in the training set\nvec_counts_taxonomyid = train_taxonomy['taxonomyID'].value_counts()\ncounts_taxonomyid = train_taxonomy['taxonomyID'].value_counts().to_dict()\nset_train_taxonomy_id = set(counts_taxonomyid.keys())\nclass bcolors:\n    HEADER = '\\033[95m'\n    OKBLUE = '\\033[94m'\n    OKCYAN = '\\033[96m'\n    OKGREEN = '\\033[92m'\n    WARNING = '\\033[93m'\n    FAIL = '\\033[91m'\n    ENDC = '\\033[0m'\n    BOLD = '\\033[1m'\n    UNDERLINE = '\\033[4m'\nfor taxonomy_id in set_test_taxonomy_id:\n    if taxonomy_id in counts_taxonomyid:\n        print(\"taxonomy_id:\", taxonomy_id, \", scientific name:\", taxoniq.Taxon(taxonomy_id).scientific_name, \", counts\", counts_taxonomyid[taxonomy_id])\n    else:\n        print(\"taxonomy_id:\", taxonomy_id, \", scientific name:\", taxoniq.Taxon(taxonomy_id).scientific_name, \",\", bcolors.WARNING, \"not found\", bcolors.ENDC)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:45:17.609204Z","iopub.execute_input":"2023-04-27T20:45:17.609646Z","iopub.status.idle":"2023-04-27T20:45:19.700318Z","shell.execute_reply.started":"2023-04-27T20:45:17.609607Z","shell.execute_reply":"2023-04-27T20:45:19.698793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# find LCA of two species","metadata":{}},{"cell_type":"code","source":"# find LCA of two species\nspecies = vec_counts_taxonomyid.keys()\ns_lca = lca(taxoniq.Taxon(species[0]), taxoniq.Taxon(species[1]))\nprint(\"LCA of\\n\", taxoniq.Taxon(species[0]).tax_id, taxoniq.Taxon(species[0]).scientific_name, \"\\n\",\n      taxoniq.Taxon(species[1]).tax_id, taxoniq.Taxon(species[1]).scientific_name, \"\\n\",\n      \"is\\n\",\n      s_lca.tax_id, s_lca.scientific_name)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:17:13.921997Z","iopub.execute_input":"2023-04-27T20:17:13.922407Z","iopub.status.idle":"2023-04-27T20:17:13.932528Z","shell.execute_reply.started":"2023-04-27T20:17:13.922372Z","shell.execute_reply":"2023-04-27T20:17:13.930904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ranks = set()\nfor rank in taxoniq.Rank:\n    ranks.add(rank)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:43:04.817958Z","iopub.execute_input":"2023-04-27T20:43:04.818434Z","iopub.status.idle":"2023-04-27T20:43:04.824657Z","shell.execute_reply.started":"2023-04-27T20:43:04.818390Z","shell.execute_reply":"2023-04-27T20:43:04.823132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tax = taxoniq.Taxon(9606)\nprint(tax.scientific_name, \",\", tax.rank)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:44:27.538596Z","iopub.execute_input":"2023-04-27T20:44:27.539062Z","iopub.status.idle":"2023-04-27T20:44:27.546105Z","shell.execute_reply.started":"2023-04-27T20:44:27.539020Z","shell.execute_reply":"2023-04-27T20:44:27.544831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set_train_taxonomy_id), len(set_test_taxonomy_id)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:45:55.010129Z","iopub.execute_input":"2023-04-27T20:45:55.010598Z","iopub.status.idle":"2023-04-27T20:45:55.018839Z","shell.execute_reply.started":"2023-04-27T20:45:55.010558Z","shell.execute_reply":"2023-04-27T20:45:55.017467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_ranks = set()\ncount_not_found = 0\nfor taxid in set_train_taxonomy_id:\n    try:\n        tax = taxoniq.Taxon(taxid)\n        print(taxid, tax.scientific_name, \",\", tax.rank)\n        unique_ranks.add(tax.rank)\n    except:\n        count_not_found += 1\n        print(taxid, bcolors.WARNING, \"not found\", bcolors.ENDC)\nprint(\"\\nIn total\", count_not_found, \"species not found\")\nprint()\nfor taxid in set_test_taxonomy_id:\n    tax = taxoniq.Taxon(taxid)\n    unique_ranks.add(tax.rank)\n    print(taxid, tax.scientific_name, \",\", tax.rank)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:58:50.299124Z","iopub.execute_input":"2023-04-27T20:58:50.299598Z","iopub.status.idle":"2023-04-27T20:58:50.447321Z","shell.execute_reply.started":"2023-04-27T20:58:50.299554Z","shell.execute_reply":"2023-04-27T20:58:50.445995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_ranks","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:58:57.026557Z","iopub.execute_input":"2023-04-27T20:58:57.027058Z","iopub.status.idle":"2023-04-27T20:58:57.035625Z","shell.execute_reply.started":"2023-04-27T20:58:57.027014Z","shell.execute_reply":"2023-04-27T20:58:57.034059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for tax in taxoniq.Taxon(39947).lineage:\n    print(tax.scientific_name)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:14:25.408778Z","iopub.execute_input":"2023-04-27T21:14:25.409930Z","iopub.status.idle":"2023-04-27T21:14:25.418259Z","shell.execute_reply.started":"2023-04-27T21:14:25.409875Z","shell.execute_reply":"2023-04-27T21:14:25.416711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_tax = []\nfor tax in taxoniq.Taxon(1).child_nodes:\n    list_tax = list_tax + tax.child_nodes\n    print(tax.scientific_name)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:22:58.742389Z","iopub.execute_input":"2023-04-27T21:22:58.742845Z","iopub.status.idle":"2023-04-27T21:22:58.751417Z","shell.execute_reply.started":"2023-04-27T21:22:58.742805Z","shell.execute_reply":"2023-04-27T21:22:58.749772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for tax in list_tax:\n    print(tax.scientific_name)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:23:18.566766Z","iopub.execute_input":"2023-04-27T21:23:18.567212Z","iopub.status.idle":"2023-04-27T21:23:18.574261Z","shell.execute_reply.started":"2023-04-27T21:23:18.567173Z","shell.execute_reply":"2023-04-27T21:23:18.573198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(taxoniq.Taxon(131567).scientific_name)\ntaxoniq.Taxon(131567).child_nodes","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:26:52.920684Z","iopub.execute_input":"2023-04-27T21:26:52.921201Z","iopub.status.idle":"2023-04-27T21:26:52.930294Z","shell.execute_reply.started":"2023-04-27T21:26:52.921159Z","shell.execute_reply":"2023-04-27T21:26:52.928808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for tax in taxoniq.Taxon(367110).lineage:\n    print(tax.scientific_name)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:28:21.380624Z","iopub.execute_input":"2023-04-27T21:28:21.381064Z","iopub.status.idle":"2023-04-27T21:28:21.389030Z","shell.execute_reply.started":"2023-04-27T21:28:21.381026Z","shell.execute_reply":"2023-04-27T21:28:21.387564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(taxoniq.Taxon(2759).scientific_name)\nfor tax in taxoniq.Taxon(2759).child_nodes:\n    print(tax.scientific_name)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:29:33.013604Z","iopub.execute_input":"2023-04-27T21:29:33.014212Z","iopub.status.idle":"2023-04-27T21:29:33.022385Z","shell.execute_reply.started":"2023-04-27T21:29:33.014158Z","shell.execute_reply":"2023-04-27T21:29:33.021178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(taxoniq.Taxon(33154).scientific_name)\nprint()\nfor tax in taxoniq.Taxon(33154).child_nodes:\n    print(tax.scientific_name)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:34:25.270973Z","iopub.execute_input":"2023-04-27T21:34:25.271401Z","iopub.status.idle":"2023-04-27T21:34:25.278753Z","shell.execute_reply.started":"2023-04-27T21:34:25.271363Z","shell.execute_reply":"2023-04-27T21:34:25.277203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxoniq.Taxon(9606).lineage","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:33:29.433046Z","iopub.execute_input":"2023-04-27T21:33:29.433508Z","iopub.status.idle":"2023-04-27T21:33:29.442631Z","shell.execute_reply.started":"2023-04-27T21:33:29.433468Z","shell.execute_reply":"2023-04-27T21:33:29.441040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for taxid in set_test_taxonomy_id:\n    tax = taxoniq.Taxon(taxid)\n    unique_ranks.add(tax.rank)\n    print(taxid, tax.scientific_name, \",\", tax.rank)\n    for tax_1 in tax.lineage:\n        print(tax_1.scientific_name, end=\", \")\n    print()\n    print()","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:37:18.652722Z","iopub.execute_input":"2023-04-27T21:37:18.653195Z","iopub.status.idle":"2023-04-27T21:37:18.702584Z","shell.execute_reply.started":"2023-04-27T21:37:18.653155Z","shell.execute_reply":"2023-04-27T21:37:18.701129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for taxid in set_test_taxonomy_id:\n    tax = taxoniq.Taxon(taxid)\n    print(taxid, tax.scientific_name)\n    if taxoniq.Taxon(2) in tax.lineage:\n        print(taxoniq.Taxon(2).scientific_name)\n    elif taxoniq.Taxon(2157) in tax.lineage:\n        print(taxoniq.Taxon(2157).scientific_name)\n    elif taxoniq.Taxon(2759) in tax.lineage:\n        print(taxoniq.Taxon(2759).scientific_name)\n        for tax_1 in taxoniq.Taxon(2759).child_nodes:\n            if tax_1 in tax.lineage:\n                print(\"--\", tax_1.scientific_name)\n    else:\n        print(\"not of these\")\n    print()\n    #unique_ranks.add(tax.rank)\n    #print(taxid, tax.scientific_name, \",\", tax.rank)\n    #for tax_1 in tax.lineage:\n    #    print(tax_1.scientific_name, end=\", \")\n    #print()\n    #print()","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:51:01.129684Z","iopub.execute_input":"2023-04-27T21:51:01.130229Z","iopub.status.idle":"2023-04-27T21:51:01.367635Z","shell.execute_reply.started":"2023-04-27T21:51:01.130183Z","shell.execute_reply":"2023-04-27T21:51:01.366181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxoniq.Taxon(131567).child_nodes","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:41:43.725276Z","iopub.execute_input":"2023-04-27T21:41:43.725710Z","iopub.status.idle":"2023-04-27T21:41:43.733695Z","shell.execute_reply.started":"2023-04-27T21:41:43.725675Z","shell.execute_reply":"2023-04-27T21:41:43.732402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxoniq.Taxon(2759).scientific_name","metadata":{"execution":{"iopub.status.busy":"2023-04-27T21:47:39.046919Z","iopub.execute_input":"2023-04-27T21:47:39.047817Z","iopub.status.idle":"2023-04-27T21:47:39.055341Z","shell.execute_reply.started":"2023-04-27T21:47:39.047765Z","shell.execute_reply":"2023-04-27T21:47:39.054308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}