| 1 |
#!/usr/bin/env python3 |
| 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 |
# cat smear.asn | blst2gm | gm2ranges | grep minus | cut -f 2- | fuse-ranges |
| 7 |
# minus 5599 5731 133 |
| 8 |
# minus 5740 6520 781 |
| 9 |
# minus 6609 7098 490 |
| 10 |
# minus 9055 9679 625 |
| 11 |
|
| 12 |
import sys |
| 13 |
|
| 14 |
def getRanges(zipped): |
| 15 |
for start, span, strand in zipped: |
| 16 |
if start == -1: |
| 17 |
continue |
| 18 |
stop = start + span - 1 |
| 19 |
if strand == "minus": |
| 20 |
start, stop = stop, start |
| 21 |
yield f'{start}..{stop}' |
| 22 |
|
| 23 |
def getStrand(col): |
| 24 |
if "plus" in col and "minus" in col: |
| 25 |
return "mixed" |
| 26 |
elif "minus" in col: |
| 27 |
return "minus" |
| 28 |
return "plus" |
| 29 |
|
| 30 |
def doLine(cols): |
| 31 |
zipped = zip(map(int, cols[2].split("|")), |
| 32 |
map(int, cols[3].split("|")), |
| 33 |
cols[4].split("|")) |
| 34 |
ranges = list(getRanges(zipped)) |
| 35 |
num = str(len(ranges)) |
| 36 |
strand = getStrand(cols[4]) |
| 37 |
ivals = ",".join(ranges) |
| 38 |
fields = [cols[0], cols[1], num, strand, ivals] |
| 39 |
print("\t".join(fields)) |
| 40 |
|
| 41 |
for line in sys.stdin: |
| 42 |
line = line.rstrip() |
| 43 |
if len(line) < 1: |
| 44 |
continue |
| 45 |
cols = line.split() |
| 46 |
if len(cols) < 5: |
| 47 |
continue |
| 48 |
doLine(cols) |