Sha256: d5395d1b0f5f57c4af22d3c89bbffed500a0a9c4336431bc6c93234ec9616826

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

#!/usr/bin/env ruby
require 'typhoeus'
require 'cri'

Signal.trap("PIPE", "EXIT")
Signal.trap("INT", "EXIT")

valid_formats = Set.new ["fasta", "txt", "xml", "rdf", "gff"]

def get_uniprot_entry(arg, format)
  if format.nil? || format.empty?
    resp = Typhoeus.get("http://www.uniprot.org/uniprot/#{arg}.fasta")
    if resp.success?
      puts resp.response_body.lines.map(&:chomp)[1..-1].join("")
    end
  else
    # other format has been specified, just download and output
    resp = Typhoeus.get("http://www.uniprot.org/uniprot/#{arg}.#{format}")
    if resp.success?
      puts resp.response_body
    end
  end
end

root_cmd = Cri::Command.new_basic_root.modify do
  name 'uniprot'
  description 'Download protein sequence from uniprot.org'
  required :f, :format, "specify output format (available: " + valid_formats.to_a.join(", ") + ")"
  run do |opts, args, cmd|
    if !opts[:format].nil? and !valid_formats.include? opts[:format]
      $stderr.puts opts[:format] + " is not a valid output format. Available formats are: " + valid_formats.to_a.join(", ")
      exit 1
    end
    iterator = args.empty? ? $stdin.each_line : args
    iterator.each do |pept|
      get_uniprot_entry(pept.chomp, opts[:format])
    end
  end
end

# run_this runs this command without subcommands! Absolutely needed here
root_cmd.run_this(ARGV)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unipept-0.6.4 bin/uniprot
unipept-0.6.2 bin/uniprot