#!/bin/bash -Ceuo pipefail samtools idxstats HCC1395_tumor.recalibrated.bam > idxstats.txt x_map=$(grep -E "^chrX\s|^X\s" idxstats.txt | cut -f 3) x_len=$(grep -E "^chrX\s|^X\s" idxstats.txt | cut -f 2) y_map=$(grep -E "^chrY\s|^Y\s" idxstats.txt | cut -f 3) y_len=$(grep -E "^chrY\s|^Y\s" idxstats.txt | cut -f 2) awk -v xm="$x_map" -v xl="$x_len" -v ym="$y_map" -v yl="$y_len" ' BEGIN { if (ym == 0 || yl == 0) { print "XX" } else { x_cov = xm / xl y_cov = ym / yl ratio = x_cov / y_cov if (ratio > 50.0) print "XX" else print "XY" } }' > HCC1395_tumor.sex.txt