Sort chromosome

There are two rules that you might want to sort your chromosome by:
– Sort in a case-sensitive lexicographical order (eg. chr1, chr10, chr11 …), usually required by bedtools, UCSC utilities (eg. bedGraphToBigWig etc.) and so on;
– Or sort in a numeric order for other purpose (eg. chr1, chr2, chr3 …).

Sort in lexicographical order:

sort -k1,1 -k2,2n input.bed > sorted.bed
sortBed input.bed > sorted.bed # bedtools function
LC_COLLATE=C sort -k1,1 -k2,2n input.bedgraph > sorted.bedgraph

Sort in numeric order:

./sort_chr.sh input.bed

Download the code from here:
https://github.com/bioinfocore/bashCore/blob/master/sort_chr.sh

Source:
https://www.biostars.org/p/150036/
http://seqanswers.com/forums/showthread.php?t=63932

Substitute ‘\r’ in Excel output to ‘\n’ for Unix

When outputing tsv from Excel directly, it added an unpleasant ‘\r’ at the end of each line. And you won’t even notive it by opening the file in TextWrangler. It can be checked by head -n 2 input.txt, you will see the difference.

Here is how to replace the ‘\r’ with ‘\n’:

cat input.txt | tr '\r' '\n' > output.txt
cat input.txt | tr '^M' '\n' > output.txt

While typing ^M in the command, do not use shift+6 & M. You should use ctrl+v+m

Source: https://github.com/bioinfocore/bashCore