-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_bam_paths.sh
More file actions
executable file
·42 lines (32 loc) · 952 Bytes
/
fix_bam_paths.sh
File metadata and controls
executable file
·42 lines (32 loc) · 952 Bytes
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
#!/usr/bin/env bash
# fix_bam_paths.sh
# Safely rewrites BAM column (last column) without disturbing any other fields.
input="$1"
awk -F'\t' -v OFS='\t' '
NR==1 {
# Print header unchanged
print $0
next
}
{
# BAM is ALWAYS the last column
bamCol = NF
sample = $1
readset = $2
# Readset prefix (before first dot)
nrs = split(readset, parts, /\./)
rs_prefix = parts[1]
# Only change BAM when sample != prefix and Readset contains a dot
if (nrs >= 2 && sample != rs_prefix) {
# Extract filename from existing BAM
nb = split($bamCol, bparts, "/")
bamfile = bparts[nb]
# Fallback filename if empty
if (bamfile == "" || $bamCol ~ /\/$/)
bamfile = rs_prefix ".sorted.bam"
# Rewrite BAM to raw_reads/<prefix>/<filename>
$bamCol = "/lb/project/mugqic/projects/MOH/MAIN/raw_reads/" rs_prefix "/" bamfile
}
print $0
}
' "$input"