| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright [2015-2024] EMBL-European Bioinformatics Institute |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
use strict; |
| 18 |
use File::Temp 'tempdir'; |
| 19 |
use Cwd; |
| 20 |
use File::Path qw(make_path); |
| 21 |
use Getopt::Long; |
| 22 |
|
| 23 |
my $htslib_version = "1.5"; |
| 24 |
my $version = get_version(); |
| 25 |
|
| 26 |
my $help = "INSTALL.pl [-h|--help] [--prefix=filepath] [--static] [~/prefix/path]\n"; |
| 27 |
$help .= "--help (-h) - this help message\n"; |
| 28 |
$help .= "--prefix - Path to install Bio::DB::HTS.\n"; |
| 29 |
$help .= " Alternative to providing a path at the end of comandline\n"; |
| 30 |
$help .= " If neither are provided defaults to root install (requires root access)\n"; |
| 31 |
$help .= "--static - Build Bio::DB::HTS using the static libhts.a library and do not install htslib\n"; |
| 32 |
$help .= "--htslib_version - Build Bio::DB::HTS using the specified HTSlib version (Default ".$htslib_version.")\n"; |
| 33 |
|
| 34 |
my $cwd = system 'pwd'; |
| 35 |
|
| 36 |
|
| 37 |
my $opts = parse_options(); |
| 38 |
my $prefix_path; |
| 39 |
$prefix_path = $opts->{'prefix'} if(exists($opts->{'prefix'}) && defined($opts->{'prefix'})); |
| 40 |
$htslib_version = $opts->{'htslib_version'} if($opts->{'htslib_version'}) ; |
| 41 |
|
| 42 |
my ($version_major,$version_minor,$version_revision) = split /\./, $htslib_version ; |
| 43 |
|
| 44 |
|
| 45 |
# STEP 0: various dependencies |
| 46 |
my $git = `which git`; |
| 47 |
$git or die <<END; |
| 48 |
'git' command not in path. Please install git and try again. |
| 49 |
On Debian/Ubuntu systems you can do this with the command: |
| 50 |
|
| 51 |
apt-get install git |
| 52 |
END |
| 53 |
|
| 54 |
|
| 55 |
`which cc` or die <<END; |
| 56 |
'cc' command not in path. Please install it and try again. |
| 57 |
On Debian/Ubuntu systems you can do this with the command: |
| 58 |
|
| 59 |
apt-get install build-essential |
| 60 |
END |
| 61 |
|
| 62 |
`which make` or die <<END; |
| 63 |
'make' command not in path. Please install it and try again. |
| 64 |
On Debian/Ubuntu systems you can do this with the command: |
| 65 |
|
| 66 |
apt-get install build-essential |
| 67 |
END |
| 68 |
|
| 69 |
# The following libraries are version number dependant for HTSlib |
| 70 |
if( $version_major >= 2 || ($version_major==1 && $version_minor>=5) ) |
| 71 |
{ |
| 72 |
-e '/usr/include/lzma.h' or die <<END; |
| 73 |
lzma.h library header not found in /usr/include. Please install it and try again. |
| 74 |
On Debian/Ubuntu systems you can do this with the command: |
| 75 |
|
| 76 |
apt-get install liblzma-dev |
| 77 |
END |
| 78 |
|
| 79 |
-e '/usr/include/bzlib.h' or die <<END; |
| 80 |
zlib.h library header not found in /usr/include. Please install it and try again. |
| 81 |
On Debian/Ubuntu systems you can do this with the command: |
| 82 |
|
| 83 |
apt-get install libbz2-dev |
| 84 |
END |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
-e '/usr/include/zlib.h' or die <<END; |
| 89 |
zlib.h library header not found in /usr/include. Please install it and try again. |
| 90 |
On Debian/Ubuntu systems you can do this with the command: |
| 91 |
|
| 92 |
apt-get install zlib1g-dev |
| 93 |
END |
| 94 |
|
| 95 |
; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
eval "require Bio::SeqFeature::Lite" or die <<END; |
| 100 |
BioPerl does not seem to be installed. Please install it and try again. |
| 101 |
On Debian/Ubuntu systems you can do this with the command: |
| 102 |
|
| 103 |
apt-get install bioperl |
| 104 |
|
| 105 |
On other systems use the CPAN shell: |
| 106 |
|
| 107 |
perl -MCPAN -e 'install Bio::Perl' |
| 108 |
END |
| 109 |
; |
| 110 |
|
| 111 |
# STEP 1: Create a clean directory for building |
| 112 |
my $install_dir = tempdir(CLEANUP => 1); |
| 113 |
info("Performing build in $install_dir"); |
| 114 |
|
| 115 |
|
| 116 |
# STEP 2: Download HTSlib |
| 117 |
info("Checking out HTSlib v".$htslib_version); |
| 118 |
chdir $install_dir; |
| 119 |
my $htslib_archive = $htslib_version.".zip" ; |
| 120 |
my $htslib_archive_url = "https://github.com/samtools/htslib/archive/".$htslib_version.".zip" ; |
| 121 |
system "wget -O " . $htslib_archive . " " .$htslib_archive_url ; |
| 122 |
if ( $? == -1 ) |
| 123 |
{ |
| 124 |
die "HTSlib fetch command failed: $!\n"; |
| 125 |
} |
| 126 |
-f './'.$htslib_archive or die "Could not fetch HTSlib archive ".$htslib_archive_url ; |
| 127 |
system "unzip ".$htslib_archive ; |
| 128 |
system "mv htslib-$htslib_version htslib" ; |
| 129 |
-d './htslib' or die "Unzip seems to have failed. Could not find $install_dir/htslib directory"; |
| 130 |
|
| 131 |
|
| 132 |
# STEP 3: Download Bio-DB-HTS |
| 133 |
info("Fetching version $version of Bio-DB-HTS from GitHub"); |
| 134 |
chdir $install_dir; |
| 135 |
my $biodbhts_archive = "$version.zip" ; |
| 136 |
my $biodbhts_archive_url = "https://github.com/Ensembl/Bio-DB-HTS/archive/$version.zip" ; |
| 137 |
system "wget -O " . $biodbhts_archive . " " .$biodbhts_archive_url ; |
| 138 |
if ( $? == -1 ) |
| 139 |
{ |
| 140 |
die "Bio-DB-HTS fetch command failed: $!\n"; |
| 141 |
} |
| 142 |
-f './'.$biodbhts_archive or die "Could not fetch Bio::DB::HTS archive ".$biodbhts_archive_url ; |
| 143 |
system "unzip ".$biodbhts_archive ; |
| 144 |
system "mv Bio-DB-HTS-$version Bio-DB-HTS" ; |
| 145 |
-d './Bio-DB-HTS' or die "Unzip seems to have failed. Could not find $install_dir/Bio-DB-HTS directory"; |
| 146 |
|
| 147 |
|
| 148 |
# Step 4: Build libhts.a |
| 149 |
info("Building HTSlib"); |
| 150 |
chdir "$install_dir/htslib"; |
| 151 |
# patch makefile |
| 152 |
open my $in, '<','Makefile' or die "Couldn't open Makefile for reading: $!"; |
| 153 |
open my $out,'>','Makefile.new' or die "Couldn't open Makefile.new for writing: $!"; |
| 154 |
while (<$in>) { |
| 155 |
chomp; |
| 156 |
if (/^CFLAGS/ && !/-fPIC/) { |
| 157 |
s/#.+//; # get rid of comments |
| 158 |
$_ .= " -fPIC -Wno-unused -Wno-unused-result"; |
| 159 |
} |
| 160 |
} continue { |
| 161 |
print $out $_,"\n"; |
| 162 |
} |
| 163 |
|
| 164 |
close $in; |
| 165 |
close $out; |
| 166 |
rename 'Makefile.new','Makefile' or die "Couldn't rename Makefile.new to Makefile: $!"; |
| 167 |
warn "***MAKE***\n"; |
| 168 |
system "make"; |
| 169 |
if(defined $prefix_path && !$opts->{'static'}) { |
| 170 |
warn "***MAKE INSTALL***\n"; |
| 171 |
system "make install prefix=$prefix_path"; |
| 172 |
} |
| 173 |
if($opts->{'static'}){ |
| 174 |
system "rm -f $install_dir/htslib/libhts.so*"; |
| 175 |
} |
| 176 |
-e 'libhts.a' or die "Compile didn't complete. No libhts.a library file found"; |
| 177 |
|
| 178 |
# Step 5: Build Bio::DB::HTS |
| 179 |
info("Building Bio::DB::HTS"); |
| 180 |
chdir "$install_dir/Bio-DB-HTS"; |
| 181 |
my $cmd; |
| 182 |
if(defined $prefix_path) { |
| 183 |
if(!$opts->{'static'}) { |
| 184 |
$cmd = "env HTSLIB_DIR=$prefix_path/lib perl Build.PL --install_base=$prefix_path"; |
| 185 |
} else { |
| 186 |
$cmd = "env HTSLIB_DIR=$install_dir/htslib perl Build.PL --install_base=$prefix_path"; |
| 187 |
} |
| 188 |
} else { |
| 189 |
$cmd = "env HTSLIB_DIR=$install_dir/htslib perl Build.PL"; |
| 190 |
} |
| 191 |
|
| 192 |
if($opts->{'static'}) { |
| 193 |
$cmd .= " --static=1"; |
| 194 |
} |
| 195 |
|
| 196 |
warn "***CMD*** : $cmd\n"; |
| 197 |
system $cmd; |
| 198 |
|
| 199 |
-e "./Build" or die "Build.PL didn't execute properly: no Build file found"; |
| 200 |
system "./Build"; |
| 201 |
`./Build test` =~ /Result: PASS/ or die "Build test failed. Not continuing"; |
| 202 |
|
| 203 |
# Step 6: Install |
| 204 |
my $sudo = `which sudo`; chomp($sudo); |
| 205 |
|
| 206 |
if(defined $prefix_path) { |
| 207 |
info("Installing Bio::DB::HTS to $prefix_path."); |
| 208 |
`./Build install` =~ /Installing.+?HTS\.pm/ or die "Couldn't install to $prefix_path: $?"; |
| 209 |
} elsif ($sudo) { |
| 210 |
info("Installing Bio::DB::HTS using sudo. You will be asked for your password."); |
| 211 |
info("If this step fails because sudo isn't installed, go back and run this script again as superuser."); |
| 212 |
system "sudo ./Build install";; |
| 213 |
} else { |
| 214 |
info("Installing Bio::DB::HTS system wide without sudo."); |
| 215 |
info("Make sure you have the right privileges."); |
| 216 |
`./Build install` =~ /Installing.+?HTS\.pm/ or die "Couldn't install at system level without sudo: $?"; |
| 217 |
} |
| 218 |
|
| 219 |
if($opts->{'static'}){ |
| 220 |
system "rm -f $install_dir/htslib/libhts.a"; |
| 221 |
} |
| 222 |
|
| 223 |
# Step 7: Yay! |
| 224 |
info("Bio::DB::HTS v$version is now installed."); |
| 225 |
chdir '/'; |
| 226 |
|
| 227 |
exit 0; |
| 228 |
|
| 229 |
sub parse_options { |
| 230 |
my ($factory) = @_; |
| 231 |
my %opts = (); |
| 232 |
|
| 233 |
my $result = &GetOptions ( |
| 234 |
'static' => \$opts{'static'}, |
| 235 |
'prefix=s' => \$opts{'prefix'}, |
| 236 |
'htslib_version=s' => \$opts{'htslib_version'}, |
| 237 |
'h|help' => \$opts{'h'}, |
| 238 |
); |
| 239 |
|
| 240 |
if($opts{'h'}){ |
| 241 |
warn $help,"\n"; |
| 242 |
exit(0); |
| 243 |
} |
| 244 |
|
| 245 |
if(defined($opts{'prefix'})){ |
| 246 |
$opts{'prefix'} = prefix_install($opts{'prefix'}); |
| 247 |
}elsif(!defined($opts{'prefix'}) && @ARGV > 0){ |
| 248 |
$opts{'prefix'} = prefix_install(shift(@ARGV)); |
| 249 |
}elsif(exists($ENV{PERL_LOCAL_LIB_ROOT}) && defined($ENV{PERL_LOCAL_LIB_ROOT})){ |
| 250 |
$opts{'prefix'} = prefix_install($ENV{PERL_LOCAL_LIB_ROOT}); |
| 251 |
} |
| 252 |
|
| 253 |
$opts{'static'} = 0 if(!defined($opts{'static'})); |
| 254 |
|
| 255 |
$opts{'static'} = $ENV{STATIC_HTS} if((!$opts{'static'}) && |
| 256 |
exists($ENV{STATIC_HTS}) && defined($ENV{STATIC_HTS})); |
| 257 |
|
| 258 |
|
| 259 |
return \%opts; |
| 260 |
} |
| 261 |
|
| 262 |
sub info { |
| 263 |
my $msg = shift; |
| 264 |
chomp $msg; |
| 265 |
print STDERR "\n*** $msg ***\n"; |
| 266 |
} |
| 267 |
|
| 268 |
sub prefix_install { |
| 269 |
my $prefix_path = shift; |
| 270 |
if($prefix_path =~ s/^\~//) { |
| 271 |
$prefix_path = $ENV{HOME}.$prefix_path |
| 272 |
} |
| 273 |
elsif($prefix_path !~ m|^/|) { |
| 274 |
$prefix_path = getcwd().'/'.$prefix_path; |
| 275 |
} |
| 276 |
my $err; |
| 277 |
unless(-e $prefix_path) { |
| 278 |
make_path($prefix_path, {verbose => 1, error => \$err}); |
| 279 |
if (@$err) { |
| 280 |
for my $diag (@$err) { |
| 281 |
my ($dir, $message) = %$diag; |
| 282 |
if ($dir eq '') { print "general error: $message\n"; } |
| 283 |
else { print "problem creating dir $dir: $message\n"; } |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
return $prefix_path; |
| 288 |
} |
| 289 |
|
| 290 |
sub get_version { |
| 291 |
use FindBin '$Bin'; |
| 292 |
my $version = `grep -m 1 "HTS::VERSION" $Bin/lib/Bio/DB/HTS.pm`; |
| 293 |
$version =~ /(\d+?\.\d+?)\D/; |
| 294 |
$version = $1; |
| 295 |
|
| 296 |
use Carp; |
| 297 |
croak "Couldn't get a meaningful library version: $version" |
| 298 |
unless $version =~ /\d+?\.\d+?/; |
| 299 |
|
| 300 |
return $version; |
| 301 |
} |