Debian Codegraph @

fuse-segments

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 NormalizeSegments() {
7
8 while IFS=$'\t' read frst scnd ignore
9 do
10 if [ "$frst" -lt "$scnd" ]
11 then
12 echo -e "${frst}\t${scnd}"
13 else
14 echo -e "${scnd}\t${frst}"
15 fi
16 done |
17 sort-table -k 1,1n -k 2,2n |
18 uniq
19 }
20
21 CombineSegments() {
22
23 lft=0
24 rgt=0
25 while IFS=$'\t' read frst scnd ignore
26 do
27 if [ "$lft" -eq 0 ] && [ "$rgt" -eq 0 ]
28 then
29 lft="$frst"
30 rgt="$scnd"
31 elif [ "$frst" -le "$((rgt + 1))" ]
32 then
33 if [ "$rgt" -lt "$scnd" ]
34 then
35 rgt="$scnd"
36 fi
37 else
38 len=$(( $rgt + 1 - $lft ))
39 echo -e "${lft}\t${rgt}\t${len}"
40 lft="$frst"
41 rgt="$scnd"
42 fi
43 done
44 len=$(( $rgt + 1 - $lft ))
45 echo -e "${lft}\t${rgt}\t${len}"
46 }
47
48 grep '^[1-9]' |
49 sort-table -k 1,1n -k 2,2n |
50 uniq -i |
51 NormalizeSegments |
52 CombineSegments