{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom Bio import SeqIO\nfrom Bio.Seq import Seq\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-27T07:41:46.664248Z","iopub.execute_input":"2023-06-27T07:41:46.66514Z","iopub.status.idle":"2023-06-27T07:41:46.808118Z","shell.execute_reply.started":"2023-06-27T07:41:46.665107Z","shell.execute_reply":"2023-06-27T07:41:46.807019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids_test = []\nfasta_sequences = SeqIO.parse(open('/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset.fasta'),'fasta')\nfor fasta in fasta_sequences:\n    name, sequence = fasta.id, str(fasta.seq)\n    #print(sequence)\n    ids_test.append(name)\n    #break\nprint(len(ids_test),len(set(ids_test)))\nids_test = set(ids_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T07:41:46.810148Z","iopub.execute_input":"2023-06-27T07:41:46.810819Z","iopub.status.idle":"2023-06-27T07:41:49.442118Z","shell.execute_reply.started":"2023-06-27T07:41:46.810781Z","shell.execute_reply":"2023-06-27T07:41:49.440787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids_train = []\nfasta_sequences = SeqIO.parse(open('/kaggle/input/cafa-5-protein-function-prediction/Train/train_sequences.fasta'),'fasta')\nfor fasta in fasta_sequences:\n    name, sequence = fasta.id, str(fasta.seq)\n    #print(sequence)\n    ids_train.append(name)\n    #break\nprint(len(ids_train),len(set(ids_train)))\nids_train = set(ids_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T07:41:49.443484Z","iopub.execute_input":"2023-06-27T07:41:49.443866Z","iopub.status.idle":"2023-06-27T07:41:51.738627Z","shell.execute_reply.started":"2023-06-27T07:41:49.443838Z","shell.execute_reply":"2023-06-27T07:41:51.737289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_test_ids = list(set(ids_test) & set(ids_train))\nlen(unique_test_ids)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T07:41:56.310695Z","iopub.execute_input":"2023-06-27T07:41:56.311825Z","iopub.status.idle":"2023-06-27T07:41:56.392107Z","shell.execute_reply.started":"2023-06-27T07:41:56.311789Z","shell.execute_reply":"2023-06-27T07:41:56.391042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = 0\nunique_test = open('unique_test.fa', 'w')\ndone = {}\nfasta_sequences = SeqIO.parse(open('/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset.fasta'), 'fasta')\nfasta_sequences2  = SeqIO.parse(open('//kaggle/input/cafa-5-protein-function-prediction/Train/train_sequences.fasta'), 'fasta')\n\nfor fasta in fasta_sequences:\n    if (fasta.id in unique_test_ids) and (fasta.id not in done):\n        \n        fasta.seq = Seq(str(fasta.seq).replace('U', 'C')\n                                     .replace('X', 'A')\n                                     .replace('Z', 'Q')\n                                     .replace('B', 'N')\n                                     .replace('O', 'K')\n                                     .replace('J', 'L'))\n        fasta.description = ''\n        SeqIO.write(fasta, unique_test, 'fasta')\n        done[fasta.id]=1\n        \n        a += 1\n        \nfor fasta in fasta_sequences2:\n    if (fasta.id in unique_test_ids) and (fasta.id not in done):\n        \n        fasta.seq = Seq(str(fasta.seq).replace('U', 'C')\n                                     .replace('X', 'A')\n                                     .replace('Z', 'Q')\n                                     .replace('B', 'N')\n                                     .replace('O', 'K')\n                                     .replace('J', 'L'))\n        fasta.description = ''\n        SeqIO.write(fasta, unique_test, 'fasta')\n        done[fasta.id]=1\n        \n        a += 1\n        \nunique_test.close()\nprint(a)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T08:08:02.499829Z","iopub.execute_input":"2023-06-27T08:08:02.500362Z","iopub.status.idle":"2023-06-27T08:08:09.656142Z","shell.execute_reply.started":"2023-06-27T08:08:02.500336Z","shell.execute_reply":"2023-06-27T08:08:09.655373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head unique_test.fa","metadata":{"execution":{"iopub.status.busy":"2023-06-27T08:08:09.657523Z","iopub.execute_input":"2023-06-27T08:08:09.657946Z","iopub.status.idle":"2023-06-27T08:08:10.673602Z","shell.execute_reply.started":"2023-06-27T08:08:09.657918Z","shell.execute_reply":"2023-06-27T08:08:10.672243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from Bio import SeqIO\nimport itertools\n\ndef batch(iterable, size):\n    it = iter(iterable)\n    while True:\n        chunk = tuple(itertools.islice(it, size))\n        if not chunk:\n            return\n        yield chunk\n\nfilename = \"unique_test.fa\"  # Input your fastq file name here\n\nrecord_iter = SeqIO.parse(filename, \"fasta\")\nfor i, batch_records in enumerate(batch(record_iter, 1000)):\n    with open(f\"input_{i+1}.fa\", \"w\") as handle:\n        \n        SeqIO.write(batch_records, handle, \"fasta\")\n","metadata":{"execution":{"iopub.status.busy":"2023-06-27T07:53:20.769521Z","iopub.execute_input":"2023-06-27T07:53:20.769913Z","iopub.status.idle":"2023-06-27T07:53:26.815857Z","shell.execute_reply.started":"2023-06-27T07:53:20.769885Z","shell.execute_reply":"2023-06-27T07:53:26.815025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#batch_records[0:10]","metadata":{"execution":{"iopub.status.busy":"2023-06-27T07:54:25.125303Z","iopub.execute_input":"2023-06-27T07:54:25.125683Z","iopub.status.idle":"2023-06-27T07:54:25.130052Z","shell.execute_reply.started":"2023-06-27T07:54:25.125652Z","shell.execute_reply":"2023-06-27T07:54:25.128939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f\"input_test_script.fa\", \"w\") as handle:\n    SeqIO.write(batch_records[0:100], handle, \"fasta\")","metadata":{"execution":{"iopub.status.busy":"2023-06-27T07:54:40.426563Z","iopub.execute_input":"2023-06-27T07:54:40.426947Z","iopub.status.idle":"2023-06-27T07:54:40.43331Z","shell.execute_reply.started":"2023-06-27T07:54:40.426915Z","shell.execute_reply":"2023-06-27T07:54:40.43242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}