Skip to content

Commit

Permalink
working on a better coverage calc
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimens committed May 13, 2024
1 parent 07be7f5 commit 1fb94a3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/harpy/scripts/depthWindows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /usr/bin/env python

import sys
import argparse

parser = argparse.ArgumentParser(prog = 'depthWindows.py', description = 'Reads the output of samtools depth -a from stdin and calculates a windowed mean.')
parser.add_argument('windowsize', type= int, help = "The window size to use to calcualte mean depth over (non-overlapping)")

args = parser.parse_args()
_sum = 0
start = 1
end = start + args.windowsize - 1
lastcontig = None
for line in sys.stdin:
# Remove the newline character at the end of the line
line = line.rstrip().split()
contig = line[0]
# the contig has changed, make the end position the last position, print output
if lastcontig and contig != lastcontig:
winsize = (position + 1) - start
print(f"{lastcontig}\t{position}\t{_sum / winsize}", file = sys.stdout)
# reset the window start/end and sum
start = 1
end = start + args.windowsize - 1
_sum = 0

position = int(line[1])
_sum += int(line[2])

if position == end:
print(f"{contig}\t{end}\t{_sum / args.windowsize}", file = sys.stdout)
start = end + 1
end = start + args.windowsize - 1
lastcontig = contig

0 comments on commit 1fb94a3

Please sign in to comment.