Simple R functions

This is a very simple R function to calculate the fragments per nanogram based on an average size of fragments
frag <- function(x){                             # X = average size of fragments
molmass <- x * 649                            # calculates molar mass for ds fragments
frag.g <- 6.022e+23/molmass          # avogadro number frag in frag/g
frag.ng <- frag.g*1e-9                        # number frag in frag/ng
return(frag.ng)                                    # return result
}

This is another R function I took somewhere from internet to load a sequence from Genbank into R

getncbiseq <- function(accession) #function to load entire genome in R
{
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=””)
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”))
}

Advertisements

One thought on “Simple R functions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s