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

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, check_param, 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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 146

def self.check_params(params)
  errors=[]
  
  comment='Blast E-value used as cut-off when searching for adapters or primers'
  default_value = 1e-6
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='Path for adapter database'
default_value = File.join($FORMATTED_DB_PATH,'adapters.fasta')
params.check_param(errors,'adapters_db','DB',default_value,comment)   
  
  return errors
end

Instance Method Details

#cut_by_right(adapter, seq) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 36

def cut_by_right(adapter,seq)
  
  left_size = adapter.q_beg-seq.insert_start+1
  right_size = seq.insert_end-adapter.q_end+1
  left_size=0 if (left_size<0)
  right_size=0 if (right_size<0) 
  
  return (left_size>(right_size/2).to_i)
  
end

#do_blasts(seqs) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 56

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 -evalue #{@params.get_param('blast_evalue_adapters')} -perc_identity #{@params.get_param('blast_percent_adapters')}")  
   $LOG.info('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)
   
   # puts blast_table_results.inspect
  
   return blast_table_results
end

#exec_seq(seq, blast_query) ⇒ Object



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
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 78

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.info "[#{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')}")  
  
  # 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
  
  #blast_table_results = BlastTableResult.new(res) 
  
  # blast_table_results.inspect         
  
  adapters=[]
  # blast_table_results.querys.each do |query|     # first round to save adapters without overlap
    merge_hits(blast_query,adapters)
  # end

  begin 
    adapters2=adapters                            # second round to save adapters without overlap
    adapters = []
    merge_hits(adapters2,adapters)
  end until (adapters2.count == adapters.count) 

  actions=[] 
  adapter_size=0
  @stats['adapter_size']={}
  adapters.each do |ad|                           # adds the correspondent action to the sequence  
     
     type = get_type_adapter(ad.q_beg,ad.q_end,seq)
     a = seq.new_action(ad.q_beg,ad.q_end,type) 
     # puts " state left_action #{a.left_action} right_action #{a.right_action}"
       

     adapter_size=ad.q_end-ad.q_beg+1 
     
     if cut_by_right(ad,seq)
      
      # puts "action right end1 #{seq.insert_end}"
      
      a.right_action=true    #mark rigth action to get the left insert
    else 
        
      # puts " cut1 by left #{seq.insert_start} ad #{ad.q_beg+seq.insert_start} #{ad.q_end+seq.insert_start}"
               
      a.left_action = true   #mark left action to get the right insert        
      
    end 
    
    a.message = ad.subject_id 
    a.reversed = ad.reversed 
    actions.push a 
    
    # @stats[:adapter_size]={adapter_size => 1} 
    add_stats('adapter_size',adapter_size)  
      
  end 
  seq.add_actions(actions)
  #    
end

#execute(seqs) ⇒ Object

Begins the plugin1’s execution to warn that there are contaminants in the sequence “seq”



48
49
50
51
52
53
54
# File 'lib/seqtrimnext/plugins/plugin_adapters.rb', line 48

def execute(seqs)
  blasts= do_blasts(seqs)
  
  seqs.each_with_index do |s,i|
    exec_seq(s,blasts.querys[i])
  end
end

#get_type_adapter(p_start, p_end, seq) ⇒ Object



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

def get_type_adapter(p_start,p_end,seq)
     #if q_beg is nearer the left, add adapter action by the left,
     #if q_end esta is nearer the right , add adapter action by  the right
     #NOTE: If the adapter is very near from left and rigth,
     #then the sequence isn't valid, because almost sequence is adapter.
     
     
     v1= p_end.to_i
     v2= p_start.to_i   
     
      # puts " startadapter #{v2} endadapter #{v1} insert_start #{seq.insert_start}  insert_end #{seq.insert_end}"
     
      # puts " #{v2+seq.insert_start} <? #{seq.seq_fasta.length - v1 - 1 + seq.seq_fasta_orig.length - seq.insert_end-1}"
     if (v2+seq.insert_start  < (seq.seq_fasta.length - v1 - 1+ seq.seq_fasta_orig.length - seq.insert_end-1)) #IF THE NEAREST ONE IS THE LEFT
       type = "ActionLeftAdapter"          
       
     else
        type = "ActionRightAdapter"
       
     end
     return type
end