|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import string |
| 7 | + |
| 8 | +def parse_args(args=None): |
| 9 | + """ |
| 10 | + Parse command line arguments for the script. |
| 11 | +
|
| 12 | + Required arguments: |
| 13 | + FILE_IN: Input fasta file path |
| 14 | + ID: Identifier for the protein sequence (will be used in output filename and JSON) |
| 15 | +
|
| 16 | + Optional arguments: |
| 17 | + -ms/--model_seed: AlphaFold3 model seed(s) to use (default: [11]) |
| 18 | + """ |
| 19 | + Description = "Convert fasta files to Alphafold3 json format." |
| 20 | + Epilog = "Example usage: python fasta_to_alphafold3_json.py <FILE_IN> <ID>" |
| 21 | + |
| 22 | + parser = argparse.ArgumentParser(description=Description, epilog=Epilog) |
| 23 | + |
| 24 | + ## REQUIRED PARAMETERS |
| 25 | + parser.add_argument( |
| 26 | + "FILE_IN", |
| 27 | + help="Input fasta file." |
| 28 | + ) |
| 29 | + parser.add_argument( |
| 30 | + "ID", |
| 31 | + help="ID for file name and for json id tag." |
| 32 | + ) |
| 33 | + |
| 34 | + ## OPTIONAL PARAMETERS |
| 35 | + parser.add_argument( |
| 36 | + "-ms", |
| 37 | + "--model_seed", |
| 38 | + type=int, |
| 39 | + nargs='+', |
| 40 | + dest="MODEL_SEED", |
| 41 | + default=[11], |
| 42 | + help="Alphafold 3 model seed." |
| 43 | + ) |
| 44 | + |
| 45 | + return parser.parse_args(args) |
| 46 | + |
| 47 | +def sanitised_name(id): |
| 48 | + """ |
| 49 | + Sanitize the input ID to create a valid filename. |
| 50 | +
|
| 51 | + This function is copied from AlphaFold3 source code to ensure consistent naming: |
| 52 | + https://github.com/google-deepmind/alphafold3/blob/7fdf96161d61a6e18048e5c62bf7e1d711992943/src/alphafold3/common/folding_input.py#L1166-L1170 |
| 53 | + It converts the ID to lowercase, replaces spaces with underscores, and removes |
| 54 | + any characters that aren't allowed in filenames. |
| 55 | +
|
| 56 | + Args: |
| 57 | + id (str): Input identifier |
| 58 | +
|
| 59 | + Returns: |
| 60 | + str: Sanitized version of the ID suitable for use as a filename |
| 61 | + """ |
| 62 | + lower_spaceless_name = id.lower().replace(' ', '_') |
| 63 | + allowed_chars = set(string.ascii_lowercase + string.digits + '_-.') |
| 64 | + return ''.join(l for l in lower_spaceless_name if l in allowed_chars) |
| 65 | + |
| 66 | +def fasta_to_alphafold3_json(file_in, id): |
| 67 | + """ |
| 68 | + Convert a single-sequence FASTA file to AlphaFold3 JSON format. |
| 69 | +
|
| 70 | + This function reads a FASTA file and converts it to the format required by AlphaFold3. |
| 71 | + It only processes single-sequence FASTA files and raises an error for multi-sequence files. |
| 72 | +
|
| 73 | + The function expects a samplesheet.csv with the following format: |
| 74 | + id,fasta |
| 75 | + T1024,path/to/T1024.fasta |
| 76 | + T1026,path/to/T1026.fasta |
| 77 | +
|
| 78 | + Args: |
| 79 | + file_in (str): Path to input FASTA file |
| 80 | + id (str): Identifier for the sequence |
| 81 | +
|
| 82 | + Returns: |
| 83 | + dict: Dictionary containing the sequence information in AlphaFold3 format |
| 84 | +
|
| 85 | + Raises: |
| 86 | + RuntimeError: If the input file contains multiple sequences |
| 87 | + """ |
| 88 | + sequence_list = [] |
| 89 | + sequence = None |
| 90 | + fasta_mapping_dict = {} |
| 91 | + |
| 92 | + with open(file_in, "r", encoding="utf-8-sig") as fin: |
| 93 | + n_seq = 0 |
| 94 | + for l in fin: |
| 95 | + l = l.strip() |
| 96 | + if l.startswith(">"): |
| 97 | + if n_seq > 1: |
| 98 | + raise RuntimeError("Multifasta files are not allowed") |
| 99 | + n_seq += 1 |
| 100 | + if sequence: |
| 101 | + sequence_list.append(sequence) |
| 102 | + sequence = {"id": id, "sequence": ""} |
| 103 | + else: |
| 104 | + sequence["sequence"] += l |
| 105 | + |
| 106 | + return sequence |
| 107 | + |
| 108 | +def create_json_dict(sequence, model_seed): |
| 109 | + """ |
| 110 | + Create the final JSON dictionary in AlphaFold3 format. |
| 111 | +
|
| 112 | + The function creates a JSON structure that follows AlphaFold3's requirements: |
| 113 | + { |
| 114 | + "name": "sequence_id", |
| 115 | + "sequences": [ |
| 116 | + { |
| 117 | + "protein": { |
| 118 | + "id": "A", |
| 119 | + "sequence": "protein_sequence" |
| 120 | + } |
| 121 | + } |
| 122 | + ], |
| 123 | + "modelSeeds": [seed_values], |
| 124 | + "dialect": "alphafold3", |
| 125 | + "version": 1 |
| 126 | + } |
| 127 | +
|
| 128 | + Args: |
| 129 | + sequence (dict): Dictionary containing sequence information |
| 130 | + model_seed (list): List of model seeds to use |
| 131 | +
|
| 132 | + Returns: |
| 133 | + dict: JSON-compatible dictionary in AlphaFold3 format |
| 134 | + """ |
| 135 | + json_sequence_dict = {} |
| 136 | + |
| 137 | + item = { |
| 138 | + "name": f"{sequence['id']}", |
| 139 | + "sequences": [ |
| 140 | + { |
| 141 | + "protein": { |
| 142 | + "id": "A", |
| 143 | + "sequence": sequence["sequence"] |
| 144 | + } |
| 145 | + }, |
| 146 | + ], |
| 147 | + "modelSeeds": model_seed, |
| 148 | + "dialect": "alphafold3", |
| 149 | + "version": 1 |
| 150 | + } |
| 151 | + |
| 152 | + json_sequence_dict[sequence["id"]] = item |
| 153 | + |
| 154 | + return json_sequence_dict |
| 155 | + |
| 156 | +def main(args=None): |
| 157 | + """ |
| 158 | + Main function to process FASTA files and create AlphaFold3 JSON files. |
| 159 | +
|
| 160 | + The script: |
| 161 | + 1. Parses command line arguments |
| 162 | + 2. Sanitizes the input ID for filename use |
| 163 | + 3. Reads and processes the FASTA file |
| 164 | + 4. Creates the JSON structure |
| 165 | + 5. Writes the output to a JSON file |
| 166 | +
|
| 167 | + The output filename will be the sanitized ID with .json extension. |
| 168 | + """ |
| 169 | + args = parse_args(args) |
| 170 | + id = args.ID |
| 171 | + |
| 172 | + if id.endswith(".json"): |
| 173 | + id = id[:-5] |
| 174 | + reformatted_id = sanitised_name(id) |
| 175 | + else: |
| 176 | + reformatted_id = sanitised_name(id) |
| 177 | + |
| 178 | + out_json = f"{reformatted_id}.json" |
| 179 | + |
| 180 | + sequence = fasta_to_alphafold3_json(args.FILE_IN, reformatted_id) |
| 181 | + json_dict = create_json_dict(sequence, args.MODEL_SEED) |
| 182 | + |
| 183 | + print ("json file " + out_json) |
| 184 | + with open(out_json, "w") as fout: |
| 185 | + json.dump(json_dict[reformatted_id], fout, indent=4) |
| 186 | + |
| 187 | + with open(out_json, 'r') as f: |
| 188 | + json_str = f.read() |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + sys.exit(main()) |
0 commit comments