File size: 35,542 Bytes
5a0e691
60aee05
f8dfa58
5a0e691
 
 
9a28cd0
5a0e691
 
4939f56
240d7ca
 
 
 
f8dfa58
 
 
 
 
 
 
 
 
 
 
 
 
240d7ca
 
 
 
 
 
 
 
 
 
 
 
 
5a0e691
 
 
 
 
cee7881
f3682aa
5a0e691
240d7ca
5a0e691
 
 
 
cee7881
f3682aa
5a0e691
 
 
 
 
240d7ca
5a0e691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305458b
 
 
 
 
f42f5a6
 
 
 
9a28cd0
 
 
 
 
 
f42f5a6
 
9a28cd0
f42f5a6
 
 
5a0e691
76fb273
 
 
 
 
 
 
 
 
 
 
 
 
 
7acf89d
 
 
 
 
 
 
 
 
 
 
 
f8dfa58
 
 
 
 
 
 
 
00b6857
 
 
 
 
 
 
 
 
 
 
 
 
 
f8dfa58
 
 
 
 
 
7acf89d
 
 
 
 
 
 
 
 
f8dfa58
7acf89d
 
 
 
 
 
 
 
 
 
 
76fb273
 
 
 
 
 
 
 
f0b48d6
5d5e978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b48d6
 
 
 
 
 
cc7ed3f
f0b48d6
 
0f1caf6
 
990c867
 
 
 
240d7ca
990c867
 
 
cc7ed3f
 
 
 
f0b48d6
 
 
f0992bc
8321d59
 
f0992bc
 
 
 
 
 
 
 
8321d59
 
f0992bc
 
 
 
 
 
 
 
60aee05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
033540e
 
 
 
 
60aee05
 
 
 
 
 
 
 
033540e
 
 
 
 
60aee05
 
 
 
 
033540e
60aee05
033540e
 
 
 
 
 
 
60aee05
 
 
f0b48d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89fd26d
 
 
 
 
 
 
 
 
 
 
9a28cd0
f0b48d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ce1e37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bde6aea
8ce1e37
bde6aea
8ce1e37
 
 
305458b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ce1e37
 
 
 
fb8c550
a6857c7
 
 
 
 
 
 
 
fb8c550
 
 
5bb077e
a6857c7
fb8c550
4320faf
 
fb8c550
 
a6857c7
 
 
 
e86bafa
a6857c7
e86bafa
 
a6857c7
e86bafa
a6857c7
 
 
 
 
 
e86bafa
a6857c7
e86bafa
 
a6857c7
e86bafa
a6857c7
 
 
 
 
e86bafa
 
 
 
 
 
 
 
a6857c7
 
e86bafa
 
 
 
 
 
 
 
 
 
 
 
a6857c7
 
 
 
 
 
 
e86bafa
a6857c7
 
fb8c550
 
 
 
 
 
 
 
5bb077e
 
 
 
 
 
 
 
 
 
 
 
fb8c550
 
 
4320faf
 
 
fb8c550
a6857c7
 
 
fb8c550
 
 
 
5561093
240d7ca
fb8c550
 
 
 
240d7ca
fb8c550
 
1cb972c
a6857c7
e619a9b
a6857c7
 
1cb972c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00b6857
1cb972c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99fb209
5fa9145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e9035
 
 
5fa9145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132a669
5fa9145
 
 
 
 
95e9035
5fa9145
 
 
 
 
 
 
 
 
95e9035
5fa9145
 
 
 
 
 
 
95e9035
5fa9145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12ba364
 
 
 
 
 
 
 
 
 
 
 
 
5fa9145
 
 
 
 
 
 
 
 
 
 
 
99fb209
 
 
 
5bb077e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99fb209
 
5bb077e
 
99fb209
5bb077e
99fb209
 
 
 
 
 
 
 
24c3fcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe8fda4
24c3fcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe8fda4
24c3fcb
 
 
 
 
 
 
 
 
 
 
 
 
 
99fb209
 
5bb077e
 
 
 
 
 
 
 
99fb209
 
 
5bb077e
 
 
 
 
 
 
 
 
 
 
 
 
38e4510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99fb209
 
 
 
 
 
 
3fba508
 
 
 
 
99fb209
 
 
cbef588
99fb209
 
8e3419e
 
 
 
99fb209
cbef588
 
 
 
3fba508
 
 
 
99fb209
bd95b9f
 
 
 
 
99fb209
 
09fc54e
 
 
 
 
 
2583376
 
 
 
 
 
 
 
 
 
 
 
 
09fc54e
 
99fb209
 
 
 
240d7ca
99fb209
 
 
 
 
 
 
 
 
 
 
 
 
0e1d7f2
99fb209
 
 
 
 
240d7ca
99fb209
 
 
 
240d7ca
99fb209
 
be36a29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e21c532
be36a29
 
 
c6a8542
be36a29
 
 
e21c532
 
 
 
 
be36a29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b28e93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e21c532
 
 
 
 
1b28e93
 
 
 
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
import axios from 'axios';
import type { JobStatus, UniprotSummary, SequenceResult, SequenceValidation, SequenceSearchResponse, PairwiseAlignResult, SequenceUtilitiesResult, MotifPatternScanResult, MotifLibraryResult, MotifLibraryPattern, DotPlotResult } from '@/types/pipeline';
import { getSupabase } from './supabase';

const api = axios.create({
  baseURL: '/api/backend',
  timeout: 30_000,
});

export const longApi = axios.create({
  baseURL: '/api/backend',
  timeout: 660_000,
});

api.interceptors.request.use(async (config) => {
  try {
    const supabase = getSupabase();
    const { data: { session } } = await supabase.auth.getSession();
    if (session?.access_token) {
      config.headers.Authorization = `Bearer ${session.access_token}`;
    }
  } catch {
    // Session fetch failed silently β€” requests will be anonymous
  }
  return config;
});

longApi.interceptors.request.use(async (config) => {
  try {
    const supabase = getSupabase();
    const { data: { session } } = await supabase.auth.getSession();
    if (session?.access_token) {
      config.headers.Authorization = `Bearer ${session.access_token}`;
    }
  } catch {
    // Session fetch failed silently
  }
  return config;
});

export async function runPipeline(
  sequence: string,
  pipelineType: string = 'protein_analysis',
  database: string = 'uniprotkb_swissprot',
  maxHits: number = 10,
  queryAccession?: string,
  fastMode: boolean = false,
): Promise<{ job_id: string; status: string }> {
  const res = await longApi.post('/api/pipelines/run', {
    sequence,
    pipeline_type: pipelineType,
    database,
    max_hits: maxHits,
    query_accession: queryAccession ?? '',
    fast_mode: fastMode,
  });
  return res.data;
}

export async function getJob(jobId: string): Promise<JobStatus> {
  const res = await longApi.get(`/api/jobs/${jobId}`);
  return res.data;
}

export async function getJobs(): Promise<JobStatus[]> {
  const res = await api.get('/api/jobs');
  return res.data.jobs || [];
}

export async function getJobCount(): Promise<{ count: number; limit: number; remaining: number }> {
  const res = await api.get('/api/jobs/count');
  return res.data;
}

export async function getSharedResult(token: string): Promise<JobStatus> {
  const res = await api.get(`/api/share/${token}`);
  return res.data;
}

export async function createShareLink(jobId: string): Promise<{ token: string; url: string }> {
  const res = await api.post('/api/share', { job_id: jobId });
  return res.data;
}

export async function interpretStream(payload: {
  pipeline_type: string;
  context: unknown;
}): Promise<Response> {
  const supabase = getSupabase();
  const { data: { session } } = await supabase.auth.getSession();
  const headers: Record<string, string> = { 'Content-Type': 'application/json' };
  if (session?.access_token) {
    headers['Authorization'] = `Bearer ${session.access_token}`;
  }
  const res = await fetch('/api/backend/api/ai/interpret/stream', {
    method: 'POST',
    headers,
    body: JSON.stringify(payload),
  });
  return res;
}

export async function fetchSequence(accession: string, dbPreference?: string): Promise<SequenceResult> {
  const res = await api.post('/api/sequences/fetch', {
    accession,
    db_preference: dbPreference,
  });
  return res.data;
}

export async function validateSequence(sequence: string): Promise<SequenceValidation> {
  const res = await api.post('/api/sequences/validate', { sequence });
  return res.data;
}

export type UniprotSearchOptions = {
  reviewed?: boolean;
  organism?: string;
};

export async function searchUniprot(query: string, maxResults: number = 20, opts: UniprotSearchOptions = {}): Promise<{ results: UniprotSearchResult[]; count: number }> {
  const res = await api.post('/api/uniprot/search', {
    query,
    max_results: maxResults,
    reviewed: opts.reviewed ?? false,
    organism: opts.organism ?? '',
  });
  return res.data;
}

export async function getUniprotDetail(accession: string): Promise<UniprotSummary> {
  const res = await api.post('/api/uniprot/detail', { accession });
  return res.data;
}

export type UniprotCDSResult = {
  uniprot_accession: string;
  embl_accession: string;
  sequence: string;
  length: number;
  description: string;
  organism: string;
};

export async function fetchUniprotCds(accession: string, emblAccession: string): Promise<UniprotCDSResult> {
  const res = await api.post('/api/uniprot/cds', { accession, embl_accession: emblAccession });
  return res.data;
}

export type UniprotSearchResult = {
  accession: string;
  name: string;
  gene_names: string[];
  organism: string;
  length: number;
  reviewed: boolean;
};

export type ScanPrositeMatch = {
  signature_ac: string;
  name: string;
  start: number;
  stop: number;
  level_tag: string;
};

export type ScanPrositeResult = {
  sequence_length: number;
  count: number;
  matches: ScanPrositeMatch[];
};

export async function scanPrositeSequence(sequence: string): Promise<ScanPrositeResult> {
  const res = await api.post('/api/domains/scan', { sequence });
  return res.data;
}
export async function searchSequences(query: string, db: string = 'protein', maxResults: number = 10): Promise<SequenceSearchResponse> {
  const res = await api.post('/api/sequences/search', {
    query,
    db,
    max_results: maxResults,
  });
  return res.data;
}

export type PrimerSearchHit = {
  accession: string;
  title: string;
  organism: string;
  length: number;
  record_type: string;
  suggested_use: string;
};

export type PrimerSearchResponse = {
  query: string;
  db: string;
  count: number;
  results: PrimerSearchHit[];
  error?: string;
};

export type PrimerStructure = {
  dg: number;
  stem_length: number;
  stem: string;
  loop?: string;
  involves_a3?: boolean;
  involves_b3?: boolean;
  risk: string;
  note: string;
};

export type PrimerQC = {
  sequence: string;
  length: number;
  gc: number;
  tm_50mM: number;
  hairpin: PrimerStructure;
  self_dimer: PrimerStructure;
};

export type PrimerAnalyzeResponse = {
  qc: {
    left: PrimerQC;
    right: PrimerQC;
    hetero_dimer: PrimerStructure;
  };
  pcr?: {
    template_length: number;
    forward_binding_sites: number;
    reverse_binding_sites: number;
    forward_positions: number[];
    reverse_positions: number[];
    specific: boolean;
    amplicons: { start: number; end: number; length: number }[];
    primer3_consistent: boolean | null;
    matches_product_size: boolean | null;
    note: string | null;
  };
};

export async function searchPrimerTargets(query: string, maxResults: number = 12): Promise<PrimerSearchResponse> {
  const res = await api.post('/api/primers/search', { query, max_results: maxResults });
  return res.data;
}

export async function analyzePrimer(payload: {
  left_seq: string;
  right_seq: string;
  template?: string;
  left_pos?: number;
  right_pos?: number;
  expected_product?: number;
}): Promise<PrimerAnalyzeResponse> {
  const res = await api.post('/api/primers/analyze', payload);
  return res.data;
}

export type AlignmentResult = {
  job_id: string;
  aln_fasta: string;
  aln_clustal: string;
  phylotree: string;
  stype: string;
  method?: string;
};

export async function runPipelineV2(sequence: string, steps?: string[], alignmentMode?: 'global' | 'local'): Promise<{ job_id: string }> {
  const res = await longApi.post('/api/pipeline/v2/run', { sequence, steps: steps || undefined, alignment_mode: alignmentMode || 'global' });
  return res.data;
}

export async function getPipelineStatusV2(jobId: string): Promise<any> {
  const res = await longApi.get(`/api/pipeline/v2/status/${jobId}`);
  return res.data;
}

export type AlignmentMethod = 'clustalo' | 'muscle' | 'kalign' | 'mafft' | 'tcoffee';

export async function runAlignment(sequence: string, stype: string = 'protein', method: AlignmentMethod = 'clustalo'): Promise<AlignmentResult> {
  const res = await api.post('/api/alignment/run', { sequence, stype, method });
  return res.data;
}

export type PairwiseAlignOptions = {
  hit_accession?: string;
  subject_sequence?: string;
  query_sequence?: string;
  query_accession?: string;
  mode?: 'global' | 'local';
  matrix?: 'blosum62' | 'pam250';
};

export async function runPairwiseAlignment(options: PairwiseAlignOptions): Promise<PairwiseAlignResult> {
  const res = await api.post('/api/alignment/pairwise', {
    hit_accession: options.hit_accession ?? '',
    subject_sequence: options.subject_sequence ?? '',
    query_sequence: options.query_sequence ?? '',
    query_accession: options.query_accession ?? '',
    mode: options.mode ?? 'global',
    matrix: options.matrix ?? 'blosum62',
  });
  return res.data;
}

export async function analyzeSequence(payload: {
  sequence: string;
  seq_type?: 'auto' | 'dna' | 'rna' | 'protein';
}): Promise<SequenceUtilitiesResult> {
  const res = await api.post('/api/seq-tools/analyze', {
    sequence: payload.sequence,
    seq_type: payload.seq_type ?? 'auto',
  });
  return res.data;
}

export async function scanMotifPattern(payload: {
  sequence: string;
  pattern: string;
}): Promise<MotifPatternScanResult> {
  const res = await api.post('/api/seq-tools/motif-scan', payload);
  return res.data;
}

export async function scanMotifLibrary(
  sequence: string,
  categories?: string[],
): Promise<MotifLibraryResult> {
  const res = await api.post('/api/seq-tools/motif-library', { sequence, categories });
  return res.data;
}

export async function fetchMotifPatterns(): Promise<MotifLibraryPattern[]> {
  const res = await api.get('/api/seq-tools/motif-library/patterns');
  return res.data;
}

export async function fetchMotifCategories(): Promise<string[]> {
  const res = await api.get('/api/seq-tools/motif-library/categories');
  return res.data;
}

export async function runDotPlot(payload: {
  seq_a: string;
  seq_b: string;
  window: number;
  stringency: number;
  scoring?: string;
}): Promise<DotPlotResult> {
  const res = await api.post('/api/seq-tools/dotplot', {
    seq_a: payload.seq_a,
    seq_b: payload.seq_b,
    window: payload.window,
    stringency: payload.stringency,
    scoring: payload.scoring ?? 'identity',
  });
  return res.data;
}

export type StructureResult = {
  source: string;
  pdb_id?: string;
  title?: string;
  method?: string;
  resolution?: number;
  deposited?: string;
  pdb_url?: string;
  cif_url?: string;
  uniprot_accession?: string;
  confidence?: number;
  model_created_date?: string;
};

export async function fetchStructure(query: string): Promise<StructureResult> {
  const res = await api.post('/api/structures/fetch', { query });
  return res.data;
}

export type StructureInventory = {
  pdb_id: string;
  chains: Array<{ id: string; residue_count: number }>;
  ligands: Array<{ id: string; chain: string; residue_count: number }>;
};

export async function getStructureInventory(pdbId: string): Promise<StructureInventory> {
  const res = await api.post('/api/structures/inventory', { pdb_id: pdbId });
  return res.data;
}

/** @deprecated Unused β€” search is handled by fetchStructure */
export async function searchStructures(query: string): Promise<{ results: { pdb_id: string; score: number }[]; count: number }> {
  const res = await api.post('/api/structures/search', { query });
  return res.data;
}

export type PathwayResult = {
  pathway_id: string;
  name: string;
  species: string;
  url: string;
};

export async function searchPathways(query: string, species: string = 'Homo sapiens'): Promise<{ results: PathwayResult[]; count: number }> {
  const res = await api.post('/api/pathways/search', { query, species });
  return res.data;
}

export type PathwayDetail = {
  pathway_id: string;
  name: string;
  species: string;
  description: string;
  url: string;
};

export async function fetchPathwayDetail(pathwayId: string): Promise<PathwayDetail> {
  const res = await api.post('/api/pathways/detail', { pathway_id: pathwayId });
  return res.data;
}

export type KEGGPathwayResult = {
  pathway_id: string;
  name: string;
  organism: string;
  url: string;
  image_url: string;
};

export async function searchKEGGPathways(query: string): Promise<{ results: KEGGPathwayResult[]; count: number }> {
  const res = await api.post('/api/pathways/kegg/search', { query });
  return res.data;
}

export type EnrichmentResult = {
  token: string;
  pathways: Array<{
    stId: string;
    name: string;
    species: string;
    entitiesFound: number;
    entitiesTotal: number;
    geneRatio: number;
    entitiesFDR: number;
    entitiesPValue: number;
  }>;
};

export type ApiKey = {
  id: string;
  name: string;
  key_prefix: string;
  created_at: string;
  last_used_at: string | null;
};

export async function getApiKeys(): Promise<ApiKey[]> {
  const res = await api.get('/api/keys');
  return res.data.keys || [];
}

export async function createApiKey(name: string): Promise<{ key: string; key_prefix: string; name: string }> {
  const res = await api.post('/api/keys', { name });
  return res.data;
}

export async function deleteApiKey(id: string): Promise<void> {
  await api.delete(`/api/keys/${id}`);
}

export function getExportUrl(jobId: string, format: 'pdf' | 'json'): string {
  return `/api/backend/api/export/job/${jobId}?format=${format}`;
}

export async function runEnrichment(identifiers: string[]): Promise<EnrichmentResult> {
  const res = await api.post('/api/pathways/enrichment', { identifiers });
  return res.data;
}

export type DockingAtom = {
  x: number;
  y: number;
  z: number;
  element: string;
  atom_type: string;
};

export type DockingPose = {
  model: number;
  atoms: number;
  hydrogens?: number;
  coords?: DockingAtom[];
  affinity: number | null;
  rmsd_lb?: number | null;
  rmsd_ub?: number | null;
};

export type DockingInteraction = {
  hbonds: Array<{
    type: string;
    ligand_atom: string;
    ligand_coords: [number, number, number];
    protein_residue: string;
    protein_residue_seq: number;
    protein_chain: string;
    protein_atom: string;
    protein_coords: [number, number, number];
    distance: number;
    confidence: string;
  }>;
  hydrophobic: Array<{
    type: string;
    ligand_atom: string;
    ligand_coords: [number, number, number];
    protein_residue: string;
    protein_residue_seq: number;
    protein_chain: string;
    protein_atom: string;
    protein_coords: [number, number, number];
    distance: number;
  }>;
  pi_stacking: Array<{
    type: string;
    protein_residue: string;
    protein_residue_seq: number;
    protein_chain: string;
    ring_centroid: [number, number, number];
    ring_normal: [number, number, number];
    ligand_centroid: [number, number, number];
    distance: number;
    angle: number;
    stacking_type: string;
    confidence: string;
  }>;
  salt_bridges: Array<{
    type: string;
    ligand_atom: string;
    ligand_coords: [number, number, number];
    protein_residue: string;
    protein_residue_seq: number;
    protein_chain: string;
    protein_atom: string;
    protein_coords: [number, number, number];
    distance: number;
    charge_pair: string;
  }>;
};

export type DockingPoseInteractions = {
  model: number;
  hbonds: number;
  hydrophobic: number;
  pi_stacking: number;
  salt_bridges: number;
};

export type DockingResult = {
  job_id: string;
  status: string;
  result?: {
    pdb_id: string;
    smiles: string;
    poses: DockingPose[];
    num_poses: number;
    ligand_properties?: {
      molecular_formula: string;
      molecular_weight: number;
      heavy_atoms: number;
      hydrogen_count: number;
      total_atoms: number;
      rotatable_bonds: number;
      tpsa: number;
      hbd: number;
      hba: number;
      logp: number;
    };
    box_center: { x: number; y: number; z: number };
    box_size: { x: number; y: number; z: number };
    vina_log?: string;
    vina_version?: string;
    vina_seed?: number | null;
    vina_exhaustiveness?: number | null;
    from_cache?: boolean;
    interactions?: DockingInteraction;
    pose_interactions?: DockingPoseInteractions[];
    ligand_pdb?: string;
  };
  error?: string;
};

export async function runDocking(pdbId: string, smiles: string, pdbUrl?: string): Promise<{ job_id: string; status: string }> {
  const res = await longApi.post('/api/docking/run', { pdb_id: pdbId, smiles, pdb_url: pdbUrl || '' });
  return res.data;
}

export async function getDockingStatus(jobId: string): Promise<DockingResult> {
  const res = await longApi.get(`/api/docking/status/${jobId}`);
  return res.data;
}

export function getDockingPdbUrl(jobId: string): string {
  return `/api/backend/docking/result/${jobId}/pdb`;
}

export type SequencingQC = {
  total_reads: number;
  total_bases: number;
  avg_read_length: number;
  min_read_length: number;
  max_read_length: number;
  gc_percent: number;
  mean_quality: number;
  min_quality: number;
  max_quality: number;
  q20_percent: number;
  q30_percent: number;
  overrepresented_sequences: { sequence: string; count: number; percent: number }[];
};

export type SequencingAlignment = {
  mapped_reads: number;
  unmapped_reads: number;
  total_alignments: number;
};

export type SequencingVariant = {
  pos: number;
  ref: string;
  alt: string;
  depth: number;
  alt_count: number;
  freq: number;
};

export type SequencingResult = {
  job_id: string;
  status: string;
  result?: {
    reference: string;
    qc: SequencingQC;
    alignment: SequencingAlignment;
    variants: SequencingVariant[];
    consensus_sequence?: string;
    report: {
      reference: string;
      qc_summary: { total_reads: number; total_bases: number; mean_quality: number; q30_percent: number; gc_percent: number };
      variant_summary: { total_variants: number; snv_count: number; avg_depth: number };
      variants: SequencingVariant[];
    };
    steps_completed: string[];
  };
  error?: string;
};

export type SequencingReference = {
  id: string;
  name: string;
};

export async function runSequencing(fastqUrl: string, reference: string = 'sars-cov-2'): Promise<{ job_id: string; status: string }> {
  const res = await api.post('/api/sequencing/run', { fastq_url: fastqUrl, reference });
  return res.data;
}

export async function getSequencingStatus(jobId: string): Promise<SequencingResult> {
  const res = await api.get(`/api/sequencing/status/${jobId}`);
  return res.data;
}

export async function listSequencingReferences(): Promise<SequencingReference[]> {
  const res = await api.get('/api/sequencing/references');
  return res.data.references || [];
}

// ---------------------------------------------------------------------------
// NGS Pipeline
// ---------------------------------------------------------------------------

export type NGSQC = {
  tool: string;
  total_reads: number;
  total_bases: number;
  avg_read_length: number;
  min_read_length: number;
  max_read_length: number;
  gc_percent: number;
  mean_quality: number;
  min_quality: number;
  max_quality: number;
  q20_percent: number;
  q30_percent: number;
  quality_by_position?: { position: number; mean: number; q10: number; q90: number }[];
  gc_by_window?: number[];
  read_length_distribution?: { length: number; count: number }[];
  overrepresented_sequences: { sequence: string; count: number; percent: number }[];
};

export type NGSTrimming = {
  tool: string;
  reads_before: number;
  reads_after: number;
  reads_discarded: number;
};

export type NGSAlignment = {
  tool: string;
  mapped_reads: number;
  unmapped_reads: number;
  total_alignments: number;
  read_region?: string;
};

export type NGSAnnotation = {
  tool: string;
  reference: string;
  annotations: { pos: number; ref: string; alt: string; gene: string; mutation: string; significance: string; depth: number; freq: number; protein_change?: string }[];
  total_annotated: number;
  known_variants_found: number;
};

export type NGSResult = {
  job_id: string;
  status: string;
  result?: {
    reference: string;
    reference_size: number;
    fastq_source: string;
    qc: NGSQC;
    trimming: NGSTrimming;
    alignment: NGSAlignment;
    variants: { pos: number; ref: string; alt: string; depth: number; alt_count: number; freq: number }[];
    annotation: NGSAnnotation;
    consensus_sequence?: string;
    file_urls?: { bam?: string; bai?: string; sam?: string; vcf?: string; reference?: string };
    report: {
      reference: string;
      qc_summary: { total_reads: number; total_bases: number; mean_quality: number; q30_percent: number; gc_percent: number };
      trimming_summary: { reads_before: number; reads_after: number };
      alignment_summary: { mapped_reads: number; unmapped_reads: number; mapping_rate: number };
      variant_summary: { total_variants: number; snv_count: number; known_variants: number; novel_variants: number };
    };
    steps_completed: string[];
    progress: Record<string, string>;
    tools_used: Record<string, string>;
  };
  error?: string;
};

export type NGSReference = {
  id: string;
  name: string;
};

export async function runNGS(fastqUrl: string, reference: string = 'sars-cov-2'): Promise<{ job_id: string; status: string }> {
  for (let attempt = 0; attempt < 2; attempt++) {
    try {
      const res = await api.post('/api/ngs/run', { fastq_url: fastqUrl, reference });
      return res.data;
    } catch (err: any) {
      if (err?.response?.status === 503 && attempt === 0) {
        await new Promise((r) => setTimeout(r, 5000));
        continue;
      }
      throw err;
    }
  }
  throw new Error('Server is waking up β€” please try again in a moment');
}

export async function getNGSStatus(jobId: string): Promise<NGSResult> {
  const res = await longApi.get(`/api/ngs/status/${jobId}`);
  return res.data;
}

export async function listNGSReferences(): Promise<NGSReference[]> {
  const res = await api.get('/api/ngs/references');
  return res.data.references || [];
}

// ---------------------------------------------------------------------------
// ADMET descriptors
// ---------------------------------------------------------------------------

export type ADMETSwissADME = {
  physicochemical: {
    formula: string;
    molecular_weight: number;
    fraction_csp3: number;
    rotatable_bonds: number;
    hba: number;
    hbd: number;
    tpsa: number;
  };
  lipophilicity: {
    ilogp: number | null;
    xlogp3: number | null;
    wlogp: number;
    mlogp: number | null;
    silicos_it: number | null;
    consensus_log_p: number;
    note: string;
  };
  water_solubility: {
    esol_log_s: number;
    esol_class: string;
    esol_mol_per_l: number;
    esol_mg_per_ml: number;
    note: string;
  };
  pharmacokinetics: {
    gi_absorption: string;
    bbb_permeant: string;
    pgp_substrate: string;
    cyp1a2_inhibitor: string;
    cyp2c19_inhibitor: string;
    cyp2c9_inhibitor: string;
    cyp2d6_inhibitor: string;
    cyp3a4_inhibitor: string;
    log_kp_skin: number;
    boiled_egg: {
      tpsa: number;
      wlogp: number;
      in_white_gia: boolean;
      in_yolk_bbb: boolean;
      region: string;
      polygons: { white: [number, number][]; yolk: [number, number][] };
    };
  };
  drug_likeness: {
    lipinski: { pass: boolean; violations: string[]; violation_count: number };
    ghose: { pass: boolean; violations: string[]; violation_count: number };
    veber: { pass: boolean; violations: string[]; violation_count: number };
    egan: { pass: boolean; violations: string[]; violation_count: number };
    muegge: { pass: boolean; violations: string[]; violation_count: number };
    bioavailability_score: number;
  };
  medicinal_chemistry: {
    pains_alerts: { pass: boolean; alerts: string[]; alert_count: number };
    brenk_alerts: { pass: boolean; alerts: string[]; alert_count: number };
    lead_likeness_violations: number;
    synthetic_accessibility: number | null;
  };
  bioavailability_radar: {
    axes: { axis: string; label: string; value: number; min: number; max: number; note: string }[];
    all_optimal: boolean;
  };
};

export type ADMETResult = {
  smiles: string;
  chemical_name?: string;
  pubchem_cid?: number;
  formula: string;
  swissadme?: ADMETSwissADME;
  heavy_atoms: number;
  molecular_weight: number;
  logp: number;
  tpsa: number;
  hbd: number;
  hba: number;
  rotatable_bonds: number;
  qed_score: number;
  molar_refractivity: number;
  molecular_volume: number;
  fsp3: number;
  labute_asa: number;
  estate_sum: number;
  wiener_index: number;
  zagreb_index: number;
  ring_count: number;
  aromatic_ring_count: number;
  aliphatic_ring_count: number;
  num_heteroatoms: number;
  num_amide_bonds: number;
  num_atom_stereocenters: number;
  num_unspecified_stereocenters: number;
  functional_groups: Record<string, number>;
  _methodology?: Record<string, { tier: string; confidence: string; method: string; note: string }>;
  drug_likeness: {
    overall_score: number;
    qed_score: number;
    lipinski: { pass: boolean; violations: string[]; violation_count: number };
    veber: { pass: boolean; violations: string[]; violation_count: number };
    ghose: { pass: boolean; violations: string[]; violation_count: number };
    egan: { pass: boolean; violations: string[]; violation_count: number };
    mddr: { pass: boolean; violations: string[]; violation_count: number };
  };
  structural_alerts: {
    pains: { pass: boolean; alerts: string[]; alert_count: number };
    brenk: { pass: boolean; alerts: string[]; alert_count: number };
    total_alert_count: number;
  };
  absorption: {
    oral_bioavailability: number;
    caco2_permeability: string;
    pgp_substrate: string;
    pgp_inhibitor: string;
    hia: string;
  };
  distribution: {
    volume_of_distribution: number;
    bbb_permeability: string;
    plasma_protein_binding: string;
    cns_penetration: string;
  };
  metabolism: {
    cyp_inhibition: Record<string, string>;
    cyp_substrate_risk: string;
    half_life_class: string;
    lipophilic_efficiency: number;
  };
  toxicity: {
    _disclaimer?: string;
    ames_mutagenicity: string;
    ames_alerts: string[];
    herg_liability: string;
    hepatotoxicity_dili: string;
    skin_sensitization: string;
    skin_sensitization_factors: string[];
    acute_toxicity_ld50: string;
    ld50_estimate_log: number;
    risk_score: number;
  };
  clearance: {
    clearance_class: string;
    half_life_class: string;
  };
};

export type ADMETInput = {
  smiles?: string;
  name?: string;
  cid?: number;
};

export async function computeADMET(input: ADMETInput): Promise<{ result: ADMETResult }> {
  const res = await api.post('/api/admet/descriptors', input);
  return res.data;
}

export type ADMETSearchHit = {
  cid: number;
  name: string;
  formula?: string;
  smiles?: string;
};

export async function searchCompounds(query: string, limit = 10): Promise<ADMETSearchHit[]> {
  if (!query.trim()) return [];
  const res = await api.get('/api/admet/search', { params: { q: query, limit } });
  return res.data.results || [];
}

export type ProToxResult = {
  task_id: string;
  input: string;
  input_type: string;
  requested_models: string;
  chemical_name?: string;
  pubchem_cid?: number;
  acute_toxicity: Record<string, string>;
  model_results: Record<string, string>[];
  toxicity_targets: Record<string, string>[];
  methodology?: { tier: string; confidence: string; method: string; note: string };
};

export async function computeProTox(input: { smiles?: string; name?: string; models?: string }): Promise<{ result: ProToxResult }> {
  const res = await longApi.post('/api/admet/protox', input);
  return res.data;
}

// ---------------------------------------------------------------------------
// MD Simulation
// ---------------------------------------------------------------------------

export type MDSimulationResult = {
  pdb_id: string;
  mode: string;
  engine: string;
  forcefield: string;
  implicit_solvent: string;
  temperature_k: number;
  timestep_fs: number;
  minimization_steps: number;
  equilibration_steps: number;
  production_steps: number;
  production_ps?: number;
  final_energy_kj_mol: number;
  energy: { minimization: { step: number; energy: number }[]; production: { step: number; energy: number }[] };
  temperature?: { step: number; temperature_k: number; kinetic_kj_mol: number }[];
  radius_of_gyration?: { step: number; rg_angstrom: number }[];
  sasa?: { step: number; sasa_angstrom2: number }[];
  sasa_avg_angstrom2?: number;
  rmsd: { frame: number; rmsd: number }[];
  rmsd_basis?: string;
  rmsd_avg_angstrom?: number;
  minimization_drift_angstrom?: number;
  rmsd_source?: string;
  rmsf: { residue: string; rmsf_angstrom: number }[];
  atom_count: number;
  residue_count: number;
  elapsed_seconds: number;
  status: string;
  note?: string;
  chain_count?: number;
  radius_of_gyration_angstrom?: number;
  avg_bfactor?: number;
  secondary_structure?: { helix: number; sheet: number; coil: number };
};

export type MDRunOptions = {
  forcefield?: string;
  solvent?: string;
  run_length_ps?: number;
};

export type MDForceFieldsMenu = {
  forcefields: { value: string; label: string }[];
  solvents: { value: string; label: string }[];
  combos: Record<string, string[]>;
  defaults: { forcefield: string; solvent: string };
  probe?: { system: string; forcefields_tested: number; solvents_tested: number };
};

export async function getMDForceFields(): Promise<MDForceFieldsMenu> {
  const res = await api.get('/api/md/forcefields');
  return res.data;
}

export async function runMD(pdbId: string, mode: string = 'minimize', options?: MDRunOptions): Promise<{ job_id: string; status: string }> {
  const res = await longApi.post('/api/md/run', { pdb_id: pdbId, mode, ...options });
  return res.data;
}

export async function getMDStatus(jobId: string): Promise<{ job_id: string; status: string; result?: MDSimulationResult; error?: string }> {
  const res = await longApi.get(`/api/md/status/${jobId}`);
  return res.data;
}

// ---------------------------------------------------------------------------
// Function Prediction (DeepFRI-inspired)
// ---------------------------------------------------------------------------

export type FunctionPredictionResult = {
  pdb_id: string;
  sequence_length: number;
  go_terms: { go_id: string; name: string; namespace: string; confidence: number }[];
  ec_numbers: { number: string; confidence: number }[];
  saliency: number[];
  composition?: { aa: string; fractions: Record<string, number> };
  method: string;
  note: string;
};

export async function predictFunction(pdbId: string): Promise<{ job_id: string; status: string }> {
  const res = await longApi.post('/api/function/predict', { pdb_id: pdbId });
  return res.data;
}

export async function getFunctionStatus(jobId: string): Promise<{ job_id: string; status: string; result?: FunctionPredictionResult; error?: string }> {
  const res = await longApi.get(`/api/function/status/${jobId}`);
  return res.data;
}

// ─── CASTp ──────────────────────────────────────────────────────────────────

export interface PocketInfo {
  id: number;
  area_sa: number;
  volume_sa: number;
  num_residues: number;
  residues: string[];
  centroid: number[];
  radius: number;
}

export interface CastpResult {
  pdb_id: string;
  probe_radius: number;
  total_residues: number;
  pockets: PocketInfo[];
  sequence_source?: string;
}

export async function runCastp(pdbId: string, probeRadius = 1.4): Promise<CastpResult> {
  const res = await longApi.post('/api/castp/analyze', { pdb_id: pdbId, probe_radius: probeRadius });
  return res.data;
}

export async function runCastpSequence(sequence: string, probeRadius = 1.4): Promise<CastpResult> {
  const res = await longApi.post('/api/castp/analyze', { sequence, probe_radius: probeRadius });
  return res.data;
}

// ─── SWISS-MODEL ────────────────────────────────────────────────────────────

export interface SwissModelTemplate {
  template: string | null;
  provider: string | null;
  method: string | null;
  coverage: number | null;
  oligo_state: string | null;
  from_res: number | null;
  to_res: number | null;
  created_date: string | null;
  coordinates_url: string | null;
  ligands: { hetid: string; description: string }[];
  complex_with: { chain: string; uniprot_ac: string; description: string }[];
}

export interface SwissModelResult {
  accession: string;
  sequence: string;
  sequence_length: number;
  models: SwissModelTemplate[];
  experimental: SwissModelTemplate[];
}

export async function querySwissModel(accession: string): Promise<SwissModelResult> {
  const res = await api.post('/api/swissmodel/repository', { accession });
  return res.data;
}

export async function getSwissModelCoordinates(accession: string): Promise<{ accession: string; pdb: string }> {
  const res = await api.get(`/api/swissmodel/coordinates/${accession}`);
  return res.data;
}

// ─── Structure Prediction (ESMFold) ─────────────────────────────────────────

export interface PredictionJob {
  job_id: string;
  status: string;
}

export interface PredictionResult {
  job_id: string;
  status: string;
  pdb: string | null;
  mean_plddt: number | null;
  ptm: number | null;
  error: string | null;
}

export async function predictStructure(sequence: string, jobTitle = ''): Promise<PredictionJob> {
  const res = await longApi.post('/api/structure-predict/predict', { sequence, job_title: jobTitle });
  return res.data;
}

export async function getPredictionStatus(jobId: string): Promise<PredictionResult> {
  const res = await longApi.get(`/api/structure-predict/status/${jobId}`);
  return res.data;
}

// ─── Structure Preparation Pipeline ──────────────────────────────────────────

export interface StructurePrepJob {
  job_id: string;
  status: string;
}

export interface StructurePrepResult {
  job_id: string;
  status: string;
  step: string;
  chain_health: {
    has_missing_residues: boolean;
    missing_residue_count: number;
    missing_ranges: string[];
    has_chain_breaks: boolean;
    chain_break_count: number;
    chain_breaks: { chain: string; from_resnum: number; to_resnum: number; distance: number }[];
    is_broken: boolean;
    chains: string[];
    total_residues: number;
  } | null;
  fpocket_pockets: { id: number; druggability_score: number; volume: number; area: number; score: number; num_residues: number }[];
  castp_pockets: { id: number; area_sa: number; volume_sa: number }[];
  cleaned_pdb: string;
  error: string | null;
}

export async function runStructurePrep(pdbId: string, probeRadius = 1.4): Promise<StructurePrepJob> {
  const res = await longApi.post('/api/structure-prep/run', { pdb_id: pdbId, probe_radius: probeRadius });
  return res.data;
}

export async function runStructurePrepSequence(sequence: string, probeRadius = 1.4): Promise<StructurePrepJob> {
  const res = await longApi.post('/api/structure-prep/run', { sequence, probe_radius: probeRadius });
  return res.data;
}

export async function getStructurePrepStatus(jobId: string): Promise<StructurePrepResult> {
  const res = await longApi.get(`/api/structure-prep/status/${jobId}`);
  return res.data;
}