Skip to main content

Posts

Showing posts with the label ruby

Merging multiple files columnwise

Here's a little parser script, to merge multiple files. Each file has a header and a time course. I need all time courses in a single file, next to each other. basedir = "./MassSpectrum/" allData = [] # ms data from all files filelist = Dir.new(basedir).entries filelist.each {|f| if f !~ /\.txt/ # f is not a data file next end data = [] # ms data from this files massToCharge = 50 readData = false # set to true once we are reading in ms data File.open( basedir+f).each { |line| case line when /^SPECTRUM/ next when /raw/ data.push line.chop.rstrip puts ":#{line.chop.rstrip}:" next when /^Mass/ puts "starting" readData= true next end if readData # we are reading ms data, should go from 50 to 650 arr = line.split while arr.first.to_i != massToCharge # a mass to charge ratio was skipped data.push 0 massToCharge = massToCharge + 1 end ...

I wish I'd have more time reading

Weekend Reading: Ruby Best Practice Chapter 1, Driving Code Through Tests Yes, I'm a little behind with my reading, but it was definitely worth while. I was happy to find such a good chapter on testing, because most other books only cover trivial testing, but they don't show how to build useful tests. What did I learn? * use single tests for every different case. This results in better readability, and in case an assertion fails, I know exactly in which scenario it failed and all assertions in different tests, will be tried, so I'll know right away if all of them failed or just a particular one * use tests more as a way to drive your software, not only as regression tests * there's a when ? Yes, switch-case is a case-when in Ruby * goal for next time Ruby programming: force myself to use a stub/mock * embedding tests in library files For my math programming (Macaulay2) I rely heavily on tests: for every little function a handful of tests, and a few functi...