Topic: file processing question

Hello,

I want to process a text tab-delimited file based on conditions in fields 2 and 5 (arr[1] and
arr[4]. Not clear on how to get my 'arr.each do |a|' to process based on conditions in arr[1] and arr[4]. Any ideas?

Here is my code:

File.open('/file.txt', 'r').each_line do |l|
arr = l.chomp.split("\t")
out = File.new('/newfile.txt', 'w')
  arr.each do |a|
    if arr[1] =~ /\d+TD/ # the fields here have 5digitsfollowed by TD or TU
      out.puts " #{arr[1]}F\t#{arr[1]}R\t#{arr[1]}P\n"
    elsif arr[1] =~ /\d+TD-2/
      out.puts " #{arr[1]}F2\t#{arr[1]}R2\t#{arr[1]}P2\n"
    next if arr[1] =~/\d+TD/ && arr[4] =~ /No/
    elsif arr[1] =~ /\d+TU/
      out.puts " #{arr[1]}F\t#{arr[1]}R\t#{arr[1]}P\n"
    next if arr[1] =~/\d+TU/ && arr[4] =~ /No/
    else arr[1] =~ /\d+TU-2/
      out.puts " #{arr[1]}F2\t#{arr[1]}R2\t#{arr[1]}P2\n"
    end
  end
  out.close
end

Re: file processing question

Can you post the file you are processing (sample of it) and the file you would like created (sample of that too)?

Right now it seems odd to loop through your arr if you are just looking up arr[1] and arr[4] by hand and never using 'a'.

Re: file processing question

file to be parsed format:
D_Date    Target    Synthesized?    Paid?    Performance
Pilot    10000TU    Yes    SO# 46858    Yes
Pilot    10000TD    Yes    SO# 46858    Yes
Pilot    10001TU    Yes    SO# 46858    Yes
Pilot    10001TD    Yes    SO# 46858    Yes
Pilot    10002TU    Yes    SO# 46858    No
    10002TU-2    Yes    SO# 57555    Yes
Pilot    10002TD    Yes    SO# 46858    Yes
Pilot    10003TU    Yes    SO# 46858    Yes

file to be written format:
10000TUF 10000TUR 10000TUP
10000TDF 10000TDR 10000TDP
.
.
.
10003TUF 10003TUR 10003TUP
10003TDF 10003TDR 10003TDP

Re: file processing question

I can parse the file and see it in stdout, but I am unable to get it to print the stdout in a new file.

Here is my code:
File.open('/home/mgavi/velocigene2/all_oligos.txt', 'r').each_line do |l|
arr = l.chomp.split("\t")
target,perform=arr[1],arr[4]
puts "#{target}F\t#{target}R\t#{target}P\n"
out = File.new('/home/mgavi/velocigene2/du_targets.txt', 'w')
next if target.nil?
next if perform =~ /No/
oligo = out.puts "#{target}F\t#{target}R\t#{target}P\n"
end

Re: file processing question

I figured it out. I was using 'w' instead of 'a' for file permission.