Class: PluginIndeterminations

Inherits:
Plugin
  • Object
show all
Defined in:
lib/seqtrimnext/plugins/plugin_indeterminations.rb

Constant Summary collapse

MAX_RUBBISH =
3

Instance Attribute Summary

Attributes inherited from Plugin

#stats

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#add_plugin_stats, #add_stats, #add_text_stats, auto_setup, #can_execute?, check_param, #do_blasts, #execute, get_graph_filename, get_graph_title, graph_ignored?, ignored_graphs, #initialize, #merge_hits, #overlapX?, plot_setup, valid_graphs

Constructor Details

This class inherits a constructor from Plugin

Class Method Details

.check_params(params) ⇒ Object

Returns an array with the errors due to parameters are missing



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/seqtrimnext/plugins/plugin_indeterminations.rb', line 191

def self.check_params(params)
  errors=[]
  
	comment='Minimum number of Ns within the sequence to be rejected by having an internal segment of indeterminations. Indeterminations at the end of the sequence will be removed regardless of their size and without rejecting the sequence'
default_value = 15
params.check_param(errors,'poly_n_length','Integer',default_value,comment)
  
	comment='Minimum percent of Ns in a segment to be considered a valid indetermination'
default_value = 80
params.check_param(errors,'poly_n_percent','Integer',default_value,comment)

  comment='Maximum distance to the end of the sequence to be considered an internal segment'
  default_value = 15
  params.check_param(errors,'poly_n_max_to_end','Integer',default_value,comment)

	comment='Rejects sequences with indeterminations in the middle'
default_value = 'true'
params.check_param(errors,'middle_indetermination_rejects','String',default_value,comment)
  
  return errors
end

Instance Method Details

#check_poly_length(poly_start, poly_end) ⇒ Object



120
121
122
123
# File 'lib/seqtrimnext/plugins/plugin_indeterminations.rb', line 120

def check_poly_length(poly_start,poly_end)
  #puts "poly_length: #{1+(poly_end-poly_start)} nt"
  return (1+(poly_end-poly_start)) >= @params.get_param('poly_n_length').to_i
end

#check_poly_percent(poly, poly_base) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/seqtrimnext/plugins/plugin_indeterminations.rb', line 125

def check_poly_percent(poly,poly_base)
  
  # count Ts en poly['found']
  s=poly['found']
  ta_count = s.count(poly_base.downcase+poly_base.upcase)
  #puts "poly_percent: #{(ta_count.to_f/s.size.to_f)*100}%"
  res=((ta_count.to_f/s.size.to_f)*100 >= @params.get_param('poly_n_percent').to_i)
  
  return res
end

#exec_seq(seq, blast_query) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/seqtrimnext/plugins/plugin_indeterminations.rb', line 145

def exec_seq(seq,blast_query)

  $LOG.debug "[#{self.class.to_s}, seq: #{seq.seq_name}]: removing indeterminations N+" 
  
  actions=[]
  
 # find simple indeterminations at the beginning of sequence
   match=seq.seq_fasta.match(/^[nN]+/)
  
  if !match.nil?
    found=match[0].length
    
    a = seq.new_action(0,found-1,'ActionIndetermination')
    actions.push a       

    #Add actions  
    seq.add_actions(actions)
    actions=[]
    add_stats('indetermination_size',found)
  
  end
  

  # find simple indeterminations at end of sequence
match=seq.seq_fasta.match(/[nN]+$/)
  
  if !match.nil?
 found=match[0].length
    
    a = seq.new_action(seq.seq_fasta.length-found,seq.seq_fasta.length,'ActionIndetermination')
    a.right_action=true
    actions.push a       

    #Add actions  
    seq.add_actions(actions)
    actions=[]
    add_stats('indetermination_size',found)
	
  end
  
  find_polys('[N]',seq,actions)
  seq.add_actions(actions)
  
end

#find_polys(ta, seq, actions) ⇒ Object

Uses the param poly_at_length to look for at least that number of contiguous A’s



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/seqtrimnext/plugins/plugin_indeterminations.rb', line 28

def find_polys(ta,seq,actions)

  minn = 4
  m2 = 1#(minn/2) 
  m4 = (minn/4)
  r = [-1,0,0]
  re2 = /((#{ta}{#{m2},})(.{0,3})(#{ta}{#{1},}))/i
  
  
  type='ActionIndetermination'
  poly_base = 'N'
  
  matches = re2.global_match(seq.seq_fasta,3)

matches2 = /[^N]N$/.match(seq.seq_fasta)


  # HASH
  polys = []

  # crear una region poly nuevo
  poly = {}
  #i=0

  matches.each do |pattern2|

    #puts pattern2.match[0]
      m_start = pattern2.match.begin(0)+pattern2.offset
      m_end = pattern2.match.end(0)+pattern2.offset-1   
      
	 #puts "MATCH: #{m_start} #{m_end}"

     # does one exist in polys with overlap?

     # yes => group it, updated end

     # no => one new

     if (e=overlap(polys,m_start,m_end))
       
       e['end'] = m_end
       e['found'] = seq.seq_fasta.slice(e['begin'],e['end']-e['begin']+1)
       
     else
        poly={}
        poly['begin'] = m_start
        poly['end'] = m_end #  the next pos to pattern's end
        poly['found'] = seq.seq_fasta.slice(poly['begin'],poly['end']-poly['begin']+1)
        polys.push poly
     end
     
  end  
  
  
  poly_size=0 

  polys.each do |poly|
    #puts "NEW POLY: #{poly.to_json}"
  		
  		if poly_near_end(poly['end'],seq.seq_fasta) # near right side
  		  #puts "near end"
      a = seq.new_action(poly['begin'],poly['end'],type)
      a.right_action=true
      actions.push a
      
      poly_size=poly['end']-poly['begin']+1
      add_stats('size',poly_size)
    else
    		#puts "far of end"
     if check_poly_length(poly['begin'],poly['end']) and (check_poly_percent(poly,poly_base))
     		#puts "ok"
	    a = seq.new_action(poly['begin'],poly['end'],type)
      a.right_action=true
      actions.push a
  
        if @params.get_param('middle_indetermination_rejects').to_s=='true'
        seq.seq_rejected=true 
          seq.seq_rejected_by_message='Indeterminations in middle of sequence'
        end
      
      poly_size=poly['end']-poly['begin']+1
      add_stats('size',poly_size)
     end
    
    
    end
  end 
  
  
end

#overlap(polys, mi_start, mi_end) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/seqtrimnext/plugins/plugin_indeterminations.rb', line 14

def overlap(polys,mi_start,mi_end)
	
	# overlap = polys.find{|e| ( mi_start < e['end'])}
	overlap = polys.find{|e| ( overlapX?(mi_start,mi_end, e['begin'],e['end']) )}
	# puts " Overlap #{mi_start} #{mi_end} => #{overlap}"
	
	return overlap
end

#poly_near_end(pos, seq_fasta) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/seqtrimnext/plugins/plugin_indeterminations.rb', line 136

def poly_near_end(pos,seq_fasta)

	max_to_end = @params.get_param('poly_n_max_to_end').to_i
	
	res = (pos>=(seq_fasta.length-max_to_end))
	
end