/rcdk/R/iterating.R

http://github.com/rajarshi/cdkr · R · 43 lines · 37 code · 6 blank · 0 comment · 12 complexity · 0b193e96301db920685409f811732988 MD5 · raw file

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