Class: Transrate::ReadMetrics
- Inherits:
-
Object
- Object
- Transrate::ReadMetrics
- Defined in:
- lib/transrate/read_metrics.rb,
ext/transrate/transrate.c
Instance Attribute Summary collapse
-
#bad ⇒ Object
readonly
Returns the value of attribute bad.
-
#fragments ⇒ Object
readonly
Returns the value of attribute fragments.
-
#fragments_mapping ⇒ Object
readonly
Returns the value of attribute fragments_mapping.
-
#good ⇒ Object
readonly
Returns the value of attribute good.
-
#has_run ⇒ Object
readonly
Returns the value of attribute has_run.
-
#p_good_mapping ⇒ Object
readonly
Returns the value of attribute p_good_mapping.
-
#read_length ⇒ Object
readonly
Returns the value of attribute read_length.
-
#supported_bridges ⇒ Object
readonly
Returns the value of attribute supported_bridges.
Instance Method Summary collapse
- #analyse_bam(bamfile, csv_output) ⇒ Object
- #analyse_expression(salmon_output) ⇒ Object
- #analyse_read_mappings(bamfile) ⇒ Object
- #assign_and_quantify(bamfile, threads) ⇒ Object
- #get_bin_path(bin) ⇒ Object
- #get_read_length(left, right) ⇒ Object
- #initial_values ⇒ Object
-
#initialize(assembly) ⇒ ReadMetrics
constructor
A new instance of ReadMetrics.
- #load_executables ⇒ Object
- #populate_contig_data(row) ⇒ Object
- #read_stats ⇒ Object
- #run(left, right, threads: 8) ⇒ Object
- #update_proportions ⇒ Object
Constructor Details
#initialize(assembly) ⇒ ReadMetrics
13 14 15 16 17 18 19 20 21 |
# File 'lib/transrate/read_metrics.rb', line 13 def initialize assembly @assembly = assembly # Transrate::Assembly @mapper = Snap.new @salmon = Salmon.new self.initial_values load_executables @read_length = 100 end |
Instance Attribute Details
#bad ⇒ Object (readonly)
Returns the value of attribute bad.
8 9 10 |
# File 'lib/transrate/read_metrics.rb', line 8 def bad @bad end |
#fragments ⇒ Object (readonly)
Returns the value of attribute fragments.
7 8 9 |
# File 'lib/transrate/read_metrics.rb', line 7 def fragments @fragments end |
#fragments_mapping ⇒ Object (readonly)
Returns the value of attribute fragments_mapping.
7 8 9 |
# File 'lib/transrate/read_metrics.rb', line 7 def fragments_mapping @fragments_mapping end |
#good ⇒ Object (readonly)
Returns the value of attribute good.
8 9 10 |
# File 'lib/transrate/read_metrics.rb', line 8 def good @good end |
#has_run ⇒ Object (readonly)
Returns the value of attribute has_run.
10 11 12 |
# File 'lib/transrate/read_metrics.rb', line 10 def has_run @has_run end |
#p_good_mapping ⇒ Object (readonly)
Returns the value of attribute p_good_mapping.
7 8 9 |
# File 'lib/transrate/read_metrics.rb', line 7 def p_good_mapping @p_good_mapping end |
#read_length ⇒ Object (readonly)
Returns the value of attribute read_length.
11 12 13 |
# File 'lib/transrate/read_metrics.rb', line 11 def read_length @read_length end |
#supported_bridges ⇒ Object (readonly)
Returns the value of attribute supported_bridges.
9 10 11 |
# File 'lib/transrate/read_metrics.rb', line 9 def supported_bridges @supported_bridges end |
Instance Method Details
#analyse_bam(bamfile, csv_output) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/transrate/read_metrics.rb', line 184 def analyse_bam bamfile, csv_output if !File.exist?(csv_output) cmd = "#{@bam_reader} #{bamfile} #{csv_output} 0.7" reader = Cmd.new cmd reader.run if !reader.status.success? msg = "Couldn't get information from bam file: #{bamfile}\n" msg << "#{reader.stdout}\n#{reader.stderr}" raise TransrateError.new msg end end end |
#analyse_expression(salmon_output) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/transrate/read_metrics.rb', line 123 def analyse_expression salmon_output salmon_output.each_pair do |name, expr| contig_name = Bio::FastaDefline.new(name.to_s).entry_id contig_name.gsub!(/;$/, '') # trim trailing semicolon contig = @assembly[contig_name] if expr[:eff_len]==0 coverage = 0 else coverage = expr[:eff_count] * @read_length / expr[:eff_len] end @contigs_uncovered += 1 if coverage < 1 @contigs_lowcovered += 1 if coverage < 10 contig.coverage = coverage.round(2) contig.eff_length = expr[:eff_len] contig.eff_count = expr[:eff_count] contig.tpm = expr[:tpm] end end |
#analyse_read_mappings(bamfile) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/transrate/read_metrics.rb', line 142 def analyse_read_mappings bamfile if File.exist?(bamfile) && File.size(bamfile) > 0 csv_output = "#{File.basename(@assembly.file)}_bam_info.csv" csv_output = File.(csv_output) analyse_bam bamfile, csv_output # open output csv file @potential_bridges = 0 CSV.foreach(csv_output, :headers => true, :header_converters => :symbol, :converters => :all) do |row| populate_contig_data row end @bad = @fragments_mapped - @good else raise TransrateError.new "couldn't find bamfile: #{bamfile}" end salmon_results = "#{File.basename @assembly.file}_quant.sf" if File.exist?(salmon_results) analyse_expression(@salmon.load_expression(salmon_results)) else abort "Can't find #{salmon_results}" end update_proportions end |
#assign_and_quantify(bamfile, threads) ⇒ Object
119 120 121 |
# File 'lib/transrate/read_metrics.rb', line 119 def assign_and_quantify(bamfile, threads) @salmon.run(@assembly, bamfile, threads) end |
#get_bin_path(bin) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/transrate/read_metrics.rb', line 27 def get_bin_path bin which_bin = Cmd.new("which #{bin}") which_bin.run if !which_bin.status.success? raise TransrateIOError.new("ReadMetrics: could not find #{bin} in path") end which_bin.stdout.split("\n").first end |
#get_read_length(left, right) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/transrate/read_metrics.rb', line 100 def get_read_length(left, right) count=0 file = File.open(left.split(",").first) name = file.readline.chomp seq = file.readline.chomp na = file.readline.chomp qual = file.readline.chomp read_length = 0 while name and count < 5000 # get max read length from first 5000 reads read_length = [read_length, seq.length].max name = file.readline.chomp rescue nil seq = file.readline.chomp rescue nil na = file.readline.chomp rescue nil qual = file.readline.chomp rescue nil count+=1 end read_length end |
#initial_values ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/transrate/read_metrics.rb', line 223 def initial_values @fragments = 0 @fragments_mapped = 0 @good = 0 @bad = 0 @bases_uncovered = 0 @contigs_uncovbase = 0 # any base cov < 1 @contigs_uncovered = 0 # mean cov < 1 @contigs_lowcovered = 0 # mean cov < 10 @contigs_segmented = 0 # p_not_segmented < 0.5 end |
#load_executables ⇒ Object
23 24 25 |
# File 'lib/transrate/read_metrics.rb', line 23 def load_executables @bam_reader = get_bin_path 'bam-read' end |
#populate_contig_data(row) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/transrate/read_metrics.rb', line 197 def populate_contig_data row name = Bio::FastaDefline.new(row[:name].to_s).entry_id name.gsub!(/;$/, '') # trim trailing semicolon contig = @assembly[name] contig.p_seq_true = row[:p_seq_true] contig.uncovered_bases = row[:bases_uncovered] @bases_uncovered += contig.uncovered_bases if row[:fragments_mapped] and row[:fragments_mapped] > 1 contig.p_good = row[:good]/row[:fragments_mapped].to_f end contig.p_not_segmented = row[:p_not_segmented] if contig.p_not_segmented < 0.5 @contigs_segmented += 1 end contig.in_bridges = row[:bridges] if row[:bridges] > 1 @potential_bridges += 1 end @fragments_mapped += row[:fragments_mapped] contig.good = row[:good] @good += row[:good] if row[:bases_uncovered] > 0 @contigs_uncovbase += 1 end end |
#read_stats ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/transrate/read_metrics.rb', line 78 def read_stats { :fragments => @fragments, :fragments_mapped => @fragments_mapped, :p_fragments_mapped => @p_fragments_mapped, :good_mappings => @good, :p_good_mapping => @p_good_mapping, :bad_mappings => @bad, :potential_bridges => @potential_bridges, :bases_uncovered => @bases_uncovered, :p_bases_uncovered => @p_bases_uncovered, :contigs_uncovbase => @contigs_uncovbase, :p_contigs_uncovbase => @p_contigs_uncovbase, :contigs_uncovered => @contigs_uncovered, :p_contigs_uncovered => @p_contigs_uncovered, :contigs_lowcovered => @contigs_lowcovered, :p_contigs_lowcovered => @p_contigs_lowcovered, :contigs_segmented => @contigs_segmented, :p_contigs_segmented => @p_contigs_segmented } end |
#run(left, right, threads: 8) ⇒ Object
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 |
# File 'lib/transrate/read_metrics.rb', line 36 def run left, right, threads:8 # check all read files exist [left, right].each do |readfile| raise TransrateIOError.new "Read file is nil" if readfile.nil? readfile.split(",").each do |file| unless File.exist? file raise TransrateIOError.new "ReadMetrics: read file does not " + "exist: #{file}" end end end # estimate max read length @read_length = get_read_length(left, right) # map reads @mapper.build_index(@assembly.file, threads) bamfile = @mapper.map_reads(@assembly.file, left, right, threads: threads) @fragments = @mapper.read_count assigned_bam = "postSample.bam" final_bam = "#{File.basename(bamfile, '.bam')}.assigned.bam" # check for latest files first and create what is needed if !File.exist?(final_bam) if !File.exist?(assigned_bam) assigned_bam = assign_and_quantify(bamfile, threads) end if File.exist?(assigned_bam) File.rename(assigned_bam, final_bam) else logger.error "Couldn't find #{assigned_bam} to rename" raise ReadMetricsError end end # analyse the final mappings analyse_read_mappings final_bam @has_run = true end |
#update_proportions ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/transrate/read_metrics.rb', line 170 def update_proportions nbases = @assembly.n_bases.to_f ncontigs = @assembly.size.to_f @p_bases_uncovered = @bases_uncovered / nbases @p_contigs_uncovbase = @contigs_uncovbase / ncontigs @p_contigs_uncovered = @contigs_uncovered / ncontigs @p_contigs_lowcovered = @contigs_lowcovered / ncontigs @p_contigs_segmented = @contigs_segmented / ncontigs @p_good_mapping = @good.to_f / @fragments.to_f @p_fragments_mapped = @fragments_mapped / @fragments.to_f end |