Download scripts/select-export-samples.sh from prescience-bio/pop-1kgp: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/datasets/prescience-bio/pop-1kgp/resolve/main/scripts/select-export-samples.sh
- Command line
-
hf download hf://datasets/prescience-bio/pop-1kgp/scripts/select-export-samples.sh
-
curl -L -o select-export-samples.sh https://huggingface.co/datasets/prescience-bio/pop-1kgp/resolve/main/scripts/select-export-samples.sh
3.51 kB
| 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" | |