Advertisement
csortu

New functions to UniProt.ws

Sep 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.92 KB | None | 0 0
  1. # Setting general options to store the location of temporary speclist file
  2.  
  3. options("tempspecfile"=tempfile("specfile."))
  4.  
  5. # specfileloc
  6.  
  7. specfileloc <- function(){
  8.   specfile <- system.file("extdata/speclist.txt")
  9.  
  10.   # Check if speclist.txt is available online, if yes, download, if not, fall back to local copy
  11.   localfile <- getOption("tempspecfile")
  12.   if(require(RCurl)){
  13.     if(!file.exists(localfile)){
  14.       specurl <- 'http://www.uniprot.org/docs/speclist.txt'
  15.       test <- getBinaryURL(specurl)
  16.       if(length(test)>1){
  17.         writeBin(test,localfile)
  18.         specfile <- localfile
  19.       }
  20.     }else{
  21.       specfile <- localfile
  22.     }
  23.   }
  24.  
  25.   # Check if specfile exists or throw error
  26.  
  27.   if(!file.exists(specfile)){
  28.     stop("speclist.txt cannot be downloaded from UniProt.org, and it is not found locally")
  29.   }
  30.  
  31.   return(specfile)
  32.  
  33. }
  34.  
  35. # digest specfile
  36.  
  37. digestspecfile <- function(specfile = specfileloc()){
  38.   my.text <- readLines(specfile)
  39.   codetable <- my.text[grep("^[A-Z0-9]+ +[ABEVO]", my.text, perl=TRUE, value=FALSE)]
  40.  
  41.   codes <- data.frame(domain = substr(codetable,7,7),
  42.                       taxId = as.numeric(substr(codetable,8,15)),
  43.                       taxname = sapply(strsplit(codetable, ": N="), '[', 2),
  44.                       row.names = sapply(strsplit(codetable, " +[ABEVO] +",perl=TRUE), '[', 1))
  45.  
  46.   codes$species <- gsub("^([^ ]* [^ ]*) .*$","\\1",codes$taxname, perl=TRUE)
  47.  
  48.  
  49.   return(codes)
  50. }
  51.  
  52. # taxname2species
  53.  
  54. taxname2species <- function(taxname){
  55.   codetable <- digestspecfile()
  56.   specnames <- codetable[taxname,"species"]
  57.   return(specnames)
  58. }
  59.  
  60. # taxname2taxid
  61.  
  62. taxname2taxid  <- function(taxname){
  63.   codetable <- digestspecfile()
  64.   taxids <- codetable[taxname,"taxId"]
  65.   return(taxids)
  66. }
  67.  
  68. #taxname2domain
  69.  
  70. taxname2domain <- function(taxname){
  71.   codetable <- digestspecfile()
  72.   domains <- codetable[taxname,"domain"]
  73.   return(domains)
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement