Class: IndexedAceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/scbi_ace/indexed_ace_file.rb

Direct Known Subclasses

AceParser

Constant Summary collapse

READ_REG_EXP =
/^([^\s]+)\s+([^\s]+)\s+([^\s]+)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ace_file_name) ⇒ IndexedAceFile

Returns a new instance of IndexedAceFile.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/scbi_ace/indexed_ace_file.rb', line 11

def initialize(ace_file_name)
  #@ace_file = File.open(ace_file_name)
  @ace_file_name = ace_file_name

  index_file_name=ace_file_name+'.scbi_index'

  if !File.exists?(index_file_name)
    create_index_file(index_file_name)
  end

  @index_file = File.open(index_file_name)

end

Instance Attribute Details

#ace_file_nameObject (readonly)

Returns the value of attribute ace_file_name.



9
10
11
# File 'lib/scbi_ace/indexed_ace_file.rb', line 9

def ace_file_name
  @ace_file_name
end

Class Method Details

.open(ace_file_name) ⇒ Object



25
26
27
# File 'lib/scbi_ace/indexed_ace_file.rb', line 25

def self.open(ace_file_name)
  return new(ace_file_name)
end

Instance Method Details

#closeObject



146
147
148
149
# File 'lib/scbi_ace/indexed_ace_file.rb', line 146

def close
  #@ace_file.close
  @index_file.close
end

#create_contig_indexesObject



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
# File 'lib/scbi_ace/indexed_ace_file.rb', line 92

def create_contig_indexes
  res=[]
  
  start_pos=0
  end_pos = 0
  contig=nil
  (ace_file=File.open(@ace_file_name)).grep(/^\s*CO\s/) do |line|
    # puts ace_file.pos, line.length
    end_pos = ace_file.pos - line.length

    #puts line
    if line=~/^\s*CO\s+([^\s]+\s)/

      if contig
        #from = last_pos if from == 0

        res << "#{contig}\t#{start_pos}\t#{end_pos-start_pos}"

        # puts "#{contig}\t#{start_pos}\t#{offset}"


      end

      start_pos = end_pos

      contig = $1

    end

    #last_pos = ace_file.pos
    #offset=last_pos - from
  end

  if contig
    end_pos = ace_file.pos
    res << "#{contig}\t#{start_pos}\t#{end_pos-start_pos}"
    # puts "#{contig}\t#{start_pos}\t#{end_pos-start_pos}"
  end



  return res
end

#create_index_file(index_file_name) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/scbi_ace/indexed_ace_file.rb', line 136

def create_index_file(index_file_name)

  puts "creating index file #{index_file_name}"

  @index_file = File.open(index_file_name,'w+')
  @index_file.puts(create_contig_indexes.join("\n"))
  @index_file.close
end

#each_contigObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scbi_ace/indexed_ace_file.rb', line 51

def each_contig
  
  @index_file.pos=0

  begin
    contig = next_contig

    if !contig.nil?
      yield contig
    end
    
  end while !contig.nil?

end

#next_contigObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scbi_ace/indexed_ace_file.rb', line 29

def next_contig
  contig = nil

  if !@index_file.eof?
    line = @index_file.readline

    e=line.chomp

    # parse params
    if e=~ READ_REG_EXP
      length=$3.to_i
      offset=$2.to_i

      #puts line
      contig = File.read(@ace_file_name,length,offset)
    end
  end
  
  return contig
end

#read_contig(contig_name) ⇒ Object

read a contig by its name



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/scbi_ace/indexed_ace_file.rb', line 67

def read_contig(contig_name)
  res = nil
  @index_file.pos=0

  @index_file.grep(/^#{contig_name}\s/) do |line|

    e=line.chomp

    # parse params
    if e=~ READ_REG_EXP

      #get line

      length=$3.to_i #4152
      offset=$2.to_i #3289232
      #puts line
      res=File.read(@ace_file_name,length,offset)

    end

  end

  return res
end