Skip to content

Instantly share code, notes, and snippets.

@cknoxrun
Created June 16, 2010 21:12
Show Gist options
  • Save cknoxrun/441276 to your computer and use it in GitHub Desktop.
Save cknoxrun/441276 to your computer and use it in GitHub Desktop.
A small module to download and convert PubMed citations to TextTile format.
require 'rubygems'
require 'bio'
module PubMed
# Download and annotate the given pubmed_id and return a citation in textile format.
# Example Citation:
# Smythe MA, Stephens JL, Koerber JM, Mattson JC: A comparison of lepirudin and argatroban outcomes.
# Clin Appl Thromb Hemost. 2005 Oct;11(4):371-4. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/16244762
def PubMed.annotate(pubmed_id)
raise ArgumentError unless pubmed_id.to_i > 0
citation = ''
result = Bio::PubMed.pmfetch(pubmed_id)
raise ArgumentError if result =~ /Error occurred/
medline = Bio::MEDLINE.new(result)
if medline.au && !( medline.au.strip.nil? || medline.au.strip == '')
citation = medline.au.gsub(/\n/, ', ') + ': ' + medline.title + ' ' + medline.source
elsif !(medline.nil? || medline == '')
citation = medline.title + ' ' + medline.source
else
raise ArgumentError
end
citation.gsub!(/\n/, ' ')
citation.gsub!(/\s{2,}/, ' ')
citation = "\n# #{citation} \"Pubmed\":http://www.ncbi.nlm.nih.gov/pubmed/#{pubmed_id}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment