-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbed2sort
executable file
·43 lines (38 loc) · 1.28 KB
/
bed2sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
#PBS -l nodes=1:ppn=4
#### usage ####
usage() {
echo Program: "bed2sort (sort input BED file by width of region)"
echo Author: BRIC, University of Copenhagen, Denmark
echo Version: 1.0
echo Contact: [email protected]
echo "Usage: bed2sort -i <file>"
echo "Options:"
echo " -i <file> [input file containing genomic coordinate in BED format (can be stdin)]"
echo " -h [help]"
echo
exit 0
}
#### parse options ####
while getopts i:h ARG; do
case "$ARG" in
i) INPUTBEDFILE=$OPTARG;;
h) HELP=1;;
esac
done
## usage, if necessary file and directories are given/exist
if [ -z "$INPUTBEDFILE" -o "$HELP" ]; then
usage
fi
## create temporary BED file if input is from stdin
if [ "$INPUTBEDFILE" == "stdin" ]; then
TMP=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
while read LINE; do
echo ${LINE}
done | perl -ane '$line=""; foreach(@F) { $line.="$_\t"; } $line=~s/\t$//g; print "$line\n";' > $TMP
INPUTBEDFILE=$TMP
fi
## sort input bed file by width
NCOL=$(head -n 1 $INPUTBEDFILE | perl -ane 'printf("%d", scalar(@F));')
NEW_COL=$(head -n 1 $INPUTBEDFILE | perl -ane 'printf("%d", scalar(@F)+1);')
less $INPUTBEDFILE | perl -ane '$len=$F[2]-$F[1]; chomp($_); print "$_\t$len\n";' | sort -k $NEW_COL"n",$NEW_COL | cut -f 1-$NCOL