Skip to content

Instantly share code, notes, and snippets.

@taoliu
Last active March 22, 2024 21:00
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save taoliu/2469050 to your computer and use it in GitHub Desktop.
Save taoliu/2469050 to your computer and use it in GitHub Desktop.
bedGraph to bigWig
#!/bin/bash
# check commands: slopBed, bedGraphToBigWig and bedClip
which bedtools &>/dev/null || { echo "bedtools not found! Download bedTools: <http://code.google.com/p/bedtools/>"; exit 1; }
which bedGraphToBigWig &>/dev/null || { echo "bedGraphToBigWig not found! Download: <http://hgdownload.cse.ucsc.edu/admin/exe/>"; exit 1; }
which bedClip &>/dev/null || { echo "bedClip not found! Download: <http://hgdownload.cse.ucsc.edu/admin/exe/>"; exit 1; }
# end of checking
if [ $# -lt 2 ];then
echo "Need 2 parameters! <bedgraph> <chrom info>"
exit
fi
F=$1
G=$2
bedtools slop -i ${F} -g ${G} -b 0 | bedClip stdin ${G} ${F}.clip
LC_COLLATE=C sort -k1,1 -k2,2n ${F}.clip > ${F}.sort.clip
bedGraphToBigWig ${F}.sort.clip ${G} ${F/bdg/bw}
rm -f ${F}.clip ${F}.sort.clip
@brentp
Copy link

brentp commented Apr 23, 2012

What's this syntax?

${F/bdg/bw}

@taoliu
Copy link
Author

taoliu commented Apr 23, 2012

To replace 'bdg' with 'bw' in the filename. It's a BASH string manipulation.

@brentp
Copy link

brentp commented Apr 23, 2012

thanks! great. that will save me from doing

$(dirname $F)/$(basename $F .bdg).bw

@melerz
Copy link

melerz commented May 21, 2015

Thank you, the bedClip binary fixed my errors while converting bedGraph to bigWig.

@elgartmi
Copy link

elgartmi commented Aug 4, 2015

Great stuff!
HOwever I have a small correction: sometimes (always in general case) you need to sort the bdg file or bedGraphToBigWig crashes with:
".bdg.clip is not case-sensitive sorted at line 31766. Please use "sort -k1,1 -k2,2n" with LC_COLLATE=C, or bedSort and try again."

So here is my fix:

F=$1
G=$2
bedtools slop -i ${F} -g ${G} -b 0 | bedClip stdin ${G} ${F}.clip
LC_COLLATE=C sort -k1,1 -k2,2n ${F}.clip > ${F}.sort.clip
bedGraphToBigWig ${F}.sort.clip ${G} ${F/bdg/bw}

@olegs
Copy link

olegs commented Jul 15, 2016

Same here, thank @elgartmi for the fix.

@taoliu
Copy link
Author

taoliu commented Apr 14, 2017

Thank @elgartmi ! I incorporated your fix

@varsh1090
Copy link

Hi, is there a command line option similar to bedClip in bedGraphToBigWig to avoid the error - End coordinate bigger than chr? Thanks!

@parsboy66
Copy link

parsboy66 commented Nov 20, 2018

how can i use this code??? use as ./..sh file
help me plz

@hyjforesight
Copy link

bedGraphToBigWig: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory

@ultrav71
Copy link

ultrav71 commented Jun 1, 2022

bedGraphToBigWig: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory

Can someone please help with this? I am losing my mind...

@hyjforesight
Copy link

@ultrav71
Copy link

ultrav71 commented Jun 3, 2022

@hyjforesight thank you!

@sx998508
Copy link

hey there taoliu. would you mind to help me sloving quisiton when i run script and errors came out like bedgraph file contain Homo sapiens unplaced genomic contig ?

@ShannonTown
Copy link

ShannonTown commented Aug 25, 2022

Hi I have some questions about this script, first, I ran the original script and ran into the error about "Overlapping regions in a BedGraph file", then I made some modifications according to the last post in here and was able to make the bw file. I don't really understand what the codes do....but I was able to make it work.. Do you have further suggestions regarding "Overlapping regions in a BedGraph file"?

In the bw file, I noticed that the "binning" is larger than the original bedgraph, is it possible to keep the original "binning"? It is changed because there's a "bedtool merge" step in the modification (which is to solve the overlapping region error?)?
image

Thank you!

@sx998508
Copy link

sx998508 commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment