Inspired by a bioinformatics question on Stack Overflow, I wanted to brush up on my Ruby knowledge, since I’ve been meaning to replace my command-line use of Perl by Ruby.
Briefly, the problem description is as follows:
Given is a SAM file. This is a whitespace-delimited file with at least 11 columns. After the 11th column, there can be further optional columns in a specific format1. If there is an optional column starting with
RX:Z:
then remove all dashes in that column.
Here’s my Ruby “one-liner”:
ruby -ane '
$F[11..].grep(/^RX:Z:/).map! {|x| x.gsub!(/-/, "")}
puts $F.join("\t")
' file.sam
This works but I’m wondering if there’s a way of making this more idiomatic and/or more concise. For instance, I’m puzzled by the apparent necessity to manually join the modified $F
back together. There doesn’t seem to be a way of using the -p
flag instead of -n
, and getting the in-place changes to $F
to propagate to $_
. perl -ae
has the same limitation as far as I know.
1 This isn’t relevant to the question but each optional column is roughly described by the regular expression [a-zA-Z][a-zA-Z0-9]:[AifZHB]:\S*
. The actual description is more complicated (for instance, if the character between the two colons is Z
then the field after the second colon is a zero-terminated C-string of printable characters, and notably can include space).