File size: 8,925 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
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
"""CLI experiment command and sub-commands."""

import json
import os
import shutil
import zipfile
from pathlib import Path
from typing import List, Optional

import requests
import typer
from rich import print  # pylint:disable=redefined-builtin
from rich.console import Console
from rich.table import Table
from typing_extensions import Annotated

from folding_studio.config import API_URL, REQUEST_TIMEOUT
from folding_studio.utils.headers import get_auth_headers

app = typer.Typer(
    no_args_is_help=True,
    help=(
        "Get experiment information and metadata, like its status, "
        "results or the generated features (msa, templates, etc.).\n"
        "Read more at https://int-bio-foldingstudio-gcp.nw.r.appspot.com/how-to-guides/af2_openfold/fetch_folding_job_status/."
    ),
)

experiment_ID_argument = typer.Argument(help="ID of the experiment.")


def _download_file_from_signed_url(
    exp_id: str,
    endpoint: str,
    output: Path,
    force: bool,
    unzip: bool = False,
) -> None:
    """Download a zip file from an experiment id.

    Args:
        exp_id (str): Experiment id.
        endpoint (str): API endpoint to call.
        output (Path): Output file path.
        force (bool): Force file writing if it already exists.
        unzip (bool): Unzip the zip file after downloading.

    Raises:
        typer.Exit: If output file path exists but force set to false.
        typer.Exit: If unzip set to true but the directory already exists and force set to false.
        typer.Exit: If an error occurred during the initial API call.
    """
    if output.exists() and not force:
        print(
            f"Warning: The file '{output}' already exists. Use the --force flag to overwrite it."
        )
        raise typer.Exit(code=1)

    if unzip:
        if not output.suffix == ".zip":
            print(
                "Error: The downloaded file is not a .zip file. Please ensure the correct file format."
            )
            raise typer.Exit(code=1)

        dir_path = output.with_suffix("")
        if dir_path.exists() and not force:
            print(
                f"Warning: The --unzip flag is raised but the directory '{dir_path}' "
                "already exists. Use the --force flag to overwrite it."
            )
            raise typer.Exit(code=1)

    headers = get_auth_headers()
    url = API_URL + endpoint

    response = requests.get(
        url,
        params={"experiment_id": exp_id},
        headers=headers,
        timeout=REQUEST_TIMEOUT,
    )
    if not response.ok:
        print(f"Failed to download the file: {response.content.decode()}.")
        raise typer.Exit(code=1)

    file_response = requests.get(
        response.json()["signed_url"],
        stream=True,
        timeout=REQUEST_TIMEOUT,
    )
    with output.open("wb") as f:
        file_response.raw.decode_content = True
        shutil.copyfileobj(file_response.raw, f)
    print(f"File downloaded successfully to {output}.")

    if unzip:
        dir_path.mkdir(parents=True, exist_ok=True)
        with zipfile.ZipFile(output, "r") as zip_ref:
            zip_ref.extractall(dir_path)
            print(f"Extracted all files to {dir_path}.")


@app.command()
def status(
    exp_id: Annotated[str, experiment_ID_argument],
):
    """Get an experiment status."""
    headers = get_auth_headers()
    url = API_URL + "getExperimentStatus"
    response = requests.get(
        url,
        params={"experiment_id": exp_id},
        headers=headers,
        timeout=REQUEST_TIMEOUT,
    )

    if not response.ok:
        print(f"An error occurred : {response.content.decode()}")
        raise typer.Exit(code=1)

    message = response.json()
    print(message["status"])


@app.command()
def list(
    limit: Annotated[
        int,
        typer.Option(
            help=("Max number of experiment to display in the terminal."),
        ),
    ] = 100,
    output: Annotated[
        Optional[Path],
        typer.Option(
            "--output",
            "-o",
            help=(
                "Path to the file where the job metadata returned by the server are written."
            ),
        ),
    ] = None,
):  # pylint:disable=redefined-builtin
    """Get all your done and pending experiment ids. The IDs are provided in the order of submission, starting with the most recent."""
    headers = get_auth_headers()
    url = API_URL + "getDoneAndPendingExperiments"
    response = requests.get(
        url,
        headers=headers,
        timeout=REQUEST_TIMEOUT,
    )

    if not response.ok:
        print(f"An error occurred : {response.content.decode()}")
        raise typer.Exit(code=1)

    response_json = response.json()
    if output:
        with open(output, "w") as f:
            json.dump(response_json, f, indent=4)

    print(f"Done and pending experiments list written to [bold]{output}[/bold]")

    table = Table(title="Done and pending experiments")

    table.add_column("Experiment ID", justify="right", style="cyan", no_wrap=True)
    table.add_column("Status", style="magenta")

    total_exp_nb = 0
    for status, exp_list in response_json.items():
        total_exp_nb += len(exp_list)
        for exp in exp_list:
            table.add_row(exp, status)
        if limit < total_exp_nb:
            print(
                f"The table below is truncated to the last [bold]{limit}[/bold] submitted experiments. Increase '--limit' to see more."
            )
            if not output:
                print("Use '--output' to get the full list in file format.")
            else:
                print(f"See the full list in file format at [bold]{output}[/bold]")
            break

    console = Console()
    console.print(table)


@app.command()
def features(
    exp_id: Annotated[str, experiment_ID_argument],
    output: Annotated[
        Optional[Path],
        typer.Option(
            help="Local path to download the zip to. Default to '<exp_id>_features.zip'."
        ),
    ] = None,
    force: Annotated[
        bool,
        typer.Option(
            help=(
                "Forces the download to overwrite any existing file "
                "with the same name in the specified location."
            )
        ),
    ] = False,
    unzip: Annotated[
        bool, typer.Option(help="Automatically unzip the file after its download.")
    ] = False,
):
    """Get an experiment features."""
    if output is None:
        output = Path(f"{exp_id}_features.zip")

    _download_file_from_signed_url(
        exp_id=exp_id,
        endpoint="getZippedExperimentFeatures",
        output=output,
        force=force,
        unzip=unzip,
    )


@app.command()
def results(
    exp_id: Annotated[str, experiment_ID_argument],
    output: Annotated[
        Optional[Path],
        typer.Option(
            help="Local path to download the zip to. Default to '<exp_id>_results.zip'."
        ),
    ] = None,
    force: Annotated[
        bool,
        typer.Option(
            help=(
                "Forces the download to overwrite any existing file "
                "with the same name in the specified location."
            )
        ),
    ] = False,
    unzip: Annotated[
        bool, typer.Option(help="Automatically unzip the file after its download.")
    ] = False,
):
    """Get an experiment results."""

    if output is None:
        output = Path(f"{exp_id}_results.zip")

    _download_file_from_signed_url(
        exp_id=exp_id,
        endpoint="getZippedExperimentResults",
        output=output,
        force=force,
        unzip=unzip,
    )


@app.command()
def cancel(
    exp_id: Annotated[List[str], experiment_ID_argument],
):
    """Cancel experiments job executions.

    You can pass one or more experiment id
    """
    headers = get_auth_headers()

    url = API_URL + "cancelJob"
    response = requests.post(
        url,
        data={"experiment_ids": exp_id},
        headers=headers,
        timeout=REQUEST_TIMEOUT,
    )

    if not response.ok:
        print(f"An error occurred : {response.content.decode()}")
        raise typer.Exit(code=1)
    message = response.json()
    print(message)


@app.command()
def logs(
    exp_id: Annotated[str, experiment_ID_argument],
    output: Annotated[
        Optional[Path],
        typer.Option(
            help="Local path to download the logs to. Default to '<exp_id>_logs.txt'."
        ),
    ] = None,
    force: Annotated[
        bool,
        typer.Option(
            help=(
                "Forces the download to overwrite any existing file "
                "with the same name in the specified location."
            )
        ),
    ] = False,
):
    """Get an experiment logs."""
    if output is None:
        output = Path(f"{exp_id}_logs.txt")

    _download_file_from_signed_url(
        exp_id=exp_id,
        endpoint="getExperimentLogs",
        output=output,
        force=force,
    )