File size: 3,509 Bytes
3ae2f5f | 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #!/usr/bin/env bash
set -Eeuo pipefail
usage() {
cat <<'EOF'
Usage: select-export-samples.sh [OPTIONS]
Select one male and one female from each 1000 Genomes super-population.
NA12878 is included by default, occupying the EUR/female slot.
Options:
--include IID Pin a sample in its sex/super-population slot (repeatable)
--no-default Do not pin NA12878
-o, --output Manifest path (default: export/samples.tsv)
-h, --help Show this help
The remaining slots are filled deterministically from data/chr01.psam.
EOF
}
script_dir="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
repo_root="$(CDPATH= cd -- "$script_dir/.." && pwd)"
psam="$repo_root/data/chr01.psam"
output="$repo_root/export/samples.tsv"
pinned=(NA12878)
while [[ $# -gt 0 ]]; do
case "$1" in
--include)
[[ $# -ge 2 ]] || { printf '%s requires an IID\n' "$1" >&2; exit 2; }
pinned+=("$2")
shift 2
;;
--no-default)
pinned=()
shift
;;
-o|--output)
[[ $# -ge 2 ]] || { printf '%s requires a path\n' "$1" >&2; exit 2; }
output="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
printf 'Unknown option: %s\n' "$1" >&2
usage >&2
exit 2
;;
esac
done
[[ -f "$psam" ]] || { printf 'Sample metadata not found: %s\n' "$psam" >&2; exit 1; }
mkdir -p "$(dirname -- "$output")"
tmp="$(mktemp "$output.tmp.XXXXXX")"
trap 'rm -f "$tmp"' EXIT
pinned_csv=""
if [[ ${#pinned[@]} -gt 0 ]]; then
pinned_csv="$(IFS=,; printf '%s' "${pinned[*]}")"
fi
awk -F '\t' -v OFS='\t' -v pinned_csv="$pinned_csv" '
BEGIN {
superpop[1] = "AFR"
superpop[2] = "AMR"
superpop[3] = "EAS"
superpop[4] = "EUR"
superpop[5] = "SAS"
sex_name[1] = "male"
sex_name[2] = "female"
if (pinned_csv != "") {
pinned_count = split(pinned_csv, requested, ",")
}
}
NR == 1 { next }
{
iid[++sample_count] = $1
sex[$1] = $4
sp[$1] = $5
pop[$1] = $6
}
END {
failed = 0
for (i = 1; i <= pinned_count; i++) {
id = requested[i]
if (!(id in sex)) {
printf "Requested sample not found: %s\n", id > "/dev/stderr"
failed = 1
continue
}
if (sex[id] != 1 && sex[id] != 2) {
printf "Requested sample has unknown sex: %s\n", id > "/dev/stderr"
failed = 1
continue
}
key = sp[id] SUBSEP sex[id]
if (key in chosen && chosen[key] != id) {
printf "Requested samples occupy the same slot: %s and %s\n", chosen[key], id > "/dev/stderr"
failed = 1
continue
}
chosen[key] = id
}
if (failed) exit 1
for (p = 1; p <= 5; p++) {
for (s = 1; s <= 2; s++) {
key = superpop[p] SUBSEP s
if (!(key in chosen)) {
for (i = 1; i <= sample_count; i++) {
id = iid[i]
if (sp[id] == superpop[p] && sex[id] == s) {
chosen[key] = id
break
}
}
}
if (!(key in chosen)) {
printf "No sample found for %s/%s\n", superpop[p], sex_name[s] > "/dev/stderr"
exit 1
}
}
}
print "#IID", "Sex", "SuperPop", "Population"
for (p = 1; p <= 5; p++) {
for (s = 1; s <= 2; s++) {
id = chosen[superpop[p] SUBSEP s]
print id, sex_name[s], sp[id], pop[id]
}
}
}
' "$psam" > "$tmp"
mv "$tmp" "$output"
trap - EXIT
printf 'Selected samples: %s\n' "$output"
column -t -s $'\t' "$output"
|