]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/biopieces_installer-0.48.sh
added bzip2-ruby gem requirement
[biopieces.git] / bp_scripts / biopieces_installer-0.48.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_aux_program "blastall"
213     test_aux_program "blat"
214     test_aux_program "bwa"
215     test_aux_program "bowtie"
216     test_aux_program "formatdb"
217     test_aux_program "gnuplot"
218     test_aux_program "idba"
219     test_aux_program "muscle"
220     test_aux_program "mummer"
221     test_aux_program "mysql"
222     test_aux_program "prodigal"
223     test_aux_program "Ray"
224     test_aux_program "scan_for_matches"
225     test_aux_program "usearch"
226     test_aux_program "velveth"
227     test_aux_program "velvetg"
228     test_aux_program "vmatch"
229
230     echo ""
231     echo "   Any WARNINGs indicate that the executable for that auxillary"
232     echo "   program could not be found. While not critical, this will"
233     echo "   cause some Biopieces to FAIL."
234
235     while true; do
236         read -p "Continue (yes/no)? " answer
237         case $answer in
238             [Yy]* ) break;;
239             [Nn]* ) exit_abort;;
240         esac
241     done
242 }
243
244 # Function to prompt the selection of the code directory.
245 function prompt_install_dir_code
246 {
247     read -p "Enter directory for the Biopieces code (default: $bp_code): " answer;
248
249     bp_code=${answer:-"$bp_code"}
250
251     if [ ! -d "$bp_code" ]; then
252         while true; do
253             read -p "Create directory: $bp_code (yes/no)? " answer
254             case $answer in
255                 [Yy]* ) dir_create $bp_code && break;;
256                 [Nn]* ) exit_abort;;
257             esac
258         done
259     fi
260
261     dir_writable $bp_code
262 }
263
264 # Function to prompt the selection of the data directory.
265 function prompt_install_dir_data
266 {
267     read -p "Enter directory for the Biopieces data (default: $bp_data): " answer;
268
269     bp_data=${answer:-"$bp_data"}
270
271     if [ ! -d "$bp_data" ]; then
272         while true; do
273             read -p "Create directory: $bp_data (yes/no)? " answer
274             case $answer in
275                 [Yy]* ) dir_create $bp_data && break;;
276                 [Nn]* ) exit_abort;;
277             esac
278         done
279     fi
280
281     dir_writable $bp_data
282 }
283
284 # Function to prompt the selection of the log directory.
285 function prompt_install_dir_log
286 {
287     read -p "Enter directory for the Biopieces log file (default: $bp_log): " answer;
288
289     bp_log=${answer:-"$bp_log"}
290
291     if [ ! -d "$bp_log" ]; then
292         while true; do
293             read -p "Create directory: $bp_log (yes/no)? " answer
294             case $answer in
295                 [Yy]* ) dir_create $bp_log && break;;
296                 [Nn]* ) exit_abort;;
297             esac
298         done
299     fi
300
301     dir_writable $bp_log
302 }
303
304 # Function to prompt the selection of the tmp directory.
305 function prompt_install_dir_tmp
306 {
307     read -p "Enter directory for the Biopieces temporary files (default: $bp_tmp): " answer;
308
309     bp_tmp=${answer:-"$bp_tmp"}
310
311     if [ ! -d "$bp_tmp" ]; then
312         while true; do
313             read -p "Create directory: $bp_tmp (yes/no)? " answer
314             case $answer in
315                 [Yy]* ) dir_create $bp_tmp && break;;
316                 [Nn]* ) exit_abort;;
317             esac
318         done
319     fi
320
321     dir_writable $bp_tmp
322 }
323
324 # Function to prompt the appending of a section to bashrc.
325 function prompt_append_bashrc
326 {
327     local skip=0
328     local section="
329
330 # >>>>>>>>>>>>>>>>>>>>>>> Enabling Biopieces <<<<<<<<<<<<<<<<<<<<<<<
331
332 export BP_DIR=\"$bp_code\"
333 export BP_DATA=\"$bp_data\"
334 export BP_TMP=\"$bp_tmp\"
335 export BP_LOG=\"$bp_log\"
336
337 source \"\$BP_DIR/bp_conf/bashrc\"
338
339 # >>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<
340
341 "
342
343     echo ""
344     echo "We need to append the below section to your .bashrc file."
345
346     echo_yellow "$section"
347
348     while true; do
349         read -p "Append (yes/no/abort)? " answer
350         case $answer in
351             [Yy]* ) skip=0 && break;;
352             [Nn]* ) skip=1 && break;;
353             [Aa]* ) exit_abort;;
354         esac
355     done
356
357     if [ $skip == 1 ]; then
358         echo "Skipping"
359     else
360         if [ -f "$HOME/.bashrc" ]; then
361             echo "Existing .bashrc file located: $HOME/.bashrc"
362             echo -n "Creating backup: "
363
364             if ! cp "$HOME/.bashrc" "$HOME/.bashrc_biopieces"; then
365                 echo_red "FAIL"
366                 abort
367             else
368                 echo_green "OK"
369                 echo "   Backup is $HOME/.bashrc_biopieces"
370             fi
371         fi
372
373         echo -n "Appending $HOME/.bashrc: "
374
375         if ! echo "$section" >> "$HOME/.bashrc"; then
376             echo_red "FAIL"
377             abort
378         else
379             echo_green "OK"
380         fi
381
382         echo -n "Testing $HOME/.bashrc: "
383
384         if ! source "$HOME/.bashrc"; then
385             echo_red "FAIL"
386             abort
387         else
388             echo_green "OK"
389         fi
390
391         export BP_DIR="$bp_code"
392         export BP_DATA="$bp_data"
393         export BP_TMP="$bp_tmp"
394         export BP_LOG="$bp_log"
395
396         echo ""
397         echo "   \$BP_DIR is now set to: $BP_DIR"
398         echo "   \$BP_DATA is now set to: $BP_DATA"
399         echo "   \$BP_TMP is now set to: $BP_TMP"
400         echo "   \$BP_LOG is now set to: $BP_LOG"
401         echo ""
402
403         while true; do
404             read -p "Continue (yes/no)? " answer
405             case $answer in
406                 [Yy]* ) break;;
407                 [Nn]* ) exit_abort;;
408             esac
409         done
410     fi
411 }
412
413 # Function to test if we are running bash.
414 function test_bash
415 {
416     echo -n "   Testing if the running shell is bash: "
417
418     if [ `echo $SHELL | grep "bash"` ]; then
419         echo_green "OK"
420     else
421         echo_red "FAIL"
422         echo "      Biopieces requires bash shell not - $SHELL."
423         exit_abort
424     fi
425 }
426
427 # Function to test if subversion client is in $PATH.
428 function test_svn
429 {
430     local program="svn"
431
432     echo -n "   Testing subversion client - \"$program\": "
433
434     if command -v $program >/dev/null; then
435         echo_green "OK"
436     else
437         echo_red "FAIL"
438         echo "      Subversion client $program was not found."
439         exit_abort
440     fi
441 }
442
443 # Function to test if the required version of Perl is installed.
444 function test_perl
445 {
446     echo -n "   Testing Perl version: "
447
448     if error=$( perl -e 'use 5.8.0;' 2>&1 ); then
449         echo_green "OK"
450     else
451         echo_red "FAIL"
452         echo "   $error" | sed "s/, stopped.*//"
453         exit_abort
454     fi  
455 }
456
457 # Function to test if a given Perl module is installed.
458 function test_perl_module
459 {
460     local module=$1
461
462     echo -n "   Testing required Perl module - \"$module\": "
463
464     if ! error=$( perl -M$module -e '' 2>&1 > /dev/null ); then
465         echo_red "FAIL"
466         echo "   Try: perl -MCPAN -e 'install $module'"
467         exit_abort
468     else
469         echo_green "OK"
470     fi
471 }
472
473 # Function to test if the required version of Ruby is installed.
474 function test_ruby
475 {
476     echo -n "   Testing Ruby version: "
477
478     if ! [ `which ruby` ]; then
479         echo_red "FAIL"
480         echo "    Ruby version 1.9 required"
481         exit_abort
482     fi
483
484     if error=$( ruby -e 'raise "Ruby version 1.9 required--this is only #{RUBY_VERSION}" if RUBY_VERSION < "1.9"' 2>&1 ); then
485         echo_green "OK"
486     else
487         echo_red "FAIL"
488         echo $error
489         exit_abort
490     fi  
491 }
492
493 # Function to test if a given Ruby gem is installed.
494 function test_ruby_gem
495 {
496     local gem=$1
497
498     echo -n "   Testing required Ruby gem - \"$gem\": "
499
500     if error=$( gem list --local | grep $gem ); then
501         echo_green "OK"
502     elif [[ $gem == 'RubyInline' ]] && error=$( ruby -rinline -e '' ); then
503         echo_yellow "OK"
504     else
505         echo_red "FAIL"
506         echo "      Try: gem install $gem"
507         exit_abort
508     fi
509 }
510
511 # Function to test is a given auxillary program is in $PATH.
512 function test_aux_program
513 {
514     local program=$1
515
516     echo -n "   Testing auxiliary program - \"$program\": "
517
518     if command -v $program >/dev/null; then
519         echo_green "OK"
520     else
521         echo_yellow "WARNING"
522     fi
523 }
524
525 # Function to checkout the Biopieces code from subversion.
526 function checkout_code
527 {
528     echo -n "Downloading Biopieces code from repository (please wait): "
529
530     if error=$( svn checkout http://biopieces.googlecode.com/svn/trunk/ $bp_code ); then
531         echo_green "OK"
532     else
533         echo_red "FAIL"
534         echo "   $error"
535         exit_abort
536     fi  
537 }
538
539 # Function to checkout the Biopieces wiki from subversion.
540 function checkout_wiki
541 {
542     echo -n "Downloading Biopieces wiki from repository (please wait): "
543
544     if error=$( svn checkout http://biopieces.googlecode.com/svn/wiki/ "$bp_code/bp_usage" ); then
545         echo_green "OK"
546     else
547         echo_red "FAIL"
548         echo "   $error"
549         exit_abort
550     fi  
551 }
552
553 prompt_install
554 prompt_install_existing
555 prompt_test_prerequisites
556 prompt_install_dir_code
557 prompt_install_dir_data
558 prompt_install_dir_log
559 prompt_install_dir_tmp
560
561 checkout_code
562 checkout_wiki
563
564 prompt_append_bashrc
565
566 exit_success
567