<commit_before>class Blast::Command

  #############################
  #
  # Options:
  #   Blast::Command.execute(:test_file_path => '/path/to/test.fasta',
  #     :target_file_path => '/path/to/target.fasta',
  #     :evalue => '0.0001',
  #     :output_file_prefix => 'Output1')
  #
  #############################

  cattr_accessor :nr_database_directory
  cattr_accessor :nt_database_directory
  ########################################3
  #  nr.*tar.gz:	Non-redundant protein sequence database with entries from
  #     GenPept, Swissprot, PIR, PDF, PDB, and NCBI RefSeq.
  #     NOTE that nr does NOT contain sequences found in pataa and env_nr databases.
  #
  #  nt.*tar.gz:	Nucleotide sequence database, with entries from all traditional
  #    divisions of GenBank, EMBL, and DDBJ excluding bulk divisions (gss, sts,
  #    pat, est, and htg) as well as wgs entries. Minimally non-redundant.
  #
  ########################################3


  PROGRAMS = ['blastp', 'blastn', 'blastx', 'psitblastn', 'tblastn', 'tblastx'] 

  def self.execute(program, blast_result, params)
    if program.to_sym == :blastall
      self.execute_blastall(blast_result,params)
    elsif program.to_sym == :blastcl3
      self.execute_blastcl3(blast_result,params)
    else
      raise "Invalid program '#{program} called for Blast::Command.execute"
    end
  end

  def self.execute_blastall(blast_result, params)
    required_params = [:test_file_path, :evalue, :output_file_prefix]
    required_params.each do | required_option|
      raise "Blast Error: Option #{required_option} is blank" if params[required_option].blank?
    end
    if params[:evalue]  !~ /^10e-/
      params[:evalue] = "10e-#{params[:evalue]}"
    end
    cli ={}
    cli['-d'] = params[:nt] ? self.nt_database_directory : params[:target_file_path]
    cli['-i'] = params[:test_file_path]
    cli['-e'] = params[:evalue]
    cli['-b'] =  params[:number_of_hits_per_query].blank? ? 20 :  params[:number_of_hits_per_query]
    options =  cli.to_a.join(' ')

    program  = PROGRAMS.include?(params[:program]) ? params[:program] : 'blastn'
    command = " blastall -p #{program}  #{options} "
    puts command
    blast_result.command = command if blast_result
    logger.info("[kenglish] blast command = #{command}")
    output_file_handle = Tempfile.new("#{params[:output_file_prefix].gsub(/ /,"_")}_Blast_Result.txt")

    output_file_handle.close(false)
    command <<  "-o  #{output_file_handle.path} "
    system(*command)
    output_file_handle.close
    output_file_handle

  end<commit_msg>made program part of cli options<commit_after>class Blast::Command

  #############################
  #
  # Options:
  #   Blast::Command.execute(:test_file_path => '/path/to/test.fasta',
  #     :target_file_path => '/path/to/target.fasta',
  #     :evalue => '0.0001',
  #     :output_file_prefix => 'Output1')
  #
  #############################

  cattr_accessor :nr_database_directory
  cattr_accessor :nt_database_directory
  ########################################3
  #  nr.*tar.gz:	Non-redundant protein sequence database with entries from
  #     GenPept, Swissprot, PIR, PDF, PDB, and NCBI RefSeq.
  #     NOTE that nr does NOT contain sequences found in pataa and env_nr databases.
  #
  #  nt.*tar.gz:	Nucleotide sequence database, with entries from all traditional
  #    divisions of GenBank, EMBL, and DDBJ excluding bulk divisions (gss, sts,
  #    pat, est, and htg) as well as wgs entries. Minimally non-redundant.
  #
  ########################################3


  PROGRAMS = ['blastp', 'blastn', 'blastx', 'psitblastn', 'tblastn', 'tblastx'] 

  def self.execute(program, blast_result, params)
    if program.to_sym == :blastall
      self.execute_blastall(blast_result,params)
    elsif program.to_sym == :blastcl3
      self.execute_blastcl3(blast_result,params)
    else
      raise "Invalid program '#{program} called for Blast::Command.execute"
    end
  end

  def self.execute_blastall(blast_result, params)
    required_params = [:test_file_path, :evalue, :output_file_prefix]
    required_params.each do | required_option|
      raise "Blast Error: Option #{required_option} is blank" if params[required_option].blank?
    end
    if params[:evalue]  !~ /^10e-/
      params[:evalue] = "10e-#{params[:evalue]}"
    end
    cli ={}
    program  = PROGRAMS.include?(params[:program]) ? params[:program] : 'blastn'
    cli['-p'] = program
    cli['-e'] = params[:evalue]
    cli['-b'] =  params[:number_of_hits_per_query].blank? ? 20 :  params[:number_of_hits_per_query]
    cli['-d'] = params[:nt] ? self.nt_database_directory : params[:target_file_path]
    cli['-i'] = params[:test_file_path]
    options =  cli.to_a.join(' ')

    command = " blastall #{options} "
    puts command
    blast_result.command = command if blast_result
    logger.info("[kenglish] blast command = #{command}")
    output_file_handle = Tempfile.new("#{params[:output_file_prefix].gsub(/ /,"_")}_Blast_Result.txt")

    output_file_handle.close(false)
    command <<  "-o  #{output_file_handle.path} "
    system(*command)
    output_file_handle.close
    output_file_handle

  end
{'fraction_non_alphanumeric': 0.17772603802871556, 'fraction_numerical': 0.006208769887466045, 'mean_word_length': 3.966281310211946, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5732000', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1696, 'n_words': 358}