/rcdk/R/iterating.R
R | 43 lines | 37 code | 6 blank | 0 comment | 12 complexity | 0b193e96301db920685409f811732988 MD5 | raw file
1hasNext <- function(obj, ...) { UseMethod("hasNext") } 2hasNext.iload.molecules <- function(obj, ...) obj$hasNext() 3iload.molecules<- function(molfile, type = 'smi', aromaticity = TRUE, typing = TRUE, isotopes = TRUE, skip=TRUE) { 4 5 if (!file.exists(molfile) && length(grep('http://', molfile)) == 0) 6 stop(paste(molfile, ": Does not exist", sep='')) 7 8 fr <- .jnew("java/io/FileReader", as.character(molfile)) 9 dcob <- .get.chem.object.builder() 10 if (type == 'smi') { 11 sreader <- .jnew("org/openscience/cdk/io/iterator/IteratingSMILESReader",.jcast(fr, "java/io/Reader"), dcob) 12 } else if (type == 'sdf') { 13 sreader <- .jnew("org/openscience/cdk/io/iterator/IteratingSDFReader",.jcast(fr, "java/io/Reader"), dcob) 14 .jcall(sreader, "V", "setSkip", skip) 15 } 16 hasNext <- NA 17 mol <- NA 18 molr <- NA 19 20 hasNx <- function() { 21 hasNext <<- .jcall(sreader, "Z", "hasNext") 22 if (!hasNext) { 23 .jcall(sreader, "V", "close") 24 mol <<- NA 25 } 26 return(hasNext) 27 } 28 29 nextEl <- function() { 30 mol <<- .jcall(sreader, "Ljava/lang/Object;", "next") 31 mol <<- .jcast(mol, "org/openscience/cdk/interfaces/IAtomContainer") 32 if (aromaticity) do.aromaticity(mol) 33 if (typing) do.typing(mol) 34 if (isotopes) do.isotopes(mol) 35 36 hasNext <<- NA 37 return(mol) 38 } 39 40 obj <- list(nextElem = nextEl, hasNext = hasNx) 41 class(obj) <- c("iload.molecules", "abstractiter", "iter") 42 obj 43}