forked from logicmoo/vspace-metta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINSTALL.sh
executable file
·363 lines (295 loc) · 13.4 KB
/
INSTALL.sh
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
#!/bin/bash
# Run this file with ./INSTALL.md
# ```
# Function to prompt for user confirmation with 'N' as the default
confirm_with_default() {
echo -e -n "$2"
while true; do
if [ "$1" == "N" ]; then
read -s -p " (y/N): " -n 1 yn
else
read -s -p " (${1}/n): " -n 1 yn
fi
if [ -z "$yn" ]; then
yn="$1" # Corrected assignment without spaces
fi
case $yn in
[Yy]* ) echo "Y" && return 0;;
[Nn]* ) echo "N" && return 1;;
* ) echo -e "${YELLOW}Please answer yes or no.${NC}";;
esac
done
}
# Function to prompt for input with a default value
prompt_for_input() {
read -e -i "$2" -p "$1" value
echo -e "${value:-$2}"
}
# ANSI escape codes
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BOLD='\033[1m'
# ANSI escape code to reset color
NC='\033[0m' # No Color
# Initialize easy_install to a default value of '?'
easy_install="?"
# Check command line arguments for --steps or --easy
for arg in "$@"
do
case $arg in
--steps)
easy_install="N"
shift # Remove --steps from the list of arguments
;;
--easy)
easy_install="Y"
shift # Remove --easy from the list of arguments
;;
*)
# Ignore unknown options
;;
esac
done
# Ask the user if easy_install is still '?'
if [ "$easy_install" == "?" ]; then
if confirm_with_default "Y" "Would you like to use easy installation mode?"; then
easy_install="Y"
else
easy_install="N"
fi
fi
echo -e "${BLUE}Starting the installation process..${NC}."
# Check if SWI-Prolog is installed
if ! command -v swipl &> /dev/null; then
if confirm_with_default "Y" "SWI-Prolog is not installed. Would you like to install it?"; then
echo -e "${BLUE}Installing SWI-Prolog.${NC}."
sudo apt-add-repository -y ppa:swi-prolog/devel
sudo apt-get update
sudo apt-get install -y swi-prolog
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to install SWI-Prolog. Exiting script${NC}."
exit 1
fi
else
echo -e "${RED}SWI-Prolog installation aborted. Exiting script${NC}."
exit 1
fi
else
swi_prolog_version=$(swipl --version)
if [[ $swi_prolog_version == *"9.1"* ]]; then
echo -e "${GREEN}SWI-Prolog version 9.1 is already installed${NC}."
else
echo "${YELLOW}SWI-Prolog is not version 9.1${NC}."
sudo apt-add-repository -y ppa:swi-prolog/devel
sudo apt-get remove -y swi-prolog*
sudo apt-get update
sudo apt-get install -y swi-prolog
# sudo apt-get install -y swi-prolog-bdb swi-prolog-odbc swi-prolog-java
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to install SWI-Prolog. Exiting script${NC}."
exit 1
fi
swi_prolog_version=$(swipl --version)
if [[ $swi_prolog_version == *"9.1"* ]]; then
echo -e "${GREEN}SWI-Prolog upgraded to 9.1{NC}."
else
echo "${YELLOW}SWI-Prolog is still not version 9.1 .. So Janus will probably fail if not already installed${NC}."
fi
fi
fi
function ensure_pip() {
# Check if pip is installed
if ! command -v pip &> /dev/null; then
echo "pip is not installed. Installing pip..."
sudo apt-get update
sudo apt-get install -y python3-pip
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to install pip. Exiting script${NC}."
exit 1
fi
else
echo "pip is already installed."
fi
}
# Assuming SWI-Prolog 9.1 is installed successfully
# Install Janus for SWI-Prolog
echo -e "${BLUE}Checking if Janus Python support is already installed${NC}..."
if ! swipl -g "use_module(library(janus)), halt(0)." -t "halt(1)" 2>/dev/null; then
# janus not installed, prompt the user
if [ "${easy_install}" == "Y" ] || confirm_with_default "Y" "Would you like to install Python (Janus) support"; then
echo "Installing Janus for SWI-Prolog..."
ensure_pip
sudo pip install git+https://github.com/SWI-Prolog/packages-swipy.git
sudo apt install -y libpython3-dev
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to install Janus. Exiting script${NC}."
exit 1
else
echo "Janus installed successfully."
fi
else
echo -e "${YELLOW}Skipping Janus Python support installation${NC}."
fi
else
echo -e "${GREEN}Janus Python support is already installed${NC}."
fi
# Install PySWIP for SWI-Prolog
echo -e "${BLUE}Checking if Pyswip is already installed${NC}..."
if ! python3 -c "import pyswip" &> /dev/null; then
# Pyswip not installed, prompt the user
if [ "${easy_install}" == "Y" ] || confirm_with_default "Y" "Would you like to install Pyswip"; then
echo -e "${BLUE}Installing Pyswip..${NC}."
ensure_pip
sudo pip install git+https://github.com/logicmoo/pyswip.git
echo -e "${GREEN}Pyswip installation complete${NC}."
else
echo -e "${YELLOW}Skipping Pyswip installation${NC}."
fi
else
echo -e "${GREEN}Pyswip is already installed${NC}."
fi
echo -e "${BLUE}Updating SWI-Prolog packages...${NC}"
if ! swipl -g "use_module(library(predicate_streams)), halt(0)." -t "halt(1)" 2>/dev/null; then
echo "Installing predicate_streams..."
echo -e "${YELLOW}${BOLD}If asked, say yes to everything and/or accept the defaults...${NC}"
swipl -g "pack_install(predicate_streams,[interactive(false)])" -t halt
else
echo -e "${GREEN}Pack predicate_streams is already installed${NC}."
fi
if ! swipl -g "use_module(library(logicmoo_utils)), halt(0)." -t "halt(1)" 2>/dev/null; then
echo "Installing logicmoo_utils..."
echo -e "${YELLOW}${BOLD}If asked, say yes to everything and/or accept the defaults...${NC}"
swipl -g "pack_install('https://github.com/TeamSPoon/logicmoo_utils.git',[insecure(true),interactive(false),git(true),verify(false)])" -t halt
else
echo -e "${GREEN}Pack logicmoo_utils is already installed${NC}."
fi
if ! swipl -g "use_module(library(dictoo)), halt(0)." -t "halt(1)" 2>/dev/null; then
echo "Installing dictoo..."
echo -e "${YELLOW}${BOLD}If asked, say yes to everything and/or accept the defaults...${NC}"
swipl -g "pack_install(dictoo,[interactive(false)])" -t halt
else
echo -e "${GREEN}Pack dictoo is already installed${NC}."
fi
# Setting PYTHONPATH environment variable
echo -e "${BLUE}Setting PYTHONPATH environment variable..${NC}."
export PYTHONPATH=$PWD/metta_vspace:$PYTHONPATH
# Confirming download of Quick Loadable Flybase files
if [ "${easy_install}" == "Y" ] || confirm_with_default "Y" "Download Quick Loadable Flybase files"; then
if [ -f whole_flybase.qlf ]; then
echo -e "${YELLOW}whole_flybase.qlf already exists. Skipping download and extraction.${NC}"
else
echo -e "${BLUE}Downloading whole_flybase.qlf...${NC}"
wget --show-progress https://logicmoo.org/public/metta/data/whole_flybase.qlf.gz && echo "Unzipping..." && gunzip whole_flybase.qlf.gz || { echo -e "${RED}Error in download or unzipping.${NC}" && exit 1; }
echo -e "${BLUE}Download and unzipping complete.${NC}"
fi
# Checking for whole_flybase in README.md
echo -e "${BLUE}Checking for whole_flybase in README.md...${NC}"
grep -B 3 -A 4 whole_flybase README.md || { echo -e "${RED}Error or no matches in README.md${NC}" && exit 1; }
echo -e "${Green}Process completed successfully.${NC}"
if [ "${easy_install}" == "Y" ]; then
exit 0
fi
else
echo -e "${YELLOW}Download of Quick Loadable Flybase files skipped.${NC}"
fi
echo -e "${BLUE}Allowing user to override FBPC_VERSION..${NC}."
export FBPC_VERSION=$(prompt_for_input "Enter the Flybase version slug (or press <enter> to use this default): " "2023_05")
echo -e "${GREEN}"
set -x
export FBPC_URL="ftp.flybase.org/releases/FB$FBPC_VERSION/precomputed_files/"
export FBPC_LOC="./data/$FBPC_URL"
set +x
echo -e "${NC}"
need_fb_files="Y"
if find "$FBPC_LOC" -mindepth 2 -type f -print -quit | grep -q '.'; then
need_fb_files="N"
echo -e -n "${GREEN}Looks like we already have the Flybase data.. \n${BLUE}Really RE-"
else
echo -e "${YELLOW}Looks like we *need* have the Flybase data..${NC}."
fi
if confirm_with_default "${need_fb_files}" "Download Flybase Release?"; then
echo -e "${BLUE}You may override the Flybase URL${NC}..."
export FBPC_URL=$(prompt_for_input "Enter the FBPC URL http://" "${FBPC_URL}")
echo -e "${BLUE}Downloading necessary files..${NC}."
wget -c --no-parent -r -P ./data/ --reject="index.htm*" http://$FBPC_URL
echo -e "${BLUE}Setting the precomputed location..${NC}."
export FBPC_LOC=./data/$FBPC_URL
echo -e "${BLUE}Checking and deleting duplicated files..${NC}."
find $FBPC_LOC -type f -name '*_fb_*' -exec bash -c 'if [[ -f ${1/_fb_????_??/} ]]; then rm -f ${1/_fb_????_??/}; fi' _ {} \;
echo -e "${BLUE}Calculating disk usage (should be around 587M)..${NC}."
du -hs $FBPC_LOC
echo ""
echo -e "${BLUE}Decompressing the downloaded files..${NC}."
find $FBPC_LOC -type f -name "*.gz" -execdir bash -c 'if [ ! -f "${1%.gz}" ]; then gunzip -k "$1"; else echo "File exists, skipping: $1"; fi' bash {} \;
#find $FBPC_LOC -type f -name "*.zip" -print -execdir unzip -o -d . {} \;
echo ""
echo -e "${BLUE}Normalizing identifiers in files..${NC}."
set -x
find $FBPC_LOC -name "*.fb" -exec sed -i -e 's/FB:FB/FB/g' {} \;
find $FBPC_LOC -name "*.json" -exec sed -i -e 's/FLYBASE:FB/FB/g' {} \;
time find $FBPC_LOC -type f ! -name "*.gz" -exec sed -i -e 's/\(FB[a-z]\{2\}\):\([0-9]\)/\1\2/g' -e 's/[Ff][Ll][Yy][Bb][Aa][Ss][Ee]:\([A-Za-z]\)/\1/g' {} \;
set +x
fi
echo -e "${BLUE}Checking disk usage for flybase (should be around 7.9G)..${NC}."
du -hs $FBPC_LOC
echo ""
if confirm_with_default "N" "Building the Loadable Files might take around 30 minutes. Do you want to continue"; then
echo -e "${BLUE}Building the Loadable File..${NC}."
set -x
./MeTTa -G "'!(create-flybase-pl! ${FBPC_LOC})'" -G "'!(halt! 777)'"
set +x
echo -e "${BLUE}Loadable File build complete${NC}."
echo -e "${BLUE}No metta files for:${NC}."
find ftp.flybase.org -type f ! -name "*.png" ! -name "*.gz" ! -name "*.metta" ! -name "*.datalog" -exec bash -c 'for file; do base="${file%.*}"; [[ -z $(find "$(dirname "$file")" -type f -wholename "$base*etta") ]] && echo "$file"; done' bash {} +
echo -e "${BLUE}No datalog files for:${NC}."
find ftp.flybase.org -type f ! -name "*.png" ! -name "*.gz" ! -name "*.zip" ! -name "*.datalog" -exec bash -c 'for file; do base="${file%.*}"; [[ -z $(find "$(dirname "$file")" -type f -wholename "$base*atalog") ]] && echo "$file"; done' bash {} +
echo -e "${BLUE}Removing ':' in datalog files..${NC}."
find ./data -type f -name "*.datalog" -print -exec sed -i -e 's/\(FB[a-z]\{2\}\):\([0-9]\)/\1\2/g' -e 's/[Ff][Ll][Yy][Bb][Aa][Ss][Ee]:\([A-Za-z]\)/\1/g' {} \;
echo -e "${BLUE}Removing duplicate lines in datalog files..${NC}."
time find ./data -type f -name "*.datalog" -exec sh -c 'awk '\''!seen[$0]++'\'' "$1" > tmpfile && mv -f tmpfile "$1"' sh {} \;
echo -e "${BLUE}Removing duplicate lines in metta files..${NC}."
time find ./data -type f -name "*.metta" -exec sh -c 'awk '\''!seen[$0]++'\'' "$1" > tmpfile && mv -f tmpfile "$1"' sh {} \;
echo -e "${BLUE}Alligning OBO preds.${NC}."
time find ./data/supplimental/12_ontologies/ -type f -name "*.datalog?*" -print -exec sed -i -e 's/obo-is-a/obo-is-type/g' {} \;
echo -e "${BLUE}Alligning OBO preds.${NC}."
time find ./data -type f -name "*.datalog?*" -print -exec sed -i -e 's/obo-has-definition/obo-def/g' -e 's/obo-Inheritance/obo-is-a/g' -e 's/obo-Member/Member/g' -e 's/obo-has-name/obo-name/g' {} \;
# -e 's/obo-[A-Z][A-z]+-/obo-/g'
echo -e "${BLUE}Combining all of the datalog files into one big one..${NC}."
find ./data -mindepth 2 -type f -name "*.datalog" -exec cat ./data/whole_header.datalog {} + > ./data/whole_flybase.metta.datalog
echo -e "${BLUE}Combining all of the MeTTa files into one big one..${NC}."
find ./data -mindepth 2 -type f -name "*.metta" -exec cat {} + > ./data/whole_flybase.metta
echo -e "${BLUE}Removing any duplicates from one big one..${NC}."
time awk '!seen[$0]++' ./data/whole_flybase.metta.datalog > tmpfile && mv -f tmpfile ./data/whole_flybase.metta.datalog
echo -e "${BLUE}Removing any duplicates from one big one..${NC}."
time awk '!seen[$0]++' ./data/whole_flybase.metta > tmpfile && mv -f tmpfile ./data/whole_flybase.metta
else
echo -e "${BLUE}Skipping the building of the Loadable File${NC}."
fi
ls -lh whole_flybase.datalog2
wc - l whole_flybase.datalog2
if confirm_with_default "Y" "Building the Quick Load File might take around 30 minutes. Do you want to continue"; then
echo -e "${BLUE}Building the Quick Load File..${NC}."
set -x
time swipl -g "qcompile('data/whole_flybase.datalog2')" -t halt
set +x
echo -e "${BLUE}Quick Load File build complete${NC}."
else
echo -e "${BLUE}Skipping the building of the Quick Load File${NC}."
fi
# Optional Rust Metta loading
if confirm_with_default_no "Would you like to be able to load these Flybase Metta files into Rust Metta"; then
echo -e "${BLUE}Converting data for Rust Metta..${NC}."
set -x
./scripts/convert_to_metta.sh $FBPC_LOC
set +x
echo -e "${BLUE}Counting atoms (should be at least 56 million)..${NC}."
find $FBPC_LOC -type f -name "*.metta" -exec wc -l {} +
fi
echo -e "${BLUE}Installation and setup complete!"
if confirm_with_default "N" "Show README.md"; then
cat README.md
fi
# End of the script
# ```