-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbb.sh
More file actions
executable file
·1133 lines (982 loc) · 34.3 KB
/
bb.sh
File metadata and controls
executable file
·1133 lines (982 loc) · 34.3 KB
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
#!/bin/bash
# Biobtree Build & Management Script (bb.sh)
# ==============================================
# Build datasets, generate databases, manage versions, and run services.
# Each dataset has its own log file for easy debugging.
# Long-running commands run in background - check logs for progress.
#
# Usage:
# ./bb.sh # Update all datasets (default: out/)
# ./bb.sh <output_dir> # Update all datasets to specified directory
# ./bb.sh --status # Show dataset status
# ./bb.sh --check # Check for changes only
# ./bb.sh --from pubchem # Resume from specific dataset
# ./bb.sh --only pubchem # Run single dataset
# ./bb.sh --generate # Run generate phase only (build database)
# ./bb.sh --db-versions # Show database versions
# ./bb.sh --activate # Activate latest db version
# ./bb.sh --activate 2 # Activate specific db version
# ./bb.sh --cleanup # Remove old db versions (keep last 2)
# ./bb.sh --web # Start web server
# ./bb.sh --test # Run integration tests
set -e
# Re-run script in background if BUILD_IN_BG is not set
# This makes the script itself run in background with output to log file
# Check all args for flags that should run in foreground
RUN_FOREGROUND=false
for arg in "$@"; do
case "$arg" in
--status|--check|--help|-h|--web|--test|--dry-run|--db-versions|--activate|--cleanup) RUN_FOREGROUND=true ;;
esac
done
if [[ -z "$BUILD_IN_BG" && "$RUN_FOREGROUND" == "false" ]]; then
mkdir -p logs/archive
LOG_FILE="logs/archive/build_$(date +%Y%m%d_%H%M%S).log"
echo "Running in background. Log: $LOG_FILE"
echo "Monitor: tail -f $LOG_FILE"
BUILD_IN_BG=1 nohup "$0" "$@" > "$LOG_FILE" 2>&1 &
echo "PID: $!"
exit 0
fi
# ============================================================================
# CONFIGURATION - Edit these settings as needed
# ============================================================================
# Taxonomy IDs for model organisms (AlphaFold's 16 model organisms)
TAXIDS="9606,10090,10116,7955,7227,6239,559292,284812,511145,3702,39947,4577,3847,44689,237561,243232"
# Default settings
MAXCPU=8
LOG_DIR="logs"
# Global options applied to ALL datasets
GLOBAL_OPTS="--include-optionals --lookupdb"
# ----------------------------------------------------------------------------
# DATASET-SPECIFIC OPTIONS
# Format: OPTS_<dataset>="extra options"
# These are added to GLOBAL_OPTS for specific datasets
# ----------------------------------------------------------------------------
OPTS_ensembl="--eo --tax ${TAXIDS}"
OPTS_string="--tax ${TAXIDS}"
OPTS_entrez="--genome-taxids ${TAXIDS}"
OPTS_refseq="--genome-taxids ${TAXIDS}"
OPTS_pubchem="--pubchem-sdf-workers 1"
OPTS_patent="--bucket-sort-workers 8"
OPTS_bgee="--bucket-sort-workers 8"
# ----------------------------------------------------------------------------
# DATASETS LIST - Order matters (foundations first)
# Comment out datasets you don't want to run
# ----------------------------------------------------------------------------
DATASETS=(
# Foundation
taxonomy
hgnc
ensembl
# Core biological
uniprot
interpro
chebi
# ChEMBL (expanded - was group "chembl")
chembl_document
chembl_assay
chembl_activity
chembl_molecule
chembl_target
chembl_target_component
chembl_cell_line
# Structure & function
alphafold
pdb
rnacentral
reactome
rhea
# Variants & disease
clinvar
gwas_study
gwas
dbsnp
alphamissense
alphamissense_transcript
# Interactions & pathways
intact
biogrid
string
signor
collectri
jaspar
encode_ccre
corum
cellphonedb
bgee
cellxgene
cellxgene_celltype
scxa
scxa_expression
fantom5_promoter
diamond_similarity
esm2_similarity
# Compounds & drugs
hmdb
lipidmaps
swisslipids
bindingdb
pharmgkb
# Enzymes & biochemistry
brenda
# PubChem (large - run one at a time)
pubchem
pubchem_activity
pubchem_assay
# Clinical & medical
clinical_trials
antibody
gencc
ctd
msigdb
# Patents
patent
# Ontologies (expanded - was group "ontology")
go
eco
efo
uberon
cl
mondo
hpo
oba
pato
obi
xco
bao
orphanet
# Vocabularies
mesh
# NCBI (run last - depends on others for xrefs)
entrez
refseq
)
# ============================================================================
# ARGUMENT PARSING
# ============================================================================
# Default output directory
OUT_DIR="out"
show_help() {
echo "Usage: $0 [output_dir] [OPTIONS]"
echo ""
echo "Arguments:"
echo " output_dir Output directory (default: out/)"
echo ""
echo "Options:"
echo " --check Check for source changes without updating"
echo " --from <dataset> Resume from specific dataset"
echo " --only <datasets> Run specific dataset(s), comma-separated (e.g., uniprot,hgnc,go)"
echo " --generate Run generate phase only (build database)"
echo " --generate-after Run generate after --only completes successfully"
echo " --federation <name> With --generate or --generate-after: build specific federation (main, dbsnp)"
echo " --force Force update even if unchanged"
echo " --maxcpu <N> Max CPUs (default: 8)"
echo " --dry-run Show what would be done"
echo " --status Show dataset status from state file"
echo " --web Start web server"
echo " --test Run integration tests (requires server on localhost:9291)"
echo " --prod With --test: test against production MCP server (localhost:8000)"
echo " --help Show this help message"
echo ""
echo "Database Version Management:"
echo " --db-versions Show database versions for all federations"
echo " --activate [N] Activate db version N (default: latest) for all federations"
echo " --cleanup [N] Remove old db versions, keeping last N (default: 2)"
echo ""
echo "Federations:"
echo " main Default federation (most datasets)"
echo " dbsnp dbSNP variants (separate large database)"
echo ""
echo "Available datasets:"
echo " ${DATASETS[*]}" | fold -s -w 70
exit 0
}
# Check if first argument is a directory (not an option)
if [[ -n $1 && ! $1 == --* ]]; then
OUT_DIR=$1
shift
fi
# Options
CHECK_ONLY="false"
FROM_DATASET=""
ONLY_DATASET=""
GENERATE_ONLY="false"
FORCE="false"
DRY_RUN="false"
SHOW_STATUS="false"
WEB_SERVER="false"
RUN_TESTS="false"
TEST_PROD="false"
FEDERATION=""
SHOW_DB_VERSIONS="false"
ACTIVATE_VERSION=""
DO_ACTIVATE="false"
DO_CLEANUP="false"
CLEANUP_KEEP=2
GENERATE_AFTER="false"
while [[ $# -gt 0 ]]; do
case "$1" in
--check) CHECK_ONLY="true"; shift ;;
--from) FROM_DATASET=$2; shift 2 ;;
--only) ONLY_DATASET=$2; shift 2 ;;
--generate) GENERATE_ONLY="true"; shift ;;
--federation) FEDERATION=$2; shift 2 ;;
--force) FORCE="true"; shift ;;
--maxcpu) MAXCPU=$2; shift 2 ;;
--dry-run) DRY_RUN="true"; shift ;;
--status) SHOW_STATUS="true"; shift ;;
--web) WEB_SERVER="true"; shift ;;
--test) RUN_TESTS="true"; shift ;;
--prod) TEST_PROD="true"; shift ;;
--db-versions) SHOW_DB_VERSIONS="true"; shift ;;
--activate)
DO_ACTIVATE="true"
# Check if next arg is a version number (not another option)
if [[ -n "$2" && ! "$2" == --* ]]; then
ACTIVATE_VERSION=$2
shift 2
else
shift
fi
;;
--cleanup)
DO_CLEANUP="true"
# Check if next arg is a number
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
CLEANUP_KEEP=$2
shift 2
else
shift
fi
;;
--generate-after) GENERATE_AFTER="true"; shift ;;
--help|-h) show_help ;;
*) echo "Unknown option: $1"; show_help ;;
esac
done
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
timestamp() {
date "+%Y-%m-%d %H:%M:%S"
}
log_header() {
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " $(timestamp) | $1"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
}
get_dataset_options() {
local dataset=$1
local opts="${GLOBAL_OPTS} --maxcpu ${MAXCPU}"
# Get dataset-specific options from OPTS_<dataset> variable
local var_name="OPTS_${dataset}"
local extra_opts="${!var_name}"
if [[ -n "$extra_opts" ]]; then
opts="$opts $extra_opts"
fi
if [[ "$FORCE" == "true" ]]; then
opts="$opts --force"
fi
echo "$opts"
}
# Archive log file to subfolder with timestamp
# Usage: rotate_log <log_file>
# Moves: logs/uniprot.log -> logs/archive/uniprot_2026-02-14_093045.log
rotate_log() {
local log_file=$1
if [[ ! -f "$log_file" ]]; then
return 0
fi
local log_dir=$(dirname "$log_file")
local log_name=$(basename "$log_file" .log)
local archive_dir="${log_dir}/archive"
local timestamp=$(date +%Y-%m-%d_%H%M%S)
local archive_file="${archive_dir}/${log_name}_${timestamp}.log"
# Create archive directory if needed
mkdir -p "$archive_dir"
# Move current log to archive with timestamp
mv "$log_file" "$archive_file"
}
# Backward compatibility alias
backup_log() {
rotate_log "$1"
}
run_dataset() {
local dataset=$1
local log_file="${LOG_DIR}/${dataset}.log"
local opts=$(get_dataset_options "$dataset")
log_header "$dataset"
echo "Log: $log_file"
if [[ "$DRY_RUN" == "true" ]]; then
echo "[DRY RUN] ./biobtree --out-dir \"$OUT_DIR\" $opts -d \"$dataset\" update"
return 0
fi
# Rotate previous logs before writing new one
rotate_log "$log_file"
# Run update (script itself is already in background)
if ./biobtree --out-dir "$OUT_DIR" $opts -d "$dataset" update > "$log_file" 2>&1; then
echo "✓ $dataset completed successfully"
return 0
else
echo "✗ $dataset FAILED - see $log_file"
echo ""
echo "Last 20 lines of log:"
tail -20 "$log_file"
return 1
fi
}
check_dataset() {
local dataset=$1
local opts=$(get_dataset_options "$dataset")
echo -n " $dataset: "
if ./biobtree --out-dir "$OUT_DIR" $opts -d "$dataset" check 2>/dev/null | grep -q "changed"; then
echo "CHANGED"
else
echo "unchanged"
fi
}
format_duration() {
local secs=$1
# Handle decimal seconds
local int_secs=$(printf "%.0f" "$secs")
local hours=$((int_secs / 3600))
local mins=$(((int_secs % 3600) / 60))
local remaining=$((int_secs % 60))
if [[ $hours -gt 0 ]]; then
printf "%dh %dm %ds" $hours $mins $remaining
elif [[ $mins -gt 0 ]]; then
printf "%dm %ds" $mins $remaining
else
printf "%ds" $remaining
fi
}
format_size() {
local bytes=$1
if [[ -z "$bytes" || "$bytes" == "null" || "$bytes" == "0" ]]; then
echo "-"
return
fi
local gb=$(echo "scale=2; $bytes / 1073741824" | bc)
local mb=$(echo "scale=2; $bytes / 1048576" | bc)
local kb=$(echo "scale=2; $bytes / 1024" | bc)
if (( $(echo "$gb >= 1" | bc -l) )); then
printf "%.1f GB" "$gb"
elif (( $(echo "$mb >= 1" | bc -l) )); then
printf "%.1f MB" "$mb"
else
printf "%.1f KB" "$kb"
fi
}
# Get federation for a dataset (dbsnp -> dbsnp, others -> main)
get_federation() {
local dataset=$1
case "$dataset" in
dbsnp) echo "dbsnp" ;;
*) echo "main" ;;
esac
}
# ============================================================================
# DATABASE VERSION MANAGEMENT
# ============================================================================
# Get all db versions for a federation (returns space-separated version numbers)
get_db_versions() {
local federation=$1
local fed_dir="$OUT_DIR/$federation"
if [[ ! -d "$fed_dir" ]]; then
echo ""
return
fi
# Find db_v* directories and extract version numbers
ls -1 "$fed_dir" 2>/dev/null | grep -E '^db_v[0-9]+$' | sed 's/db_v//' | sort -n | tr '\n' ' '
}
# Get current version (what db symlink points to)
get_current_version() {
local federation=$1
local symlink="$OUT_DIR/$federation/db"
if [[ ! -L "$symlink" ]]; then
# Not a symlink - might be old format directory
if [[ -d "$symlink" ]]; then
echo "legacy"
else
echo "none"
fi
return
fi
local target=$(readlink "$symlink")
if [[ "$target" =~ ^db_v([0-9]+)$ ]]; then
echo "${BASH_REMATCH[1]}"
else
echo "unknown"
fi
}
# Get latest (highest) version number
get_latest_version() {
local federation=$1
local versions=$(get_db_versions "$federation")
if [[ -z "$versions" ]]; then
echo "0"
return
fi
# Get last (highest) version
echo "$versions" | tr ' ' '\n' | tail -1
}
# Activate a specific db version (update symlink)
activate_db_version() {
local federation=$1
local version=$2
local fed_dir="$OUT_DIR/$federation"
local symlink="$fed_dir/db"
local target="db_v$version"
local target_path="$fed_dir/$target"
# Verify target exists
if [[ ! -d "$target_path" ]]; then
echo "ERROR: Version $version does not exist for federation '$federation'"
echo "Available versions: $(get_db_versions "$federation")"
return 1
fi
# Handle existing symlink or directory
if [[ -L "$symlink" ]]; then
rm "$symlink"
elif [[ -d "$symlink" ]]; then
# Legacy directory - rename to db_v0
echo "Migrating legacy db directory to db_v0..."
mv "$symlink" "$fed_dir/db_v0"
fi
# Create new symlink (relative path)
ln -s "$target" "$symlink"
echo "✓ Activated db_v$version for federation '$federation'"
}
# Cleanup old versions, keeping the last N
cleanup_old_versions() {
local federation=$1
local keep=${2:-2}
local fed_dir="$OUT_DIR/$federation"
local current=$(get_current_version "$federation")
local versions=($(get_db_versions "$federation"))
local count=${#versions[@]}
if [[ $count -le $keep ]]; then
echo "Only $count version(s) exist, nothing to cleanup"
return
fi
local to_delete=$((count - keep))
echo "Cleaning up $to_delete old version(s), keeping last $keep..."
for ((i=0; i<to_delete; i++)); do
local ver=${versions[$i]}
if [[ "$ver" == "$current" ]]; then
echo " Skipping db_v$ver (currently active)"
continue
fi
echo " Removing db_v$ver..."
rm -rf "$fed_dir/db_v$ver"
done
}
# Show db versions for all federations
show_db_versions() {
echo ""
echo "============================================"
echo "Database Versions"
echo "============================================"
echo "Output directory: $OUT_DIR"
echo ""
for federation in main dbsnp; do
local fed_dir="$OUT_DIR/$federation"
if [[ ! -d "$fed_dir" ]]; then
continue
fi
local current=$(get_current_version "$federation")
local latest=$(get_latest_version "$federation")
local versions=$(get_db_versions "$federation")
echo "Federation: $federation"
echo " Current: ${current:-none}"
echo " Latest: ${latest:-none}"
echo -n " Available: "
if [[ -z "$versions" ]]; then
echo "none"
else
for v in $versions; do
if [[ "$v" == "$current" ]]; then
echo -n "v$v* "
else
echo -n "v$v "
fi
done
echo ""
fi
# Show sizes
for v in $versions; do
local size=$(du -sh "$fed_dir/db_v$v" 2>/dev/null | cut -f1)
echo " db_v$v: ${size:-unknown}"
done
echo ""
done
}
# Calculate actual KV size from index files for a dataset
# Counts: {dataset}_sorted.*.index.gz + {dataset}_from_*.index.gz
# Uses federation-aware paths: {OUT_DIR}/{federation}/index/
calc_kv_size() {
local dataset=$1
local federation=$(get_federation "$dataset")
local index_dir="$OUT_DIR/$federation/index"
if [[ ! -d "$index_dir" ]]; then
echo "0"
return
fi
# Count main file + from_* files (files that add data TO this dataset)
local total=$(ls -la "$index_dir" 2>/dev/null | grep -E "^-.* ${dataset}_sorted\..*\.index\.gz$|^-.* ${dataset}_from_.*\.index\.gz$" | awk '{total += $5} END {print total+0}')
echo "${total:-0}"
}
show_status() {
local state_file="$OUT_DIR/dataset_state.json"
if [[ ! -f "$state_file" ]]; then
echo "ERROR: State file not found: $state_file"
echo "Run an update first to generate the state file."
exit 1
fi
# Check if jq is available
if ! command -v jq &> /dev/null; then
echo "ERROR: jq is required for --status option"
echo "Install with: apt install jq"
exit 1
fi
echo ""
echo "============================================"
echo "Dataset Status Report"
echo "============================================"
echo "State file: $state_file"
echo ""
# Global info
local build_time=$(jq -r '.last_build_time // "N/A"' "$state_file" | cut -d'T' -f1,2 | tr 'T' ' ' | cut -d'.' -f1)
local build_version=$(jq -r '.build_version // "N/A"' "$state_file")
# Sum KV size across all federation index directories
local total_kv=0
for fed_index in "$OUT_DIR"/*/index; do
if [[ -d "$fed_index" ]]; then
local fed_size=$(ls -la "$fed_index" 2>/dev/null | grep -E "\.index\.gz$" | awk '{total += $5} END {print total+0}')
total_kv=$((total_kv + fed_size))
fi
done
echo "Last Build: $build_time"
echo "Version: $build_version"
echo "Total KV Size: $(format_size $total_kv)"
echo ""
# Header
printf "%-25s %-8s %-12s %-20s %-12s %-12s\n" "DATASET" "FED" "STATUS" "LAST BUILD" "KV SIZE" "DURATION"
printf "%-25s %-8s %-12s %-20s %-12s %-12s\n" "-------------------------" "--------" "------------" "--------------------" "------------" "------------"
# Collect all dataset info for sorting
local temp_file=$(mktemp)
for dataset in "${DATASETS[@]}"; do
local status=$(jq -r ".datasets[\"$dataset\"].status // \"-\"" "$state_file")
local last_build=$(jq -r ".datasets[\"$dataset\"].last_build_time // \"\"" "$state_file")
local kv_size=$(calc_kv_size "$dataset")
local duration=$(jq -r ".datasets[\"$dataset\"].build_duration_sec // 0" "$state_file")
local federation=$(get_federation "$dataset")
# Format build time (extract date and time)
if [[ "$last_build" == "" || "$last_build" == "0001-01-01T00:00:00Z" ]]; then
last_build="-"
else
last_build=$(echo "$last_build" | cut -d'T' -f1,2 | tr 'T' ' ' | cut -d'.' -f1 | cut -d'+' -f1)
fi
# Format status with color codes
local status_display="$status"
if [[ "$status" == "merged" ]]; then
status_display="✓ merged"
elif [[ "$status" == "processing" ]]; then
status_display="⏳ processing"
elif [[ "$status" == "-" || "$status" == "" ]]; then
status_display="- not built"
fi
# Format sizes and duration
local kv_display=$(format_size "$kv_size")
local duration_display="-"
if [[ "$duration" != "0" && "$duration" != "null" ]]; then
duration_display=$(format_duration "$duration")
fi
# Store with raw kv_size for sorting (tab-separated: raw_size, formatted_line)
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "$kv_size" "$dataset" "$federation" "$status_display" "$last_build" "$kv_display" "$duration_display" >> "$temp_file"
done
# Sort by kv_size (first field) descending and print formatted output
sort -t$'\t' -k1 -n -r "$temp_file" | while IFS=$'\t' read -r raw_size dataset federation status_display last_build kv_display duration_display; do
printf "%-25s %-8s %-12s %-20s %-12s %-12s\n" "$dataset" "$federation" "$status_display" "$last_build" "$kv_display" "$duration_display"
done
rm -f "$temp_file"
echo ""
echo "============================================"
# Summary counts - count from DATASETS array, not state file
local merged_count=0
local processing_count=0
local not_built_count=0
for dataset in "${DATASETS[@]}"; do
local status=$(jq -r ".datasets[\"$dataset\"].status // \"\"" "$state_file")
if [[ "$status" == "merged" ]]; then
((merged_count++)) || true
elif [[ "$status" == "processing" ]]; then
((processing_count++)) || true
else
((not_built_count++)) || true
fi
done
echo "Summary: $merged_count merged, $processing_count processing, $not_built_count not built"
echo ""
}
run_tests() {
local server_url="${1:-http://localhost:9291}"
local use_mcp="${2:-false}"
local test_dir="tests/xintegration"
local test_script="${test_dir}/run_integration_tests.py"
if [[ ! -f "$test_script" ]]; then
echo "ERROR: Test script not found: $test_script"
exit 1
fi
# Check if python3 is available
if ! command -v python3 &> /dev/null; then
echo "ERROR: python3 is required for integration tests"
exit 1
fi
echo ""
echo "============================================"
echo "Running Integration Tests"
echo "============================================"
echo "Server: $server_url"
echo "Mode: $([ "$use_mcp" == "true" ] && echo "MCP API" || echo "Biobtree direct")"
echo "Test script: $test_script"
echo ""
# Run the integration tests
local extra_args=""
if [[ "$use_mcp" == "true" ]]; then
extra_args="--mcp"
fi
python3 "$test_script" --server "$server_url" $extra_args
local exit_code=$?
if [[ $exit_code -eq 0 ]]; then
echo ""
echo "✓ All integration tests passed"
else
echo ""
echo "✗ Some integration tests failed"
fi
return $exit_code
}
# ============================================================================
# MAIN
# ============================================================================
echo "============================================"
echo "BioBTree Build & Management Script"
echo "============================================"
echo "Output: $OUT_DIR"
echo "CPUs: $MAXCPU"
echo "Mode: $([ "$CHECK_ONLY" == "true" ] && echo "CHECK ONLY" || echo "UPDATE")"
echo ""
# Create directories
mkdir -p "$OUT_DIR" "$LOG_DIR"
# Check mode
if [[ "$CHECK_ONLY" == "true" ]]; then
echo "Checking for source changes..."
echo ""
for dataset in "${DATASETS[@]}"; do
check_dataset "$dataset"
done
exit 0
fi
# Status mode
if [[ "$SHOW_STATUS" == "true" ]]; then
show_status
exit 0
fi
# DB versions mode
if [[ "$SHOW_DB_VERSIONS" == "true" ]]; then
show_db_versions
exit 0
fi
# Activate version mode
if [[ "$DO_ACTIVATE" == "true" ]]; then
echo ""
echo "============================================"
echo "Activating Database Version"
echo "============================================"
for federation in main dbsnp; do
fed_dir="$OUT_DIR/$federation"
if [[ ! -d "$fed_dir" ]]; then
continue
fi
version="$ACTIVATE_VERSION"
if [[ -z "$version" ]]; then
version=$(get_latest_version "$federation")
fi
if [[ "$version" == "0" || -z "$version" ]]; then
echo "No versions found for federation '$federation'"
continue
fi
current=$(get_current_version "$federation")
if [[ "$current" == "$version" ]]; then
echo "Federation '$federation': already on db_v$version"
else
activate_db_version "$federation" "$version"
fi
done
echo ""
echo "Note: Restart the web service to use the new version"
exit 0
fi
# Cleanup mode
if [[ "$DO_CLEANUP" == "true" ]]; then
echo ""
echo "============================================"
echo "Cleaning Up Old Database Versions"
echo "============================================"
echo "Keeping last $CLEANUP_KEEP version(s)"
echo ""
for federation in main dbsnp; do
fed_dir="$OUT_DIR/$federation"
if [[ ! -d "$fed_dir" ]]; then
continue
fi
echo "Federation: $federation"
cleanup_old_versions "$federation" "$CLEANUP_KEEP"
echo ""
done
exit 0
fi
# Test mode
if [[ "$RUN_TESTS" == "true" ]]; then
if [[ "$TEST_PROD" == "true" ]]; then
run_tests "http://localhost:8000" "true"
else
run_tests "http://localhost:9291" "false"
fi
exit $?
fi
# Web server mode
if [[ "$WEB_SERVER" == "true" ]]; then
if [[ "$OUT_DIR" == "out" ]]; then
# Default directory - run in foreground
echo "Starting web server (foreground)..."
exec ./biobtree web
else
# Custom directory - run in background with --prod
echo "Starting web server in background..."
WEB_LOG="${LOG_DIR}/web.log"
rotate_log "$WEB_LOG"
echo "Log: $WEB_LOG"
nohup ./biobtree --out-dir "$OUT_DIR" --prod web > "$WEB_LOG" 2>&1 &
echo "PID: $!"
echo "Monitor: tail -f ${LOG_DIR}/web.log"
fi
exit 0
fi
# Selected datasets mode (supports comma-separated list)
if [[ -n "$ONLY_DATASET" ]]; then
# Split by comma into array
IFS=',' read -ra SELECTED_DATASETS <<< "$ONLY_DATASET"
FAILED_SELECTED=()
COMPLETED_SELECTED=0
TOTAL_SELECTED=${#SELECTED_DATASETS[@]}
for dataset in "${SELECTED_DATASETS[@]}"; do
# Trim whitespace
dataset=$(echo "$dataset" | xargs)
((COMPLETED_SELECTED++)) || true
echo ""
echo "[$COMPLETED_SELECTED/$TOTAL_SELECTED] Processing: $dataset"
if ! run_dataset "$dataset"; then
FAILED_SELECTED+=("$dataset")
echo "WARNING: $dataset failed, continuing..."
fi
done
echo ""
echo "============================================"
if [[ ${#FAILED_SELECTED[@]} -eq 0 ]]; then
echo "✓ All $TOTAL_SELECTED dataset(s) completed successfully"
else
echo "Completed: $((TOTAL_SELECTED - ${#FAILED_SELECTED[@]}))/$TOTAL_SELECTED"
echo "Failed: ${FAILED_SELECTED[*]}"
exit 1
fi
# Run generate after if requested
if [[ "$GENERATE_AFTER" == "true" ]]; then
if [[ -n "$FEDERATION" ]]; then
log_header "Generate ($FEDERATION federation)"
echo "Building $FEDERATION federation database..."
GEN_LOG="${LOG_DIR}/generate_${FEDERATION}.log"
rotate_log "$GEN_LOG"
echo "Log: $GEN_LOG"
if [[ "$DRY_RUN" == "true" ]]; then
echo "[DRY RUN] ./biobtree --out-dir \"$OUT_DIR\" --federation \"$FEDERATION\" --lmdb-safety-factor 4.5 generate"
elif ./biobtree --out-dir "$OUT_DIR" --federation "$FEDERATION" --lmdb-safety-factor 4.5 generate > "$GEN_LOG" 2>&1; then
echo "✓ Generate complete ($FEDERATION federation)"
# Show version info
latest=$(get_latest_version "$FEDERATION")
current=$(get_current_version "$FEDERATION")
echo ""
echo "New version created: db_v$latest"
echo "Current active: db_v$current"
if [[ "$latest" != "$current" ]]; then
echo ""
echo "To activate the new version:"
echo " $0 $OUT_DIR --activate"
fi
else
echo "✗ Generate FAILED ($FEDERATION) - see $GEN_LOG"
exit 1
fi
else
log_header "Generate (all federations)"
echo "Building all federation databases..."
GEN_LOG="${LOG_DIR}/generate.log"
rotate_log "$GEN_LOG"
echo "Log: $GEN_LOG"
if [[ "$DRY_RUN" == "true" ]]; then
echo "[DRY RUN] ./biobtree --out-dir \"$OUT_DIR\" --lmdb-safety-factor 4.5 generate"
elif ./biobtree --out-dir "$OUT_DIR" --lmdb-safety-factor 4.5 generate > "$GEN_LOG" 2>&1; then
echo "✓ Generate complete (all federations)"
# Show version info for each federation
echo ""
echo "Version Summary:"
for federation in main dbsnp; do
fed_dir="$OUT_DIR/$federation"
if [[ ! -d "$fed_dir" ]]; then
continue
fi
latest=$(get_latest_version "$federation")
current=$(get_current_version "$federation")
if [[ "$latest" != "0" ]]; then
echo " $federation: created db_v$latest (active: db_v$current)"
fi
done
echo ""
echo "To activate the new version(s):"
echo " $0 $OUT_DIR --activate"
else
echo "✗ Generate FAILED - see $GEN_LOG"
exit 1
fi
fi
fi
exit 0
fi
# Generate only mode
if [[ "$GENERATE_ONLY" == "true" ]]; then
if [[ -n "$FEDERATION" ]]; then
log_header "Generate ($FEDERATION federation)"
echo "Building $FEDERATION federation database..."
GEN_LOG="${LOG_DIR}/generate_${FEDERATION}.log"
rotate_log "$GEN_LOG"