view EnzymeCompound.R @ 1:253d531a0193 draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 36c9d8099c20a1ae848f1337c16564335dd8fb2b
author prog
date Sat, 03 Sep 2016 17:02:01 -0400
parents e66bb061af06
children
line wrap: on
line source

if ( ! exists('EnzymeCompound')) { # Do not load again if already loaded

	source('BiodbEntry.R')

	#####################
	# CLASS DECLARATION #
	#####################

	EnzymeCompound <- setRefClass("EnzymeCompound", contains = 'BiodbEntry')

	###########
	# FACTORY #
	###########

	createEnzymeCompoundFromTxt <- function(contents, drop = TRUE) {

		library(stringr)

		compounds <- list()
	
		# Define fields regex
		regex <- character()
		regex[[BIODB.ACCESSION]] <- "^ID\\s+([0-9.]+)$"
		regex[[BIODB.DESCRIPTION]] <- "^DE\\s+(.+)$"

		for (text in contents) {

			# Create instance
			compound <- EnzymeCompound$new()

			lines <- strsplit(text, "\n")
			for (s in lines[[1]]) {

				# Test generic regex
				parsed <- FALSE
				for (field in names(regex)) {
					g <- str_match(s, regex[[field]])
					if ( ! is.na(g[1,1])) {
						compound$setField(field, g[1,2])
						parsed <- TRUE
						break
					}
				}
				if (parsed)
					next
			}

			compounds <- c(compounds, compound)
		}

		# Replace elements with no accession id by NULL
		compounds <- lapply(compounds, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x)

		# If the input was a single element, then output a single object
		if (drop && length(contents) == 1)
			compounds <- compounds[[1]]
	
		return(compounds)

	}
}