-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-ncbi-seq.R
31 lines (26 loc) · 956 Bytes
/
get-ncbi-seq.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
library("seqinr")
getncbiseq <- function(accession) {
# retrieve sequence from NCBI via accession number
# e.g. dengueseq <- getncbiseq("NC_001477")
require("seqinr") # this function requires the SeqinR R package
# first find which ACNUC database the accession is stored in:
dbs <- c("genbank","refseq","refseqViruses","bacterial")
numdbs <- length(dbs)
for (i in 1:numdbs) {
db <- dbs[i]
choosebank(db)
# check if the sequence is in ACNUC database 'db':
resquery <- try(query(".tmpquery", paste("AC=", accession)), silent = TRUE)
if (!(inherits(resquery, "try-error"))) {
queryname <- "query2"
thequery <- paste("AC=",accession,sep="")
query2 <- query(`queryname`,`thequery`)
# see if a sequence was retrieved:
seq <- getSequence(query2$req[[1]])
closebank()
return(seq)
}
closebank()
}
print(paste("ERROR: accession",accession,"was not found"))
}