Skip to content

Commit

Permalink
allow setting bam sample name via env
Browse files Browse the repository at this point in the history
closes #115
  • Loading branch information
brentp committed Jun 15, 2023
1 parent 1e87c38 commit d5e5a03
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/somalier.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,25 @@ import strformat
import argparse
import strutils

const ENV_SAMPLE_NAME = "SOMALIER_SAMPLE_NAME"

proc get_sample_name(bam:Bam): string =
var txt = newString(bam.hdr.hdr.l_text)
copyMem(txt[0].addr, bam.hdr.hdr.text, txt.len)
for line in txt.split("\n"):
if line.startsWith("@RG") and "\tSM:" in line:
result = line.split("\tSM:")[1].split("\t")[0].strip()

var env_name = getEnv(ENV_SAMPLE_NAME)
# if the result was already set, we warn.
if result.len != 0 and env_name.len != 0:
stderr.write_line &"[somalier] using sample name from ENV: {env_name}"
# always use env if it was given.
if env_name.len != 0:
result = env_name

if result.len == 0:
raise newException(ValueError, "[somalier] no read-group in bam file")
raise newException(ValueError, &"[somalier] no read-group in bam file or given via '{ENV_SAMPLE_NAME}'")

proc looks_like_gvcf_variant(v:Variant): bool {.inline.} =
result = v.c.n_allele == 1
Expand Down

0 comments on commit d5e5a03

Please sign in to comment.