File size: 5,662 Bytes
44459bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

import pytest
from folding_studio.cli import app
from folding_studio.query import ChaiQuery
from typer.testing import CliRunner

runner = CliRunner()


RESTRAINTS_CSV_CONTENT = """
chainA,res_idxA,chainB,res_idxB,connection_type,confidence,min_distance_angstrom,max_distance_angstrom,comment,restraint_id
A,C387,B,Y101,contact,1.0,0.0,5.5,protein-heavy,restraint_1
C,I32,A,S483,contact,1.0,0.0,5.5,protein-light,restraint_2
"""


@pytest.fixture(scope="module")
def tmp_files(tmp_directory: Path, tmp_files: dict):
    """Generate temporary files."""

    # A valid restraints CSV file
    valid_restraints = tmp_directory / "example_restraints.csv"
    valid_restraints.write_text(RESTRAINTS_CSV_CONTENT)

    tmp_files["valid_restraints"] = valid_restraints

    yield tmp_files


def test_chai_fold_with_invalid_source(tmp_files):
    """
    Running the command with an unsupported file type should return an error.
    """
    result = runner.invoke(
        app,
        [
            "predict",
            "chai",
            str(tmp_files["invalid_source"]),
            "--project-code",
            "TEST_PROJECT",
        ],
    )
    assert result.exit_code != 0


def test_chai_fold_with_valid_fasta_file(
    mock_send_request, mock_download_results, tmp_files
):
    """
    Running the command with a valid FASTA file (without restraints) should
    process successfully and generate the correct payload.
    """
    result = runner.invoke(
        app,
        [
            "predict",
            "chai",
            str(tmp_files["monomer_fasta"]),
            "--project-code",
            "TEST_PROJECT",
            "--output",
            tmp_files["output_dir"],
        ],
    )
    assert result.exit_code == 0, result.output

    expected_query = ChaiQuery.from_file(
        path=str(tmp_files["monomer_fasta"]),
        use_msa_server=True,
        use_templates_server=False,
        num_trunk_recycles=3,
        seed=0,
        num_diffn_timesteps=200,
        restraints=None,
        recycle_msa_subsample=0,
        num_trunk_samples=1,
    )
    mock_send_request.assert_called_once()
    actual_query = mock_send_request.call_args[0][0]
    payload = actual_query.payload
    expected = expected_query.payload
    assert payload == expected

    mock_download_results.assert_called_once()


def test_chai_fold_with_valid_fasta_and_restraints(
    mock_send_request, mock_download_results, tmp_files
):
    """
    Running the command with a valid FASTA file and a valid restraints CSV file
    should include the restraints content in the payload.
    """
    result = runner.invoke(
        app,
        [
            "predict",
            "chai",
            str(tmp_files["monomer_fasta"]),
            "--project-code",
            "TEST_PROJECT",
            "--restraints",
            str(tmp_files["valid_restraints"]),
            "--output",
            tmp_files["output_dir"],
        ],
    )
    assert result.exit_code == 0, result.output

    restraints_content = tmp_files["valid_restraints"].read_text().strip()

    expected_query = ChaiQuery.from_file(
        path=str(tmp_files["monomer_fasta"]),
        use_msa_server=True,
        use_templates_server=False,
        num_trunk_recycles=3,
        seed=0,
        num_diffn_timesteps=200,
        restraints=str(tmp_files["valid_restraints"]),
        recycle_msa_subsample=0,
        num_trunk_samples=1,
    )
    mock_send_request.assert_called_once()
    actual_query = mock_send_request.call_args[0][0]
    payload = actual_query.payload
    expected = expected_query.payload
    assert payload == expected
    assert payload["restraints"] == restraints_content

    mock_download_results.assert_called_once()


def test_chai_fold_with_valid_fasta_directory(
    mock_send_request, mock_download_results, tmp_files
):
    """
    Running the command with a directory containing valid FASTA files should
    process successfully and submit a single request for the directory.
    """
    result = runner.invoke(
        app,
        [
            "predict",
            "chai",
            str(tmp_files["valid_dir"]),
            "--project-code",
            "TEST_PROJECT",
            "--output",
            tmp_files["output_dir"],
        ],
    )
    assert result.exit_code == 0, result.output

    expected_query = ChaiQuery.from_directory(
        path=str(tmp_files["valid_dir"]),
        use_msa_server=True,
        use_templates_server=False,
        num_trunk_recycles=3,
        seed=0,
        num_diffn_timesteps=200,
        restraints=None,
        recycle_msa_subsample=0,
        num_trunk_samples=1,
    )

    expected_fasta_files = expected_query.payload["fasta_files"]

    actual_fasta_files = [
        call_args[0][0].payload["fasta_files"]
        for call_args in mock_send_request.call_args_list
    ]

    # Check that the expected FASTA files match one of the actual request payloads
    assert expected_fasta_files in actual_fasta_files, (
        f"FASTA file {expected_fasta_files} was expected but not found in actual requests."
    )

    mock_download_results.assert_called()



def test_chai_fold_with_empty_directory(tmp_files):
    """
    Running the command with an empty directory should return an error,
    since no FASTA files are available.
    """
    result = runner.invoke(
        app,
        [
            "predict",
            "chai",
            str(tmp_files["empty_dir"]),
            "--project-code",
            "TEST_PROJECT",
            "--output",
            tmp_files["output_dir"],
        ],
    )
    assert result.exit_code != 0, result.output