Class: FastaQualFile

Inherits:
Object
  • Object
show all
Defined in:
lib/scbi_fasta/fasta_qual_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fasta_file_name, qual_file_name = '', qual_to_array = false) ⇒ FastaQualFile


Initialize instance




24
25
26
27
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
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 24

def initialize(fasta_file_name,qual_file_name='',qual_to_array=false)

	
	if !File.exist?(fasta_file_name)
  	raise "File #{fasta_file_name} doesn't exists" 
	end
	
	@with_qual = true
	
	if qual_file_name.nil? or qual_file_name.empty? or !File.exist?(qual_file_name)
		@with_qual = false
    #raise "File #{qual_file_name} doesn't exists" 
	end
# puts "With_qual #{fasta_file_name}?: #{@with_qual}"
		
    @num_seqs = 0 ;      
    
    @seq_name = '';      
    @seq_comment='';
    @file_fasta = File.open(fasta_file_name) ;
    @end_fasta=false;
    
    if @with_qual  		
    
      @qual_to_array = qual_to_array

     @seq_qual_name='';
     @file_qual = File.open(qual_file_name) ;
     @end_qual=false;
    end
    
end

Instance Attribute Details

#end_fastaObject

Returns the value of attribute end_fasta.



19
20
21
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 19

def end_fasta
  @end_fasta
end

#num_seqsObject

Returns the value of attribute num_seqs.



19
20
21
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 19

def num_seqs
  @num_seqs
end

Instance Method Details

#closeObject



57
58
59
60
61
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 57

def close
    @file_fasta.close
    
    @file_qual.close if @with_qual
end

#eachObject


Scans all sequences




67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 67

def each
      
  rewind

 n,f,q,c = next_seq
  while (!n.nil?)
 
			if @with_qual

				yield(n,f,q,c)
			else
			  yield(n,f,q)
			end
			
				n,f,q,c=next_seq
  end

	rewind
	
end

#next_seqObject


Scans a file, firing events to process content




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
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 109

def next_seq
  
  #init variables
  res = nil
  
 # envia on_process_sequence
  if ((!@end_fasta) && (!@with_qual or !@end_qual))

     name_f,fasta,comment=read_fasta 
	    
      if @with_qual
			  
			  name_q,qual=read_qual 
			  
          if (name_f!=name_q)
					 raise DifferentNamesException.new, "Sequence(<#{name_f}>) and qual(<#{name_q}>) names differs. Sequence will be ignored."
           else
					@num_seqs=@num_seqs+1
		
					#storage a string of qualities in an array of qualities
		 	    # a_qual = qual.strip.split(/\s/).map{|e| e.to_i}
					# if ((!a_qual.nil?) && (!a_qual.empty?) && (fasta.size==a_qual.size))						

					if fasta.length == qual.count(' ') + 1
						if @qual_to_array
							a_qual = qual.strip.split(/\s/).map{|e| e.to_i}
							res =[name_f,fasta,a_qual,comment]
						else
							res =[name_f,fasta,qual,comment]
						end
				
					else #if (!a_qual.empty?)
						 raise DifferentSizesException.new,  "Sequence(<#{name_f}>) and qual(<#{name_q}>) sizes differs (#{fasta.length},#{qual.count(' ')} ). Sequence will be ignored."
					end 
				 
          end
		 
      else # without qual				 
		 		res =[name_f,fasta,comment]
      end
  end
	
	return res
end

#rewindObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 89

def rewind
   
   @num_seqs = 0 ;      
   
   @seq_name = '';      
   @seq_comment= '';
   @file_fasta.pos=0
   @end_fasta=false;

   if @with_qual
		 @seq_qual_name='';
		 @file_qual.pos=0
		 @end_qual=false;   
   end
  
end

#with_qual?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/scbi_fasta/fasta_qual_file.rb', line 154

def with_qual?
  @with_qual
end