Class: AceParser

Inherits:
IndexedAceFile show all
Defined in:
lib/scbi_ace/ace_parser.rb

Constant Summary

Constants inherited from IndexedAceFile

IndexedAceFile::READ_REG_EXP

Instance Attribute Summary

Attributes inherited from IndexedAceFile

#ace_file_name

Instance Method Summary collapse

Methods inherited from IndexedAceFile

#create_contig_indexes, #create_index_file, #each_contig, open

Constructor Details

#initialize(ace_file) ⇒ AceParser

attr_accessor :name,:contigs,:reads_count



12
13
14
15
16
17
18
# File 'lib/scbi_ace/ace_parser.rb', line 12

def initialize(ace_file)
  super(ace_file)

  # @ace_file = File.open(ace_file)
  # reads_count = @ace_file.readline.sub(/^AS\s+\d+\s+/,'')

end

Instance Method Details

#closeObject



20
21
22
23
# File 'lib/scbi_ace/ace_parser.rb', line 20

def close
  super
  # @ace_file.close
end

#next_contigObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/scbi_ace/ace_parser.rb', line 25

def next_contig
  
  res=super()

  if !res.nil?
    contig = parse_contig(res)
  end

  return contig
end

#parse_contig(contig_lines) ⇒ Object

def add_contig(contig_name,contig_length,reads_num,bs_num,orientation)

contig= Contig.new(contig_name,contig_length,reads_num,bs_num,orientation)

# @contigs.push contig
return contig

end



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
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/scbi_ace/ace_parser.rb', line 114

def parse_contig(contig_lines)
  contig = nil
  read = nil
  save_fasta = false
  save_read_fasta = false
  save_qual = false
  contig_fasta = ''
  read_fasta = ''
  contig_qual = ''
  bs = {}

  # my_ace = File.open(@ace_file)
  #         @reads_count = my_ace.readline.sub(/^AS\s+\d+\s+/,'')

  contig_lines.split("\n").each do |line|
    # @ace_file.each do |line|
    line.chomp!
    #---------------------------------------------- contig fasta
    if (save_fasta)
      if !(line =~ /^./)
        contig.add_fasta(contig_fasta)
        save_fasta = false
        contig_fasta = ''
      end
      contig_fasta << line
    end
    #---------------------------------------------- contig qual
    if (save_qual)
      if !(line =~ /^./)
        contig.add_qual(contig_qual)
        save_qual = false
        contig_qual = ''
      end
      contig_qual << line
    end
    #---------------------------------------------- read fasta
    if (save_read_fasta)
      if !(line =~ /^./)
        read.add_fasta(read_fasta)
        save_read_fasta = false
        read_fasta = ''
      end
      read_fasta << line
    end

    #---------------------------------------------- CO
    if (line =~ /^CO\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/)
      contig_name = $1
      contig_length = $2
      reads_num = $3
      bs_num = $4
      orientation = $5
      save_fasta = true
      # contig = add_contig(contig_name,contig_length,reads_num,bs_num,orientation)
      contig= Contig.new(contig_name,contig_length,reads_num,bs_num,orientation)
    end

    #---------------------------------------------- BQ
    if (line =~ /^BQ/)
      save_qual = true
    end

    #---------------------------------------------- AF
    if (line =~ /^AF\s+([^\s]+)\s+([A-Z])\s+([^\s]+)/)
      read_name = $1
      orientation = $2
      start_consensus = $3

      contig.add_read(read_name,orientation,start_consensus)
    end

    #---------------------------------------------- BS
    if (line =~ /^BS\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/)
      padded_start_consensus = $1
      padded_end_consensus = $2
      read_name = $3

      bs['padded_start_consensus'] = padded_start_consensus
      bs['padded_end_consensus'] = padded_end_consensus
      bs['read_name'] = read_name

      read = contig.reads[read_name]
      read.add_bs(bs)
      bs = {}
    end

    #---------------------------------------------- RD
    if (line =~ /^RD\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/)
      read_name = $1
      padded_bases_num = $2
      item_num = $3
      tag_num = $4
      save_read_fasta = true

      read = contig.reads[read_name]
      read.add_data(padded_bases_num,item_num,tag_num)
    end

    #---------------------------------------------- QA
    if (line =~ /^QA\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/)
      qual_clip_start = $1
      qual_clip_end = $2
      align_clip_start = $3
      align_clip_end = $4

      read.add_qual_clipping(qual_clip_start, qual_clip_end, align_clip_start, align_clip_end)
    end
  end

  return contig
end

#read_contig(contig_name) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/scbi_ace/ace_parser.rb', line 36

def read_contig(contig_name)
  res=super(contig_name)
  
  if !res.nil?
    contig = parse_contig(res)
  end

  return contig
end

#write_ace(contigs) ⇒ Object



46
47
48
49
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
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
# File 'lib/scbi_ace/ace_parser.rb', line 46

def write_ace(contigs)

  ace_to_print = []
  reads_count=0
  contigs.map {|c| reads_count+=c.reads.count}

  ace_to_print.push "AS #{contigs.count} #{reads_count}"

  contigs.each do |contig|
    #---------------------------------------------- CO
    ace_to_print.push "CO #{contig.name} #{contig.length} #{contig.reads_num} #{contig.bs_num} #{contig.orientation}"
    ace_to_print.push contig.fasta.scan(/.{1,60}/m)

    #---------------------------------------------- BQ
    ace_to_print.push "\nBQ"
    seq_qual = contig.qual.split(" ")
    count = 0
    qv_line = ''
    seq_qual.each do |qv|
      count+=1
      qv_line << "#{qv} "
      if (count % 60 == 0)
        ace_to_print.push qv_line
        qv_line = ''
      end
    end

    ace_to_print.push ""
    #---------------------------------------------- AF
    af_array = []
    contig.reads.each_value do |read|
      ace_to_print.push "AF #{read.name} #{read.orientation} #{read.start_in_consensus}"
      af_array.push read.name
    end

    #---------------------------------------------- BS
    bs_array = []
    contig.reads.each_value do |read|
      bs_array = bs_array + read.bs
    end

    bs_array.sort!{|a,b| a['padded_start_consensus'].to_i<=>b['padded_start_consensus'].to_i}
    bs_array.each do |bs_hash|
      ace_to_print.push "BS #{bs_hash['padded_start_consensus']} #{bs_hash['padded_end_consensus']} #{bs_hash['read_name']}"
    end

    #----------------------------------------------- RD
    af_array.each do |name|
      contig.reads[name]
      ace_to_print.push "\nRD #{name} #{contig.reads[name].padded_bases_num} #{contig.reads[name].item_num} #{contig.reads[name].tag_num}"
      ace_to_print.push contig.reads[name].fasta.scan(/.{1,60}/m)
      ace_to_print.push "\nQA #{contig.reads[name].qual_clip_start} #{contig.reads[name].qual_clip_end} #{contig.reads[name].align_clip_start} #{contig.reads[name].align_clip_end}"
      ace_to_print.push "DS"
    end
  end

  return ace_to_print.join("\n")

end