File size: 13,076 Bytes
b8a625b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6354ea8
 
 
 
 
 
 
 
 
b8a625b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""Utils for validating the FASTA files for the AF3 like models."""

import logging
import re
import shutil
from abc import abstractmethod
from collections import defaultdict
from enum import Enum
from pathlib import Path

from Bio import SeqIO


class EntityType(str, Enum):
    """Enum for the entity type of a given sequence."""

    DNA = "dna"
    RNA = "rna"
    PROTEIN = "protein"
    PEPTIDE = "peptide"
    ION = "ion"
    LIGAND = "ligand"
    SMILES = "smiles"
    CCD = "ccd"


def get_entity_type(sequence: str) -> EntityType:
    """Get the entity type of a given sequence.

    The entity type is determined based on the sequence composition.

    Args:
        sequence (str): The input sequence.

    Returns:
        EntityType: The entity type of the input sequence.
    """
    DNA_SEQUENCE_SET = set("ACGT")
    RNA_SEQUENCE_SET = set("ACGU")
    PROTEIN_SEQUENCE_SET = set("ACDEFGHIKLMNPQRSTVWY")

    # Detect IONS (e.g., Mg2+, Na+, Cl-)
    if re.fullmatch(r"[A-Za-z]{1,2}[\d\+\-]*", sequence):
        return EntityType.ION

    # Detect DNA
    if set(sequence.upper()).issubset(DNA_SEQUENCE_SET):
        return EntityType.DNA

    # Detect RNA
    elif set(sequence.upper()).issubset(RNA_SEQUENCE_SET):
        return EntityType.RNA

    # Detect PROTEIN
    elif set(sequence.upper()).issubset(PROTEIN_SEQUENCE_SET):
        return EntityType.PROTEIN

    # Default to LIGAND
    return EntityType.LIGAND


def has_multiple_chains(header: str) -> bool:
    """Check if a given header contains multiple chains in RCSB format.

    A header with multiple chains will have the following in the header:
    ```
    Chains A, B, C, ...
    ```
    where `A`, `B`, `C`, ... are the chain identifiers.


    Args:
        header (str): The input header string containing chain information.

    Returns:
        bool: True if the header contains multiple chains, False otherwise.
    """
    match = re.search(r"chains?\s+([A-Za-z, ]+)", header, re.I)
    return len(match.group(1).replace(" ", "").split(",")) > 1 if match else False


class BaseFastaValidator:
    """Base class for validating FASTA files."""

    @abstractmethod
    def is_valid_fasta(self, fasta_path: Path) -> tuple[bool, str | None]:
        """Validate whether a given FASTA file follows the required format.

        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            tuple[bool, str | None]: Tuple containing a boolean indicating if the format is correct and an error message if not
        """
        raise NotImplementedError("Subclasses must implement this method")

    @abstractmethod
    def transform_fasta(self, fasta_path: Path) -> str:
        """Transform a FASTA file into the required format.

        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            Transformed FASTA content in the required format.
        """
        raise NotImplementedError("Subclasses must implement this method")

    def process_directory(self, input_dir: str, output_dir: str) -> None:
        """Process all FASTA files in the input directory, validate or transform them, and save them to the output directory.

        Args:
            input_dir (str): Path to the directory containing FASTA files.
            output_dir (str): Path to the output directory where processed files will be saved.
        """

        output_path = Path(output_dir)
        output_path.mkdir(parents=True, exist_ok=True)

        for fasta_file in Path(input_dir).glob("*.fasta"):
            output_file = output_path / fasta_file.name
            if has_multiple_chains(fasta_file.read_text()):
                logging.warning(
                    f"Skipping {fasta_file} because it contains multiple chains in a single sequence.\n"
                    "Please split multiple chains into separate sequences using the following format:\n"
                    ">Chain A\n"
                    "MTEIVLKFL...\n"
                    ">Chain B\n"
                    "MTEIVLKFL...\n\n"
                    "Instead of:\n"
                    ">Chains A, B\n"
                    "MTEIVLKFL..."
                )
                continue
            if self.is_valid_fasta(fasta_file):
                shutil.copy(fasta_file, output_file)
            else:
                transformed_content = self.transform_fasta(fasta_file)
                output_file.write_text(transformed_content)


class BoltzFastaValidator(BaseFastaValidator):
    """Validate whether a given FASTA file follows the required format for Boltz."""

    SUPPORTED_ENTITY_TYPES = {
        EntityType.PROTEIN,
        EntityType.RNA,
        EntityType.DNA,
        EntityType.SMILES,
        EntityType.CCD,
    }

    def is_valid_fasta(self, fasta_path: Path) -> tuple[bool, str | None]:
        """Validate whether a given FASTA file follows the required format.

        The expected FASTA header format is:
        ```
        >CHAIN_ID|ENTITY_TYPE
        ```
        where `ENTITY_TYPE` must be one of: "protein", "rna", "dna", "smiles" or "ccd".

        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            tuple[bool, str | None]: Tuple containing a boolean indicating if the format is correct and an error message if not
        """
        with fasta_path.open("r") as f:
            for record in SeqIO.parse(f, "fasta"):
                header_parts = record.id.split("|")
                if not (1 < len(header_parts) <= 3):
                    msg = "BOLTZ Validation Error: Invalid header format. Expected '>CHAIN_ID|ENTITY_TYPE'"
                    return False, msg
                if header_parts[1].lower() not in self.SUPPORTED_ENTITY_TYPES:
                    return (
                        False,
                        f"BOLTZ Validation Error: Invalid entity type '{header_parts[1]}'. Supported types: {', '.join(self.SUPPORTED_ENTITY_TYPES)}",
                    )
        return True, None

    def transform_fasta(self, fasta_path: Path) -> str:
        """Transform a FASTA file into the '>CHAIN_ID|ENTITY_TYPE|MSA_ID' format.

        This function extracts chain identifiers from the FASTA header and determines
        the entity type (DNA, RNA, or PROTEIN) based on the sequence composition.

        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            Transformed FASTA content in the required format.
        """
        transformed_lines = []

        with fasta_path.open("r") as f:
            for record_index, record in enumerate(SeqIO.parse(f, "fasta")):
                chain = chr(ord("A") + record_index)
                # extract entity type
                entity_type = get_entity_type(str(record.seq))
                transformed_lines.append(f">{chain.upper()}|{entity_type.value}")
                # append sequence
                transformed_lines.append(str(record.seq))

        return "\n".join(transformed_lines)


class ChaiFastaValidator(BaseFastaValidator):
    """Validate whether a given FASTA file follows the required format for Chai."""

    SUPPORTED_ENTITY_TYPES = EntityType.__members__.values()

    def is_valid_fasta(self, fasta_path: Path) -> tuple[bool, str | None]:
        """Validate whether a given FASTA file follows the required format.

        The expected FASTA header format is:
        ```
        >ENTITY_TYPE|name=NAME
        ```
        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            tuple[bool, str | None]: Tuple containing a boolean indicating if the format is correct and an error message if not
        """

        seen_names = set()
        with fasta_path.open("r") as f:
            for record in SeqIO.parse(f, "fasta"):
                # validate header format
                match = re.match(r"^([A-Za-z]+)\|name=([\w\-]+)$", record.description)
                if not match:
                    return (
                        False,
                        "CHAI Validation Error: Invalid header format. Expected '>ENTITY_TYPE|name=NAME'",
                    )
                # validate entity type
                entity_type, name = match.groups()
                if entity_type not in self.SUPPORTED_ENTITY_TYPES or not name:
                    return (
                        False,
                        f"CHAI Validation Error: Invalid entity type '{entity_type}'. Supported types: {', '.join(self.SUPPORTED_ENTITY_TYPES)}",
                    )
                # check uniqueness of name
                if name in seen_names:
                    return (
                        False,
                        f"CHAI Validation Error: Duplicate name '{name}'. Each sequence must have a unique name",
                    )
                seen_names.add(name)
                # validate sequence format
                sequence = str(record.seq).strip()
                if (
                    entity_type in {EntityType.PEPTIDE, EntityType.PROTEIN}
                    and not get_entity_type(sequence) == entity_type
                ):
                    return (
                        False,
                        f"CHAI Validation Error: Sequence type mismatch. Expected '{entity_type}' but found '{get_entity_type(sequence)}'",
                    )

        return True, None

    def transform_fasta(self, fasta_path: Path) -> str:
        """Transform a FASTA file into the '>TYPE|name=NAME' format by ensuring each main header
        is unique (adding a number if necessary).

        The expected output format is:
            '>protein|name=NAME'
            'SEQUENCE'

        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            Transformed FASTA content in the required Chai format.
        """
        transformed_lines = []
        header_map = {}

        with fasta_path.open("r") as f:
            for record in SeqIO.parse(f, "fasta"):
                main_header = record.description.split("|")[0].strip()

                if main_header not in header_map:
                    header_map[main_header] = 1
                    updated_header = main_header

                else:
                    header_map[main_header] += 1
                    updated_header = main_header + "_" + str(header_map[main_header])

                entity_type = get_entity_type(str(record.seq))
                header = f">{entity_type.value}|name={updated_header}"

                transformed_lines.append(header)
                transformed_lines.append(str(record.seq))

        return "\n".join(transformed_lines)


class ProtenixFastaValidator(BaseFastaValidator):
    """Validate whether a given FASTA file follows the required format for Protenix."""

    def is_valid_fasta(self, fasta_path: Path) -> tuple[bool, str | None]:
        """Validate whether a given FASTA file follows the required format.

        The expected FASTA header format is:
        ```
        > UNIQUE ID[|...]
        ```

        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            tuple[bool, str | None]: Tuple containing a boolean indicating if the format is correct and an error message if not
        """
        seen_headers = set()

        with fasta_path.open("r") as f:
            for record in SeqIO.parse(f, "fasta"):
                main_header = record.description.split("|")[0].strip()
                if main_header in seen_headers:
                    return (
                        False,
                        f"PROTENIX Validation Error: Duplicate header '{main_header}'. Each sequence must have a unique header",
                    )
                seen_headers.add(main_header)

        return True, None

    def transform_fasta(self, fasta_path: Path) -> str:
        """Transform a FASTA file into the '>NAME|Chain X' format by ensuring each main header
        is unique (adding a number if necessary).

        The expected output format is:
            '>protein_1 | Chain A'
            'SEQUENCE'
            '>protein_2 | Chain B'
            'SEQUENCE'

        Args:
            fasta_path (Path): Path to the FASTA file.

        Returns:
            Transformed FASTA content in the required Protenix format.
        """
        transformed_lines = []
        header_count = defaultdict(int)

        with fasta_path.open("r") as f:
            for record in SeqIO.parse(f, "fasta"):
                header_parts = [part.strip() for part in record.description.split("|")]
                main_header = header_parts[0]

                # Ensure unique headers
                header_count[main_header] += 1
                updated_main_header = (
                    f"{main_header}_{header_count[main_header]}"
                    if header_count[main_header] > 1
                    else main_header
                )

                transformed_lines.append(f">{updated_main_header}")
                transformed_lines.append(str(record.seq))

        return "\n".join(transformed_lines)