]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/biopieces_installer.sh
cbe5f9f6a877daeb2bb0fe2b59ed405afca896cb
[biopieces.git] / bp_scripts / biopieces_installer.sh
1 #!/bin/bash
2
3 # Install script for Biopieces.
4 # October 2011, Martin A. Hansen
5
6 bp_code="$HOME/biopieces"
7 bp_data="$HOME/BP_DATA"
8  bp_log="/var/log"
9  bp_tmp="/tmp"
10
11 # Function that renders a given message in ASCII green.
12 function echo_green
13 {
14     local msg=$1
15
16     echo -e "\033[32;38m$msg\033[0m"
17 }
18
19 # Function that renders a given message in ASCII yellow.
20 function echo_yellow
21 {
22     local msg=$1
23
24     echo -e "\033[33;38m$msg\033[0m"
25 }
26
27 # Function that renders a given message in ASCII red.
28 function echo_red
29 {
30     local msg=$1
31
32     echo -e "\033[31;38m$msg\033[0m"
33 }
34
35 # Function to output an abort message and exit
36 function exit_abort
37 {
38     echo_red "abort"
39
40     exit
41 }
42
43 # Function to output an success message and exit
44 function exit_success
45 {
46     echo ""
47     echo_green "Congratulations - you have now installed Biopieces."
48     echo ""
49     echo "   To list all available Biopieces try 'list_biopieces'"
50     echo ""
51     echo "   To see the synopsis of a Biopiece try 'read_fastq'"
52     echo ""
53     echo "   To see the full description and exampels try 'read_fastq -?'"
54     echo ""
55     echo "   Don't forget to join the Biopieces Google Group for important"
56     echo "   messages, questions, discussion, and suggestions:"
57     echo ""
58     echo "      http://groups.google.com/group/biopieces"
59     echo ""
60     echo "   And of cause there is the introduction:"
61     echo ""
62     echo "      http://code.google.com/p/biopieces/wiki/Introduction"
63     echo ""
64     echo "   Happy hacking!"
65     echo ""
66
67     exit
68 }
69
70 # Function to create a directory if it doesn't exist.
71 function dir_create
72 {
73     local dir=$1
74
75     echo -n "Creating $dir: "
76
77     if error=$( mkdir $dir 2>&1 ); then
78         echo_green "OK"
79     else
80         echo_red "FAIL"
81         echo $error
82         exit_abort
83     fi
84 }
85
86 # Function to prompt for continuation of installation.
87 function prompt_install
88 {
89     echo ""
90     echo "Welcome to the Biopieces installer."
91     echo ""
92     echo "   This installer is experimental, and is being evaluated to replace"
93     echo "   the previous, tedious way:"
94     echo ""
95     echo "      http://code.google.com/p/biopieces/wiki/Installation"
96     echo ""
97     echo "   If you experience problems, please report to the Google Group (see below)"
98     echo ""
99     echo "   The installer will now do the following:"
100     echo ""
101     echo "   -  Check for existing Biopieces installation."
102     echo "   -  Check prerequisites:"
103     echo "      *  Subversion client"
104     echo "      *  Perl"
105     echo "      *  Perl modules"
106     echo "      *  Ruby"
107     echo "      *  Ruby gems"
108     echo "      *  Auxillary programs"
109     echo ""
110     echo "   -  Create installation directories."
111     echo "   -  Download code from repository."
112     echo "   -  Set environment in .bashrc."
113     echo "   -  Run tests"
114     echo ""
115     echo "   Problems? Check out the FAQ:"
116     echo ""
117     echo "      http://code.google.com/p/biopieces/wiki/FAQ"
118     echo ""
119     echo "   Help is available at the Biopieces Google Group:"
120     echo ""
121     echo "      http://groups.google.com/group/biopieces"
122     echo ""
123
124     while true; do
125         read -p "Continue (yes/no)? " answer
126         case $answer in
127             [Yy]* ) break;;
128             [Nn]* ) exit_abort;;
129         esac
130     done
131 }
132
133 # Function to prompt the checking of any existing Biopieces installation.
134 function prompt_install_existing
135 {
136     echo "Checking for existing Biopieces installation:"
137
138     if [ $BP_DIR ]; then
139         echo_yellow "   WARNING: \$BP_DIR is already set to: $BP_DIR"
140         found=1
141     fi
142
143     if [ $BP_DATA ]; then
144         echo_yellow "   WARNING: \$BP_DATA is already set to: $BP_DATA"
145         found=1
146     fi
147
148     if [ $BP_TMP ]; then
149         echo_yellow "   WARNING: \$BP_TMP is already set to: $BP_TMP"
150         found=1
151     fi
152
153     if [ $BP_LOG ]; then
154         echo_yellow "   WARNING: \$BP_LOG is already set to: $BP_LOG"
155         found=1
156     fi
157
158     if [ $found ]; then
159         echo ""
160         echo "   It appears that an old installation of Biopeices exists."
161     fi
162
163     while true; do
164         read -p "Continue (yes/no)? " answer
165         case $answer in
166             [Yy]* ) break;;
167             [Nn]* ) exit_abort;;
168         esac
169     done
170 }
171
172 # Function to prompt the testing of prerequisites.
173 function prompt_test_prerequisites
174 {
175     echo "Testing prerequisites:"
176
177     test_svn
178     test_perl
179     test_perl_module "Inline"
180     test_perl_module "JSON::XS"
181     test_perl_module "SVG"
182     test_perl_module "Bit::Vector"
183     test_perl_module "Time::HiRes"
184     test_ruby
185     test_ruby_gem "gnuplot"
186     test_ruby_gem "narray"
187     test_aux_program "blastall"
188     test_aux_program "blat"
189     test_aux_program "bwa"
190     test_aux_program "bowtie"
191     test_aux_program "formatdb"
192     test_aux_program "gnuplot"
193     test_aux_program "idba"
194     test_aux_program "muscle"
195     test_aux_program "mummer"
196     test_aux_program "mysql"
197     test_aux_program "prodigal"
198     test_aux_program "Ray"
199     test_aux_program "scan_for_matches"
200     test_aux_program "uclust"
201     test_aux_program "velveth"
202     test_aux_program "velvetg"
203     test_aux_program "vmatch"
204
205     echo ""
206     echo "   Any WARNINGs indicate that the executable for that auxillary"
207     echo "   program could not be found. While not critical, this will"
208     echo "   cause some Biopieces to fail."
209
210     while true; do
211         read -p "Continue (yes/no)? " answer
212         case $answer in
213             [Yy]* ) break;;
214             [Nn]* ) exit_abort;;
215         esac
216     done
217 }
218
219 # Function to prompt the selection of the code directory.
220 function prompt_install_dir_code
221 {
222     read -p "Enter directory to install Biopieces code (default: $bp_code): " answer;
223
224     bp_code=${answer:-"$bp_code"}
225
226     if [ ! -d "$bp_code" ]; then
227         while true; do
228             read -p "Create directory: $bp_code (yes/no)? " answer
229             case $answer in
230                 [Yy]* ) dir_create $bp_code && break;;
231                 [Nn]* ) exit_abort;;
232             esac
233         done
234     fi
235 }
236
237 # Function to prompt the selection of the data directory.
238 function prompt_install_dir_data
239 {
240     read -p "Enter directory to install Biopieces data (default: $bp_data): " answer;
241
242     bp_data=${answer:-"$bp_data"}
243
244     if [ ! -d "$bp_data" ]; then
245         while true; do
246             read -p "Create directory: $bp_data (yes/no)? " answer
247             case $answer in
248                 [Yy]* ) dir_create $bp_data && break;;
249                 [Nn]* ) exit_abort;;
250             esac
251         done
252     fi
253 }
254
255 # Function to prompt the selection of the log directory.
256 function prompt_install_dir_log
257 {
258     read -p "Enter directory to install Biopieces log (default: $bp_log): " answer;
259
260     bp_log=${answer:-"$bp_log"}
261
262     if [ ! -d "$bp_log" ]; then
263         while true; do
264             read -p "Create directory: $bp_log (yes/no)? " answer
265             case $answer in
266                 [Yy]* ) dir_create $bp_log && break;;
267                 [Nn]* ) exit_abort;;
268             esac
269         done
270     fi
271 }
272
273 # Function to prompt the selection of the tmp directory.
274 function prompt_install_dir_tmp
275 {
276     read -p "Enter directory to install Biopieces tmp (default: $bp_tmp): " answer;
277
278     bp_tmp=${answer:-"$bp_tmp"}
279
280     if [ ! -d "$bp_tmp" ]; then
281         while true; do
282             read -p "Create directory: $bp_tmp (yes/no)? " answer
283             case $answer in
284                 [Yy]* ) dir_create $bp_tmp && break;;
285                 [Nn]* ) exit_abort;;
286             esac
287         done
288     fi
289 }
290
291 # Function to prompt the appending of a section to bashrc.
292 function prompt_append_bashrc
293 {
294     local skip=0
295     local section="
296
297 # >>>>>>>>>>>>>>>>>>>>>>> Enabling Biopieces <<<<<<<<<<<<<<<<<<<<<<<
298
299 export BP_DIR=\"$bp_code\"
300 export BP_DATA=\"$bp_data\"
301 export BP_TMP=\"$bp_tmp\"
302 export BP_LOG=\"$bp_log\"
303
304 source \"\$BP_DIR/bp_conf/bashrc\"
305
306 # >>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<
307
308 "
309
310     echo ""
311     echo "We need to append the below section to your .bashrc file."
312
313     echo_yellow "$section"
314
315     while true; do
316         read -p "Append (yes/no/abort)? " answer
317         case $answer in
318             [Yy]* ) skip=0 && break;;
319             [Nn]* ) skip=1 && break;;
320             [Aa]* ) exit_abort;;
321         esac
322     done
323
324     if [ $skip == 1 ]; then
325         echo "Skipping"
326     else
327         echo -n "Appending $HOME/.bashrc: "
328
329         cp "$HOME/.bashrc" "$HOME/.bashrc_biopieces"
330
331         echo "$section" >> "$HOME/.bashrc"
332
333         source "$HOME/.bashrc"
334
335         echo_green "OK"
336         echo ""
337         echo "   Backup is $HOME/.bashrc_biopieces"
338     fi
339 }
340
341 # Function to test if subversion client is in $PATH.
342 function test_svn
343 {
344     local program="svn"
345
346     echo -n "   Testing subversion client - \"$program\": "
347
348     if command -v $program >/dev/null; then
349         echo_green "OK"
350     else
351         echo_red "FAIL"
352         echo "      Subversion client $program was not found."
353         exit_abort
354     fi
355 }
356
357 # Function to test if the required version of Perl is installed.
358 function test_perl
359 {
360     echo -n "   Testing Perl version: "
361
362     if error=$( perl -e 'use 5.8.0;' 2>&1 ); then
363         echo_green "OK"
364     else
365         echo_red "FAIL"
366         echo $error | sed "s/, stopped.*//"
367         exit_abort
368     fi  
369 }
370
371 # Function to test if a given Perl module is installed.
372 function test_perl_module
373 {
374     local module=$1
375
376     echo -n "   Testing required Perl module - \"$module\": "
377
378     if ! error=$( perl -M$module -e '' 2>&1 > /dev/null ); then
379         echo_red "FAIL"
380         echo "   Try: perl -MCPAN -e 'install $module'"
381         exit_abort
382     else
383         echo_green "OK"
384     fi
385 }
386
387 # Function to test if the required version of Ruby is installed.
388 function test_ruby
389 {
390     echo -n "   Testing Ruby version: "
391
392     if error=$( ruby -e 'raise "Ruby version 1.9 required--this is only #{RUBY_VERSION}" if RUBY_VERSION < "1.9"' 2>&1 ); then
393         echo_green "OK"
394     else
395         echo_red "FAIL"
396         echo $error | sed "s/.*: //"
397         exit_abort
398     fi  
399 }
400
401 # Function to test if a given Ruby gem is installed.
402 function test_ruby_gem
403 {
404     local gem=$1
405
406     echo -n "   Testing required Ruby gem - \"$gem\": "
407
408     if error=$( gem list --local | grep $gem ); then
409         echo_green "OK"
410     else
411         echo_red "FAIL"
412         echo "      Try: gem install $gem"
413         exit_abort
414     fi
415 }
416
417 # Function to test is a given auxillary program is in $PATH.
418 function test_aux_program
419 {
420     local program=$1
421
422     echo -n "   Testing auxiliary program - \"$program\": "
423
424     if command -v $program >/dev/null; then
425         echo_green "OK"
426     else
427         echo_yellow "WARNING"
428     fi
429 }
430
431 # Function to checkout the Biopieces code from subversion.
432 function checkout_code
433 {
434     echo -n "Downloading Biopieces code from repository: "
435
436     if error=$( svn checkout http://biopieces.googlecode.com/svn/trunk/ $bp_code ); then
437         echo_green "OK"
438     else
439         echo_red "FAIL"
440         echo $error
441         exit_abort
442     fi  
443 }
444
445 # Function to checkout the Biopieces wiki from subversion.
446 function checkout_wiki
447 {
448     echo -n "Downloading Biopieces wiki from repository: "
449
450     if error=$( svn checkout http://biopieces.googlecode.com/svn/wiki/ "$bp_code/bp_usage" ); then
451         echo_green "OK"
452     else
453         echo_red "FAIL"
454         echo $error
455         exit_abort
456     fi  
457 }
458
459 # Function to run the Biopieces tests.
460 function prompt_test_biopieces
461 {
462     echo "Running the Biopieces test suite:"
463     echo ""
464
465     "$BP_DIR/bp_test/test_all"
466
467     echo ""
468     echo "   Any FAIL indicates broken Biopieces - seek help!"
469
470     while true; do
471         read -p "Continue (yes/no)? " answer
472         case $answer in
473             [Yy]* ) break;;
474             [Nn]* ) exit_abort;;
475         esac
476     done
477 }
478
479 prompt_install
480 prompt_install_existing
481 prompt_test_prerequisites
482 prompt_install_dir_code
483 prompt_install_dir_data
484 prompt_install_dir_log
485 prompt_install_dir_tmp
486
487 checkout_code
488 checkout_wiki
489
490 prompt_append_bashrc
491 prompt_test_biopieces
492
493 exit_success