#!/usr/bin/env ruby
# framefinder V0.1
# Created by Gisle Vestergaard (gislevestergaard@gmail.com)

require 'pp'
require 'optparse'

ARGV << "-h" if ARGV.empty?

cmd_init = File.basename($0) + " " + ARGV.join(" ")

options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: #{File.basename(__FILE__)} [options]"

  opts.on("-h", "--help", "Display this screen" ) do
    $stderr.puts opts
    exit
  end

  opts.on("-i", "--input_file <file>", String, "vsearch with userfields query+target+id+alnlen+tilo") do |o|
    options[:input_file] = o
  end

end.parse!

File.open(options[:input_file]) do |ios|
  ios.each do |line|
    fields  = line.chomp.split("\t")
    query   = fields[0].to_s
    subject = fields[1].to_s
    id      = fields[2].to_s
    lenght  = fields[3].to_s
    start   = fields[4].to_i
    divide  = start % 3
    if
      divide == 2
        frame = 2
      elsif divide == 1
        frame = 1
      elsif divide == 0
        frame = 3
    end    
    puts query + "\t" + subject + "\t" + id + "\t" + lenght + "\t" + start.to_s + "\t" + frame.to_s
  end
end
