Sha256: a1db427192e9f50aea024898344e3a50de32d88e1905ec3af96cc923ab24224a

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Lederhosen
  class CLI

    desc 'otu_filter', 'works like uc_filter but uses an OTU table as input'

    method_option :input, :type   =>  :string, :required => true
    method_option :output, :type  =>  :string, :required => true
    method_option :reads, :type   => :numeric, :required => true
    method_option :samples, :type => :numeric, :required => true

    def otu_filter
      input   = options[:input]
      output  = options[:output]
      reads   = options[:reads]
      min_samples = options[:samples]

      ohai "filtering otu file #{input} (reads = #{reads}, samples = #{min_samples}), saving to #{output}"

      ##
      # Iterate over otu table line by line.
      # Only print if cluster meets criteria
      #
      kept_clusters = 0
      total_reads   = 0
      kept_reads    = 0

      out = File.open(output, 'w')

      File.open(input) do |handle|
        header  = handle.gets.strip
        header  = header.split(',')
        samples = header[1..-1]

        out.puts header.join(',')

        handle.each do |line|
          line       = line.strip.split(',')
          cluster_no = line[0]
          counts     = line[1..-1].collect { |x| x.to_i }

          # should be the same as uc_filter
          if counts.reject { |x| x < reads }.length > min_samples
            out.puts line.join(',')
            kept_clusters += 1
            kept_reads += counts.inject(:+)
          end
          total_reads += counts.inject(:+)
        end
      end

      ohai "kept #{kept_reads} reads (#{kept_reads/total_reads.to_f})."
      ohai "kept #{kept_clusters} clusters."
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lederhosen-0.3.5 lib/lederhosen/tasks/otu_filter.rb