[de0dc9]: / bin / center_around_peak.sh

Download this file

31 lines (25 with data), 888 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# First file (ex. chrom_sizes.txt): Load chromosome sizes into an array
NR==FNR { chrom_sizes[$1] = $2; next; }
# Second file (ex. input.bed): Process BED data
# Print header lines as they are
/^#/ { print; next; }
{
mid = $4; # extract peak piosition
left = int(N/2); # Floor division for left padding
right = N - left; # Ensures total length is exactly N
# the new start and end values
start = mid - left;
ends = mid + right;
# Ensure start is not negative
if (start < 0) start = 0;
# Ensure end does not exceed chromosome size
if ($1 in chrom_sizes && ends > chrom_sizes[$1]) {
ends = chrom_sizes[$1];
}
# Print updated start, end, and ALL remaining columns
printf "%s\t%d\t%d", $1, start, ends;
for (i=4; i<=NF; i++) {
printf "\t%s", $i;
}
print ""; # Newline
}