require 'snp-search'
require 'snp_db_connection'
require 'snp_db_models'
require 'snp_db_schema'

gem "slop", "~> 2.4.0"
require 'slop'

opts = Slop.new :help do
  banner "ruby snp-search [OPTIONS]"

  on :V, :verbose, 'Enable verbose mode'
  on :n, :name=, 'Name of database, Required', true 
  on :d, :database_reference_file=, 'Reference genome file, in gbk or embl file format, Required', true
  on :v, :vcf_file=, '.vcf file, Required', true
  on :c, :cuttoff_snp=, 'SNP quality cutoff, (default = 90)', :default => 90 
  on :t, :cuttoff_genotype=, 'Genotype quality cutoff (default = 30)', :default => 30
   
end
opts.parse

  error_msg = ""

  error_msg += "You must supply the -n option, it's a required field\n" unless opts[:name]
  error_msg += "You must supply the -d option, it's a required field\n" unless opts[:database_reference_file]
  error_msg += "You must supply the -v option, it's a required field" unless opts[:vcf_file]
  
  unless error_msg == "" 
    puts error_msg
    puts opts.help unless opts.empty?
    exit
  end
   
  abort "#{opts[:database_reference_file]} file does not exist!" unless File.exist?(opts[:database_reference_file])
  
  abort "#{opts[:vcf_file]} file does not exist!" unless File.exist?(opts[:vcf_file])


# Name of your database 
establish_connection(opts[:name])

# Schema will run here
db_schema

ref = opts[:database_reference_file]

sequence_format = guess_sequence_format(ref)

      case sequence_format
      when :genbank
        sequence_flatfile = Bio::FlatFile.open(Bio::GenBank,opts[:database_reference_file]).next_entry
      when :embl
        sequence_flatfile = Bio::FlatFile.open(Bio::EMBL,opts[:database_reference_file]).next_entry
      else
        puts "All sequence files should be of genbank or embl format"
        exit
      end


# path for vcf file here
vcf_mpileup_file = opts[:vcf_file]

# The populate_features_and_annotations method populates the features and annotations.  It uses the embl/gbk file.
populate_features_and_annotations(sequence_flatfile)

#The populate_snps_alleles_genotypes method populates the snps, alleles and genotypes.  It uses the vcf file, and if specified, the SNP quality cutoff and genotype quality cutoff
populate_snps_alleles_genotypes(vcf_mpileup_file, opts[:cuttoff_snp].to_i, opts[:cuttoff_genotype].to_i)