Created
April 20, 2013 00:01
-
-
Save itguy51/5424069 to your computer and use it in GitHub Desktop.
Convert binary to Base Pairs (Experimental, for encoding files to DNA - Related to my Internship)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#There are 4 bases in DNA (ATGC) | |
#There are 4 combinations of 2 bits. | |
class String | |
def is_i? | |
!!(self =~ /^[-+]?[0-9]+$/) | |
end | |
end | |
puts "Autodetecting type of input..." | |
inp = ARGV[0] | |
if inp.is_i? | |
puts "Binary to BP" | |
else | |
puts "BP to Binary" | |
end | |
out = "" | |
dec = "" | |
bptobin = { | |
'A' => '00', | |
'T' => '01', | |
'G' => '10', | |
'C' => '11' | |
} | |
bintobp = { | |
'00' => 'A', | |
'01' => 'T', | |
'10' => 'G', | |
'11' => 'C' | |
} | |
if inp.is_i? | |
data = inp.scan(/../) | |
data.each do |item| | |
out = out + bintobp[item] | |
end | |
puts out | |
else | |
out = inp.split(//) | |
out.each do |char| | |
#puts char | |
dec = dec + bptobin[char] | |
end | |
puts dec | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment