Class: SequenceWithAction

Inherits:
Sequence show all
Defined in:
lib/seqtrimnext/classes/sequence_with_action.rb

Overview

Author

Almudena Bocinos Rioboo

This class create the structure to storage the actions associated to a sequence. It allows to add action, and to write at file the actions for every sequence

Inherit

Sequence

Constant Summary collapse

SHOW_QUAL =
false
SHOW_FINAL_INSERTS =
true

Instance Attribute Summary collapse

Attributes inherited from Sequence

#seq_comment, #seq_fasta, #seq_name, #seq_qual, #seq_rejected, #seq_rejected_by_message, #seq_repeated, #seq_reversed

Instance Method Summary collapse

Methods inherited from Sequence

#ns_present?, #seq_is_long_enough, #to_fasta, #to_qual, #xs_present?

Constructor Details

#initialize(seq_name, seq_fasta, seq_qual, seq_comment = '') ⇒ SequenceWithAction

Creates an instance with the structure to storage the actions associated to a sequence



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 21

def initialize(seq_name,seq_fasta,seq_qual, seq_comment = '')
  super

  @actions = []
  @seq_fasta_orig = seq_fasta
  @seq_fasta = seq_fasta
  
  @seq_qual_orig = seq_qual
  @seq_qual = seq_qual     
  
  @insert_start = 0
  @insert_end = seq_fasta.length-1 
  
  @stats={}
  @comments=[]
  
  @file_tags=[]
  
  # for paired ends
  @order_in_tuple=0
  @tuple_id=0
  @tuple_size=0
  @file_tag_tuple_priority=0
  
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def actions
  @actions
end

#insert_endObject

Returns the value of attribute insert_end.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def insert_end
  @insert_end
end

#insert_end_lastObject

Returns the value of attribute insert_end_last.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def insert_end_last
  @insert_end_last
end

#insert_startObject

Returns the value of attribute insert_start.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def insert_start
  @insert_start
end

#insert_start_lastObject

Returns the value of attribute insert_start_last.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def insert_start_last
  @insert_start_last
end

#order_in_tupleObject

Returns the value of attribute order_in_tuple.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def order_in_tuple
  @order_in_tuple
end

#seq_fasta_origObject

Returns the value of attribute seq_fasta_orig.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def seq_fasta_orig
  @seq_fasta_orig
end

#seq_qual_origObject

Returns the value of attribute seq_qual_orig.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def seq_qual_orig
  @seq_qual_orig
end

#statsObject

Returns the value of attribute stats.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def stats
  @stats
end

#tuple_idObject

Returns the value of attribute tuple_id.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def tuple_id
  @tuple_id
end

#tuple_sizeObject

Returns the value of attribute tuple_size.



18
19
20
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 18

def tuple_size
  @tuple_size
end

Instance Method Details

#action_right(a, p_beg, p_end) ⇒ Object



431
432
433
434
435
436
437
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 431

def action_right(a,p_beg,p_end) 
  
    # $LOG.debug " is right action" if ((a.start_pos-p_beg)>(p_end-a.end_pos-1))   
    # $LOG.debug " is left action " if !((a.start_pos-p_beg)>(p_end-a.end_pos-1))   
    return ((a.start_pos-p_beg)>(p_end-a.end_pos-1))
  
end

#add_action(a) ⇒ Object

Adds a new action to the sequence



91
92
93
94
95
96
97
98
99
100
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 91

def add_action(a)
  $LOG.debug("Adding action #{a.type} to #{seq_name}")
        
  @actions.push a
  
  a.apply_to(self)
  
  return a
  
end

#add_actions(actions) ⇒ Object

TODO - Nuevo algoritmo de corte de seqs. 1 - Ordenar seqs con cut=true en izq y der y por posición de ends o begs 2 - Obtener izq.ends.max y der.begs.min 3 - El corte lo definen esos min y max



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
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
189
190
191
192
193
194
195
196
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
222
223
224
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 128

def add_actions(actions)
             
 if !actions.empty?             
    start_pos=0
    end_pos = 0
    cut = false              

    max_end_pos = 0 
    
        
    
    p_beg = @insert_start
    p_end = @seq_fasta.length-1+@insert_start   
    # p_end = @seq_fasta.length-1   
    # puts "ADDING ACTIONS"
    # puts "=" * 50
    # puts 'actions ' + actions.inspect                               
    # para cada accion ordenada por start_pos
    actions.sort{|e,f| e.start_pos<=>f.start_pos}.each do |action| 
      # puts ' current ' + action.inspect
      # puts " UUUUUUUUUU1 "
      # puts "vect in pos #{action.start_pos} #{action.end_pos}" if (action.type=='ActionVectors') 
      # puts " UUUUUUUUUU2 " 
                     
      # puts "ADD ACTION:",action.to_json
                      
      # añadir el inicio del inserto si es necesario         
      if action.start_pos !=0 or action.end_pos != 0
         action.start_pos+=@insert_start    
         action.end_pos+=@insert_start            
      end       
      
      # guardar accion  
      a = add_action(action)
      
      start_pos= a.start_pos                     
      end_pos=a.end_pos             
                                 
                    
      # si hay que cortar y es accion izquierda
      # if (a.cut && ( left_action(@seq_fasta,start_pos-p_beg,end_pos) ||   # action is left in insert
      if (a.cut && a.left_action?(@seq_fasta.length))
      # if (a.cut && ( a.left_action==true || 
      #                       ( a.right_action==false && (left_action(@seq_fasta,start_pos-p_beg,end_pos-p_beg) ||   # action is left in insert
      #                        (start_pos==p_beg)) )))     # action is right in insert but it's continous to the before action  
      #              
          # puts "in seq w action left act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"                                                                        
           # puts "Cut left: #{a.inspect}"
          if (end_pos+1) > p_beg
            p_beg=end_pos+1
          end
          a.left_action=true
          cut=true     
          # puts "in seq w action left act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"                                                                        
          
       # elsif (a.cut && right_action(@seq_fasta,start_pos-p_beg,end_pos)) # action is rigth  in insert
      elsif (a.cut && a.right_action?(@seq_fasta.length))
        # puts "Cut right: #{a.inspect}"
       # elsif (a.cut && (a.right_action==true || right_action(@seq_fasta,start_pos-p_beg,end_pos-p_beg))) # action is rigth  in insert 
          # puts "in seq w action right act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"   
          if (start_pos-1) < p_end
            p_end = start_pos-1   
          end
          a.right_action=true
          cut=true  
          # puts "in seq w action right act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"  
      elsif !a.cut
          # puts "NO cut action"
          if a.right_action?(@seq_fasta.length)
            a.right_action=true
          else
            a.left_action = true
          end

      end  
       
      # puts "p_beg: #{p_beg} , p_end: #{p_end}"
     
   end
   
 
   if cut then 
            
     @seq_fasta = @seq_fasta_orig[p_beg..p_end] 
     # puts @seq_fasta
     @seq_qual = @seq_qual_orig[p_beg..p_end] if !@seq_qual_orig.nil?
                 
     # puts "in seq w action1 #{@insert_start} #{@insert_end}"
     @insert_start = p_beg
     size_cut_right = @insert_end - p_end
     @insert_end -= size_cut_right
     # puts "in seq w action2 #{@insert_start} #{@insert_end}" 
     
     
   end    
  end
end

#add_actions_no_left_rigth_parameters(actions) ⇒ Object

Adds a set of actions to the sequence, update the positon of the cut sequence. # Version without the parameters left_action and rigth action #



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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 230

def add_actions_no_left_rigth_parameters(actions)
             
 if !actions.empty?
    start_pos=0
    end_pos = 0
    cut = false              

    max_end_pos = 0 
    
        
    
    p_beg = @insert_start
    p_end = @seq_fasta.length-1+@insert_start   
    # p_end = @seq_fasta.length-1   
          
    # puts 'actions ' + actions.inspect                               
    # para cada accion ordenada por start_pos
    actions.sort!{|e,f| e.start_pos<=>f.start_pos}.each do |action| 
      # puts ' current ' + action.inspect
      # puts " UUUUUUUUUU1 "
      # puts "vect in pos #{action.start_pos} #{action.end_pos}" if (action.type=='ActionVectors') 
      # puts " UUUUUUUUUU2 " 
                     
      # puts "ADD ACTION:",action.to_json
                      
      # añadir el inicio del inserto si es necesario         
      if action.start_pos !=0 or action.end_pos != 0
         action.start_pos+=@insert_start    
         action.end_pos+=@insert_start            
      end       
      
      # guardar accion  
      a = add_action(action)
      
      start_pos= a.start_pos                     
      end_pos=a.end_pos             
                                 
                     
      # si hay que cortar y es accion izquierda
      # if (a.cut && ( left_action(@seq_fasta,start_pos-p_beg,end_pos) ||   # action is left in insert
      if (a.cut && ( left_action(@seq_fasta,start_pos-p_beg,end_pos-p_beg) ||   # action is left in insert
                    (start_pos==p_beg)) )     # action is right in insert but it's continous to the before action  
          
          puts "in seq w action left act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"                                                                        
          
          p_beg=end_pos+1
          cut=true     
          a.left_action=true
          # puts "in seq w action left act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"                                                                        
          
       # elsif (a.cut && right_action(@seq_fasta,start_pos-p_beg,end_pos)) # action is rigth  in insert 
       elsif (a.cut && right_action(@seq_fasta,start_pos-p_beg,end_pos-p_beg)) # action is rigth  in insert 
          puts "in seq w action right act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"  
          p_end = start_pos-1   
          a.right_action=true
          cut=true  
          # puts "in seq w action right act  #{start_pos} #{end_pos} pbeg #{p_beg} p_end #{p_end}"  
       elsif !a.cut
         puts "NO cut action"
         if right_action(@seq_fasta,start_pos-p_beg,end_pos-p_beg)
           a.right_action=true
         end
         
       end  
       
 
     
   end
   
 
   if cut then 
            
     @seq_fasta = @seq_fasta_orig[p_beg..p_end] 
     @seq_qual = @seq_qual_orig[p_beg..p_end] if !@seq_qual_orig.nil?
                 
     # puts "in seq w action1 #{@insert_start} #{@insert_end}"
     @insert_start = p_beg
     size_cut_right = @insert_end - p_end
     @insert_end -= size_cut_right   
     # puts "in seq w action2 #{@insert_start} #{@insert_end}" 
     
 
     
   end    
  end
end

#add_comment(comment) ⇒ Object



47
48
49
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 47

def add_comment(comment)
  @comments.push comment
end

#add_file_tag(tag_level, tag_value, tag_type, priority = 0) ⇒ Object

add a file tag to sequence



56
57
58
59
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 56

def add_file_tag(tag_level, tag_value, tag_type, priority=0)
  @file_tags<< {:level => tag_level, :name => tag_value, :type=> tag_type}
  @file_tag_tuple_priority=priority
end

#get_actions(type = nil) ⇒ Object

Receive a type of action. Be carefull, type is not a string . Return an array of actions of this type



446
447
448
449
450
451
452
453
454
455
456
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 446

def get_actions(type=nil)
   res = []
             
   @actions.each do |a|
      if a.is_a?(type) or type.nil?
        res.push a
      end
   end
   
   return res
end

#get_comment_lineObject



51
52
53
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 51

def get_comment_line
  return ([@seq_rejected_by_message]+@comments).compact.join(';')
end

#get_file_tag_pathObject

join file tags into a path



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

def get_file_tag_path
  levels=@file_tags.map{|e| e[:level]}.uniq

  dirpath = []
  levels.sort.each do |level|
    # select names from all that are not files
    level_path = @file_tags.select{|e| ((e[:level]==level) && (e[:type]!=:file))}.map{|tag| tag[:name]}
    dirpath << level_path.join('_') if !level_path.empty?
  end
  
  filepath = []
  levels.sort.each do |level|
    # select names from all that are not files
    level_path = @file_tags.select{|e| ((e[:level]==level) && (e[:type]!=:dir))}.map{|tag| tag[:name]}
    filepath << level_path.join('_') if !level_path.empty?
  end
  
  
  filename=filepath.join('_')
  dirname=File.join(dirpath)
  
  
  # puts "#{dirname}, #{filename}"
  
  return [dirname,filename,@file_tag_tuple_priority]
  
end

#get_insertsObject



461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 461

def get_inserts
   
  inserts = get_actions(ActionInsert)
  
  res =[]         
  inserts.each do |insert|
    res.push @seq_fasta_orig[insert.start_pos..insert.end_pos]
  end

  return res 

end

#get_qual_insertsObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 474

def get_qual_inserts
   
  inserts = get_actions(ActionInsert)
  
  res =[]
  
  if @seq_qual_orig
    inserts.each do |insert|
      res.push @seq_qual_orig[insert.start_pos..insert.end_pos]
    end
  end
  
  return res 

end

#insert_boundsObject



490
491
492
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 490

def insert_bounds
  return [@insert_start,@insert_end]
end

#new_action(start_pos, end_pos, action_type) ⇒ Object

Adds a new action to the sequence



103
104
105
106
107
108
109
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 103

def new_action(start_pos,end_pos,action_type)
  
  a = ActionManager.new_action(start_pos,end_pos,action_type)
  
  return a
  
end

#range_inside_action_type?(q_beg, q_end, action_type) ⇒ Boolean

check if range defined by q_beg and q_end is inside some action of the type indicated by action_type

Returns:

  • (Boolean)


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 318

def range_inside_action_type?(q_beg,q_end,action_type)
		res = false

action_list = get_actions(action_type)

action_list.each do |action|
	
	if action.contains_action?(q_beg+@insert_start,q_end+@insert_start,10)
		res = true
		break
	end
end
		
		return res

end

#save_to_fileObject

Saves a sequence with its actions to a file



418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 418

def save_to_file
  File.open("results/#{seq_name}"+".txt", 'w') { |file|
   
    n=1
    
    @actions.each do |a|
      file.puts a.description
      n +=1   
    end
  } 
  
end

#to_jsonObject



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 494

def to_json
   s={}
   s[:seq_name]=@seq_name
   s[:seq_fasta]=@seq_fasta_orig
   s[:seq_qual]=''
   if @seq_qual_orig
     s[:seq_qual]=@seq_qual_orig.join(' ')
   end
   s[:rejected]=@rejected
   

   s[:fasta_inserts]= get_inserts
   s[:qual_inserts]=get_qual_inserts.map { |e| e.join(' ') }
   s[:actions]=[]
   
   @actions.each { |a| s[:actions].push a.to_hash }
   # puts "YAML",s.to_yaml
   return JSON.pretty_generate(s)
end

#to_textObject

Prints a sequence with its actions to a file



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 338

def to_text
  
    output_res=[]
    
    if @seq_rejected
      output_res<< "[#{@tuple_id},#{@order_in_tuple}] Sequence #{seq_name} had the next actions: ".bold.underline + " REJECTED:  #{@seq_rejected_by_message}".red    
      # puts  @seq_name.bold + bold + ' REJECTED BECAUSE ' +@seq_rejected_by_message.bold if @seq_rejected 
    else
      output_res<< "[#{@tuple_id},#{@order_in_tuple}] Sequence #{seq_name} had the next actions: ".bold.underline  
      
    end
    
    n=1
    withMessage = ["ActionIsContaminated","ActionVectors","ActionBadAdapter","ActionLeftAdapter","ActionRightAdapter"]  
    color = red
    
    @actions.sort!{|e,f| e.start_pos<=>f.start_pos}.each do |a|     
      a_type=a.action_type
      color = a.apply_decoration(" EXAMPLE ") 
      color2 =a.apply_decoration(" #{a_type.center(8)} ") 
         
      reversed_str = ''    
      
      if a.reversed
        reversed_str = "   REVERSED  ".bold 
      end
      
      output_res<< " [#{n}] ".bold + color2+ " #{a.title} ".ljust(24).reset  +  " [ " + " #{a.start_pos+1}".center(6) + " , " + "#{a.end_pos+1}".center(6) + " ]" +  clear.to_s + "#{a.message}".rjust(a.message.size+8) + reversed_str
      
      n +=1   
    end
    
    pos = 0
    res = '' 
    
    @seq_fasta_orig.each_char do |c|
      
      @actions.each do |a|
        c= a.decorate(c,pos) 
        
      end 
      
      res += c
      
      pos += 1
    end       
            
    output_res<< res
    
    if SHOW_QUAL and @seq_qual_orig
       res = '' 
       pos=0               
       output_res<< ''
      @seq_fasta_orig.each_char do |c2|
        c=@seq_qual_orig[pos].to_s+' '
        @actions.each do |a|
          c= a.decorate(c,pos) 

        end 
        res += c
        pos += 1
      end
             
      output_res<< res
    end
    
    if SHOW_FINAL_INSERTS      		 
       output_res<<  "INSERT ==>"+get_inserts.join("\nINSERT ==>")
       output_res<< "="*80
    end
    # puts  @seq_name.bold + bold + ' rejected because ' +@seq_rejected_by_message.bold if @seq_rejected                 

    return output_res
end

#to_text_seq_fastaObject



413
414
415
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 413

def to_text_seq_fasta
   return " "*@insert_start +@seq_fasta
end