Class: Inferno::Terminology::Tasks::ProcessUMLSTranslations

Inherits:
Object
  • Object
show all
Includes:
DownloadUMLSNotice
Defined in:
lib/inferno/terminology/tasks/process_umls_translations.rb

Instance Method Summary collapse

Methods included from DownloadUMLSNotice

#download_umls_notice

Instance Method Details

#runObject

rubocop:disable Metrics/CyclomaticComplexity



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
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/inferno/terminology/tasks/process_umls_translations.rb', line 11

def run # rubocop:disable Metrics/CyclomaticComplexity
  Inferno.logger.info 'Looking for `./tmp/terminology/MRCONSO.RRF`...'
  input_file = Find.find(File.join(TEMP_DIR, 'terminology')).find { |f| /MRCONSO.RRF$/ =~f }
  if input_file
    start = Time.now
    output_filename = File.join(TEMP_DIR, 'translations_umls.txt')
    output = File.open(output_filename, 'w:UTF-8')
    line = 0
    excluded_systems = Hash.new(0)
    begin
      entire_file = File.read(input_file)
      Inferno.logger.info "Writing to #{output_filename}..."
      current_umls_concept = nil
      translation = Array.new(10)
      entire_file.split("\n").each do |l|
        row = l.split('|')
        line += 1
        concept = row[0]
        if concept != current_umls_concept && !current_umls_concept.nil?
          output.write("#{translation.join('|')}\n") unless translation[1..-2].compact.length < 2
          translation = Array.new(10)
          current_umls_concept = concept
          translation[0] = current_umls_concept
        elsif current_umls_concept.nil?
          current_umls_concept = concept
          translation[0] = current_umls_concept
        end
        code_system = row[11]
        code = row[13]
        translation[9] = row[14]
        case code_system
        when 'SNOMEDCT_US'
          translation[1] = code if row[4] == 'PF' && ['FN', 'OAF'].include?(row[12])
        when 'LNC'
          translation[2] = code
        when 'ICD10CM', 'ICD10PCS'
          translation[3] = code if row[12] == 'PT'
        when 'ICD9CM'
          translation[4] = code if row[12] == 'PT'
        when 'MTHICD9'
          translation[4] = code
        when 'RXNORM'
          translation[5] = code
        when 'CVX'
          translation[6] = code if ['PT', 'OP'].include?(row[12])
        when 'CPT'
          translation[7] = code if row[12] == 'PT'
        when 'HCPCS'
          translation[8] = code if row[12] == 'PT'
        when 'SRC'
          # 'SRC' rows define the data sources in the file
        else
          excluded_systems[code_system] += 1
        end
      end
    rescue StandardError => e
      Inferno.logger.info "Error at line #{line}"
      Inferno.logger.info e.message
    end
    output.close
    Inferno.logger.info "Processed #{line} lines."
    Inferno.logger.info "Excluded code systems: #{excluded_systems}" unless excluded_systems.empty?
    finish = Time.now
    minutes = ((finish - start) / 60)
    seconds = (minutes - minutes.floor) * 60
    Inferno.logger.info "Completed in #{minutes.floor} minute(s) #{seconds.floor} second(s)."
    Inferno.logger.info 'Done.'
  else
    download_umls_notice
  end
end