]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/biopieces_installer-0.49.sh
added bzip2-ruby gem requirement
[biopieces.git] / bp_scripts / biopieces_installer-0.49.sh
1 #!/bin/bash
2
3 # Install script for Biopieces.
4 # Copyright (C) October 2011, Martin A. Hansen
5
6 bp_code="$HOME/biopieces"
7 bp_data="$HOME/BP_DATA"
8  bp_log="$HOME/BP_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 "   Now you must either run 'source ~/.bashrc' or re-login to your system."
50     echo ""
51     echo "      * To test your Biopieces installation try 'bp_test'."
52     echo "      * To list all available Biopieces try 'list_biopieces'."
53     echo "      * To see the synopsis of a Biopiece try 'read_fastq'."
54     echo "      * To see the full description and exampels try 'read_fastq -?'."
55     echo ""
56     echo "   Don't forget to join the Biopieces Google Group for important"
57     echo "   messages, questions, discussion, and suggestions:"
58     echo ""
59     echo "      http://groups.google.com/group/biopieces"
60     echo ""
61     echo "   And of cause there is the introduction:"
62     echo ""
63     echo "      http://code.google.com/p/biopieces/wiki/Introduction"
64     echo ""
65     echo "   Happy hacking!"
66     echo ""
67
68     exit
69 }
70
71 # Function to create a directory if it doesn't exist.
72 function dir_create
73 {
74     local dir=$1
75
76     echo -n "Creating $dir: "
77
78     if error=$( mkdir $dir 2>&1 ); then
79         echo_green "OK"
80     else
81         echo_red "FAIL"
82         echo "   $error"
83         exit_abort
84     fi
85 }
86
87 # Function check if a directory is writable.
88 function dir_writable
89 {
90     local dir=$1
91     local file="$dir/bp_writable"
92
93     echo -n "Writable? $dir: "
94
95     if error=$( touch $file 2>&1 ); then
96         echo_green "OK"
97         rm $file
98     else
99         echo_red "FAIL"
100         echo "   $error"
101         exit_abort
102     fi
103 }
104
105 # Function to prompt for continuation of installation.
106 function prompt_install
107 {
108     echo ""
109     echo "Welcome to the Biopieces installer."
110     echo ""
111     echo "   This installer is experimental, and is being evaluated to replace"
112     echo "   the previous, tedious way:"
113     echo ""
114     echo "      http://code.google.com/p/biopieces/wiki/Installation"
115     echo ""
116     echo "   The installer will now do the following:"
117     echo ""
118     echo "   *  Check for existing Biopieces installation."
119     echo "   *  Check prerequisites:"
120     echo "      -  Subversion client"
121     echo "      -  Perl"
122     echo "      -  Perl modules"
123     echo "      -  Ruby"
124     echo "      -  Ruby gems"
125     echo "      -  Auxillary programs"
126     echo ""
127     echo "   *  Create installation directories."
128     echo "   *  Download code from repository."
129     echo "   *  Set environment in .bashrc."
130     echo ""
131     echo "   Problems? Check out the FAQ:"
132     echo ""
133     echo "      http://code.google.com/p/biopieces/wiki/FAQ"
134     echo ""
135     echo "   Help is available at the Biopieces Google Group:"
136     echo ""
137     echo "      http://groups.google.com/group/biopieces"
138     echo ""
139     echo "   Bugs & issues:"
140     echo ""
141     echo "      http://code.google.com/p/biopieces/issues/list"
142     echo ""
143
144     while true; do
145         read -p "Continue (yes/no)? " answer
146         case $answer in
147             [Yy]* ) break;;
148             [Nn]* ) exit_abort;;
149         esac
150     done
151 }
152
153 # Function to prompt the checking of any existing Biopieces installation.
154 function prompt_install_existing
155 {
156     echo "Checking for existing Biopieces installation:"
157
158     if [ $BP_DIR ]; then
159         echo_yellow "   WARNING: \$BP_DIR is already set to: $BP_DIR"
160         found=1
161     fi
162
163     if [ $BP_DATA ]; then
164         echo_yellow "   WARNING: \$BP_DATA is already set to: $BP_DATA"
165         found=1
166     fi
167
168     if [ $BP_TMP ]; then
169         echo_yellow "   WARNING: \$BP_TMP is already set to: $BP_TMP"
170         found=1
171     fi
172
173     if [ $BP_LOG ]; then
174         echo_yellow "   WARNING: \$BP_LOG is already set to: $BP_LOG"
175         found=1
176     fi
177
178     if [ $found ]; then
179         echo ""
180         echo "   An old installation of Biopeices appears to exists."
181     else
182         echo ""
183         echo "   No installation of Biopeices found."
184     fi
185
186     while true; do
187         read -p "Continue (yes/no)? " answer
188         case $answer in
189             [Yy]* ) break;;
190             [Nn]* ) exit_abort;;
191         esac
192     done
193 }
194
195 # Function to prompt the testing of prerequisites.
196 function prompt_test_prerequisites
197 {
198     echo "Testing prerequisites:"
199
200     test_bash
201     test_svn
202     test_perl
203     test_perl_module "Inline"
204     test_perl_module "JSON::XS"
205     test_perl_module "SVG"
206     test_perl_module "Bit::Vector"
207     test_perl_module "Time::HiRes"
208     test_ruby
209     test_ruby_gem "gnuplot"
210     test_ruby_gem "narray"
211     test_ruby_gem "RubyInline"
212     test_ruby_gem "bzip2-ruby"
213     test_aux_program "blastall"
214     test_aux_program "blat"
215     test_aux_program "bwa"
216     test_aux_program "bowtie"
217     test_aux_program "formatdb"
218     test_aux_program "gnuplot"
219     test_aux_program "idba"
220     test_aux_program "muscle"
221     test_aux_program "mummer"
222     test_aux_program "mysql"
223     test_aux_program "prodigal"
224     test_aux_program "Ray"
225     test_aux_program "scan_for_matches"
226     test_aux_program "usearch"
227     test_aux_program "velveth"
228     test_aux_program "velvetg"
229     test_aux_program "vmatch"
230
231     echo ""
232     echo "   Any WARNINGs indicate that the executable for that auxillary"
233     echo "   program could not be found. While not critical, this will"
234     echo "   cause some Biopieces to FAIL."
235
236     while true; do
237         read -p "Continue (yes/no)? " answer
238         case $answer in
239             [Yy]* ) break;;
240             [Nn]* ) exit_abort;;
241         esac
242     done
243 }
244
245 # Function to prompt the selection of the code directory.
246 function prompt_install_dir_code
247 {
248     read -p "Enter directory for the Biopieces code (default: $bp_code): " answer;
249
250     bp_code=${answer:-"$bp_code"}
251
252     if [ ! -d "$bp_code" ]; then
253         while true; do
254             read -p "Create directory: $bp_code (yes/no)? " answer
255             case $answer in
256                 [Yy]* ) dir_create $bp_code && break;;
257                 [Nn]* ) exit_abort;;
258             esac
259         done
260     fi
261
262     dir_writable $bp_code
263 }
264
265 # Function to prompt the selection of the data directory.
266 function prompt_install_dir_data
267 {
268     read -p "Enter directory for the Biopieces data (default: $bp_data): " answer;
269
270     bp_data=${answer:-"$bp_data"}
271
272     if [ ! -d "$bp_data" ]; then
273         while true; do
274             read -p "Create directory: $bp_data (yes/no)? " answer
275             case $answer in
276                 [Yy]* ) dir_create $bp_data && break;;
277                 [Nn]* ) exit_abort;;
278             esac
279         done
280     fi
281
282     dir_writable $bp_data
283 }
284
285 # Function to prompt the selection of the log directory.
286 function prompt_install_dir_log
287 {
288     read -p "Enter directory for the Biopieces log file (default: $bp_log): " answer;
289
290     bp_log=${answer:-"$bp_log"}
291
292     if [ ! -d "$bp_log" ]; then
293         while true; do
294             read -p "Create directory: $bp_log (yes/no)? " answer
295             case $answer in
296                 [Yy]* ) dir_create $bp_log && break;;
297                 [Nn]* ) exit_abort;;
298             esac
299         done
300     fi
301
302     dir_writable $bp_log
303 }
304
305 # Function to prompt the selection of the tmp directory.
306 function prompt_install_dir_tmp
307 {
308     read -p "Enter directory for the Biopieces temporary files (default: $bp_tmp): " answer;
309
310     bp_tmp=${answer:-"$bp_tmp"}
311
312     if [ ! -d "$bp_tmp" ]; then
313         while true; do
314             read -p "Create directory: $bp_tmp (yes/no)? " answer
315             case $answer in
316                 [Yy]* ) dir_create $bp_tmp && break;;
317                 [Nn]* ) exit_abort;;
318             esac
319         done
320     fi
321
322     dir_writable $bp_tmp
323 }
324
325 # Function to prompt the appending of a section to bashrc.
326 function prompt_append_bashrc
327 {
328     local skip=0
329     local section="
330
331 # >>>>>>>>>>>>>>>>>>>>>>> Enabling Biopieces <<<<<<<<<<<<<<<<<<<<<<<
332
333 export BP_DIR=\"$bp_code\"
334 export BP_DATA=\"$bp_data\"
335 export BP_TMP=\"$bp_tmp\"
336 export BP_LOG=\"$bp_log\"
337
338 source \"\$BP_DIR/bp_conf/bashrc\"
339
340 # >>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<
341
342 "
343
344     echo ""
345     echo "We need to append the below section to your .bashrc file."
346
347     echo_yellow "$section"
348
349     while true; do
350         read -p "Append (yes/no/abort)? " answer
351         case $answer in
352             [Yy]* ) skip=0 && break;;
353             [Nn]* ) skip=1 && break;;
354             [Aa]* ) exit_abort;;
355         esac
356     done
357
358     if [ $skip == 1 ]; then
359         echo "Skipping"
360     else
361         if [ -f "$HOME/.bashrc" ]; then
362             echo "Existing .bashrc file located: $HOME/.bashrc"
363             echo -n "Creating backup: "
364
365             if ! cp "$HOME/.bashrc" "$HOME/.bashrc_biopieces"; then
366                 echo_red "FAIL"
367                 abort
368             else
369                 echo_green "OK"
370                 echo "   Backup is $HOME/.bashrc_biopieces"
371             fi
372         fi
373
374         echo -n "Appending $HOME/.bashrc: "
375
376         if ! echo "$section" >> "$HOME/.bashrc"; then
377             echo_red "FAIL"
378             abort
379         else
380             echo_green "OK"
381         fi
382
383         echo -n "Testing $HOME/.bashrc: "
384
385         if ! source "$HOME/.bashrc"; then
386             echo_red "FAIL"
387             abort
388         else
389             echo_green "OK"
390         fi
391
392         export BP_DIR="$bp_code"
393         export BP_DATA="$bp_data"
394         export BP_TMP="$bp_tmp"
395         export BP_LOG="$bp_log"
396
397         echo ""
398         echo "   \$BP_DIR is now set to: $BP_DIR"
399         echo "   \$BP_DATA is now set to: $BP_DATA"
400         echo "   \$BP_TMP is now set to: $BP_TMP"
401         echo "   \$BP_LOG is now set to: $BP_LOG"
402         echo ""
403
404         while true; do
405             read -p "Continue (yes/no)? " answer
406             case $answer in
407                 [Yy]* ) break;;
408                 [Nn]* ) exit_abort;;
409             esac
410         done
411     fi
412 }
413
414 # Function to test if we are running bash.
415 function test_bash
416 {
417     echo -n "   Testing if the running shell is bash: "
418
419     if [ `echo $SHELL | grep "bash"` ]; then
420         echo_green "OK"
421     else
422         echo_red "FAIL"
423         echo "      Biopieces requires bash shell not - $SHELL."
424         exit_abort
425     fi
426 }
427
428 # Function to test if subversion client is in $PATH.
429 function test_svn
430 {
431     local program="svn"
432
433     echo -n "   Testing subversion client - \"$program\": "
434
435     if command -v $program >/dev/null; then
436         echo_green "OK"
437     else
438         echo_red "FAIL"
439         echo "      Subversion client $program was not found."
440         exit_abort
441     fi
442 }
443
444 # Function to test if the required version of Perl is installed.
445 function test_perl
446 {
447     echo -n "   Testing Perl version: "
448
449     if error=$( perl -e 'use 5.8.0;' 2>&1 ); then
450         echo_green "OK"
451     else
452         echo_red "FAIL"
453         echo "   $error" | sed "s/, stopped.*//"
454         exit_abort
455     fi  
456 }
457
458 # Function to test if a given Perl module is installed.
459 function test_perl_module
460 {
461     local module=$1
462
463     echo -n "   Testing required Perl module - \"$module\": "
464
465     if ! error=$( perl -M$module -e '' 2>&1 > /dev/null ); then
466         echo_red "FAIL"
467         echo "   Try: perl -MCPAN -e 'install $module'"
468         exit_abort
469     else
470         echo_green "OK"
471     fi
472 }
473
474 # Function to test if the required version of Ruby is installed.
475 function test_ruby
476 {
477     echo -n "   Testing Ruby version: "
478
479     if ! [ `which ruby` ]; then
480         echo_red "FAIL"
481         echo "    Ruby version 1.9 required"
482         exit_abort
483     fi
484
485     if error=$( ruby -e 'raise "Ruby version 1.9 required--this is only #{RUBY_VERSION}" if RUBY_VERSION < "1.9"' 2>&1 ); then
486         echo_green "OK"
487     else
488         echo_red "FAIL"
489         echo $error
490         exit_abort
491     fi  
492 }
493
494 # Function to test if a given Ruby gem is installed.
495 function test_ruby_gem
496 {
497     local gem=$1
498
499     echo -n "   Testing required Ruby gem - \"$gem\": "
500
501     if error=$( gem list --local | grep $gem ); then
502         echo_green "OK"
503     elif [[ $gem == 'RubyInline' ]] && error=$( ruby -rinline -e '' ); then
504         echo_yellow "OK"
505     else
506         echo_red "FAIL"
507         echo "      Try: gem install $gem"
508         exit_abort
509     fi
510 }
511
512 # Function to test is a given auxillary program is in $PATH.
513 function test_aux_program
514 {
515     local program=$1
516
517     echo -n "   Testing auxiliary program - \"$program\": "
518
519     if command -v $program >/dev/null; then
520         echo_green "OK"
521     else
522         echo_yellow "WARNING"
523     fi
524 }
525
526 # Function to checkout the Biopieces code from subversion.
527 function checkout_code
528 {
529     echo -n "Downloading Biopieces code from repository (please wait): "
530
531     if error=$( svn checkout http://biopieces.googlecode.com/svn/trunk/ $bp_code ); then
532         echo_green "OK"
533     else
534         echo_red "FAIL"
535         echo "   $error"
536         exit_abort
537     fi  
538 }
539
540 # Function to checkout the Biopieces wiki from subversion.
541 function checkout_wiki
542 {
543     echo -n "Downloading Biopieces wiki from repository (please wait): "
544
545     if error=$( svn checkout http://biopieces.googlecode.com/svn/wiki/ "$bp_code/bp_usage" ); then
546         echo_green "OK"
547     else
548         echo_red "FAIL"
549         echo "   $error"
550         exit_abort
551     fi  
552 }
553
554 prompt_install
555 prompt_install_existing
556 prompt_test_prerequisites
557 prompt_install_dir_code
558 prompt_install_dir_data
559 prompt_install_dir_log
560 prompt_install_dir_tmp
561
562 checkout_code
563 checkout_wiki
564
565 prompt_append_bashrc
566
567 exit_success
568