Debian Codegraph @

fuse-ranges

raw
1 #!/bin/bash -norc
2
3 # Public domain notice for all NCBI EDirect scripts is located at:
4 # https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice
5
6 NormalizeRanges() {
7
8 while IFS=$'\t' read score index strand aligns
9 do
10 ranges=$( echo "$aligns" | tr ',' '\n' | tr -s '.' | tr '.' '\t' )
11 echo "$ranges" |
12 while IFS=$'\t' read frst scnd
13 do
14 if [ "$frst" -lt "$scnd" ]
15 then
16 echo -e "${strand}\t${frst}\t${scnd}"
17 else
18 echo -e "${strand}\t${scnd}\t${frst}"
19 fi
20 done
21 done |
22 sort-table -k 1,1r -k 2,2n -k 3,3n |
23 uniq
24 }
25
26 CombineRanges() {
27
28 std=""
29 lft=0
30 rgt=0
31 while IFS=$'\t' read strand frst scnd
32 do
33 if [ -n "$std" ] && [ "$std" != "$strand" ]
34 then
35 len=$(( $rgt + 1 - $lft ))
36 echo -e "${std}\t${lft}\t${rgt}\t${len}"
37 lft=0
38 rgt=0
39 fi
40 std="$strand"
41 if [ "$lft" -eq 0 ] && [ "$rgt" -eq 0 ]
42 then
43 lft="$frst"
44 rgt="$scnd"
45 elif [ "$frst" -le "$((rgt + 1))" ]
46 then
47 if [ "$rgt" -lt "$scnd" ]
48 then
49 rgt="$scnd"
50 fi
51 else
52 len=$(( $rgt + 1 - $lft ))
53 echo -e "${std}\t${lft}\t${rgt}\t${len}"
54 lft="$frst"
55 rgt="$scnd"
56 fi
57 done
58 len=$(( $rgt + 1 - $lft ))
59 echo -e "${std}\t${lft}\t${rgt}\t${len}"
60 }
61
62 grep '^[1-9]' |
63 NormalizeRanges |
64 CombineRanges