Created
May 19, 2010 17:49
-
-
Save jtprince/406601 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/ruby | |
require 'zlib' | |
NETWORK_FLOAT = 'g*' | |
NETWORK_DOUBLE = 'G*' | |
LITTLE_ENDIAN_FLOAT = 'e*' | |
LITTLE_ENDIAN_DOUBLE = 'E*' | |
BASE_64 = 'm*' | |
def unpack_mzxml_string(str, precision=32, network_order=true, compressed=false) | |
unpack_code = | |
case precision | |
when 32 ; network_order ? NETWORK_FLOAT : LITTLE_ENDIAN_FLOAT | |
when 64 ; network_order ? NETWORK_DOUBLE : LITTLE_ENDIAN_DOUBLE | |
else | |
raise ArgumentError, "unknown precision (should be 32 or 64): #{precision}" | |
end | |
binary = str.unpack(BASE_64).first | |
binary = Zlib::Inflate.inflate(binary) if compressed | |
binary.unpack(unpack_code) | |
end | |
if $0 == __FILE__ | |
if ARGV.size == 0 | |
puts "usage: #{File.basename(__FILE__)} <base64string>" | |
puts "outputs rows of: m/z<TAB>intensity" | |
puts "currently assumes precision=32 and network order (mzXML default)" | |
exit | |
end | |
str = ARGV.shift | |
array_of_floats = unpack_mzxml_string(str) | |
puts array_of_floats.each_slice(2).map {|pair| pair.join("\t") }.join("\n") | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment