forked from xiaolei-lab/hiblup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiblupInstaller.sh
executable file
·188 lines (166 loc) · 5.13 KB
/
hiblupInstaller.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
#!/bin/bash
# Author: Haohao Zhang <[email protected]>
# Date: Jul 10, 2019
# Define
VERSION="1.3.1"
R_VERSION="3.5.1"
MIRROR_DEFAULT="tuna"
REPO_ROOT="https://raw.githubusercontent.com/xiaolei-lab/hiblup/master/version/${VERSION}"
# getopts
MIRROR=$MIRROR_DEFAULT
while getopts "d:m:k" opt; do
case $opt in
d)
if [[ -z ${CONDA_PREFIX} ]]; then
mkdir -p $OPTARG
INSTALL_PATH=$(cd "$OPTARG" && pwd)
else
echo "Warning: CONDA_PREFIX found, -d is ignored."
echo "CONDA_PREFIX: ${CONDA_PREFIX}"
fi
;;
r)
R_VERSION=$OPTARG
;;
v)
VERSION=$OPTARG
;;
m)
MIRROR=$OPTARG
;;
k)
CURL_OPT="-k"
;;
\?)
echo ""
echo "./hiblupInstaller.sh [-d <install_path>] [-m 'tuna'|'official'] [-k]"
echo "Usage:"
echo " -d : Specify the conda installation path. If a conda is detected, this option"
echo " will be ignored. (default: ~/miniconda3)"
echo " -m : Specify the mirror source, which can be 'tuna' or 'official'. Users in"
echo " mainland China recommend 'tuna'. Users in other regions can use"
echo " 'official'. (default: tuna)"
echo " -k : turn off curl's verification of the certificate."
echo ""
;;
esac
done
# Define
if [[ ${MIRROR} == "tuna" ]]; then
CONDA_MIRROR=https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda
CRAN_MIRROR=https://mirrors.tuna.tsinghua.edu.cn/CRAN/
elif [[ ${MIRROR} == "official" ]]; then
CONDA_MIRROR=https://repo.continuum.io/miniconda
CRAN_MIRROR=http://cran.rstudio.com
else
echo "Error: Unknow MIRROR."
exit 1
fi
# OS
if [[ "$(uname)" == "Darwin" ]]; then
CONDA_INSTALLER="Miniconda3-latest-MacOSX-x86_64.sh"
HIBLUP_PACKAGE="hiblup_${VERSION}_R_${R_VERSION}_community_x86_64_macOS.tar.gz"
R_ENV="r-base=${R_VERSION}"
PROFILE="${HOME}/.bash_profile"
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
CONDA_INSTALLER="Miniconda3-latest-Linux-x86_64.sh"
HIBLUP_PACKAGE="hiblup_${VERSION}_R_${R_VERSION}_community_x86_64_Linux.tar.gz"
R_ENV="mro-base=${R_VERSION}"
PROFILE="${HOME}/.bashrc"
else
echo "Error: Unknow OS."
exit 1
fi
# Workdir
DIR=$(pwd)
TMP_DIR=$(mktemp -d -t hiblup-XXXXXXXX)
cd $TMP_DIR
# Install Miniconda3
if [[ ! $(command -v conda) ]]; then
if [[ -z "${INSTALL_PATH}" ]]; then
INSTALL_PATH=~/miniconda3
fi
echo "Warning: conda is not installed." >&2
echo "Installing miniconda3 into ${INSTALL_PATH}..."
curl ${CURL_OPT} -O ${CONDA_MIRROR}/${CONDA_INSTALLER}
bash ${CONDA_INSTALLER} -f -u -b -p ${INSTALL_PATH}
export PATH="${INSTALL_PATH}/bin:$PATH"
conda init bash
conda config --set auto_activate_base false
if [[ ${MIRROR} == "tuna" ]]; then
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
conda config --set show_channel_urls yes
fi
fi
# hiblup command
HIBLUP_COMMAND="
hiblup () {
source ${PROFILE}
if [[ ! \$(command -v conda) ]]; then
exit 1
fi
conda activate hiblup
if [[ "\$\#" -lt 1 ]]; then
R
else
Rscript \$@
fi
conda deactivate
}"
if [[ ! -z $(grep -Fxq "hiblup () {" ${PROFILE}) ]]; then
# code if found
echo "Warning: old hiblup function found."
else
# code if not found
echo "${HIBLUP_COMMAND}" >> ${PROFILE}
fi
conda init bash
source ${PROFILE}
# check conda
if [[ ! $(command -v conda) ]]; then
echo "Error: command 'conda' not found"
exit 1
fi
# Create or update conda env
conda create -n hiblup ${R_ENV} r-essentials r-rcpp r-rcpparmadillo -y
conda activate hiblup
# Install hiblup
echo ""
echo "Downloading HIBLUP from $REPO_ROOT/${HIBLUP_PACKAGE} ..."
curl ${CURL_OPT} -O $REPO_ROOT/${HIBLUP_PACKAGE}
echo ""
echo "Installing HIBLUP ..."
Rscript -e "install.packages('bigmemory', repos='${CRAN_MIRROR}', lib='${CONDA_PREFIX}/lib/R/library')"
Rscript -e "install.packages('${HIBLUP_PACKAGE}', repos=NULL, lib='${CONDA_PREFIX}/lib/R/library')"
# R startup script
STARTUP=""
if [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
STARTUP="
suppressMessages(library(RevoUtils))
suppressMessages(library(RevoUtilsMath))
"
fi
echo ".First <- function(){
${STARTUP}
suppressMessages(library(hiblup))
if('hiblup' %in% (.packages())) {
# cat('hiblup has been loaded.')
# cat('\\nWelcome at', date(), '\\n')
} else {
cat('Warning: library(hiblup) failed.\\n')
}
}" > ${CONDA_PREFIX}/lib/R/etc/Rprofile.site
echo ""
echo "hiblup shortcut command has been installed to ${PROFILE}"
echo "Load it with the following command:"
echo " source ${PROFILE}"
echo ""
echo "Usage:"
echo "$ hiblup"
echo "$ hiblup my_script.R"
echo ""
conda deactivate
cd ${DIR}
rm -rf ${TMP_DIR}