File size: 9,105 Bytes
5d2263b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import torch
import os
os.chdir('..')
from dataloader import CellLoader
from matplotlib import pyplot as plt
from celle_main import instantiate_from_config
from omegaconf import OmegaConf
from celle.utils import process_image

def run_model(mode, sequence,
            nucleus_image_path,
            protein_image_path,
            model_ckpt_path,
            model_config_path,
            device):
    if mode == "image":
        run_image_prediction(
            sequence,
            nucleus_image_path,
            protein_image_path,
            model_ckpt_path,
            model_config_path,
            device
        )
    elif mode == "sequence":
        run_sequence_prediction(
            sequence,
            nucleus_image_path,
            protein_image_path,
            model_ckpt_path,
            model_config_path,
            device
        )

def run_sequence_prediction(
    sequence_input,
    nucleus_image_path,
    protein_image_path,
    model_ckpt_path,
    model_config_path,
    device
):
    """
    Run Celle model with provided inputs and display results.

    :param sequence: Path to sequence file
    :param nucleus_image_path: Path to nucleus image
    :param protein_image_path: Path to protein image (optional)
    :param model_ckpt_path: Path to model checkpoint
    :param model_config_path: Path to model config
    """
    
    # Instantiate dataset object
    dataset = CellLoader(
        sequence_mode="embedding",
        vocab="esm2",
        split_key="val",
        crop_method="center",
        resize=600,
        crop_size=256,
        text_seq_len=1000,
        pad_mode="end",
        threshold="median",
    )

    # Check if sequence is provided and valid
    if len(sequence_input) == 0:
        raise ValueError("Sequence must be provided.")

    if "<mask>" not in sequence_input:
        print("Warning: Sequence does not contain any masked positions to predict.")

    # Convert SEQUENCE to sequence using dataset.tokenize_sequence()
    sequence = dataset.tokenize_sequence(sequence_input)

    # Check if nucleus image path is provided and valid
    if not os.path.exists(nucleus_image_path):
        # Use default nucleus image from dataset and print warning
        nucleus_image_path = 'images/nucleus.jpg'
        print(
            "Warning: No nucleus image provided. Using default nucleus image from dataset."
        )
    else:
        # Load nucleus image from provided path
        nucleus_image = process_image(nucleus_image_path)
        
    # Check if protein image path is provided and valid
    if not os.path.exists(protein_image_path):
        # Use default nucleus image from dataset and print warning
        protein_image_path = 'images/protein.jpg'
        print(
            "Warning: No nucleus image provided. Using default protein image from dataset."
        )
    else:
        # Load protein image from provided path
        protein_image = process_image(protein_image_path)
        protein_image = (protein_image > torch.median(protein_image,dim=0))*1.0

    # Load model config and set ckpt_path if not provided in config
    config = OmegaConf.load(model_config_path)
    if config["model"]["params"]["ckpt_path"] is None:
        config["model"]["params"]["ckpt_path"] = model_ckpt_path

    # Set condition_model_path and vqgan_model_path to None
    config["model"]["params"]["condition_model_path"] = None
    config["model"]["params"]["vqgan_model_path"] = None

    # Instantiate model from config and move to device
    model = instantiate_from_config(config).to(device)

    # Sample from model using provided sequence and nucleus image
    _, predicted_sequence, _ = model.celle.sample_text(
        text=sequence,
        condition=nucleus_image,
        image=protein_image,
        force_aas=True,
        timesteps=1,
        temperature=1,
        progress=True,
    )

    formatted_predicted_sequence = ""

    for i in range(min(len(predicted_sequence), len(sequence))):
        if predicted_sequence[i] != sequence[i]:
            formatted_predicted_sequence += f"**{predicted_sequence[i]}**"
        else:
            formatted_predicted_sequence += predicted_sequence[i]

    if len(predicted_sequence) > len(sequence):
        formatted_predicted_sequence += f"**{predicted_sequence[len(sequence):]}**"

    print("predicted_sequence:", formatted_predicted_sequence)


def run_image_prediction(
    sequence_input,
    nucleus_image_path,
    protein_image_path,
    model_ckpt_path,
    model_config_path,
    device
):
    """
    Run Celle model with provided inputs and display results.

    :param sequence: Path to sequence file
    :param nucleus_image_path: Path to nucleus image
    :param protein_image_path: Path to protein image (optional)
    :param model_ckpt_path: Path to model checkpoint
    :param model_config_path: Path to model config
    """
    # Instantiate dataset object
    dataset = CellLoader(
        sequence_mode="embedding",
        vocab="esm2",
        split_key="val",
        crop_method="center",
        resize=600,
        crop_size=256,
        text_seq_len=1000,
        pad_mode="end",
        threshold="median",
    )

    # Check if sequence is provided and valid
    if len(sequence_input) == 0:
        sequence = "MSKGEELFTGVVPILVELDGDVNGHKFSVSGEGEGDATYGKLTLKFICTTGKLPVPWPTLVTTFSYGVQCFSRYPDHMKQHDFFKSAMPEGYVQERTIFFKDDGNYKTRAEVKFEGDTLVNRIELKGIDFKEDGNILGHKLEYNYNSHNVYIMADKQKNGIKVNFKIRHNIEDGSVQLADHYQQNTPIGDGPVLLPDNHYLSTQSALSKDPNEKRDHMVLLEFVTAAGITHGMDELYK"
        # Use default sequence for GFP and print warning
        print("Warning: No sequence provided. Using default sequence for GFP.")

    # Convert SEQUENCE to sequence using dataset.tokenize_sequence()
    sequence = dataset.tokenize_sequence(sequence_input)

    # Check if nucleus image path is provided and valid
    if not os.path.exists(nucleus_image_path):
        # Use default nucleus image from dataset and print warning
        nucleus_image = dataset[0]["nucleus"]
        print(
            "Warning: No nucleus image provided. Using default nucleus image from dataset."
        )
    else:
        # Load nucleus image from provided path
        nucleus_image = process_image(nucleus_image_path)

    # Load model config and set ckpt_path if not provided in config
    config = OmegaConf.load(model_config_path)
    if config["model"]["params"]["ckpt_path"] is None:
        config["model"]["params"]["ckpt_path"] = model_ckpt_path

    # Set condition_model_path and vqgan_model_path to None
    config["model"]["params"]["condition_model_path"] = None
    config["model"]["params"]["vqgan_model_path"] = None

    # Instantiate model from config and move to device
    model = instantiate_from_config(config).to(device)

    # Sample from model using provided sequence and nucleus image
    _, _, _, predicted_threshold, predicted_heatmap = model.celle.sample(
        text=sequence,
        condition=nucleus_image,
        timesteps=1,
        temperature=1,
        progress=True,
    )

    # Move predicted_threshold and predicted_heatmap to CPU and select first element of batch
    predicted_threshold = predicted_threshold.cpu()[0, 0]
    predicted_heatmap = predicted_heatmap.cpu()[0, 0]

    # Create 3 or 4 panel plot depending on whether protein image path is provided
    fig, axs = plt.subplots(1, 3 if protein_image_path is None else 4)
    axs[0].imshow(nucleus_image)
    axs[0].set_title("Nucleus Input")
    axs[1].imshow(predicted_threshold)
    axs[1].set_title("Predicted Threshold")
    if protein_image_path is not None:
        protein_image = process_image(protein_image_path)
        axs[2].imshow(protein_image)
        axs[2].set_title("Protein Image")
    axs[-1].imshow(predicted_heatmap)
    axs[-1].set_title("Predicted Heatmap")
    plt.show()


if __name__ == "__main__":
    # Parse command line arguments for input parameters
    parser = argparse.ArgumentParser(
        description="Run Celle model with provided inputs."
    )
    parser.add_argument("--mode", type=str, default="", help="Sequence or Image")
    parser.add_argument(
        "--sequence", type=str, default="", help="Path to sequence file"
    )
    parser.add_argument(
        "--nucleus_image_path",
        type=str,
        default="images/nucleus.jpg",
        help="Path to nucleus image",
    )
    parser.add_argument(
        "--protein_image_path",
        type=str,
        default=None,
        help="Path to protein image (optional)",
    )
    parser.add_argument(
        "--model_ckpt_path", type=str, required=True, help="Path to model checkpoint"
    )
    parser.add_argument(
        "--model_config_path", type=str, required=True, help="Path to model config"
    )
    parser.add_argument(
        "--device", type=str, default="cpu", required=True, help="device"
    )
    args = parser.parse_args()

    run_model(
        args.mode,
        args.sequence,
        args.nucleus_image_path,
        args.protein_image_path,
        args.model_ckpt_path,
        args.model_config_path,
        args.device
    )