<commit_before># Compares methylation data of tumor and normal samples.
# The function will run paired and un-paired Wilcoxon tests. Pairing of samples
# is determined by matching sample names in the tumors and normals matrices.
# tumors is a methylation data matrix with samples in the columns and probes in the rows
# tumors.probeann is a matrix with annotation of the probes;
# either probes are in the same order as in tumors or the rownames match
# normals is a methylation data matrix with samples in the columns and probes in the rows
# genes is a vector of genes to be tested; gene ids have to match the first column of the probe annotation matrix
# Returns a named list with results from paired and un-paired tests.

runMethComp = function(tumors, normals, probes) {
  if (!is.matrix(tumors)) {
	  tumors = matrixFromVector(tumors)
	  rownames(tumors) = probes
  }
  if (!is.matrix(normals)) {
	  normals = matrixFromVector(normals)
	  rownames(normals) = probes
  }
  # Samples from tumors that have a matched normal
  tumors.matchedsamples = intersect(colnames(tumors), colnames(normals))
  # Retain only the normal samples for which there is a tumor
  
  normals = normals[, tumors.matchedsamples, drop=FALSE]
  
  # Rename normal samples to reflect the state
  colnames(normals) = paste(colnames(normals), "normal", sep="_")

  tumors.groups = c(rep(1, times=ncol(tumors)), rep(2, times=ncol(normals)))
  names(tumors.groups) = c(colnames(tumors), colnames(normals))

  # Which probes map to any of the genes?
  selected.probes = intersect(intersect(rownames(tumors), rownames(normals)), probes)

  inpMat = cbind(tumors[selected.probes, tumors.matchedsamples, drop=FALSE], normals[selected.probes, , drop=FALSE])
  # Run paired Wilcoxon tests
  pairedwilcox.p = doWilcox(inpMat=inpMat, matchedSamples=tumors.matchedsamples)
  
  inpMat = cbind(tumors[selected.probes, , drop=FALSE], normals[selected.probes, , drop=FALSE])
  # Run un-paired Wilcoxon tests
  wilcox.p = doWilcox(inpMat=inpMat, groups=tumors.groups)

  return(list(unpaired=wilcox.p, paired=pairedwilcox.p))
}

# Impute missing values 
# This method will impute missing data using the impute.knn function as implemented 
# in the "impute" library. If this library is not installed or impute=FALSE, all
# probes with missing values will be removed.
# meth.data is the matrix with methylation probes in rows and samples in columns
# impute is a boolean indicating whether missing values should be imputed
# no.na is the threshold giving the number of missing values from which on a
# probe will be removed. By default, a probe is only removed if its value is
# missing for all samples.
cleanMethylation = function(meth.data, impute=TRUE, no.na=(ncol(meth.data)-1)) {
  # How many values are missing for each probe?
  no.nas = apply(meth.data, 1, function(x) { sum(is.na(x)) })
  if (!require(impute) || !impute) {<commit_msg>Fix bug in runMethComp: first run unpaired test using all samples and then run paired test with matched samples<commit_after># Compares methylation data of tumor and normal samples.
# The function will run paired and un-paired Wilcoxon tests. Pairing of samples
# is determined by matching sample names in the tumors and normals matrices.
# tumors is a methylation data matrix with samples in the columns and probes in the rows
# tumors.probeann is a matrix with annotation of the probes;
# either probes are in the same order as in tumors or the rownames match
# normals is a methylation data matrix with samples in the columns and probes in the rows
# genes is a vector of genes to be tested; gene ids have to match the first column of the probe annotation matrix
# Returns a named list with results from paired and un-paired tests.

runMethComp = function(tumors, normals, probes) {
	if (!is.matrix(tumors)) {
		tumors = matrixFromVector(tumors)
		rownames(tumors) = probes
	}
	if (!is.matrix(normals)) {
		normals = matrixFromVector(normals)
		rownames(normals) = probes
	}
	
	# Samples from tumors that have a matched normal
	tumors.matchedsamples = intersect(colnames(tumors), colnames(normals))
	# Rename the normal samples 
	colnames(normals) = paste(colnames(normals), "normal", sep="_")
	
	# Filter unknown probes
	selected.probes = intersect(intersect(rownames(tumors), rownames(normals)), probes)
	
	## Unpaired test
	tumors.groups = c(rep(1, times=ncol(tumors)), rep(2, times=ncol(normals)))
	names(tumors.groups) = c(colnames(tumors), colnames(normals))
	
	inpMat = cbind(tumors[selected.probes, , drop=FALSE], normals[selected.probes, , drop=FALSE])
	# Run un-paired Wilcoxon tests
	wilcox.p = doWilcox(inpMat=inpMat, groups=tumors.groups)
	
	## Paired test
	# Retain only the normal samples for which there is a tumor
	normals = normals[, tumors.matchedsamples, drop=FALSE]
		
	inpMat = cbind(tumors[selected.probes, tumors.matchedsamples, drop=FALSE], normals[selected.probes, , drop=FALSE])
	# Run paired Wilcoxon tests
	pairedwilcox.p = doWilcox(inpMat=inpMat, matchedSamples=tumors.matchedsamples)
	
	return(list(unpaired=wilcox.p, paired=pairedwilcox.p))
}

# Impute missing values 
# This method will impute missing data using the impute.knn function as implemented 
# in the "impute" library. If this library is not installed or impute=FALSE, all
# probes with missing values will be removed.
# meth.data is the matrix with methylation probes in rows and samples in columns
# impute is a boolean indicating whether missing values should be imputed
# no.na is the threshold giving the number of missing values from which on a
# probe will be removed. By default, a probe is only removed if its value is
# missing for all samples.
cleanMethylation = function(meth.data, impute=TRUE, no.na=(ncol(meth.data)-1)) {
  # How many values are missing for each probe?
  no.nas = apply(meth.data, 1, function(x) { sum(is.na(x)) })
  if (!require(impute) || !impute) {
{'fraction_non_alphanumeric': 0.08774018024145554, 'fraction_numerical': 0.0013603128719605509, 'mean_word_length': 4.852736318407961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '757957', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1718, 'n_words': 680}