Class: ExtractStats

Inherits:
Object
  • Object
show all
Defined in:
lib/seqtrimnext/classes/extract_stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(sequence_readers, params) ⇒ ExtractStats

Returns a new instance of ExtractStats.



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
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 14

def initialize(sequence_readers,params)

  @sequence_lengths = []         #array of sequences lengths
  @length_frequency = []      #number of sequences of each size (frequency)
  @keys={}  #found keys
  @params = params
  @use_qual=sequence_readers.first.with_qual?
  # @params.get_param('use_qual')

  @totalnt=0
  @qv=[]


  @sequence_lengths_stats, @length_frequency_stats, @quality_stats = extract_stats_from_sequences(sequence_readers)


  set_params_and_results

  plot_lengths

  plot_qualities if @use_qual

  print_global_stats

end

Instance Method Details

#add_key(key) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 172

def add_key(key)
  if @keys[key].nil?
   @keys[key]=1
  else
   @keys[key]+=1
  end
end

#add_qv(q, i) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 153

def add_qv(q,i)
  if !@qv[i]
    @qv[i]={:max => 0, :min => 1000000, :nseq => 0, :tot => 0}
  end
  
  # set max
  @qv[i][:tot]+=q
  @qv[i][:nseq]+=1
  @qv[i][:min]=[@qv[i][:min],q].min
  @qv[i][:max]=[@qv[i][:max],q].max
  
end

#extract_qv_from_sequence(qual) ⇒ Object



166
167
168
169
170
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 166

def extract_qv_from_sequence(qual)
  qual.each_with_index do |q,i|
    add_qv(q,i)
  end
end

#extract_stats_from_sequences(sequence_readers) ⇒ Object



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
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 40

def extract_stats_from_sequences(sequence_readers)
  sequence_readers.each do |sequence_reader|


    sequence_reader.each do |name_seq,fasta_seq,qual|
      l = fasta_seq.length

      @totalnt+=l

      #save all lengths
      @sequence_lengths.push l

      # add key value
      add_key(fasta_seq[0..3].upcase)

      # add fasta length
      @length_frequency[fasta_seq.length] = (@length_frequency[fasta_seq.length] || 1 ) + 1

      #extract qv values
      extract_qv_from_sequence(qual) if @use_qual

      # print some progress info
      if (sequence_reader.num_seqs % 10000==0)
        puts "Calculating stats: #{sequence_reader.num_seqs}"
      end

    end
  end

  length_stats = ScbiNArray.to_na(@sequence_lengths)
  length_frequency_stats = ScbiNArray.to_na(@length_frequency.map{|e| e || 0})
  quality_stats = ScbiNArray.to_na(@qv) if @use_qual

  return [length_stats, length_frequency_stats, quality_stats]
end

#get_max_keyObject



180
181
182
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 180

def get_max_key
  return @keys.keys.sort{|e1,e2| @keys[e1]<=>@keys[e2]}.last
end

#plot_lengthsObject



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
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 76

def plot_lengths

  ## PLOT RESULTS
  if !File.exists?('graphs')
    Dir.mkdir('graphs')
  end


  x = []
  y = []

  x =(0..@length_frequency.length-1).collect.to_a
  y = @length_frequency.map{|e| e || 0}

  file_name = 'graphs/size_stats.png'

  p=ScbiPlot::Lines.new(file_name,'Stats of sequence sizes')
  p.x_label= "Sequence length"
  p.y_label= "Number of sequences"

  p.add_x(x)

  p.add_series('sizes', y,'impulses',2)

  p.add_vertical_line('Mode',@length_frequency_stats.fat_mode[0])

  p.add_vertical_line('L',@params.get_param('min_sequence_size_raw').to_i)
  p.add_vertical_line('H',@params.get_param('max_sequence_size_raw').to_i)

  p.do_graph


end

#plot_qualitiesObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 110

def plot_qualities

  if !File.exists?('graphs')
    Dir.mkdir('graphs')
  end
  minimum_qual_value = @params.get_param('min_quality').to_i

  # get qualities values
  x=[]
  y=[]
  min=[]
  max=[]
  qual_limit=[]

  @qv.each_with_index do |e,i|
    x << i
    y << (e[:tot]/e[:nseq])
    min << (e[:min])
    max << (e[:max])
    qual_limit << minimum_qual_value
      # puts "#{i}: #{e[:tot]/e[:nseq]}"
    end

  # make plot of qualities

  file_name='graphs/qualities.png'

	 p=ScbiPlot::Lines.new(file_name,'Stats of sequence qualities')
   p.x_label= "Nucleotide position"
   p.y_label= "Quality value"

    p.add_x(x)

    p.add_series('mean', y)
    p.add_series('min', min)
    p.add_series('max', max)
    p.add_series('qual limit',qual_limit)


    p.do_graph
end


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 213

def print_global_stats

if !@sequence_lengths_stats.nil?
initial_stats={}
initial_stats[:sequence_count] = @sequence_lengths_stats.size
initial_stats[:smallest_sequence_size] = @sequence_lengths_stats.min
initial_stats[:biggest_sequence_size] = @sequence_lengths_stats.max

initial_stats[:min_sequence_size_raw]=@params.get_param('min_sequence_size_raw')
initial_stats[:max_sequence_size_raw]=@params.get_param('max_sequence_size_raw')
initial_stats[:coefficient_of_variance]=@sequence_lengths_stats.variance_coefficient
initial_stats[:nucleotide_count]=@totalnt
initial_stats[:mode_of_sizes]=@length_frequency_stats.fat_mode[0]
initial_stats[:mean_of_sequence_sizes]=@sequence_lengths_stats.mean

initial_stats[:qv]=@qv
initial_stats[:used_key]=get_max_key
initial_stats[:all_keys]=@keys

File.open(File.join(OUTPUT_PATH,'initial_stats.json'),'w') do |f|
  f.puts JSON.pretty_generate(initial_stats)
end

puts "_"*10+ " STATISTICS "+"_"*10
puts "Total sequence count: #{@sequence_lengths_stats.size}"

puts "Smallest sequence: #{initial_stats[:smallest_sequence_size]} nt"
puts "Biggest sequence : #{initial_stats[:biggest_sequence_size]} nt"
puts "Mean of sequence sizes : #{initial_stats[:mean_of_sequence_sizes]} nt"
puts "Mode of sequence sizes : #{initial_stats[:mode_of_sizes]} nt"

puts "Low size limit : #{initial_stats[:min_sequence_size_raw]} nt"
puts "High size limit : #{initial_stats[:max_sequence_size_raw]} nt"

puts "Coefficient of variation: #{initial_stats[:coefficient_of_variance]} %"
puts "Total nucleotide count: #{initial_stats[:nucleotide_count]} nt"

puts "_"*30


end

end

#set_params_and_resultsObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/seqtrimnext/classes/extract_stats.rb', line 184

def set_params_and_results

  if @sequence_lengths.empty? 
    puts "No sequences has been sucessfully readed " 
    return
  end
  
  
  # set limiting parameters
  
  @params.set_param('sequencing_key',get_max_key)
  @params.set_param('all_found_keys',@keys.to_json)

  # sequence min size, is taken directly from params file
  # max sequence limit is calculated here 
  if (@sequence_lengths_stats.variance_coefficient<=10) or (@params.get_param('accept_very_long_sequences').to_s=='true')

    # high size limit is calculated with stats
    @params.set_param('max_sequence_size_raw',(@sequence_lengths_stats.max+10).to_i)

  else # > 10 %

    # high size limit is calculated with stats
    @params.set_param('max_sequence_size_raw',(@sequence_lengths_stats.mean+2*@sequence_lengths_stats.stddev).to_i)
  end


end