Class: Inferno::Terminology::Tasks::DownloadUMLS

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

Constant Summary collapse

UMLS_FILE_URLS =
{
  '2019' => 'https://download.nlm.nih.gov/umls/kss/2019AB/umls-2019AB-full.zip',
  '2020' => 'https://download.nlm.nih.gov/umls/kss/2020AB/umls-2020AB-full.zip',
  '2021' => 'https://download.nlm.nih.gov/umls/kss/2021AA/umls-2021AA-full.zip',
  '2022' => 'https://download.nlm.nih.gov/umls/kss/2022AA/umls-2022AA-full.zip',
  '2023' => 'https://download.nlm.nih.gov/umls/kss/2023AA/umls-2023AA-full.zip'
}.freeze
TICKET_GRANTING_TICKET_URL =
'https://utslogin.nlm.nih.gov/cas/v1/api-key'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TempDir

#pipe_files, #umls_dir, #umls_subset_dir, #umls_zip_path, #versioned_temp_dir

Constructor Details

#initialize(version:, apikey:) ⇒ DownloadUMLS

Returns a new instance of DownloadUMLS.



22
23
24
25
# File 'lib/inferno/terminology/tasks/download_umls.rb', line 22

def initialize(version:, apikey:)
  @version = version
  @api_key = apikey
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



20
21
22
# File 'lib/inferno/terminology/tasks/download_umls.rb', line 20

def api_key
  @api_key
end

#versionObject (readonly)

Returns the value of attribute version.



20
21
22
# File 'lib/inferno/terminology/tasks/download_umls.rb', line 20

def version
  @version
end

Instance Method Details

#follow_redirect(location, file_location, ticket, cookie = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/inferno/terminology/tasks/download_umls.rb', line 75

def follow_redirect(location, file_location, ticket, cookie = nil)
  return unless location

  size = 0
  percent = 0
  current_percent = 0
  File.open(file_location, 'w') do |f|
    f.binmode
    block = proc do |response|
      Inferno.logger.info response.header['content-type']
      if response.header['content-type'] == 'application/zip'
        total = response.header['content-length'].to_i
        response.read_body do |chunk|
          f.write chunk
          size += chunk.size
          percent = ((size * 100) / total).round unless total.zero?
          if current_percent != percent
            current_percent = percent
            Inferno.logger.info "#{percent}% complete"
          end
        end
      else
        follow_redirect(response.header['location'], file_location, ticket, response.header['set-cookie'])
      end
    end
    RestClient::Request.execute(
      method: :get,
      url: "#{location}?ticket=#{ticket}",
      headers: { cookie: },
      block_response: block
    )
  end
end

#runObject



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
# File 'lib/inferno/terminology/tasks/download_umls.rb', line 27

def run
  # Adapted from https://documentation.uts.nlm.nih.gov/automating-downloads.html

  FileUtils.mkdir_p(versioned_temp_dir)

  target_file = UMLS_FILE_URLS[version]

  Inferno.logger.info 'Getting Ticket Granting Ticket'
  ticket_granting_ticket_html = RestClient::Request.execute(
    method: :post,
    url: TICKET_GRANTING_TICKET_URL,
    payload: {
      apikey: api_key
    }
  )
  ticket_granting_ticket =
    Nokogiri::HTML(ticket_granting_ticket_html.body).at_css('form').attributes['action'].value

  Inferno.logger.info 'Getting ticket'
  ticket = RestClient::Request.execute(
    method: :post,
    url: ticket_granting_ticket,
    payload: {
      service: target_file
    }
  ).body

  begin
    Inferno.logger.info 'Downloading'
    RestClient::Request.execute(
      method: :get,
      url: "#{target_file}?ticket=#{ticket}",
      max_redirects: 0
    )
  rescue RestClient::ExceptionWithResponse => e
    ticket = RestClient::Request.execute(
      method: :post,
      url: ticket_granting_ticket,
      payload: {
        service: e.response.headers[:location]
      }
    ).body
    target_location = umls_zip_path
    follow_redirect(e.response.headers[:location], target_location, ticket, e.response.headers[:set_cookie])
  end
  Inferno.logger.info 'Finished Downloading!'
end