{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.13"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":116062,"databundleVersionId":14084779,"sourceType":"competition"},{"sourceId":270542431,"sourceType":"kernelVersion"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":2428.344014,"end_time":"2025-10-24T14:04:25.598116","environment_variables":{},"exception":true,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-10-24T13:23:57.254102","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"43a6280a","cell_type":"code","source":"# https://www.kaggle.com/competitions/cafa-6-protein-function-prediction/discussion/613138\nimport gzip\n\ndef create_submission_pipeline():\n    # Paths\n    test_fasta_path = \"/kaggle/input/cafa-6-protein-function-prediction/Test/testsuperset.fasta\"\n    gaf_path = \"/kaggle/input/gaf-gz/temp_gaf.gz\"\n    output_path = \"submission.tsv\"\n    \n    print(\"Step 1: Reading test proteins...\")\n    test_proteins = set()\n    with open(test_fasta_path, 'r') as f:\n        for line in f:\n            if line.startswith('>'):\n                header = line[1:].strip()\n                parts = header.split('|')\n                protein_id = parts[1] if len(parts) >= 2 else header.split()[0]\n                test_proteins.add(protein_id)\n    \n    print(f\"Found {len(test_proteins)} test proteins\")\n    print(\"Sample protein IDs:\", list(test_proteins)[:5])\n    \n    print(\"Step 2: Processing GAF file line by line...\")\n    \n    # Process GAF line by line to avoid memory issues\n    protein_go_map = {}\n    line_count = 0\n    \n    with gzip.open(gaf_path, 'rt') as f:\n        for line in f:\n            line_count += 1\n            if line_count % 100000000 == 0:\n                print(f\"Processed {line_count:,} lines...\")\n                \n            # Skip comment lines\n            if line.startswith('!'):\n                continue\n                \n            # Parse tab-separated fields\n            fields = line.strip().split('\\t')\n            if len(fields) < 5:\n                continue\n                \n            protein_id = fields[1]\n            qualifier = fields[3]\n            go_id = fields[4]\n            \n            # Skip if not in test set or has NOT qualifier\n            if protein_id not in test_proteins or 'NOT' in qualifier:\n                continue\n                \n            # Add to our mapping\n            if protein_id not in protein_go_map:\n                protein_go_map[protein_id] = set()\n            protein_go_map[protein_id].add(go_id)\n    \n    print(f\"Finished processing {line_count:,} lines\")\n    \n    print(\"Step 3: Creating submission file in correct format...\")\n    \n    # Create submission in the correct format: protein_id, go_term, probability\n    with open(output_path, 'w') as f:\n        for protein_id in test_proteins:\n            go_terms = protein_go_map.get(protein_id, set())\n            for go_term in sorted(go_terms):\n                # Assign probability 1.0 to all predictions for baseline\n                f.write(f\"{protein_id}\\t{go_term}\\t1.0\\n\")\n    \n    # Print stats\n    total_predictions = sum(len(go_terms) for go_terms in protein_go_map.values())\n    proteins_with_preds = len(protein_go_map)\n    \n    print(f\"\\nSubmission completed!\")\n    print(f\"Total proteins: {len(test_proteins)}\")\n    print(f\"Proteins with predictions: {proteins_with_preds}\")\n    print(f\"Total predictions: {total_predictions}\")\n    print(f\"Submission saved to: {output_path}\")\n\n# Run the pipeline\nif __name__ == \"__main__\":\n    create_submission_pipeline()","metadata":{"papermill":{"duration":2421.902555,"end_time":"2025-10-24T14:04:24.650507","exception":true,"start_time":"2025-10-24T13:24:02.747952","status":"failed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null}]}