Skip to content

Instantly share code, notes, and snippets.

@aurora-mareviv
Last active August 29, 2015 14:07
Show Gist options
  • Save aurora-mareviv/7680180cbe0283f87854 to your computer and use it in GitHub Desktop.
Save aurora-mareviv/7680180cbe0283f87854 to your computer and use it in GitHub Desktop.
Download journal abbreviations table from NCBI website
######################################
### DOWNLOAD JOURNAL ABBREVIATIONS ###
######################################
# URL: ftp://ftp.ncbi.nih.gov/pubmed/J_Medline.txt
cit <- read.csv("ftp://ftp.ncbi.nih.gov/pubmed/J_Medline.txt", quote = "", sep = "\t", row.names = NULL, stringsAsFactors = FALSE)
# Tidy the original data and create data.frame
names(cit)[1] <- "X"
cit2 <- cit[!grepl("--------------------------------------------------------",cit$X),]
cit2b <- as.data.frame(cit2)
# Create split pattern
cit3 <- as.data.frame(sapply(cit2b,gsub,pattern="JrId:",replacement="JrId\t"))
cit3 <- as.data.frame(sapply(cit3,gsub,pattern="JournalTitle:",replacement="JournalTitle\t"))
cit3 <- as.data.frame(sapply(cit3,gsub,pattern="MedAbbr:",replacement="MedAbbr\t"))
cit3 <- as.data.frame(sapply(cit3,gsub,pattern="ISSN \\(Print\\):",replacement="ISSN.Print\t"))
cit3 <- as.data.frame(sapply(cit3,gsub,pattern="ISSN \\(Online\\):",replacement="ISSN.Online\t"))
cit3 <- as.data.frame(sapply(cit3,gsub,pattern="IsoAbbr:",replacement="IsoAbbr\t"))
cit3 <- as.data.frame(sapply(cit3,gsub,pattern="NlmId:",replacement="NlmId\t"))
# Split data in two columns:
library(reshape2)
yolo <- colsplit(cit3$cit2, "\\t", c("Index", "JourInfo"))
# Wide format:
len <- nrow(yolo)/7
yolo$ID <- rep(c(1:len),each=7)
journaltab <- dcast(yolo, ID ~ Index, value.var="JourInfo")
# Make names:
names(journaltab)[names(journaltab) == "JournalTitle"] <- "journal"
# Remove leading/trailing whitespaces:
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
journaltab <- lapply(journaltab, trim)
journaltab <- as.data.frame(journaltab)
# save(journaltab, file="journaltab.RData")
# write.csv(journaltab, file="journaltab.csv")
message("journaltab database succesfully downloaded")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment