]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/biopieces_installer-0.41.sh
545380bfb2559d05c7d20e470f78042d95c89414
[biopieces.git] / bp_scripts / biopieces_installer-0.41.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     echo "   Bugs & issues:"
124     echo ""
125     echo "      http://code.google.com/p/biopieces/issues/list"
126     echo ""
127
128     while true; do
129         read -p "Continue (yes/no)? " answer
130         case $answer in
131             [Yy]* ) break;;
132             [Nn]* ) exit_abort;;
133         esac
134     done
135 }
136
137 # Function to prompt the checking of any existing Biopieces installation.
138 function prompt_install_existing
139 {
140     echo "Checking for existing Biopieces installation:"
141
142     if [ $BP_DIR ]; then
143         echo_yellow "   WARNING: \$BP_DIR is already set to: $BP_DIR"
144         found=1
145     fi
146
147     if [ $BP_DATA ]; then
148         echo_yellow "   WARNING: \$BP_DATA is already set to: $BP_DATA"
149         found=1
150     fi
151
152     if [ $BP_TMP ]; then
153         echo_yellow "   WARNING: \$BP_TMP is already set to: $BP_TMP"
154         found=1
155     fi
156
157     if [ $BP_LOG ]; then
158         echo_yellow "   WARNING: \$BP_LOG is already set to: $BP_LOG"
159         found=1
160     fi
161
162     if [ $found ]; then
163         echo ""
164         echo "   It appears that an old installation of Biopeices exists."
165     fi
166
167     while true; do
168         read -p "Continue (yes/no)? " answer
169         case $answer in
170             [Yy]* ) break;;
171             [Nn]* ) exit_abort;;
172         esac
173     done
174 }
175
176 # Function to prompt the testing of prerequisites.
177 function prompt_test_prerequisites
178 {
179     echo "Testing prerequisites:"
180
181     test_bash
182     test_svn
183     test_perl
184     test_perl_module "Inline"
185     test_perl_module "JSON::XS"
186     test_perl_module "SVG"
187     test_perl_module "Bit::Vector"
188     test_perl_module "Time::HiRes"
189     test_ruby
190     test_ruby_gem "gnuplot"
191     test_ruby_gem "narray"
192     test_aux_program "blastall"
193     test_aux_program "blat"
194     test_aux_program "bwa"
195     test_aux_program "bowtie"
196     test_aux_program "formatdb"
197     test_aux_program "gnuplot"
198     test_aux_program "idba"
199     test_aux_program "muscle"
200     test_aux_program "mummer"
201     test_aux_program "mysql"
202     test_aux_program "prodigal"
203     test_aux_program "Ray"
204     test_aux_program "scan_for_matches"
205     test_aux_program "uclust"
206     test_aux_program "velveth"
207     test_aux_program "velvetg"
208     test_aux_program "vmatch"
209
210     echo ""
211     echo "   Any WARNINGs indicate that the executable for that auxillary"
212     echo "   program could not be found. While not critical, this will"
213     echo "   cause some Biopieces to FAIL."
214
215     while true; do
216         read -p "Continue (yes/no)? " answer
217         case $answer in
218             [Yy]* ) break;;
219             [Nn]* ) exit_abort;;
220         esac
221     done
222 }
223
224 # Function to prompt the selection of the code directory.
225 function prompt_install_dir_code
226 {
227     read -p "Enter directory for the Biopieces code (default: $bp_code): " answer;
228
229     bp_code=${answer:-"$bp_code"}
230
231     if [ ! -d "$bp_code" ]; then
232         while true; do
233             read -p "Create directory: $bp_code (yes/no)? " answer
234             case $answer in
235                 [Yy]* ) dir_create $bp_code && break;;
236                 [Nn]* ) exit_abort;;
237             esac
238         done
239     fi
240 }
241
242 # Function to prompt the selection of the data directory.
243 function prompt_install_dir_data
244 {
245     read -p "Enter directory for the Biopieces data (default: $bp_data): " answer;
246
247     bp_data=${answer:-"$bp_data"}
248
249     if [ ! -d "$bp_data" ]; then
250         while true; do
251             read -p "Create directory: $bp_data (yes/no)? " answer
252             case $answer in
253                 [Yy]* ) dir_create $bp_data && break;;
254                 [Nn]* ) exit_abort;;
255             esac
256         done
257     fi
258 }
259
260 # Function to prompt the selection of the log directory.
261 function prompt_install_dir_log
262 {
263     read -p "Enter directory for the Biopieces log file (default: $bp_log): " answer;
264
265     bp_log=${answer:-"$bp_log"}
266
267     if [ ! -d "$bp_log" ]; then
268         while true; do
269             read -p "Create directory: $bp_log (yes/no)? " answer
270             case $answer in
271                 [Yy]* ) dir_create $bp_log && break;;
272                 [Nn]* ) exit_abort;;
273             esac
274         done
275     fi
276 }
277
278 # Function to prompt the selection of the tmp directory.
279 function prompt_install_dir_tmp
280 {
281     read -p "Enter directory for the Biopieces temporary files (default: $bp_tmp): " answer;
282
283     bp_tmp=${answer:-"$bp_tmp"}
284
285     if [ ! -d "$bp_tmp" ]; then
286         while true; do
287             read -p "Create directory: $bp_tmp (yes/no)? " answer
288             case $answer in
289                 [Yy]* ) dir_create $bp_tmp && break;;
290                 [Nn]* ) exit_abort;;
291             esac
292         done
293     fi
294 }
295
296 # Function to prompt the appending of a section to bashrc.
297 function prompt_append_bashrc
298 {
299     local skip=0
300     local section="
301
302 # >>>>>>>>>>>>>>>>>>>>>>> Enabling Biopieces <<<<<<<<<<<<<<<<<<<<<<<
303
304 export BP_DIR=\"$bp_code\"
305 export BP_DATA=\"$bp_data\"
306 export BP_TMP=\"$bp_tmp\"
307 export BP_LOG=\"$bp_log\"
308
309 source \"\$BP_DIR/bp_conf/bashrc\"
310
311 # >>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<
312
313 "
314
315     echo ""
316     echo "We need to append the below section to your .bashrc file."
317
318     echo_yellow "$section"
319
320     while true; do
321         read -p "Append (yes/no/abort)? " answer
322         case $answer in
323             [Yy]* ) skip=0 && break;;
324             [Nn]* ) skip=1 && break;;
325             [Aa]* ) exit_abort;;
326         esac
327     done
328
329     if [ $skip == 1 ]; then
330         echo "Skipping"
331     else
332         if [ -f "$HOME/.bashrc" ]; then
333             echo "Existing .bashrc file located: $HOME/.bashrc"
334             echo -n "Creating backup: "
335
336             if ! cp "$HOME/.bashrc" "$HOME/.bashrc_biopieces"; then
337                 echo_red "FAIL"
338                 abort
339             else
340                 echo_green "OK"
341                 echo "   Backup is $HOME/.bashrc_biopieces"
342             fi
343         fi
344
345         echo -n "Appending $HOME/.bashrc: "
346
347         if ! echo "$section" >> "$HOME/.bashrc"; then
348             echo_red "FAIL"
349             abort
350         else
351             echo_green "OK"
352         fi
353
354         echo -n "Sourcing $HOME/.bashrc: "
355
356         if ! source "$HOME/.bashrc"; then
357             echo_red "FAIL"
358             abort
359         else
360             echo_green "OK"
361         fi
362
363         echo ""
364         echo "   \$BP_DIR is now set to: $BP_DIR"
365         echo "   \$BP_DATA is now set to: $BP_DATA"
366         echo "   \$BP_TMP is now set to: $BP_TMP"
367         echo "   \$BP_LOG is now set to: $BP_LOG"
368         echo ""
369     fi
370 }
371
372 # Function to test if we are running bash.
373 function test_bash
374 {
375     echo -n "   Testing if the running shell is bash: "
376
377     if [ `echo $SHELL | grep "bash"` ]; then
378         echo_green "OK"
379     else
380         echo_red "FAIL"
381         echo "      Biopieces requires bash shell not - $SHELL."
382         exit_abort
383     fi
384 }
385
386 # Function to test if subversion client is in $PATH.
387 function test_svn
388 {
389     local program="svn"
390
391     echo -n "   Testing subversion client - \"$program\": "
392
393     if command -v $program >/dev/null; then
394         echo_green "OK"
395     else
396         echo_red "FAIL"
397         echo "      Subversion client $program was not found."
398         exit_abort
399     fi
400 }
401
402 # Function to test if the required version of Perl is installed.
403 function test_perl
404 {
405     echo -n "   Testing Perl version: "
406
407     if error=$( perl -e 'use 5.8.0;' 2>&1 ); then
408         echo_green "OK"
409     else
410         echo_red "FAIL"
411         echo $error | sed "s/, stopped.*//"
412         exit_abort
413     fi  
414 }
415
416 # Function to test if a given Perl module is installed.
417 function test_perl_module
418 {
419     local module=$1
420
421     echo -n "   Testing required Perl module - \"$module\": "
422
423     if ! error=$( perl -M$module -e '' 2>&1 > /dev/null ); then
424         echo_red "FAIL"
425         echo "   Try: perl -MCPAN -e 'install $module'"
426         exit_abort
427     else
428         echo_green "OK"
429     fi
430 }
431
432 # Function to test if the required version of Ruby is installed.
433 function test_ruby
434 {
435     echo -n "   Testing Ruby version: "
436
437     if error=$( ruby -e 'raise "Ruby version 1.9 required--this is only #{RUBY_VERSION}" if RUBY_VERSION < "1.9"' 2>&1 ); then
438         echo_green "OK"
439     else
440         echo_red "FAIL"
441         echo $error | sed "s/.*: //"
442         exit_abort
443     fi  
444 }
445
446 # Function to test if a given Ruby gem is installed.
447 function test_ruby_gem
448 {
449     local gem=$1
450
451     echo -n "   Testing required Ruby gem - \"$gem\": "
452
453     if error=$( gem list --local | grep $gem ); then
454         echo_green "OK"
455     else
456         echo_red "FAIL"
457         echo "      Try: gem install $gem"
458         exit_abort
459     fi
460 }
461
462 # Function to test is a given auxillary program is in $PATH.
463 function test_aux_program
464 {
465     local program=$1
466
467     echo -n "   Testing auxiliary program - \"$program\": "
468
469     if command -v $program >/dev/null; then
470         echo_green "OK"
471     else
472         echo_yellow "WARNING"
473     fi
474 }
475
476 # Function to checkout the Biopieces code from subversion.
477 function checkout_code
478 {
479     echo -n "Downloading Biopieces code from repository: "
480
481     if error=$( svn checkout http://biopieces.googlecode.com/svn/trunk/ $bp_code ); then
482         echo_green "OK"
483     else
484         echo_red "FAIL"
485         echo $error
486         exit_abort
487     fi  
488 }
489
490 # Function to checkout the Biopieces wiki from subversion.
491 function checkout_wiki
492 {
493     echo -n "Downloading Biopieces wiki from repository: "
494
495     if error=$( svn checkout http://biopieces.googlecode.com/svn/wiki/ "$bp_code/bp_usage" ); then
496         echo_green "OK"
497     else
498         echo_red "FAIL"
499         echo $error
500         exit_abort
501     fi  
502 }
503
504 # Function to run the Biopieces tests.
505 function prompt_test_biopieces
506 {
507     echo "Running the Biopieces test suite:"
508     echo ""
509
510     "$BP_DIR/bp_test/test_all"
511
512     echo ""
513     echo "   Any FAIL indicates broken Biopieces. This is probably because of"
514     echo "   missing prerequisites (see WARNINGs). You can verify this later by"
515     echo "   inspecting the documentation for those Biopieces. Otherwise - seek help!"
516
517     while true; do
518         read -p "Continue (yes/no)? " answer
519         case $answer in
520             [Yy]* ) break;;
521             [Nn]* ) exit_abort;;
522         esac
523     done
524 }
525
526 prompt_install
527 prompt_install_existing
528 prompt_test_prerequisites
529 prompt_install_dir_code
530 prompt_install_dir_data
531 prompt_install_dir_log
532 prompt_install_dir_tmp
533
534 checkout_code
535 checkout_wiki
536
537 prompt_append_bashrc
538 prompt_test_biopieces
539
540 exit_success