File size: 6,340 Bytes
532554f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
550eaf1
 
 
 
 
 
 
532554f
550eaf1
 
532554f
 
 
 
 
 
 
 
 
 
 
 
 
 
550eaf1
 
 
 
 
 
 
532554f
550eaf1
 
532554f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { API_CONFIG } from '@/config/api.config';
import { DataResponse } from '@/schemas/data';
import { ExplorerRequestSchema, ExplorerResponse } from '@/schemas/explorer';
import { PredictionResponse } from '@/schemas/prediction';
import { SearchResult } from '@/schemas/search';
import { EmbeddingPoint } from '@/schemas/visualization';
import { Candidate,WorkflowResult } from '@/schemas/workflow';
import { Molecule, Protein } from '@/types/visualization';

const API_BASE = API_CONFIG.baseUrl;

// --- UTILS ---

async function fetchJson<T>(url: string, options?: RequestInit): Promise<T> {
    const response = await fetch(url, options);
    if (!response.ok) {
        const error = await response.json().catch(() => ({}));
        throw new Error(error.error || error.detail || `HTTP ${response.status}`);
    }
    return response.json();
}

async function fetchText(url: string, options?: RequestInit): Promise<string> {
    const response = await fetch(url, options);
    if (!response.ok) {
        const error = await response.json().catch(() => ({}));
        throw new Error(error.error || error.detail || `HTTP ${response.status}`);
    }
    return response.text();
}

// --- DATA / STATS ---

/**
 * Fetches dashboard statistics and dataset information.
 */
export async function getStats(): Promise<DataResponse> {
    return fetchJson<DataResponse>(`${API_BASE}/api/stats`, {
        next: { revalidate: 60 },
    });
}

// --- EXPLORER ---

/**
 * Fetches 3D points for the embedding explorer.
 */
export async function getExplorerPoints(
    dataset?: string,
    view?: string,
    colorBy?: string,
): Promise<ExplorerResponse> {
    const result = ExplorerRequestSchema.safeParse({ dataset, view, colorBy });

    if (!result.success) {
        throw new Error('Invalid parameters');
    }

    const apiView =
        view === 'UMAP'
            ? 'combined'
            : view === 'PCA-Drug'
                ? 'drug'
                : view === 'PCA-Target'
                    ? 'target'
                    : 'combined';

    return fetchJson<ExplorerResponse>(
        `${API_BASE}/api/points?limit=500&view=${apiView}`,
        {
            next: { revalidate: 0 },
            signal: AbortSignal.timeout(5000),
        },
    );
}

// --- MOLECULES / PROTEINS ---

interface MoleculesResponse {
    molecules: Molecule[];
    total: number;
    limit: number;
    offset: number;
}

export async function getMolecules(): Promise<Molecule[]> {
    const response = await fetchJson<MoleculesResponse>(`${API_BASE}/api/molecules`);
    return response.molecules || [];
}

export async function getMolecule(id: string): Promise<Molecule> {
    return fetchJson<Molecule>(`${API_BASE}/api/molecules/${id}`);
}

export async function getMoleculeSDF(id: string): Promise<string> {
    return fetchText(`${API_BASE}/api/molecules/${id}/sdf`);
}

export function getMoleculeSdfUrl(id: string): string {
    return `${API_BASE}/api/molecules/${id}/sdf`;
}

interface ProteinsResponse {
    proteins: Protein[];
    total: number;
    limit: number;
    offset: number;
}

export async function getProteins(): Promise<Protein[]> {
    const response = await fetchJson<ProteinsResponse>(`${API_BASE}/api/proteins`);
    return response.proteins || [];
}

export async function getProtein(id: string): Promise<Protein> {
    return fetchJson<Protein>(`${API_BASE}/api/proteins/${id}`);
}

export async function getProteinPDB(id: string): Promise<string> {
    return fetchText(`${API_BASE}/api/proteins/${id}/pdb`);
}

export function getProteinPdbUrl(id: string): string {
    // If it's a 4-letter PDB ID, use RCSB, otherwise use local API
    if (id.length === 4) {
        return `https://files.rcsb.org/download/${id.toUpperCase()}.pdb`;
    }
    return `${API_BASE}/api/proteins/${id}/pdb`;
}

// --- DISCOVERY & SEARCH ---

/**
 * General search across modalities.
 */
export async function search(params: {
    query: string;
    type?: string;
    limit?: number;
    dataset?: string;
    top_k?: number;
    use_mmr?: boolean;
}): Promise<{ results: SearchResult[] }> {
    return fetchJson(`${API_BASE}/api/search`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(params),
    });
}

/**
 * Fetches embeddings for a query.
 */
export async function getEmbeddings(
    query: string,
    method: string = 'pca',
    limit: number = 50,
): Promise<{ points: EmbeddingPoint[] }> {
    return fetchJson(
        `${API_BASE}/api/explorer/embeddings?query=${encodeURIComponent(query)}&method=${method}&limit=${limit}`,
    );
}

/**
 * Predicts binding affinity between a drug and a target.
 */
export async function predict(drug_smiles: string, target_sequence: string): Promise<PredictionResponse> {
    return fetchJson(`${API_BASE}/api/predict`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ drug_smiles, target_sequence }),
    });
}

// --- WORKFLOW ---

/**
 * Executes a discovery workflow.
 */
export async function runWorkflow(params: {
    query: string;
    num_candidates: number;
    top_k: number;
}): Promise<WorkflowResult & { top_candidates: Candidate[], execution_time_ms: number }> {
    return fetchJson(`${API_BASE}/api/agents/workflow`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(params),
    });
}

// --- INGESTION ---

export async function getIngestJob(jobId: string): Promise<{ job_id: string, status: string, source?: string, type?: string }> {
    return fetchJson(`${API_BASE}/api/ingest/jobs/${jobId}`);
}

export async function startIngestion(source: string, payload: Record<string, any>): Promise<{ job_id: string, result?: string }> {
    const endpoint = source === 'all' ? `${API_BASE}/api/ingest/all` : `${API_BASE}/api/ingest/${source}`;
    return fetchJson(endpoint, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload),
    });
}

export async function batchIngest(items: any[]): Promise<{ ingested: number }> {
    return fetchJson(`${API_BASE}/api/ingest/batch`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ items }),
    });
}