Class: Ms::Mascot::Dat::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/ms/mascot/dat/section.rb

Overview

Represents a ‘section’ section of a dat file, formatted like this:

Content-Type: application/x-Mascot; name="parameters"

LICENSE=Licensed to: Matrix Science Internal use only - Frill, (4 processors).
MP=
NM=
COM=Peptide Mass Fingerprint Example
IATOL=
...

Example from mascot data F981122.dat

Direct Known Subclasses

Header, Index, Masses, Parameters, Peptides, Proteins, Query, Summary

Constant Summary collapse

CONTENT_TYPE_REGEXP =

Matches a content-type declaration plus any preceding/following whitespace. The section name is matched in slot 0.

/\s*Content-Type: application\/x-Mascot; name=\"(.*?)\"\n\s*/
TO_S_FORMAT =

A format string used to format parameters as a string.

"%s=%s\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, section_name = self.class.section_name) ⇒ Section

Returns a new instance of Section.



70
71
72
73
# File 'lib/ms/mascot/dat/section.rb', line 70

def initialize(data={}, section_name=self.class.section_name)
  @data = data
  @section_name = section_name
end

Instance Attribute Details

#dataObject (readonly)

A hash of data in self.



65
66
67
# File 'lib/ms/mascot/dat/section.rb', line 65

def data
  @data
end

#section_nameObject (readonly)

The class section_name.



68
69
70
# File 'lib/ms/mascot/dat/section.rb', line 68

def section_name
  @section_name
end

Class Method Details

.parse(str) ⇒ Object

Parses a new instance from str. Section after then content-type declaration are parsed into the parameters hash. Section follow a simple “key=valuen” pattern.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ms/mascot/dat/section.rb', line 33

def parse(str)
  params = {}
  scanner = StringScanner.new(str)
  
  # skip whitespace and content type declaration
  unless scanner.scan(CONTENT_TYPE_REGEXP)
    raise "unknown content type: #{content_type}"
  end
  section_name = scanner[1]
  
  # scan each pair.
  while key = scanner.scan(/[^=]+/)
    scanner.skip(/=/)
    params[key] = scanner.scan(/[^\n]*/)
    scanner.skip(/\n/)
  end
  
  new(params, section_name)
end

.section_nameObject

Returns the name of the section represented by this class. Section names are by default the downcase, unnested class name, for example:

Ms::Mascot::Dat::Section.section_name  # => "parameters"


59
60
61
# File 'lib/ms/mascot/dat/section.rb', line 59

def section_name
  @section_name ||= to_s.split('::').last.downcase
end

Instance Method Details

#to_sObject

Formats self as a string with the content-type header.



76
77
78
79
80
81
82
# File 'lib/ms/mascot/dat/section.rb', line 76

def to_s
  %Q{

Content-Type: application/x-Mascot; name="#{section_name}"

#{data.to_a.collect {|entry| TO_S_FORMAT % entry}.join}}
end