Wednesday, May 30, 2007

Ruby: Separete OA and Control data

Separete OA and Control date from txt files.


#!/usr/bin/env ruby
#divOAvsControl.rb
sfh = File.new('~/myruby/lud/pairs.txt','r')
nameA=Array.new
sfh.each_line {|line| nameA.push line.chomp; }
sfh.close

dataF= Hash.new

fbase='Sta_fs'
fbase=ARGV[0] if ARGV.length>0


File.new(fbase+'.txt','r').each_line {|l|
fname=l.split[0]
dataF[fname.chomp]= l.chomp;
}

dataOA=Array.new
dataC=Array.new

nameA.each {|l|
control=l.split[0]
oa=l.split[1];
oa.gsub!(/\.tiff/,'')
control.gsub!(/\.tiff/,'')
oaData=dataF[oa.chomp]
oaC=dataF[control.chomp]
if oaData!=nil
dataOA.push oaData
else
puts oa.chomp
end
if oaC!=nil
dataC.push oaC
end
}

puts dataOA.length, dataC.length

open(fbase+'_OA.txt', 'w') do |f|
dataOA.each { |i| f << i <<"\n" }
end


open(fbase+'_Control.txt', 'w') do |f|
dataC.each { |i| f << i << "\n" }
end

Example usage:
divOAvsControl.rb Sta_fs.txt
divOAvsControl.rb Str_fs.txt
divOAvsControl.rb Std_fs.txt

Source in: ~/myruby/lud/.