<commit_before># Copyright (c) 2014, <NAME>, OMRF
# Distributed under a BSD 3-Clause
# Full license available in LICENSE.txt distributed with this software

require 'yaml'

class Optical::Configuration
  def self.from_yaml(file_path)
    confy = YAML::load_file(file_path)
    raise "Configuration file missing 'settings' section" unless confy['settings']
    raise "Configguration file mssing 'samples' section" unless confy['samples']
    raise "Configguration file mssing 'samples' section" unless confy['peak_callers']
    samples = {}
    confy['samples'].each do |name,libs|
      samples[name] = Optical::Sample.new(name,libs.map{|l| Optical::Library.new(l)})
    end
    callers = []
    confy['peak_callers'].each do |caller_name,settings|
      settings[:comparisons].each do |comp|
        treatments = comp[:treatments].split(/\s+/).map do |s|
          raise "Invalid sample #{s} in treatment section for #{caller_name}" unless samples.has_key?(s)
          samples[s]
        end
        controls = comp[:controls].split(/\s+/).map do |s|
          raise "Invalid sample #{s} in control section for #{caller_name}" unless samples.has_key?(s)
          samples[s]
        end
        callers << Optical::PeakCaller.create(settings[:algorithm],caller_name,treatments,
                                              controls,settings[:opts])
      end
    end
    self.new(samples,callers,confy['settings'])
  end

  attr_reader :output_base, :skip_fastqc, :bwa_threads, :reference_path, :min_map_quality_score,
    :alignment_filter, :remove_duplicates, :skip_alignment, :skip_visualization,
    :default_fragment_size, :wig_step_size, :genome_table_path, :igv_reference,
    :skip_peak_calling, :gene_peak_neighbor_distance, :ucsc_refflat_path

  attr_accessor :verbose

  def initialize(samples,callers,settings = {})
    @verbose = false
    @samples = samples
    @callers = callers
    @reference_path = settings.fetch(:reference)
    @peak_caller = settings.fetch(:peak_caller)
    @qsub_opts = settings.fetch(:qsub_opts,"").split(" ")
    @use_qsub = settings.fetch(:use_qsub,true)
    @skip_fastqc = settings.fetch(:skip_fastqc,false)
    @skip_alignment = settings.fetch(:skip_alignment,false)
    @skip_visualization = settings.fetch(:skip_visualization,false)
    @skip_peak_calling = settings.fetch(:skip_peak_calling,false)
    @remove_duplicates = settings.fetch(:remove_duplicates,false)
    @bwa_threads = settings.fetch(:bwa_threads,1)
    self.output_base = settings.fetch(:output_base,Dir.getwd)
    @min_map_quality_score = settings.fetch(:min_map_quality_score,0)
    @default_fragment_size= settings.fetch(:default_fragment_size,0)
    @wig_step_size = settings.fetch(:wig_step_size,20)
    self.alignment_filter = settings.fetch(:alignment_filter,"NullFilter")
    @viz_color_list_path = settings.fetch(:viz_color_list,nil)
    @genome_table_path = get_path_conf(:genome_table_path,settings)
    @ucsc_refflat_path = get_path_conf(:ucsc_refflat,settings)
    @igv_reference = get_path_conf(:igv_reference,settings)
    @gene_peak_neighbor_distance = settings.fetch(:gene_peak_neighbor_distance,10000)
  end

  def get_path_conf(key,settings)
    path = settings.fetch(key,nil)
    return nil unless path
    File.expand_path(path) unless nil == path
  end

  def alignment_filter=(filter_klass)
    filter_klass = "Optical::Filters::#{filter_klass}" unless filter_klass =~ /::/
    klass = filter_klass.split("::").inject(Object) {|o,c| o.const_get c}
    @alignment_filter = klass
  end

  def random_visualization_color()
    unless @color_list
      @color_list = IO.readlines(File.expand_path(@viz_color_list_path)).map{|x| x.chomp}<commit_msg>Add settings for to paths to idr scripts<commit_after># Copyright (c) 2014, <NAME>, OMRF
# Distributed under a BSD 3-Clause
# Full license available in LICENSE.txt distributed with this software

require 'yaml'

class Optical::Configuration
  def self.from_yaml(file_path)
    confy = YAML::load_file(file_path)
    raise "Configuration file missing 'settings' section" unless confy['settings']
    raise "Configguration file mssing 'samples' section" unless confy['samples']
    raise "Configguration file mssing 'samples' section" unless confy['peak_callers']
    samples = {}
    confy['samples'].each do |name,libs|
      samples[name] = Optical::Sample.new(name,libs.map{|l| Optical::Library.new(l)})
    end
    callers = []
    confy['peak_callers'].each do |caller_name,settings|
      settings[:comparisons].each do |comp|
        treatments = comp[:treatments].split(/\s+/).map do |s|
          raise "Invalid sample #{s} in treatment section for #{caller_name}" unless samples.has_key?(s)
          samples[s]
        end
        controls = comp[:controls].split(/\s+/).map do |s|
          raise "Invalid sample #{s} in control section for #{caller_name}" unless samples.has_key?(s)
          samples[s]
        end
        callers << Optical::PeakCaller.create(settings[:algorithm],caller_name,treatments,
                                              controls,settings[:opts])
      end
    end
    self.new(samples,callers,confy['settings'])
  end

  attr_reader :output_base, :skip_fastqc, :bwa_threads, :reference_path, :min_map_quality_score,
    :alignment_filter, :remove_duplicates, :skip_alignment, :skip_visualization,
    :default_fragment_size, :wig_step_size, :genome_table_path, :igv_reference,
    :skip_peak_calling, :gene_peak_neighbor_distance, :ucsc_refflat_path, :idr_script,
    :idr_plot_script

  attr_accessor :verbose

  def initialize(samples,callers,settings = {})
    @verbose = false
    @samples = samples
    @callers = callers
    @reference_path = settings.fetch(:reference)
    @peak_caller = settings.fetch(:peak_caller)
    @qsub_opts = settings.fetch(:qsub_opts,"").split(" ")
    @use_qsub = settings.fetch(:use_qsub,true)
    @skip_fastqc = settings.fetch(:skip_fastqc,false)
    @skip_alignment = settings.fetch(:skip_alignment,false)
    @skip_visualization = settings.fetch(:skip_visualization,false)
    @skip_peak_calling = settings.fetch(:skip_peak_calling,false)
    @remove_duplicates = settings.fetch(:remove_duplicates,false)
    @bwa_threads = settings.fetch(:bwa_threads,1)
    self.output_base = settings.fetch(:output_base,Dir.getwd)
    @min_map_quality_score = settings.fetch(:min_map_quality_score,0)
    @default_fragment_size= settings.fetch(:default_fragment_size,0)
    @wig_step_size = settings.fetch(:wig_step_size,20)
    self.alignment_filter = settings.fetch(:alignment_filter,"NullFilter")
    @viz_color_list_path = settings.fetch(:viz_color_list,nil)
    @genome_table_path = get_path_conf(:genome_table_path,settings)
    @ucsc_refflat_path = get_path_conf(:ucsc_refflat,settings)
    @idr_script = get_path_conf(:idr_script,settings)
    @idr_plot_script = get_path_conf(:idr_plot_script,settings)
    @igv_reference = get_path_conf(:igv_reference,settings)
    @gene_peak_neighbor_distance = settings.fetch(:gene_peak_neighbor_distance,10000)
  end

  def get_path_conf(key,settings)
    path = settings.fetch(key,nil)
    return nil unless path
    File.expand_path(path) unless nil == path
  end

  def alignment_filter=(filter_klass)
    filter_klass = "Optical::Filters::#{filter_klass}" unless filter_klass =~ /::/
    klass = filter_klass.split("::").inject(Object) {|o,c| o.const_get c}
    @alignment_filter = klass
  end

  def random_visualization_color()
    unless @color_list
      @color_list = IO.readlines(File.expand_path(@viz_color_list_path)).map{|x| x.chomp}
{'fraction_non_alphanumeric': 0.11823966065747614, 'fraction_numerical': 0.003976670201484623, 'mean_word_length': 4.564159292035399, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1049394', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2437, 'n_words': 432}