### R code from vignette source 'vignettes/ChemmineR/inst/doc/ChemmineR.Rnw'

###################################################
### code chunk number 1: ChemmineR.Rnw:38-40
###################################################
options(width=80)
unlink( "test.db")


###################################################
### code chunk number 2: ChemmineR.Rnw:82-84 (eval = FALSE)
###################################################
## source("http://bioconductor.org/biocLite.R") # Sources the biocLite.R installation script.
## biocLite("ChemmineR") # Installs the package.


###################################################
### code chunk number 3: ChemmineR.Rnw:89-90
###################################################
library("ChemmineR") # Loads the package


###################################################
### code chunk number 4: ChemmineR.Rnw:92-94 (eval = FALSE)
###################################################
## library(help="ChemmineR") # Lists all functions and classes 
## vignette("ChemmineR") # Opens this PDF manual from R


###################################################
### code chunk number 5: ChemmineR.Rnw:101-106
###################################################
data(sdfsample)
sdfset <- sdfsample
sdfset # Returns summary of SDFset
sdfset[1:4] # Subsetting of object 
sdfset[[1]] # Returns summarized content of one SDF


###################################################
### code chunk number 6: ChemmineR.Rnw:107-109 (eval = FALSE)
###################################################
## view(sdfset[1:4]) # Returns summarized content of many SDFs, not printed here
## as(sdfset[1:4], "list") # Returns complete content of many SDFs, not printed here


###################################################
### code chunk number 7: ChemmineR.Rnw:113-115 (eval = FALSE)
###################################################
## sdfset <- read.SDFset("http://faculty.ucr.edu/~tgirke/Documents/
##                        R_BioCond/Samples/sdfsample.sdf")


###################################################
### code chunk number 8: ChemmineR.Rnw:119-120 (eval = FALSE)
###################################################
## header(sdfset[1:4]) # Not printed here


###################################################
### code chunk number 9: ChemmineR.Rnw:121-122
###################################################
header(sdfset[[1]])


###################################################
### code chunk number 10: ChemmineR.Rnw:123-124 (eval = FALSE)
###################################################
## atomblock(sdfset[1:4]) # Not printed here


###################################################
### code chunk number 11: ChemmineR.Rnw:125-126
###################################################
atomblock(sdfset[[1]])[1:4,]


###################################################
### code chunk number 12: ChemmineR.Rnw:127-128 (eval = FALSE)
###################################################
## bondblock(sdfset[1:4]) # Not printed here


###################################################
### code chunk number 13: ChemmineR.Rnw:129-130
###################################################
bondblock(sdfset[[1]])[1:4,] 


###################################################
### code chunk number 14: ChemmineR.Rnw:131-132 (eval = FALSE)
###################################################
## datablock(sdfset[1:4]) # Not printed here


###################################################
### code chunk number 15: ChemmineR.Rnw:133-134
###################################################
datablock(sdfset[[1]])[1:4]


###################################################
### code chunk number 16: ChemmineR.Rnw:138-142
###################################################
cid(sdfset)[1:4] # Returns IDs from SDFset object
sdfid(sdfset)[1:4] # Returns IDs from SD file header block
unique_ids <- makeUnique(sdfid(sdfset))
cid(sdfset) <- unique_ids


###################################################
### code chunk number 17: ChemmineR.Rnw:146-152
###################################################
blockmatrix <- datablock2ma(datablocklist=datablock(sdfset))
   # Converts data block to matrix   
numchar <- splitNumChar(blockmatrix=blockmatrix) 
   # Splits to numeric and character matrix
numchar[[1]][1:2,1:2] # Slice of numeric matrix
numchar[[2]][1:2,10:11] # Slice of character matrix


###################################################
### code chunk number 18: ChemmineR.Rnw:156-158
###################################################
propma <- data.frame(MF=MF(sdfset), MW=MW(sdfset), atomcountMA(sdfset))
propma[1:4, ]


###################################################
### code chunk number 19: ChemmineR.Rnw:162-164
###################################################
datablock(sdfset) <- propma
datablock(sdfset[1])


###################################################
### code chunk number 20: ChemmineR.Rnw:168-171 (eval = FALSE)
###################################################
## grepSDFset("650001", sdfset, field="datablock", mode="subset") 
##    # Returns summary view of matches. Not printed here.
## .


###################################################
### code chunk number 21: ChemmineR.Rnw:173-174
###################################################
grepSDFset("650001", sdfset, field="datablock", mode="index") 


###################################################
### code chunk number 22: ChemmineR.Rnw:178-179 (eval = FALSE)
###################################################
## write.SDF(sdfset[1:4], file="sub.sdf", sig=TRUE)


###################################################
### code chunk number 23: plotstruct
###################################################
plot(sdfset[1:4], print=FALSE) # Plots structures to R graphics device


###################################################
### code chunk number 24: ChemmineR.Rnw:187-188 (eval = FALSE)
###################################################
## sdf.visualize(sdfset[1:4]) # Compound viewing in web browser


###################################################
### code chunk number 25: ChemmineR.Rnw:198-201 (eval = FALSE)
###################################################
## apset <- sdf2ap(sdfset) 
##    # Generate atom pair descriptor database for searching
## .


###################################################
### code chunk number 26: ChemmineR.Rnw:203-209
###################################################
data(apset)
   # Load sample apset data provided by library.
cmp.search(apset, apset[1], type=3, cutoff = 0.3, quiet=TRUE) 
   # Search apset database with single compound.
cmp.cluster(db=apset, cutoff = c(0.65, 0.5), quiet=TRUE)[1:4,] 
   # Binning clustering using variable similarity cutoffs.


###################################################
### code chunk number 27: ChemmineR.Rnw:307-309 (eval = FALSE)
###################################################
## sdfset <- read.SDFset("http://faculty.ucr.edu/~tgirke/Documents/
##                        R_BioCond/Samples/sdfsample.sdf")


###################################################
### code chunk number 28: ChemmineR.Rnw:311-315
###################################################
data(sdfsample) # Loads the same SDFset provided by the library
sdfset <- sdfsample 
valid <- validSDF(sdfset) # Identifies invalid SDFs in SDFset objects
sdfset <- sdfset[valid] # Removes invalid SDFs, if there are any


###################################################
### code chunk number 29: ChemmineR.Rnw:319-321 (eval = FALSE)
###################################################
## sdfstr <- read.SDFstr("http://faculty.ucr.edu/~tgirke/Documents/
##                        R_BioCond/Samples/sdfsample.sdf")


###################################################
### code chunk number 30: ChemmineR.Rnw:324-327
###################################################
sdfstr <- as(sdfset, "SDFstr")
sdfstr
as(sdfstr, "SDFset") 


###################################################
### code chunk number 31: ChemmineR.Rnw:335-336 (eval = FALSE)
###################################################
## write.SDF(sdfset[1:4], file="sub.sdf")


###################################################
### code chunk number 32: ChemmineR.Rnw:340-341 (eval = FALSE)
###################################################
## write.SDF(sdfset[1:4], file="sub.sdf", sig=TRUE, cid=TRUE, db=NULL)


###################################################
### code chunk number 33: ChemmineR.Rnw:345-348 (eval = FALSE)
###################################################
## props <- data.frame(MF=MF(sdfset), MW=MW(sdfset), atomcountMA(sdfset))
## datablock(sdfset) <- props
## write.SDF(sdfset[1:4], file="sub.sdf", sig=TRUE, cid=TRUE)


###################################################
### code chunk number 34: ChemmineR.Rnw:352-356 (eval = FALSE)
###################################################
## sdf2str(sdf=sdfset[[1]], sig=TRUE, cid=TRUE) 
##    # Uses default components
## sdf2str(sdf=sdfset[[1]], head=letters[1:4], db=NULL) 
##    # Uses custom components for header and data block


###################################################
### code chunk number 35: ChemmineR.Rnw:360-363 (eval = FALSE)
###################################################
## write.SDF(sdfset[1:4], file="sub.sdf", sig=TRUE, cid=TRUE, db=NULL)
## write.SDF(sdfstr[1:4], file="sub.sdf")
## cat(unlist(as(sdfstr[1:4], "list")), file="sub.sdf", sep="\n")


###################################################
### code chunk number 36: ChemmineR.Rnw:373-374 (eval = FALSE)
###################################################
## write.SDF(sdfset, "test.sdf")


###################################################
### code chunk number 37: ChemmineR.Rnw:378-379 (eval = FALSE)
###################################################
## sdfstr <- read.SDFstr("test.sdf")


###################################################
### code chunk number 38: ChemmineR.Rnw:383-385 (eval = FALSE)
###################################################
## write.SDFsplit(x=sdfstr, filetag="myfile", nmol=10)
##    # 'nmol' defines the number of molecules to write to each file


###################################################
### code chunk number 39: ChemmineR.Rnw:389-390 (eval = FALSE)
###################################################
## write.SDFsplit(x=sdfset, filetag="myfile", nmol=10)


###################################################
### code chunk number 40: ChemmineR.Rnw:400-401 (eval = FALSE)
###################################################
## write.SDF(sdfset, "test.sdf")


###################################################
### code chunk number 41: ChemmineR.Rnw:405-415 (eval = FALSE)
###################################################
## desc <- function(sdfset) {
##         cbind(SDFID=sdfid(sdfset),
##               # datablock2ma(datablocklist=datablock(sdfset)),
##               MW=MW(sdfset),
##               groups(sdfset),
##               APFP=desc2fp(x=sdf2ap(sdfset), descnames=1024, type="character"),
##               AP=sdf2ap(sdfset, type="character"),
##               rings(sdfset, type="count", upper=6, arom=TRUE)
##         )
## }


###################################################
### code chunk number 42: ChemmineR.Rnw:419-421 (eval = FALSE)
###################################################
## sdfStream(input="test.sdf", output="matrix.xls", fct=desc, Nlines=1000)
##    # 'Nlines': number of lines to read from input SD File at a time


###################################################
### code chunk number 43: ChemmineR.Rnw:425-427 (eval = FALSE)
###################################################
## sdfStream(input="test.sdf", output="matrix2.xls", append=FALSE, fct=desc, 
##           Nlines=1000, startline=950) 


###################################################
### code chunk number 44: ChemmineR.Rnw:431-436 (eval = FALSE)
###################################################
## indexDF <- read.delim("matrix.xls", row.names=1)[,1:4]
## indexDFsub <- indexDF[indexDF$MW < 400, ] 
##    # Selects molecules with MW < 400
## sdfset <- read.SDFindex(file="test.sdf", index=indexDFsub, type="SDFset")
##    # Collects results in 'SDFset' container


###################################################
### code chunk number 45: ChemmineR.Rnw:440-441 (eval = FALSE)
###################################################
## read.SDFindex(file="test.sdf", index=indexDFsub, type="file", outfile="sub.sdf")


###################################################
### code chunk number 46: ChemmineR.Rnw:445-447 (eval = FALSE)
###################################################
## apset <- read.AP(x="matrix.xls", type="ap", colid="AP") 
## apfp <- read.AP(x="matrix.xls", type="fp", colid="APFP")


###################################################
### code chunk number 47: ChemmineR.Rnw:451-454 (eval = FALSE)
###################################################
## apset <- read.AP(x=sdf2ap(sdfset[1:20], type="character"), type="ap")
## fpchar <- desc2fp(sdf2ap(sdfset[1:20]), descnames=1024, type="character")
## fpset <- as(fpchar, "FPset")


###################################################
### code chunk number 48: ChemmineR.Rnw:513-527
###################################################

data(sdfsample)

#create and initialize a new SQLite database
conn = initDb("test.db")

# load data and compute 3 features: molecular weight, with the MW function, 
# and counts for RINGS and AROMATIC, as computed by rings, which 
# returns a data frame itself.
ids=loadSdf(conn,sdfsample,
      function(sdfset) 
       data.frame(MW = MW(sdfset),  
                  rings(sdfset,type="count",upper=6, arom=TRUE))
      )


###################################################
### code chunk number 49: ChemmineR.Rnw:549-554
###################################################
lightIds = findCompounds(conn,"MW",c("MW < 300"))
MW(getCompounds(conn,lightIds)) 

#names of matching compounds:
getCompoundNames(conn,lightIds)


###################################################
### code chunk number 50: ChemmineR.Rnw:564-570 (eval = FALSE)
###################################################
## view(sdfset[1:4]) # Summary view of several molecules
## length(sdfset) # Returns number of molecules
## sdfset[[1]] # Returns single molecule from SDFset as SDF object
## sdfset[[1]][[2]] # Returns atom block from first compound as matrix
## sdfset[[1]][[2]][1:4,] 
## c(sdfset[1:4], sdfset[5:8]) # Concatenation of several SDFsets


###################################################
### code chunk number 51: ChemmineR.Rnw:574-577 (eval = FALSE)
###################################################
## grepSDFset("650001", sdfset, field="datablock", mode="subset") 
##    # To return index, set mode="index")
## .


###################################################
### code chunk number 52: ChemmineR.Rnw:581-588 (eval = FALSE)
###################################################
## sdfid(sdfset[1:4])
##    # Retrieves CMP IDs from Molecule Name field in header block.
## cid(sdfset[1:4])
##    # Retrieves CMP IDs from ID slot in SDFset.
## unique_ids <- makeUnique(sdfid(sdfset)) 
##    # Creates unique IDs by appending a counter to duplicates.
## cid(sdfset) <- unique_ids # Assigns uniquified IDs to ID slot


###################################################
### code chunk number 53: ChemmineR.Rnw:592-596 (eval = FALSE)
###################################################
## view(sdfset[c("650001", "650012")])
## view(sdfset[4:1])
## mylog <- cid(sdfset) %in% c("650001", "650012")
## view(sdfset[mylog])


###################################################
### code chunk number 54: ChemmineR.Rnw:600-610 (eval = FALSE)
###################################################
## atomblock(sdf); sdf[[2]]; sdf[["atomblock"]] 
##    # All three methods return the same component
## header(sdfset[1:4])
## atomblock(sdfset[1:4])
## bondblock(sdfset[1:4])
## datablock(sdfset[1:4])
## header(sdfset[[1]])
## atomblock(sdfset[[1]])
## bondblock(sdfset[[1]])
## datablock(sdfset[[1]])


###################################################
### code chunk number 55: ChemmineR.Rnw:614-617 (eval = FALSE)
###################################################
## sdfset[[1]][[2]][1,1] <- 999
## atomblock(sdfset)[1] <- atomblock(sdfset)[2]
## datablock(sdfset)[1] <- datablock(sdfset)[2]


###################################################
### code chunk number 56: ChemmineR.Rnw:621-623 (eval = FALSE)
###################################################
## datablock(sdfset) <- as.matrix(iris[1:100,])
## view(sdfset[1:4])


###################################################
### code chunk number 57: ChemmineR.Rnw:627-630 (eval = FALSE)
###################################################
## as(sdfstr[1:2], "list") 
## as(sdfstr[[1]], "SDF")
## as(sdfstr[1:2], "SDFset")


###################################################
### code chunk number 58: ChemmineR.Rnw:634-639 (eval = FALSE)
###################################################
## sdfcomplist <- as(sdf, "list")
## sdfcomplist <- as(sdfset[1:4], "list"); as(sdfcomplist[[1]], "SDF")
## sdflist <- as(sdfset[1:4], "SDF"); as(sdflist, "SDFset")
## as(sdfset[[1]], "SDFstr")
## as(sdfset[[1]], "SDFset")


###################################################
### code chunk number 59: ChemmineR.Rnw:643-646 (eval = FALSE)
###################################################
## as(sdfset[1:4], "SDF")
## as(sdfset[1:4], "list")
## as(sdfset[1:4], "SDFstr")


###################################################
### code chunk number 60: boxplot
###################################################
propma <- atomcountMA(sdfset, addH=FALSE)
boxplot(propma, col="blue", main="Atom Frequency")


###################################################
### code chunk number 61: ChemmineR.Rnw:660-661 (eval = FALSE)
###################################################
## boxplot(rowSums(propma), main="All Atom Frequency")   


###################################################
### code chunk number 62: ChemmineR.Rnw:665-667
###################################################
data(atomprop)
atomprop[1:4,] 


###################################################
### code chunk number 63: ChemmineR.Rnw:671-673
###################################################
MW(sdfset[1:4], addH=FALSE)
MF(sdfset[1:4], addH=FALSE)


###################################################
### code chunk number 64: ChemmineR.Rnw:677-678
###################################################
groups(sdfset[1:4], groups="fctgroup", type="countMA")


###################################################
### code chunk number 65: ChemmineR.Rnw:682-688
###################################################
propma <- data.frame(MF=MF(sdfset, addH=FALSE), MW=MW(sdfset, addH=FALSE), 
                     Ncharges=sapply(bonds(sdfset, type="charge"), length), 
                     atomcountMA(sdfset, addH=FALSE), groups(sdfset, 
		     type="countMA"), rings(sdfset, upper=6, type="count", 
		     arom=TRUE))
propma[1:4,]


###################################################
### code chunk number 66: ChemmineR.Rnw:692-696 (eval = FALSE)
###################################################
## datablock(sdfset) <- propma # Works with all SDF components
## datablock(sdfset)[1:4]
## test <- apply(propma[1:4,], 1, function(x) data.frame(col=colnames(propma), value=x))
## sdf.visualize(sdfset[1:4], extra = test)


###################################################
### code chunk number 67: ChemmineR.Rnw:700-702 (eval = FALSE)
###################################################
## datablocktag(sdfset, tag="PUBCHEM_NIST_INCHI")
## datablocktag(sdfset, tag="PUBCHEM_OPENEYE_CAN_SMILES")


###################################################
### code chunk number 68: ChemmineR.Rnw:706-713 (eval = FALSE)
###################################################
## blockmatrix <- datablock2ma(datablocklist=datablock(sdfset)) 
##    # Converts data block to matrix 
## numchar <- splitNumChar(blockmatrix=blockmatrix) 
##    # Splits matrix to numeric matrix and character matrix
## numchar[[1]][1:4,]; numchar[[2]][1:4,] 
##    # Splits matrix to numeric matrix and character matrix
## .


###################################################
### code chunk number 69: contable (eval = FALSE)
###################################################
## conMA(sdfset[1:2], exclude=c("H"))
##    # Create bond matrix for first two molecules in sdfset
## conMA(sdfset[[1]], exclude=c("H"))
##    # Return bond matrix for first molecule
## plot(sdfset[1], atomnum = TRUE, noHbonds=FALSE , no_print_atoms = "", atomcex=0.8)
##    # Plot its structure with atom numbering
## rowSums(conMA(sdfset[[1]], exclude=c("H")))
##    # Return number of non-H bonds for each atom
## .


###################################################
### code chunk number 70: ChemmineR.Rnw:739-742
###################################################
bonds(sdfset[[1]], type="bonds")[1:4,]
bonds(sdfset[1:2], type="charge")
bonds(sdfset[1:2], type="addNH")


###################################################
### code chunk number 71: ChemmineR.Rnw:754-755
###################################################
(ringatoms <- rings(sdfset[1], upper=Inf, type="all", arom=FALSE, inner=FALSE))


###################################################
### code chunk number 72: ChemmineR.Rnw:759-761
###################################################
atomindex <- as.numeric(gsub(".*_", "", unique(unlist(ringatoms))))
plot(sdfset[1], print=FALSE, colbonds=atomindex)


###################################################
### code chunk number 73: ChemmineR.Rnw:765-766
###################################################
plot(sdfset[1], print=FALSE, atomnum=TRUE, no_print_atoms="H")


###################################################
### code chunk number 74: ChemmineR.Rnw:770-771
###################################################
rings(sdfset[1], upper=Inf, type="all", arom=TRUE, inner=FALSE)  


###################################################
### code chunk number 75: ChemmineR.Rnw:775-776
###################################################
rings(sdfset[1], upper=6, type="arom", arom=TRUE, inner=FALSE)


###################################################
### code chunk number 76: ChemmineR.Rnw:780-781
###################################################
rings(sdfset[1:4], upper=Inf, type="count", arom=TRUE, inner=TRUE) 


###################################################
### code chunk number 77: plotstruct2
###################################################
data(sdfsample)
sdfset <- sdfsample
plot(sdfset[1:4], print=FALSE) # 'print=TRUE' returns SDF summaries


###################################################
### code chunk number 78: ChemmineR.Rnw:799-801 (eval = FALSE)
###################################################
## plot(sdfset[1:4], griddim=c(2,2), print_cid=letters[1:4], print=FALSE, 
##      noHbonds=FALSE)


###################################################
### code chunk number 79: plotstruct3
###################################################
plot(sdfset["CMP1"], atomnum = TRUE,  noHbonds=F , no_print_atoms = "", 
     atomcex=0.8, sub=paste("MW:", MW(sdfsample["CMP1"])), print=FALSE)


###################################################
### code chunk number 80: plotstruct4
###################################################
plot(sdfset[1], print=FALSE, colbonds=c(22,26,25,3,28,27,2,23,21,18,8,19,20,24))


###################################################
### code chunk number 81: ChemmineR.Rnw:820-821 (eval = FALSE)
###################################################
## sdf.visualize(sdfset[1:4])


###################################################
### code chunk number 82: ChemmineR.Rnw:831-832 (eval = FALSE)
###################################################
## sdf.visualize(sdfset[1:4], extra=month.name[1:4])


###################################################
### code chunk number 83: ChemmineR.Rnw:836-839 (eval = FALSE)
###################################################
## extra <- apply(propma[1:4,], 1, function(x) 
##          data.frame(Property=colnames(propma), Value=x))
## sdf.visualize(sdfset[1:4], extra=extra)


###################################################
### code chunk number 84: ChemmineR.Rnw:843-844 (eval = FALSE)
###################################################
## sdf.visualize(sdfset[1:4], extra=bondblock(sdfset[1:4]))


###################################################
### code chunk number 85: plotmcs
###################################################
library(fmcsR)
data(fmcstest) # Loads test sdfset object
test <- fmcs(fmcstest[1], fmcstest[2], au=2, bu=1) # Searches for MCS with mismatches
plotMCS(test) # Plots both query compounds with MCS in color


###################################################
### code chunk number 86: ChemmineR.Rnw:865-867
###################################################
ap <- sdf2ap(sdfset[[1]]) # For single compound
ap


###################################################
### code chunk number 87: ChemmineR.Rnw:868-869 (eval = FALSE)
###################################################
## apset <- sdf2ap(sdfset) # For many compounds. 


###################################################
### code chunk number 88: ChemmineR.Rnw:870-871
###################################################
view(apset[1:4])


###################################################
### code chunk number 89: ChemmineR.Rnw:875-878 (eval = FALSE)
###################################################
## cid(apset[1:4]) # Compound IDs
## ap(apset[1:4]) # Atom pair descriptors
## db.explain(apset[1]) # Return atom pairs in human readable format


###################################################
### code chunk number 90: ChemmineR.Rnw:882-885 (eval = FALSE)
###################################################
## apset2descdb(apset) # Returns old list-style AP database
## tmp <- as(apset, "list") # Returns list
## as(tmp, "APset") # Converts list back to APset


###################################################
### code chunk number 91: ChemmineR.Rnw:892-896 (eval = FALSE)
###################################################
## save(sdfset, file = "sdfset.rda", compress = TRUE)
## load("sdfset.rda")
## save(apset, file = "apset.rda", compress = TRUE)
## load("apset.rda")


###################################################
### code chunk number 92: ChemmineR.Rnw:901-903
###################################################
cmp.similarity(apset[1], apset[2])
cmp.similarity(apset[1], apset[1])


###################################################
### code chunk number 93: ChemmineR.Rnw:908-909
###################################################
cmp.search(apset, apset["650065"], type=3, cutoff = 0.3, quiet=TRUE) 


###################################################
### code chunk number 94: ChemmineR.Rnw:912-914
###################################################
cmp.search(apset, apset["650065"], type=1, cutoff = 0.3, quiet=TRUE) 
cmp.search(apset, apset["650065"], type=2, cutoff = 0.3, quiet=TRUE) 


###################################################
### code chunk number 95: ChemmineR.Rnw:921-922
###################################################
showClass("FPset")


###################################################
### code chunk number 96: ChemmineR.Rnw:926-929
###################################################
data(apset)
(fpset <- desc2fp(apset))
view(fpset[1:2])


###################################################
### code chunk number 97: ChemmineR.Rnw:933-940
###################################################
fpset[1:4] # behaves like a list
fpset[[1]] # returns FP object
length(fpset) # number of compounds
cid(fpset) # returns compound ids
fpset[10] <- 0 # replacement of 10th fingerprint to all zeros  
cid(fpset) <- 1:length(fpset) # replaces compound ids
c(fpset[1:4], fpset[11:14]) # concatenation of several FPset objects


###################################################
### code chunk number 98: ChemmineR.Rnw:944-946
###################################################
fpma <- as.matrix(fpset) # coerces FPset to matrix 
as(fpma, "FPset")


###################################################
### code chunk number 99: ChemmineR.Rnw:950-952
###################################################
fpchar <- as.character(fpset) # coerces FPset to character strings
as(fpchar, "FPset") # construction of FPset class from character vector


###################################################
### code chunk number 100: ChemmineR.Rnw:956-957
###################################################
fpSim(fpset[1], fpset, method="Tanimoto", cutoff=0.4, top=4)


###################################################
### code chunk number 101: ChemmineR.Rnw:964-967 (eval = FALSE)
###################################################
## data(sdfsample)
## sdfset <- sdfsample[1:10]
## apset <- sdf2ap(sdfset)


###################################################
### code chunk number 102: ChemmineR.Rnw:971-972 (eval = FALSE)
###################################################
## fpset <- desc2fp(apset, descnames=1024, type="FPset")


###################################################
### code chunk number 103: ChemmineR.Rnw:976-978 (eval = FALSE)
###################################################
## fpset1024 <- names(rev(sort(table(unlist(as(apset, "list")))))[1:1024])
## fpset <- desc2fp(apset, descnames=fpset1024, type="FPset")


###################################################
### code chunk number 104: ChemmineR.Rnw:982-984 (eval = FALSE)
###################################################
## fpchar <- desc2fp(x=apset, descnames=1024, type="character")
## fpchar <- as.character(fpset) 


###################################################
### code chunk number 105: ChemmineR.Rnw:988-990 (eval = FALSE)
###################################################
## fpma <- as.matrix(fpset) 
## fpset <- as(fpma, "FPset")


###################################################
### code chunk number 106: ChemmineR.Rnw:994-995 (eval = FALSE)
###################################################
## fpSim(fpset[1], fpset, method="Tanimoto")


###################################################
### code chunk number 107: ChemmineR.Rnw:999-1000 (eval = FALSE)
###################################################
## fpSim(fpset[1], fpset, method="Tversky", cutoff=0.4, top=4, alpha=0.5, beta=1)


###################################################
### code chunk number 108: ChemmineR.Rnw:1004-1006 (eval = FALSE)
###################################################
## myfct <- function(a, b, c, d) c/(a+b+c+d)
## fpSim(fpset[1], fpset, method=myfct) 


###################################################
### code chunk number 109: ChemmineR.Rnw:1010-1013 (eval = FALSE)
###################################################
## simMAap <- sapply(cid(apfpset), function(x) fpSim(x=apfpset[x], apfpset, sorted=FALSE))
## hc <- hclust(as.dist(1-simMAap), method="single")
## plot(as.dendrogram(hc), edgePar=list(col=4, lwd=2), horiz=TRUE)


###################################################
### code chunk number 110: ChemmineR.Rnw:1020-1025
###################################################
cid(sdfset) <- sdfid(sdfset)
fpset <- fp2bit(sdfset, type=1) 
fpset <- fp2bit(sdfset, type=2)
fpset <- fp2bit(sdfset, type=3)
fpset


###################################################
### code chunk number 111: ChemmineR.Rnw:1029-1030
###################################################
fpSim(fpset[1], fpset[2])


###################################################
### code chunk number 112: ChemmineR.Rnw:1035-1036
###################################################
fpSim(fpset["650065"], fpset, method="Tanimoto", cutoff=0.6, top=6) 


###################################################
### code chunk number 113: search_result
###################################################
cid(sdfset) <- cid(apset) # Assure compound name consistency among objects.
plot(sdfset[names(cmp.search(apset, apset["650065"], type=2, cutoff=4, 
     quiet=TRUE))], print=FALSE)


###################################################
### code chunk number 114: ChemmineR.Rnw:1050-1052 (eval = FALSE)
###################################################
## similarities <- cmp.search(apset, apset[1], type=3, cutoff = 10)
## sdf.visualize(sdfset[similarities[,1]], extra=similarities[,3])


###################################################
### code chunk number 115: ChemmineR.Rnw:1066-1068
###################################################
cmp.duplicated(apset, type=1)[1:4] # Returns AP duplicates as logical vector
cmp.duplicated(apset, type=2)[1:4,] # Returns AP duplicates as data frame


###################################################
### code chunk number 116: duplicates
###################################################
plot(sdfset[c("650059","650060", "650065", "650066")], print=FALSE)


###################################################
### code chunk number 117: ChemmineR.Rnw:1077-1079
###################################################
apdups <- cmp.duplicated(apset, type=1)
sdfset[which(!apdups)]; apset[which(!apdups)]


###################################################
### code chunk number 118: ChemmineR.Rnw:1088-1092
###################################################
count <- table(datablocktag(sdfset, tag="PUBCHEM_NIST_INCHI"))
count <- table(datablocktag(sdfset, tag="PUBCHEM_OPENEYE_CAN_SMILES"))
count <- table(datablocktag(sdfset, tag="PUBCHEM_MOLECULAR_FORMULA"))
count[1:4]


###################################################
### code chunk number 119: ChemmineR.Rnw:1122-1124
###################################################
clusters <- cmp.cluster(db=apset, cutoff = c(0.7, 0.8, 0.9), quiet = TRUE)
clusters[1:12,]


###################################################
### code chunk number 120: ChemmineR.Rnw:1129-1133
###################################################
fpset <- desc2fp(apset)
clusters2 <- cmp.cluster(fpset, cutoff=c(0.5, 0.7, 0.9), method="Tanimoto", 
                         quiet=TRUE) 
clusters2[1:12,]


###################################################
### code chunk number 121: ChemmineR.Rnw:1137-1139
###################################################
clusters3 <- cmp.cluster(fpset, cutoff=c(0.5, 0.7, 0.9), method="Tversky", 
                         alpha=0.3, beta=0.7, quiet=TRUE) 


###################################################
### code chunk number 122: ChemmineR.Rnw:1143-1146
###################################################
cluster.sizestat(clusters, cluster.result=1)
cluster.sizestat(clusters, cluster.result=2)
cluster.sizestat(clusters, cluster.result=3)


###################################################
### code chunk number 123: ChemmineR.Rnw:1150-1154 (eval = FALSE)
###################################################
## clusters <- cmp.cluster(db=apset, cutoff = c(0.65, 0.5, 0.3), 
##                         save.distances="distmat.rda") 
##    # Saves distance matrix to file "distmat.rda" in current working directory.
## load("distmat.rda") # Loads distance matrix.


###################################################
### code chunk number 124: ChemmineR.Rnw:1182-1184
###################################################
data(apset)
fpset <- desc2fp(apset)


###################################################
### code chunk number 125: ChemmineR.Rnw:1188-1190
###################################################
jarvisPatrick(nearestNeighbors(apset,numNbrs=6), k=5, mode="a1a2b") #Using "APset"
jarvisPatrick(nearestNeighbors(fpset,numNbrs=6), k=5, mode="a1a2b") #Using "FPset"


###################################################
### code chunk number 126: ChemmineR.Rnw:1195-1197
###################################################
cl=jarvisPatrick(nearestNeighbors(fpset,cutoff=0.6, method="Tanimoto"), k=2 ,mode="b")
byCluster(cl)


###################################################
### code chunk number 127: ChemmineR.Rnw:1201-1205
###################################################
nnm <- nearestNeighbors(fpset,numNbrs=6)
nnm$names[1:4]
nnm$ids[1:4,]
nnm$similarities[1:4,]


###################################################
### code chunk number 128: ChemmineR.Rnw:1209-1212
###################################################
nnm <- trimNeighbors(nnm,cutoff=0.4)
nnm$similarities[1:4,]



###################################################
### code chunk number 129: ChemmineR.Rnw:1216-1217
###################################################
jarvisPatrick(nnm, k=5,mode="b")


###################################################
### code chunk number 130: ChemmineR.Rnw:1221-1224
###################################################
nn = matrix(c(1,2,2,1),2,2,dimnames=list(c('one','two')))
nn
byCluster(jarvisPatrick(fromNNMatrix(nn),k=1))


###################################################
### code chunk number 131: ChemmineR.Rnw:1237-1240 (eval = FALSE)
###################################################
## cluster.visualize(apset, clusters, size.cutoff=2, quiet = TRUE) 
##    # Color codes clusters with at least two members.
## cluster.visualize(apset, clusters, quiet = TRUE) # Plots all items.


###################################################
### code chunk number 132: mds_scatter
###################################################
library(scatterplot3d)
coord <- cluster.visualize(apset, clusters, size.cutoff=1, dimensions=3, quiet=TRUE)
scatterplot3d(coord)


###################################################
### code chunk number 133: ChemmineR.Rnw:1251-1258 (eval = FALSE)
###################################################
## library(rgl)
## rgl.open(); offset <- 50; par3d(windowRect=c(offset, offset, 640+offset, 640+offset))
## rm(offset); rgl.clear(); rgl.viewpoint(theta=45, phi=30, fov=60, zoom=1)
## spheres3d(coord[,1], coord[,2], coord[,3], radius=0.03, color=coord[,4], alpha=1, 
## shininess=20); aspect3d(1, 1, 1)
## axes3d(col='black'); title3d("", "", "", "", "", col='black'); bg3d("white") 
##    # To save a snapshot of the graph, one can use the command rgl.snapshot("test.png").


###################################################
### code chunk number 134: mds_scatter
###################################################
dummy <- cmp.cluster(db=apset, cutoff=0, save.distances="distmat.rda", quiet=TRUE)
load("distmat.rda")


###################################################
### code chunk number 135: hclust
###################################################
hc <- hclust(as.dist(distmat), method="single")
hc[["labels"]] <- cid(apset) # Assign correct item labels
plot(as.dendrogram(hc), edgePar=list(col=4, lwd=2), horiz=T) 


###################################################
### code chunk number 136: fp_hclust (eval = FALSE)
###################################################
## simMA <- sapply(cid(fpset), function(x) fpSim(fpset[x], fpset, sorted=FALSE)) 
## hc <- hclust(as.dist(1-simMA), method="single")  
## plot(as.dendrogram(hc), edgePar=list(col=4, lwd=2), horiz=TRUE)


###################################################
### code chunk number 137: heatmap
###################################################
library(gplots)
heatmap.2(1-distmat, Rowv=as.dendrogram(hc), Colv=as.dendrogram(hc), 
          col=colorpanel(40, "darkblue", "yellow", "white"), 
          density.info="none", trace="none")


###################################################
### code chunk number 138: getIds (eval = FALSE)
###################################################
## compounds <- getIds(c(111,123))
## compounds


###################################################
### code chunk number 139: getIds (eval = FALSE)
###################################################
## compounds <- searchString("CC(=O)OC1=CC=CC=C1C(=O)O")
## compounds


###################################################
### code chunk number 140: getIds (eval = FALSE)
###################################################
## data(sdfsample); sdfset <- sdfsample[1]
## compounds <- searchSim(sdfset)
## compounds


###################################################
### code chunk number 141: sdf2smiles (eval = FALSE)
###################################################
## data(sdfsample); sdfset <- sdfsample[1]
## smiles <- sdf2smiles(sdfset)
## smiles


###################################################
### code chunk number 142: smiles2sdf (eval = FALSE)
###################################################
## sdf <- smiles2sdf("CC(=O)OC1=CC=CC=C1C(=O)O\tname")
## view(sdf)


###################################################
### code chunk number 143: sessionInfo
###################################################
sessionInfo()


