module SequenceServer # Module to contain methods for generating sequence retrieval links. module Links require 'erb' # Provide a method to URL encode _query parameters_. See [1]. include ERB::Util # alias_method :encode, :url_encode NCBI_ID_PATTERN = /gi\|(\d+)\|/ # Your custom method should have following pattern: # # Input # ----- # sequence_id: Array of sequence ids # # Return # ------ # The return value should be a Hash: # # { # # Required. Display title. # :title => "title", # # # Required. Generated url. # :url => url, # # # Optional. Left-right order in which the link should appear. # :order => num, # # # Optional. Classes, if any, to apply to the link. # :class => "class1 class2", # # # Optional. Class name of a FontAwesome icon to use. # :icon => "fa-icon-class" # } # # If no url could be generated, return nil. # # Helper methods # -------------- # # Following helper methods are available to help with link generation. # # encode: # URL encode query params. # # Don't use this function to encode the entire URL. Only params. # # e.g: # sequence_id = encode sequence_id # url = "http://www.ncbi.nlm.nih.gov/nucleotide/#{sequence_id}" # # querydb: # Returns an array of databases that were used for BLASTing. # # which_blastdb: # Returns the database from which the given hit came from. # # e.g: # # hit_database = which_blastdb sequence_id # # Examples: # --------- # See methods provided by default for an example implementation. def sequence_viewer(sequence_id) sequence_id = encode sequence_id database_ids = encode querydb.map(&:id).join(' ') url = "get_sequence/?sequence_ids=#{sequence_id}" \ "&database_ids=#{database_ids}" { :order => 0, :url => url, :title => 'Sequence', :class => 'view-sequence', :icon => 'fa-eye' } end def fasta_download(sequence_id) sequence_id = encode sequence_id database_ids = encode querydb.map(&:id).join(' ') url = "get_sequence/?sequence_ids=#{sequence_id}" \ "&database_ids=#{database_ids}&download=fasta" { :order => 1, :title => 'FASTA', :url => url, :icon => 'fa-download' } end def ncbi(sequence_id) return nil unless sequence_id.match(NCBI_ID_PATTERN) ncbi_id = Regexp.last_match[1] ncbi_id = encode ncbi_id url = "http://www.ncbi.nlm.nih.gov/#{querydb.first.typ}/#{ncbi_id}" { :order => 2, :title => 'View on NCBI', :url => url, :icon => 'fa-external-link' } end end end # [1]: https://stackoverflow.com/questions/2824126/whats-the-difference-between-uri-escape-and-cgi-escape