{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":116062,"databundleVersionId":14875579,"sourceType":"competition"}],"dockerImageVersionId":31194,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\n\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","trusted":true,"execution":{"iopub.status.busy":"2025-12-11T04:44:10.415809Z","iopub.execute_input":"2025-12-11T04:44:10.415961Z","iopub.status.idle":"2025-12-11T04:44:12.131479Z","shell.execute_reply.started":"2025-12-11T04:44:10.415945Z","shell.execute_reply":"2025-12-11T04:44:12.13073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install transformers accelerate sentencepiece biopython --quiet","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-11T04:44:18.507326Z","iopub.execute_input":"2025-12-11T04:44:18.507815Z","iopub.status.idle":"2025-12-11T04:45:28.938229Z","shell.execute_reply.started":"2025-12-11T04:44:18.507793Z","shell.execute_reply":"2025-12-11T04:45:28.937531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom transformers import T5Tokenizer,T5EncoderModel\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(\"Using device:\",device)\n\ntokenizer = T5Tokenizer.from_pretrained(\"Rostlab/prot_t5_xl_half_uniref50-enc\")\nmodel = T5EncoderModel.from_pretrained(\"Rostlab/prot_t5_xl_half_uniref50-enc\")\nmodel = model.to(device).half()\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-11T04:45:58.823315Z","iopub.execute_input":"2025-12-11T04:45:58.823656Z","iopub.status.idle":"2025-12-11T04:46:47.513204Z","shell.execute_reply.started":"2025-12-11T04:45:58.823628Z","shell.execute_reply":"2025-12-11T04:46:47.511379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import libraries\nimport pandas as pd\nimport numpy as np\nfrom Bio import SeqIO\nimport torch\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-11T04:46:55.323649Z","iopub.execute_input":"2025-12-11T04:46:55.324491Z","iopub.status.idle":"2025-12-11T04:46:55.387615Z","shell.execute_reply.started":"2025-12-11T04:46:55.324466Z","shell.execute_reply":"2025-12-11T04:46:55.386877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def seqs (fasta_path):\n  records = []\n  for record in SeqIO.parse(fasta_path,\"fasta\"):\n    records.append({\"id\":record.id,\"seq\":str(record.seq)})\n\n  return pd.DataFrame(records)\n\ntest_seqs = seqs(\"/kaggle/input/cafa-6-protein-function-prediction/Test/testsuperset.fasta\")\nprint(test_seqs.shape)\n\nsequences=test_seqs[\"seq\"].tolist()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-11T04:46:58.056086Z","iopub.execute_input":"2025-12-11T04:46:58.056378Z","iopub.status.idle":"2025-12-11T04:47:01.349666Z","shell.execute_reply.started":"2025-12-11T04:46:58.056356Z","shell.execute_reply":"2025-12-11T04:47:01.348769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm import tqdm\nimport numpy as np\n\ndef clean_seq(s):\n    s = s.upper()\n    return s.replace(\"U\",\"X\").replace(\"Z\",\"X\").replace(\"B\",\"X\").replace(\"J\",\"X\").replace(\"O\",\"X\")\n\nMAX_LEN = 510\n\ndef truncate_seq(s):\n    return s[:MAX_LEN]\n\ndef embed_batch(seqs):\n    spaced = [\" \".join(list(s)) for s in seqs]\n\n    tokens = tokenizer.batch_encode_plus(\n        spaced,\n        return_tensors=\"pt\",\n        padding=True,\n        truncation=True\n    ).to(device)\n\n    with torch.no_grad():\n        out = model(**tokens).last_hidden_state\n\n    emb = out.mean(dim=1)\n    return emb.cpu().numpy()\n\n\nbatch_size = 32\nall_embs = []\n\nfor i in tqdm(range(0, len(sequences), batch_size), desc=\"Embedding FASTA\"):\n    raw_batch = sequences[i:i+batch_size]\n    clean_batch = [truncate_seq(clean_seq(s)) for s in raw_batch]\n    embs = embed_batch(clean_batch)\n    all_embs.append(embs)\n\nembeddings = np.vstack(all_embs)\nembeddings.shape\nnp.save(\"/kaggle/working/test_prott5_embeddings.npy\",embeddings)\n\ntorch.save(test_embeddings, \"/kaggle/working/test_prott5_embeddings.pt\")\n\nprint(\"DONE:\", test_embeddings.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-11T04:47:07.917447Z","iopub.execute_input":"2025-12-11T04:47:07.918044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-11T04:43:36.410442Z","iopub.status.idle":"2025-12-11T04:43:36.410726Z","shell.execute_reply.started":"2025-12-11T04:43:36.410598Z","shell.execute_reply":"2025-12-11T04:43:36.410611Z"}},"outputs":[],"execution_count":null}]}