I have some files (which are in .bam format(binary)) to download from a location. I can download them using wget and then want to extract some specific information from them only. For that I use samtools. What I want to know is, is there a way I can on the fly, extract what I want to from the bam file so that I don't have to download the entire bam file and then extract what I need and then delete the original bam file.

Commands used are

wget link.bam

samtools view -h chr1 link.bam | samtools view -bS > condensed.bam

Can this be done without downloading the entire original bam file on my hard drive. I want to do this because I am short on space and these files are huge.

Hope to hear from you guys.


  • Do you know the byte-range where the desired information is located?
    – Otheus
    May 15, 2015 at 20:00

1 Answer 1


The standard method is wget -O- _url_ | samtools view -h chr1 - | samtools view -bS >condensed.bam. In case you weren't sure, that's an O for Output, not a zero.

Another optimization step can be obtained if you know how many bytes are relevant for the next pipeline. Let's say samtools needs only the first 1352 bytes. You can do this using curl (According to this question/answer, wget has a bug.)

curl -r 0-1352 | ...
  • Hi How can I know how many bytes will be required for samtools step?? May 19, 2015 at 22:29
  • I have no idea! I figured you knew that before asking!. OK, so you just needed to know how to pipe output to a pipe? First part answers that.
    – Otheus
    May 20, 2015 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.