Skip to content

Instantly share code, notes, and snippets.

@sephraim
Created March 17, 2015 17:16
Show Gist options
  • Save sephraim/ae82ec1aeaec11dd6a91 to your computer and use it in GitHub Desktop.
Save sephraim/ae82ec1aeaec11dd6a91 to your computer and use it in GitHub Desktop.
Swap nucleotide sequence to opposite chromosome strand
# Swaps the nucleotide sequence to the opposite strand
#
# This means if the sequence is on the forward strand,
# then the function will return the corresponding sequence
# on the reverse strand, and vice versa.
#
# For example:
# Original: TCCAGACAC
# Swapped: GTGTCTGGA
#
# @param seq [String] Original nucleotide sequence
# @return [String] Nucleotide sequence on opposite strand
def swap_strand(seq)
seq = seq.reverse.upcase.split("")
seq.each_with_index do |base, i|
seq[i] = 'A' if base == 'T'
seq[i] = 'T' if base == 'A'
seq[i] = 'G' if base == 'C'
seq[i] = 'C' if base == 'G'
end
return seq.join("")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment