Robert Dober
7/18/2007 11:11:00 AM
On 7/18/07, Marc Hoeppner <marc.hoeppner@molbio.su.se> wrote:
> Andreas Schwarz wrote:
> > I'd throw it all into one big ugly regex:
> > s.match(/Query=
> > (.+?\|.+?)\|.+?\(bits\)\s+Value\s+(.+?)\s+/m).to_a[1..2].join(' ')
> > => "gi|23510597 At2g21510"
>
> Thanks for the suggestion! However, if someone has a suggestion
> regarding the following code and how to fix it, I'd be happy...its
> almost working and I just need to understand why it is behaving a bit
> odd. So here is the code.
>
> def stripname line
> s = line.gsub(/Query=/, '')
> u = s.gsub(/\|emb.*/, '')
> end
>
>
> count = 0
> gene = nil
> store = Array.new
>
> ARGF.each do |l|
>
> store.push(l) unless count.zero?
> count = [0, count-1].max
>
> if l.match(/^Query=/)
> gene = stripname l
>
> elsif l.match(/^Sequences/)
> count = 2
> puts "#{gene.strip} #{store.last.to_s.strip}"
> else
>
> end
> end
>
>
>
> Problem:
>
> Reads: If line is found that starts with "Query=", use the method
> stripname on it and store it in the variable "gene". Go further, and if
> you find a line that starts with "Sequence", use the above specified
> procedure "count". Now this is the problem right now. After I wasnt able
> to figure out to get the formatting right, I decided to stick to the
> skip-line approach and instead of having it printed, to store it in an
> array. From there I simply read the last entry.
>
> BUT: instead of printing every stored hit to the corresponding "gene",
> it shifts the whole thing 1 line. So that each "gene" is associated with
> the "best hit" of the previous match to "Query=".
Are you pushing before or after you use the last element of the array?
But you should go back to your original idea, which works just fine,
now that you have discovered #strip, before my post :)
Now this is a Ruby ML, right, so maybe you would accept that I Rubyish
the code a little bit ;)
gi = nil
ARGF.each do |line|
case line
when /Query=\s*(gi\|.*?)\|/
gi = $1
when /Sequence/
puts gi.strip << " " << (1..2).map{ ARGF.readline }.last.strip
end
end
HTH
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck