Class: PluginAdapters

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

Overview

Author: Almudena Bocinos Rioboo

Defines the main methods that are necessary to execute PluginAdapters

Inherit: Plugin

Constant Summary collapse

MIN_ADAPTER_SIZE =

adapters found at end of sequence are even 2 nt wide, cut in 5 because of statistics

5
MIN_FAR_ADAPTER_SIZE =
13

Instance Attribute Summary

Attributes inherited from Plugin

#stats

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#add_stats, #add_text_stats, auto_setup, #can_execute?, check_param, #execute, graph_ignored?, ignored_graphs, #initialize, #merge_hits, #overlapX?, plot_setup

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 157

def self.check_params(params)
  errors=[]  
  
  comment='Blast E-value used as cut-off when searching for adapters'
  # default_value = 1e-6
default_value = 1
params.check_param(errors,'blast_evalue_adapters','Float',default_value,comment)

comment='Minimum required identity (%) for a reliable adapter'
default_value = 95
params.check_param(errors,'blast_percent_adapters','Integer',default_value,comment)

  comment='Adapters can be found at both ends of the sequence. The following variable indicates the number of nucleotides that are allowed for considering the adapters to be located at the right end'
default_value = 9
params.check_param(errors,'max_adapters_to_end','Integer',default_value,comment)
  
  comment='Path for adapters database'
default_value = File.join($FORMATTED_DB_PATH,'adapters.fasta')
params.check_param(errors,'adapters_db','DB',default_value,comment)
  
  return errors
end

.get_graph_filename(plugin_name, stats_name) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 189

def self.get_graph_filename(plugin_name,stats_name)
  return stats_name
  
  # case stats_name
  # when 'adapter_type'
  #   'AB adapters by type'
  # when 'adapter_size'
  #   'AB adapters by size'
  # end
end

.get_graph_title(plugin_name, stats_name) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 180

def self.get_graph_title(plugin_name,stats_name)
  case stats_name
  when 'adapter_type'
    'Adapters by type'
  when 'adapter_size'
    'Adapters by size'
  end
end

.valid_graphsObject



200
201
202
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 200

def self.valid_graphs
  return ['adapter_type']
end

Instance Method Details

#do_blasts(seqs) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 16

def do_blasts(seqs)
   # find MIDS  with less results than max_target_seqs value 
   blast=BatchBlast.new("-db #{@params.get_param('adapters_db')}",'blastn'," -task blastn-short -perc_identity #{@params.get_param('blast_percent_adapters')} -word_size #{MIN_ADAPTER_SIZE}")  
   $LOG.debug('BLAST:'+blast.get_blast_cmd)

   fastas=[]
   
   seqs.each do |seq|
    fastas.push ">"+seq.seq_name
    fastas.push seq.seq_fasta
   end
   
   # fastas=fastas.join("\n")
   
   blast_table_results = blast.do_blast(fastas,:xml)
   
   # puts blast_table_results.inspect
  
   return blast_table_results
end

#exec_seq(seq, blast_query) ⇒ Object



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
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
151
152
153
154
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 53

def exec_seq(seq,blast_query)
 if blast_query.query_id != seq.seq_name
   # raise "Blast and seq names does not match, blast:#{blast_query.query_id} sn:#{seq.seq_name}"
 end
 
  $LOG.debug "[#{self.class.to_s}, seq: #{seq.seq_name}]: looking for adapters into the sequence" 

      
  # blast=BatchBlast.new("-db #{File.join($FORMATTED_DB_PATH,'adapters.fasta')}",'blastn'," -task blastn-short -evalue #{@params.get_param('blast_evalue_adapters')} -perc_identity #{@params.get_param('blast_percent_adapters')} -word_size #{MIN_ADAPTER_SIZE}")  
  
  
  
  # blast with only one sequence, no with many sequences from a database
  #---------------------------------------------------------------------
  
  # blast_table_results = blast.do_blast(seq.seq_fasta)             #rise seq to adapterss  executing over blast
  
   #BlastTableResult.new(res)
   # puts blast_query.inspect
   # puts blast_table_results.inspect
   
   filter_hits(blast_query.hits, seq.seq_fasta.length)
  
  adapters=[]
  # blast_table_results.querys.each do |query|     # first round to save adapters without overlap
    merge_hits(blast_query.hits,adapters)
  # end

  begin 
    adapters2=adapters                            # second round to save adapters without overlap
    adapters = []
    merge_hits(adapters2,adapters)
  end until (adapters2.count == adapters.count)
  
  # puts "MERGED"
  # puts "="*50
  # adapters.each {|a| puts a.inspect}
  
  max_to_end=@params.get_param('max_adapters_to_end').to_i
  # type = 'ActionAbAdapter'
  actions=[]
  adapter_size=0 
  
  #@stats['adapter_size']={}
  adapters.each do |c|                           # adds the correspondent action to the sequence 
    # puts "is the adapter near to the end of sequence ? #{c.q_end+seq.insert_start+max_to_end} >= ? #{seq.seq_fasta_orig.size-1}"   
      adapter_size=c.q_end-c.q_beg+1
      #if ((c.q_end+seq.insert_start+max_to_end)>=seq.seq_fasta_orig.size-1)  
      right_action = true
      
      #if ab adapter is very near to the end of original sequence
     if c.q_end>=seq.seq_fasta.length-max_to_end
        # message = c.subject_id
        message = c.definition
        type = 'ActionRightAdapter'
        ignore=false 
        add_stats('adapter_type','right')
      
      elsif (c.q_beg <= 6) #&& (adapter_size>=MIN_LEFT_ADAPTER_SIZE) #left adapter
        # message = c.subject_id    
        message = c.definition
        type = 'ActionLeftAdapter'
        ignore = false
        right_action = false
        add_stats('adapter_type','left')
      elsif (adapter_size>=MIN_FAR_ADAPTER_SIZE)
        # message = c.subject_id    
        message = c.definition
        type = 'ActionMiddleAdapter'
        ignore = false
        add_stats('adapter_type','middle')
      else
        ignore=true 
      end 
      
      if !ignore
        a = seq.new_action(c.q_beg,c.q_end,type) 
        a.message = message 
        a.reversed = c.reversed 
        if right_action
          a.right_action = true    #mark as rigth action to get the left insert
        else
          a.left_action = true
        end
        actions.push a  
      
        # puts "adapter_size #{adapter_size}"

        #@stats[:adapter_size]={adapter_size => 1} 
        add_stats('adapter_size',adapter_size) 
        add_stats('adapter_id',message)
      end 
  end 
  
  if !actions.empty?
    seq.add_actions(actions)   
    add_stats('sequences_with_adapter','count')
  end
  
  
  #    
end

#filter_hits(hits, end_pos) ⇒ Object

filter hits that are in the middle and do not have a valid length



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 38

def filter_hits(hits,end_pos)
  
  hits.reverse_each do |hit|
   if (hit.q_beg>10) && (hit.q_end < (end_pos-10)) && ((hit.q_end-hit.q_beg+1)<(@params.get_adapter(hit.subject_id).length*0.85).to_i)
     hits.delete(hit)
     # puts "- DELETE #{hit.subject_id} #{(hit.q_end-hit.q_beg+1)}, < #{(@params.get_adapter(hit.subject_id).length*0.85).to_i} - R:#{hit.reversed}"
     # 
     # else
     #   puts " ** ACCEPTED #{hit.subject_id} #{hit.q_beg}>6 and #{hit.q_end}<#{end_pos}-10, #{(hit.q_end-hit.q_beg+1)}, >= #{(@params.get_adapter(hit.subject_id).length*0.85).to_i} - R:#{hit.reversed}"
     #   puts " *** #{hit.inspect}"
   end
  end
  
end