Skip to content

Commit 7b18579

Browse files
edmundmillerclaude
andcommitted
fix: Add robust error handling for flagstat parsing in bedtools genomecov
- Handle cases where flagstat files don't match expected regex patterns - Try multiple flagstat format patterns for compatibility - Fall back to default scale factor with warning for stub tests - Prevent index out of range errors during scale factor calculation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent b082d12 commit 7b18579

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

subworkflows/local/bam_bedgraph_bigwig_bedtools_ucsc.nf

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,21 @@ workflow BAM_BEDGRAPH_BIGWIG_BEDTOOLS_UCSC {
2121
.map { meta, bam, flagstat ->
2222
// Parse flagstat to get mapped reads count
2323
def flagstat_content = flagstat.text
24-
def mapped_reads = (flagstat_content =~ /(\d+) \+ \d+ mapped/)[0][1] as Integer
24+
def match = flagstat_content =~ /(\d+) \+ \d+ mapped/
25+
if (match.size() == 0) {
26+
// Try alternative pattern for different flagstat formats or stub files
27+
match = flagstat_content =~ /(\d+) mapped/
28+
if (match.size() == 0) {
29+
// For stub tests, use a default scale factor
30+
log.warn "Could not parse mapped reads from flagstat, using default scale factor of 1.0"
31+
return [meta, bam, 1.0]
32+
}
33+
}
34+
def mapped_reads = match[0][1] as Integer
35+
if (mapped_reads == 0) {
36+
log.warn "Zero mapped reads found in flagstat file, using default scale factor of 1.0"
37+
return [meta, bam, 1.0]
38+
}
2539
def scale_factor = 1000000 / mapped_reads
2640
[meta, bam, scale_factor]
2741
}

0 commit comments

Comments
 (0)