| 1 |
#! /usr/bin/env python3 |
| 2 |
# Author: Martin C. Frith 2021 |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import print_function |
| 6 |
|
| 7 |
import argparse |
| 8 |
import gzip |
| 9 |
import signal |
| 10 |
import sys |
| 11 |
|
| 12 |
def openFile(fileName): |
| 13 |
if fileName == "-": |
| 14 |
return sys.stdin |
| 15 |
if fileName.endswith(".gz"): |
| 16 |
return gzip.open(fileName, "rt") |
| 17 |
return open(fileName) |
| 18 |
|
| 19 |
def alignmentInput(lines): |
| 20 |
ranges = [] |
| 21 |
alnLines = [] |
| 22 |
for line in lines: |
| 23 |
fields = line.split() |
| 24 |
if line[0] == "#": |
| 25 |
print(line, end="") |
| 26 |
elif fields: |
| 27 |
alnLines.append(line) |
| 28 |
if line[0] == "s": |
| 29 |
seqName = fields[1] |
| 30 |
beg = int(fields[2]) |
| 31 |
span = int(fields[3]) |
| 32 |
seqLen = int(fields[5]) |
| 33 |
end = beg + span |
| 34 |
if fields[4] == "-": |
| 35 |
end = seqLen - beg |
| 36 |
beg = end - span |
| 37 |
r = seqName, seqLen, beg, end |
| 38 |
ranges.append(r) |
| 39 |
else: |
| 40 |
if alnLines: |
| 41 |
yield ranges, alnLines |
| 42 |
ranges = [] |
| 43 |
alnLines = [] |
| 44 |
if alnLines: |
| 45 |
yield ranges, alnLines |
| 46 |
|
| 47 |
def isCoveringMuchSequence(alns, nums): |
| 48 |
n = nums[0] |
| 49 |
for s in range(2): |
| 50 |
seqLen = alns[n][0][s][1] |
| 51 |
coverage = sum(alns[i][0][s][3] - alns[i][0][s][2] for i in nums) |
| 52 |
if coverage * 2 >= seqLen: return True |
| 53 |
return False |
| 54 |
|
| 55 |
def isNearInAllSeqs(iRanges, jRanges, maxDistance): |
| 56 |
for i, j in zip(iRanges, jRanges): |
| 57 |
iSeq, iLen, iBeg, iEnd = i |
| 58 |
jSeq, jLen, jBeg, jEnd = j |
| 59 |
if iSeq != jSeq: |
| 60 |
return False |
| 61 |
if jBeg > iEnd + maxDistance or iBeg > jEnd + maxDistance: |
| 62 |
return False |
| 63 |
return True |
| 64 |
|
| 65 |
def linkOneAln(args, alns, i): |
| 66 |
maxRankDifference = args.tween + 1 |
| 67 |
jIndexBeg = max(i - maxRankDifference, 0) |
| 68 |
jIndexEnd = min(i + maxRankDifference + 1, len(alns)) |
| 69 |
|
| 70 |
iRanges, iLines, iRank, iLinks = alns[i] |
| 71 |
iSeq, iLen, iBeg, iEnd = iRanges[0] |
| 72 |
|
| 73 |
j = i |
| 74 |
while j > jIndexBeg: |
| 75 |
j -= 1 |
| 76 |
jRanges, jLines, jRank, jLinks = alns[j] |
| 77 |
jSeq, jLen, jBeg, jEnd = jRanges[0] |
| 78 |
if jSeq < iSeq or jEnd + args.distance < iBeg: |
| 79 |
break |
| 80 |
if isNearInAllSeqs(iRanges, jRanges, args.distance): |
| 81 |
if abs(jRank - iRank) <= maxRankDifference: |
| 82 |
iLinks.append(j) |
| 83 |
|
| 84 |
j = i + 1 |
| 85 |
while j < jIndexEnd: |
| 86 |
jRanges, jLines, jRank, jLinks = alns[j] |
| 87 |
jSeq, jLen, jBeg, jEnd = jRanges[0] |
| 88 |
if iSeq < jSeq or iEnd + args.distance < jBeg: |
| 89 |
break |
| 90 |
if isNearInAllSeqs(iRanges, jRanges, args.distance): |
| 91 |
if abs(jRank - iRank) <= maxRankDifference: |
| 92 |
iLinks.append(j) |
| 93 |
j += 1 |
| 94 |
|
| 95 |
def connectedComponent(alns, isMarked, i): |
| 96 |
stack = [i] |
| 97 |
isMarked[i] = True |
| 98 |
while stack: |
| 99 |
j = stack.pop() |
| 100 |
yield j |
| 101 |
for k in alns[j][3]: |
| 102 |
if not isMarked[k]: |
| 103 |
stack.append(k) |
| 104 |
isMarked[k] = True |
| 105 |
|
| 106 |
def connectedComponents(alns): |
| 107 |
isMarked = [False for i in alns] |
| 108 |
for i in range(len(alns)): |
| 109 |
if not isMarked[i]: |
| 110 |
yield sorted(connectedComponent(alns, isMarked, i)) |
| 111 |
|
| 112 |
def sortKey1(aln): |
| 113 |
ranges, lines = aln |
| 114 |
return ranges[1] |
| 115 |
|
| 116 |
def main(args): |
| 117 |
alns = sorted(alignmentInput(openFile(args.infile)), key=sortKey1) |
| 118 |
alns = sorted(aln + (rank, []) for rank, aln in enumerate(alns)) |
| 119 |
for i in range(len(alns)): |
| 120 |
linkOneAln(args, alns, i) |
| 121 |
for c in connectedComponents(alns): |
| 122 |
if len(c) >= args.count or isCoveringMuchSequence(alns, c): |
| 123 |
for i in c: |
| 124 |
ranges, lines, rank, links = alns[i] |
| 125 |
print(*lines, sep="") |
| 126 |
|
| 127 |
if __name__ == "__main__": |
| 128 |
signal.signal(signal.SIGPIPE, signal.SIG_DFL) # avoid silly error message |
| 129 |
descr = "Omit any alignment not near (in both sequences) to other alignments." |
| 130 |
ap = argparse.ArgumentParser(description=descr, formatter_class= |
| 131 |
argparse.ArgumentDefaultsHelpFormatter) |
| 132 |
ap.add_argument("-c", "--count", type=int, default=3, metavar="C", |
| 133 |
help="minimum number of linked alignments") |
| 134 |
ap.add_argument("-d", "--distance", type=float, default=1000000, |
| 135 |
metavar="D", help="maximum distance") |
| 136 |
ap.add_argument("-t", "--tween", type=float, default=5, metavar="T", |
| 137 |
help="maximum number of alignments in between") |
| 138 |
ap.add_argument("infile", help="file of sequence alignments in MAF format") |
| 139 |
args = ap.parse_args() |
| 140 |
main(args) |