Class: Quadtone::CGATS

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtone/cgats.rb

Defined Under Namespace

Classes: Section

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCGATS

Returns a new instance of CGATS.



70
71
72
# File 'lib/quadtone/cgats.rb', line 70

def initialize
  @sections = []
end

Instance Attribute Details

#sectionsObject

Returns the value of attribute sections.



5
6
7
# File 'lib/quadtone/cgats.rb', line 5

def sections
  @sections
end

Class Method Details

.new_from_file(file) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/quadtone/cgats.rb', line 7

def self.new_from_file(file)
  cgats = new
  section_index = 0
  state = :header
  Path.new(file).readlines.each do |line|
    line.chomp!
    line.sub!(/#.*/, '')
    line.strip!
    next if line.empty?
    section = cgats.sections[section_index] || cgats.add_section
    case state
    when :header
      case line
      when 'BEGIN_DATA_FORMAT'
        state = :data_format
      when 'BEGIN_DATA'
        state = :data
      else
        key, value = line.split(/\s+/, 2)
        if section.header[key]
          if !section.header[key].kind_of?(Array)
            section.header[key] = [section.header[key]]
          end
          section.header[key] << value
        else
          section.header[key] = value
        end
      end
    when :data_format
      case line
      when 'END_DATA_FORMAT'
        state = :header
      else
        line.split(/\s+/).each { |f| section.data_fields << f }
      end
    when :data
      case line
      when 'END_DATA'
        # Emission data (BEGIN_DATA_EMISSION) may come after here, but we don't handle it.
        section_index += 1
        state = :header
      else
        values = line.split(/\s+/).map do |v|
          case v
          when /^-?\d+$/
            v.to_i
          when /^-?\d+\.\d+$/
            v.to_f
          else
            v.to_s
          end
        end
        set = {}
        values.each_with_index do |value, i|
          set[section.data_fields[i]] = value
        end
        section.data << set
      end
    end
  end
  cgats
end

Instance Method Details

#add_sectionObject



74
75
76
77
# File 'lib/quadtone/cgats.rb', line 74

def add_section
  @sections << Section.new
  @sections[-1]
end

#write(io) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/quadtone/cgats.rb', line 79

def write(io)
  @sections.each do |section|
    section.write(io)
    io.puts
  end
  nil
end