Class: SequenceWithAction
- 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
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#insert_end ⇒ Object
Returns the value of attribute insert_end.
-
#insert_end_last ⇒ Object
Returns the value of attribute insert_end_last.
-
#insert_start ⇒ Object
Returns the value of attribute insert_start.
-
#insert_start_last ⇒ Object
Returns the value of attribute insert_start_last.
-
#seq_fasta_orig ⇒ Object
Returns the value of attribute seq_fasta_orig.
-
#seq_qual_orig ⇒ Object
Returns the value of attribute seq_qual_orig.
-
#stats ⇒ Object
Returns the value of attribute stats.
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
- #action_right(a, p_beg, p_end) ⇒ Object
-
#add_action(a) ⇒ Object
Adds a new action to the sequence.
-
#add_actions(actions) ⇒ Object
TODO - Nuevo algoritmo de corte de seqs.
-
#add_actions_no_left_rigth_parameters(actions) ⇒ Object
Adds a set of actions to the sequence, update the positon of the cut sequence.
-
#add_file_tag(tag_level, tag_value, tag_type) ⇒ Object
add a file tag to sequence.
-
#get_actions(type = nil) ⇒ Object
Receive a type of action.
-
#get_file_tag_path ⇒ Object
join file tags into a path.
- #get_inserts ⇒ Object
- #get_qual_inserts ⇒ Object
-
#initialize(seq_name, seq_fasta, seq_qual, seq_comment = '') ⇒ SequenceWithAction
constructor
Creates an instance with the structure to storage the actions associated to a sequence.
- #insert_bounds ⇒ Object
-
#new_action(start_pos, end_pos, action_type) ⇒ Object
Adds a new action to the sequence.
-
#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.
-
#save_to_file ⇒ Object
Saves a sequence with its actions to a file.
- #to_json ⇒ Object
-
#to_text ⇒ Object
Prints a sequence with its actions to a file.
- #to_text_seq_fasta ⇒ Object
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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 20 def initialize(seq_name,seq_fasta,seq_qual, seq_comment = '') super #Tried #if @ns_present then $LOG.debug "The sequence #{seq_name} has N's" else $LOG.debug "The sequence #{seq_name} hasn't N's" end #if @xs_present then $LOG.debug "The sequence #{seq_name} has X's" else $LOG.debug "The sequence #{seq_name} hasn't X's" end @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 #@seq_qual = seq_qual @stats={} @file_tags=[] end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def actions @actions end |
#insert_end ⇒ Object
Returns the value of attribute insert_end.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def insert_end @insert_end end |
#insert_end_last ⇒ Object
Returns the value of attribute insert_end_last.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def insert_end_last @insert_end_last end |
#insert_start ⇒ Object
Returns the value of attribute insert_start.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def insert_start @insert_start end |
#insert_start_last ⇒ Object
Returns the value of attribute insert_start_last.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def insert_start_last @insert_start_last end |
#seq_fasta_orig ⇒ Object
Returns the value of attribute seq_fasta_orig.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def seq_fasta_orig @seq_fasta_orig end |
#seq_qual_orig ⇒ Object
Returns the value of attribute seq_qual_orig.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def seq_qual_orig @seq_qual_orig end |
#stats ⇒ Object
Returns the value of attribute stats.
17 18 19 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 17 def stats @stats end |
Instance Method Details
#action_right(a, p_beg, p_end) ⇒ Object
419 420 421 422 423 424 425 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 419 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
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 79 def add_action(a) $LOG.info("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
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 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 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 116 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 #
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 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 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 218 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_file_tag(tag_level, tag_value, tag_type) ⇒ Object
add a file tag to sequence
45 46 47 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 45 def add_file_tag(tag_level, tag_value, tag_type) @file_tags<< {:level => tag_level, :name => tag_value, :type=> tag_type} 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
434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 434 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_file_tag_path ⇒ Object
join file tags into a path
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 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 50 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] end |
#get_inserts ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 449 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_inserts ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 462 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_bounds ⇒ Object
478 479 480 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 478 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
91 92 93 94 95 96 97 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 91 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
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 306 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_file ⇒ Object
Saves a sequence with its actions to a file
406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 406 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_json ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 482 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_text ⇒ Object
Prints a sequence with its actions to a file
326 327 328 329 330 331 332 333 334 335 336 337 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 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 326 def to_text output_res=[] if @seq_rejected output_res<< " 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<< " 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.}".rjust(a..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_fasta ⇒ Object
401 402 403 |
# File 'lib/seqtrimnext/classes/sequence_with_action.rb', line 401 def to_text_seq_fasta return " "*@insert_start +@seq_fasta end |