Class: Taxonifi::Export::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/taxonifi/export/format/base.rb

Overview

All export classes inherit from Taxonifi::Export::Base

Direct Known Subclasses

OboNomenclature, Prolog, SpeciesFile

Constant Summary collapse

TAXRANKS =

Hash. An index of taxonomic ranks. See phenoscape.svn.sourceforge.net/svnroot/phenoscape/trunk/vocab/taxonomic_rank.obo Site: www.phenoscape.org/wiki/Taxonomic_Rank_Vocabulary Values of -1 have no correspondance in that ontology. Not all values are supported. Not all values are included.

{
  'taxonomic_rank' =>          0,
  'variety'        =>          16,
  'bio-variety'    =>          32,
  'subspecies' =>              23,
  'form' =>                    26,
  'species' =>                 5,
  'species complex' =>         12,
  'species subgroup' =>        11,      
  'species group' =>           10,     
  'species series' =>          -1,      
  'series'  =>                 31,
  'infragenus' =>              43,  
  'subgenus' =>                9,
  'genus' =>                   5,
  'genus group' =>             -1,   
  'subtribe' =>                28,
  'tribe' =>                   25,
  'supertribe' =>              57,  
  'infrafamily' =>             41,   
  'subfamily' =>               24, 
  'subfamily group' =>         -1,       
  'family' =>                  4,
  'epifamily' =>               -1, 
  'superfamily' =>             18,  
  'superfamily group' =>       -1,         
  'subinfraordinal group' =>   -1,             
  'infraorder' =>              13,  
  'suborder' =>                14,
  'order' =>                   3,
  'mirorder' =>                -1,
  'superorder' =>              20,  
  'magnorder' =>               -1,
  'parvorder' =>               21, 
  'cohort' =>                  -1,
  'supercohort' =>             -1,   
  'infraclass' =>              19,  
  'subclass' =>                7,
  'class' =>                   2,
  'superclass' =>              15,  
  'infraphylum' =>             40,   
  'subphylum' =>               8, 
  'phylum' =>                  1,
  'superphylum' =>             27,   
  'infrakingdom' =>            44,   
  'subkingdom' =>              29,  
  'kingdom' =>                 17,
  'superkingdom' =>            22,    
  'life' =>                    -1,
  'unknown' =>                 -1,
  'section' =>                 30
}
EXPORT_BASE =
File.expand_path(File.join(Dir.home(), 'taxonifi', 'export'))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



72
73
74
75
76
77
78
79
80
# File 'lib/taxonifi/export/format/base.rb', line 72

def initialize(options = {})
  opts = {
    :base_export_path => EXPORT_BASE,
    :export_folder => '.'
  }.merge!(options)

  @base_export_path = opts[:base_export_path]  
  @export_folder = opts[:export_folder] 
end

Instance Attribute Details

#base_export_pathObject

String. Defaults to EXPORT_BASE.



67
68
69
# File 'lib/taxonifi/export/format/base.rb', line 67

def base_export_path
  @base_export_path
end

#export_folderObject

String. The folder to dump output files to, subclassess contain a reasonably named default.



70
71
72
# File 'lib/taxonifi/export/format/base.rb', line 70

def export_folder
  @export_folder
end

Instance Method Details

#configure_foldersObject

Recursively (over)write the the export path.



94
95
96
# File 'lib/taxonifi/export/format/base.rb', line 94

def configure_folders
  FileUtils.mkdir_p export_path
end

#exportObject

Subclassed models expand on this method, typically writing files to the folders created here.



89
90
91
# File 'lib/taxonifi/export/format/base.rb', line 89

def export
  configure_folders
end

#export_pathObject

Return the path to which exported files will be written.



83
84
85
# File 'lib/taxonifi/export/format/base.rb', line 83

def export_path
  File.expand_path(File.join(@base_export_path, @export_folder))
end

#new_output_file(filename = 'foo') ⇒ Object

TODO: Used?! Returns a new writeable File under the



108
109
110
# File 'lib/taxonifi/export/format/base.rb', line 108

def new_output_file(filename = 'foo')
  File.new( File.expand_path(File.join(export_path, filename)), 'w+')
end

#sanitize(value) ⇒ Object

TODO: Move to SQL/String library. Returns a String with quotes handled for SQL.



133
134
135
# File 'lib/taxonifi/export/format/base.rb', line 133

def sanitize(value)
  value.to_s.gsub(/'/,"''")
end

#sql_insert_statement(tbl = nil, values = {}) ⇒ Object

TODO: Move to a SQL library. Returns a String, an INSERT statement derived from the passed values Hash.



115
116
117
118
# File 'lib/taxonifi/export/format/base.rb', line 115

def sql_insert_statement(tbl = nil, values = {})
  return "nope" if tbl.nil?
  "INSERT INTO #{tbl} (#{values.keys.sort.join(",")}) VALUES (#{values.keys.sort.collect{|k| sqlize(values[k])}.join(",")});"
end

#sqlize(value) ⇒ Object

TODO: Move to a SQL library. Returns a String that has been SQL proofed based on its class.



122
123
124
125
126
127
128
129
# File 'lib/taxonifi/export/format/base.rb', line 122

def sqlize(value)
  case value.class.to_s
  when 'String'
    "'#{sanitize(value)}'"
  else
    value
  end
end

#write_file(filename = 'foo', string = nil) ⇒ Object

Write the string to a file in the export path.

Raises:



99
100
101
102
103
104
# File 'lib/taxonifi/export/format/base.rb', line 99

def write_file(filename = 'foo', string = nil)
  raise ExportError, 'Nothing to export for #{filename}.' if string.nil? || string == ""
  f = File.new( File.expand_path(File.join(export_path, filename)), 'w+')
  f.puts string
  f.close
end