Class: Transrate::Transrater

Inherits:
Object
  • Object
show all
Defined in:
lib/transrate/transrater.rb

Overview

A transrater runs all types of metrics on an assembly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assembly, reference, threads: 1) ⇒ Transrater

A new Transrater

Parameters:

  • assembly (Assembly, String)

    the Assembly or path to the FASTA

  • reference (Assembly, String)

    the reference Assembly or path to the FASTA

  • left (String)

    path to the left reads

  • right (String)

    path to the right reads



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/transrate/transrater.rb', line 23

def initialize(assembly, reference, threads: 1)
  if assembly
    if assembly.is_a?(Assembly)
      @assembly = assembly
    else
      @assembly = Assembly.new(assembly)
    end
    @read_metrics = ReadMetrics.new @assembly
  else
    raise TransrateError.new("assembly is nil")
  end

  if reference
    if reference.is_a?(Assembly)
      @reference = reference
    else
      @reference = Assembly.new(reference)
    end
    @comparative_metrics = ComparativeMetrics.new(@assembly,
                                                  @reference,
                                                  threads)
  end
  @threads = threads
end

Instance Attribute Details

#assemblyAssembly, String (readonly)

Returns an Assembly or the path to an assembly.

Returns:

  • (Assembly, String)

    an Assembly or the path to an assembly



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/transrate/transrater.rb', line 11

class Transrater

  attr_reader :assembly
  attr_reader :read_metrics

  # A new Transrater
  #
  # @param assembly [Assembly, String] the Assembly or path to the FASTA
  # @param reference [Assembly, String] the reference Assembly or
  #   path to the FASTA
  # @param left [String] path to the left reads
  # @param right [String] path to the right reads
  def initialize(assembly, reference, threads: 1)
    if assembly
      if assembly.is_a?(Assembly)
        @assembly = assembly
      else
        @assembly = Assembly.new(assembly)
      end
      @read_metrics = ReadMetrics.new @assembly
    else
      raise TransrateError.new("assembly is nil")
    end

    if reference
      if reference.is_a?(Assembly)
        @reference = reference
      else
        @reference = Assembly.new(reference)
      end
      @comparative_metrics = ComparativeMetrics.new(@assembly,
                                                    @reference,
                                                    threads)
    end
    @threads = threads
  end

  # Run all analyses
  #
  # @param left [String] path to the left reads
  # @param right [String] path to the right reads
  def run left=nil, right=nil
    assembly_metrics
    if left && right
      read_metrics left, right
    end
    comparative_metrics
  end

  # Reduce all metrics for the assembly to a single quality score
  # by taking the geometric mean of the scores for all contigs
  # and multiplying it by the proportion of fragments whose most likely
  # mapping is consistent with the assembly
  # @return [Integer] the assembly score
  def assembly_score
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    return @score_optimiser.raw_score
  end

  def assembly_optimal_score prefix
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    return @score_optimiser.optimal_score prefix
  end

  def assembly_metrics
    @assembly.run unless @assembly.has_run
    @assembly
  end

  def read_metrics(left, right)
    unless @read_metrics.has_run
      @read_metrics.run(left, right, threads: @threads)
    end
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    score, cutoff = @score_optimiser.optimal_score
    @assembly.classify_contigs cutoff
    @read_metrics
  end

  def good_contigs
    {
      :good_contigs => @assembly.good_contigs,
      :p_good_contigs => @assembly.good_contigs/@assembly.size.to_f
    }
  end

  def comparative_metrics
    @comparative_metrics.run unless @comparative_metrics.has_run
    @comparative_metrics
  end

  def all_metrics left, right
    self.run(left, right)
    all = @assembly.basic_stats
    all.merge!(@read_metrics.read_stats)
    all.merge!(@comparative_metrics.comp_stats)
    all[:score] = @score
    all
  end

end

#comparative_metricshash (readonly)

Returns the comparative metrics if they have been calculated.

Returns:

  • (hash)

    the comparative metrics if they have been calculated



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/transrate/transrater.rb', line 11

class Transrater

  attr_reader :assembly
  attr_reader :read_metrics

  # A new Transrater
  #
  # @param assembly [Assembly, String] the Assembly or path to the FASTA
  # @param reference [Assembly, String] the reference Assembly or
  #   path to the FASTA
  # @param left [String] path to the left reads
  # @param right [String] path to the right reads
  def initialize(assembly, reference, threads: 1)
    if assembly
      if assembly.is_a?(Assembly)
        @assembly = assembly
      else
        @assembly = Assembly.new(assembly)
      end
      @read_metrics = ReadMetrics.new @assembly
    else
      raise TransrateError.new("assembly is nil")
    end

    if reference
      if reference.is_a?(Assembly)
        @reference = reference
      else
        @reference = Assembly.new(reference)
      end
      @comparative_metrics = ComparativeMetrics.new(@assembly,
                                                    @reference,
                                                    threads)
    end
    @threads = threads
  end

  # Run all analyses
  #
  # @param left [String] path to the left reads
  # @param right [String] path to the right reads
  def run left=nil, right=nil
    assembly_metrics
    if left && right
      read_metrics left, right
    end
    comparative_metrics
  end

  # Reduce all metrics for the assembly to a single quality score
  # by taking the geometric mean of the scores for all contigs
  # and multiplying it by the proportion of fragments whose most likely
  # mapping is consistent with the assembly
  # @return [Integer] the assembly score
  def assembly_score
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    return @score_optimiser.raw_score
  end

  def assembly_optimal_score prefix
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    return @score_optimiser.optimal_score prefix
  end

  def assembly_metrics
    @assembly.run unless @assembly.has_run
    @assembly
  end

  def read_metrics(left, right)
    unless @read_metrics.has_run
      @read_metrics.run(left, right, threads: @threads)
    end
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    score, cutoff = @score_optimiser.optimal_score
    @assembly.classify_contigs cutoff
    @read_metrics
  end

  def good_contigs
    {
      :good_contigs => @assembly.good_contigs,
      :p_good_contigs => @assembly.good_contigs/@assembly.size.to_f
    }
  end

  def comparative_metrics
    @comparative_metrics.run unless @comparative_metrics.has_run
    @comparative_metrics
  end

  def all_metrics left, right
    self.run(left, right)
    all = @assembly.basic_stats
    all.merge!(@read_metrics.read_stats)
    all.merge!(@comparative_metrics.comp_stats)
    all[:score] = @score
    all
  end

end

#read_metrics(left, right) ⇒ Hash (readonly)

Returns the read metrics if they have been calculated.

Returns:

  • (Hash)

    the read metrics if they have been calculated



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/transrate/transrater.rb', line 11

class Transrater

  attr_reader :assembly
  attr_reader :read_metrics

  # A new Transrater
  #
  # @param assembly [Assembly, String] the Assembly or path to the FASTA
  # @param reference [Assembly, String] the reference Assembly or
  #   path to the FASTA
  # @param left [String] path to the left reads
  # @param right [String] path to the right reads
  def initialize(assembly, reference, threads: 1)
    if assembly
      if assembly.is_a?(Assembly)
        @assembly = assembly
      else
        @assembly = Assembly.new(assembly)
      end
      @read_metrics = ReadMetrics.new @assembly
    else
      raise TransrateError.new("assembly is nil")
    end

    if reference
      if reference.is_a?(Assembly)
        @reference = reference
      else
        @reference = Assembly.new(reference)
      end
      @comparative_metrics = ComparativeMetrics.new(@assembly,
                                                    @reference,
                                                    threads)
    end
    @threads = threads
  end

  # Run all analyses
  #
  # @param left [String] path to the left reads
  # @param right [String] path to the right reads
  def run left=nil, right=nil
    assembly_metrics
    if left && right
      read_metrics left, right
    end
    comparative_metrics
  end

  # Reduce all metrics for the assembly to a single quality score
  # by taking the geometric mean of the scores for all contigs
  # and multiplying it by the proportion of fragments whose most likely
  # mapping is consistent with the assembly
  # @return [Integer] the assembly score
  def assembly_score
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    return @score_optimiser.raw_score
  end

  def assembly_optimal_score prefix
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    return @score_optimiser.optimal_score prefix
  end

  def assembly_metrics
    @assembly.run unless @assembly.has_run
    @assembly
  end

  def read_metrics(left, right)
    unless @read_metrics.has_run
      @read_metrics.run(left, right, threads: @threads)
    end
    if !@score_optimiser
      @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
    end
    score, cutoff = @score_optimiser.optimal_score
    @assembly.classify_contigs cutoff
    @read_metrics
  end

  def good_contigs
    {
      :good_contigs => @assembly.good_contigs,
      :p_good_contigs => @assembly.good_contigs/@assembly.size.to_f
    }
  end

  def comparative_metrics
    @comparative_metrics.run unless @comparative_metrics.has_run
    @comparative_metrics
  end

  def all_metrics left, right
    self.run(left, right)
    all = @assembly.basic_stats
    all.merge!(@read_metrics.read_stats)
    all.merge!(@comparative_metrics.comp_stats)
    all[:score] = @score
    all
  end

end

Instance Method Details

#all_metrics(left, right) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/transrate/transrater.rb', line 108

def all_metrics left, right
  self.run(left, right)
  all = @assembly.basic_stats
  all.merge!(@read_metrics.read_stats)
  all.merge!(@comparative_metrics.comp_stats)
  all[:score] = @score
  all
end

#assembly_metricsObject



79
80
81
82
# File 'lib/transrate/transrater.rb', line 79

def assembly_metrics
  @assembly.run unless @assembly.has_run
  @assembly
end

#assembly_optimal_score(prefix) ⇒ Object



72
73
74
75
76
77
# File 'lib/transrate/transrater.rb', line 72

def assembly_optimal_score prefix
  if !@score_optimiser
    @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
  end
  return @score_optimiser.optimal_score prefix
end

#assembly_scoreInteger

Reduce all metrics for the assembly to a single quality score by taking the geometric mean of the scores for all contigs and multiplying it by the proportion of fragments whose most likely  mapping is consistent with the assembly

Returns:

  • (Integer)

    the assembly score



65
66
67
68
69
70
# File 'lib/transrate/transrater.rb', line 65

def assembly_score
  if !@score_optimiser
    @score_optimiser = ScoreOptimiser.new(@assembly, @read_metrics)
  end
  return @score_optimiser.raw_score
end

#good_contigsObject



96
97
98
99
100
101
# File 'lib/transrate/transrater.rb', line 96

def good_contigs
  {
    :good_contigs => @assembly.good_contigs,
    :p_good_contigs => @assembly.good_contigs/@assembly.size.to_f
  }
end

#run(left = nil, right = nil) ⇒ Object

Run all analyses

Parameters:

  • left (String) (defaults to: nil)

    path to the left reads

  • right (String) (defaults to: nil)

    path to the right reads



52
53
54
55
56
57
58
# File 'lib/transrate/transrater.rb', line 52

def run left=nil, right=nil
  assembly_metrics
  if left && right
    read_metrics left, right
  end
  comparative_metrics
end