Thursday, January 2, 2025

How to build SAMTOOLS on a Windows PC

I have recently started to add functionality in the Visual genome Browser to be able to display the aligned reads from a SAM/BAM/CRAM file as a graphical representation similar to IGV. In order o be able to do that, I needed to compile a 64-bit EXE version of SAMTOOLS for Windows PCs. 

 These are the steps that I followed:

I read through the build instructions at:

In the section that says: "Windows MSYS2/MINGW64" I then realized I need a Compilation environment which is called MSYS, which contains a compiler: mingw64.exe


I then proceeded to download the file: msys2-x86_64-20241208.exe



I started the installation on my Windows PC:




Then you RUN: MSYS2 MINGW64
This will open the build environment.

First thing to do is to check if the MSYS is up to date by typing: pacman -Syuu


And respond with Y


Then you have to run it again after it has been upgraded: MSYS2 MINGW64

Also check that $MSYSTEM equals MINGW64 by typing: echo $MSYSTEM




Then update more components by typing the following one after the other and confirming by pressing enter.

pacman -S base-devel mingw-w64-x86_64-toolchain
pacman -S mingw-w64-x86_64-libdeflate mingw-w64-x86_64-zlib mingw-w64-x86_64-bzip2
pacman -S mingw-w64-x86_64-xz mingw-w64-x86_64-curl mingw-w64-x86_64-autotools
pacman -S mingw-w64-x86_64-tools-git






Now download the C language source code base for SAMTOOLS from 

These contain full source repositories which has the required libraries inside them:

Then I unzipped it. Probably a good idea to put it in a folder without spaces in the path, otherwise you will have to use "SAM\ TOOLS" with a backslash escape character to escape the spaces in the path.




Next step is to ensure that the folder where the compiler resides is in your WINDOWS Path variable:
(Depending on where you installed it initially)

E:\msys64\mingw64\bin

This is also where the SAMTOOLS will be put when it is compiled and installed.






Now you change to the folder where you have put the SAMTOOLS source code using the UNIX syntax in the console window of MINGW64
The following procedure is explained in : https://github.com/samtools/samtools/blob/develop/INSTALL

Then you have to change the current directory to where it was unzipped.

cd /F/Home/Development/SAMTOOLS/samtools-1.21

You can type  LS to get the directory listing

You then run autoreconf to create the configure file if it does not exist

Next step is to type: ./configure



Next is to compile and link the exe:
just type: make



This will use the Makefile to build the EXE



At the end of this build process you will end up with the SAMTOOLS.EXE



You can now "install" this to the E:\msys64\mingw64\bin folder which is in your path by typing:
make install

This will result in the SAMTOOLS.exe being put in the folder together with all of its dependencies:


To test if it works you can open a normal windows command line window and change to the folder:
F:\Home\Development\SAMTOOLS\samtools-1.21\test\quickcheck

Then you simply check if it can open a BAM file :
samtools view 3.quickcheck.ok.bam



That is what is needed to compile SAMTOOLS.EXE

If you want to re-run the make for some reason, you will have to remove the config.mk file first otherwise it will not work. (rm = remove)
rm config.mk
rm /e/msys64/mingw64/bin/samtools.exe
./configure

and then proceed to run
make 
and 
make install 


You can follow a similar procedure to build BCFTOOLS as obtained from:
https://www.htslib.org/download/

When you want to view a SAM file:
samtools view "F:\Home\Development\samtools-1.21\htslib-1.21\test\ce#1000.bam" 

When you want to view a CRAM file:
samtools view "F:\Home\Development\samtools-1.21\htslib-1.21\test\ce#1000.cram"

When you want to create an index to allow you to search within ranges:
samtools index "F:\Home\Development\samtools-1.21\htslib-1.21\test\ce#1000.cram"

When you only want to view a specific range:
samtools view "F:\Home\Development\samtools-1.21\htslib-1.21\test\ce#1000.cram" CHROMOSOME_I:3-3