-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |