Module: Taxonifi::Assessor::RowAssessor

Defined in:
lib/taxonifi/assessor/row_assessor.rb

Overview

Code to assess the metadata properties of a csv row.

!! Note that there are various !! CSV methods for returning row columns !! that have particular attributes

Defined Under Namespace

Classes: RowAssessor, RowAssessorError

Class Method Summary collapse

Class Method Details

.first_available(csv_row, lump = nil) ⇒ Object

Return the first column with data, scoped by lump if provided.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/taxonifi/assessor/row_assessor.rb', line 27

def self.first_available(csv_row, lump = nil)
  if lump.nil?
    csv_row.entries.each do |c,v| 
      return [c,v] if !csv_row[c].nil?
    end
  else
    lump.each do |l|
      return [l, csv_row[l.to_s]] if !csv_row[l.to_s].nil?
    end
  end
end

.geog_headers(headers) ⇒ Object

Return an Array of headers that represent geographic columns.



99
100
101
# File 'lib/taxonifi/assessor/row_assessor.rb', line 99

def self.geog_headers(headers)
  Taxonifi::Lumper::LUMPS[:basic_geog] & headers
end

.intersecting_lumps_with_data(csv_row, lumps_to_try = nil) ⇒ Object

Return lumps for which at least one column has data.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/taxonifi/assessor/row_assessor.rb', line 104

def self.intersecting_lumps_with_data(csv_row, lumps_to_try = nil)
  lumps_to_try ||= Taxonifi::Lumper.intersecting_lumps(csv_row.headers) 
  lumps = [] 
  lumps_to_try.each do |l|  
    has_data = false 
    Taxonifi::Lumper::LUMPS[l].each do |c|
      if !csv_row[c].nil? && !csv_row[c].empty?
        has_data = true 
        break
      end
    end
    has_data && lumps.push(l) 
  end
  lumps
end

.last_available(csv_row, lump = nil) ⇒ Object

Return an Array of [“header”, value] for the last column with data, scoped by lump if provided. If there is nothing available in the scope provided return [nil, nil]



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/taxonifi/assessor/row_assessor.rb', line 41

def self.last_available(csv_row, lump = nil)
  if lump.nil?
    csv_row.entries.reverse.each do |c,v| 
      return [c,v] if !csv_row[c].nil?
    end
  else
    lump.reverse.each do |l|
      return [l, csv_row[l.to_s]] if !csv_row[l.to_s].nil?
    end
  end
  [nil, nil]
end

.lump_name_rank(csv_row) ⇒ Object

Return the rank (symbol) of the taxon name rank. Raises if no name detected.

Raises:



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
# File 'lib/taxonifi/assessor/row_assessor.rb', line 56

def self.lump_name_rank(csv_row)
  # Rather than just check individual columns for data ensure a complete lump is present      
  lumps = intersecting_lumps_with_data(csv_row, [:species, :genera, :higher])
  if lumps.include?(:species) # has to be a species name
    if !csv_row['variety'].nil?
      return :variety
    else
      if csv_row['subspecies'].nil?
        return :species
      else
        return :subspecies
      end
    end
  elsif lumps.include?(:genera)
    if csv_row['subgenus'].nil?
      return :genus
    else
      return :subgenus
    end
  elsif lumps.include?(:higher)
    return Taxonifi::Assessor::RowAssessor.last_available(csv_row, Taxonifi::Lumper::LUMPS[:higher]).first.to_sym
  end

  # this far? bad
  # raise RowAssessor::RowAssessorError

  raise RowAssessorError
end

.lumps_with_data(csv_row, lumps_to_try = nil) ⇒ Object

Return lumps that have data for all columns.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/taxonifi/assessor/row_assessor.rb', line 121

def self.lumps_with_data(csv_row, lumps_to_try = nil)
  lumps_to_try ||= Taxonifi::Lumper.available_lumps(csv_row.headers) # Taxonifi::Lumper::LUMPS.keys 
  lumps = [] 
  lumps_to_try.each do |l|  
    has_data = true 
    Taxonifi::Lumper::LUMPS[l].each do |c|
      if csv_row[c].nil? || csv_row[c].empty?
        has_data = false 
        break
      end
    end
    has_data && lumps.push(l) 
  end
  lumps
end

.rank_headers(headers) ⇒ Object

Return an Array of headers that represent taxonomic ranks.



94
95
96
# File 'lib/taxonifi/assessor/row_assessor.rb', line 94

def self.rank_headers(headers)
  Taxonifi::RANKS & headers
end