IList <int> ToIndexList(INumberTable nTable, bool attributeMode, IList <string> itemList) { if (attributeMode) { return(nTable.IndexOfColumns(itemList)); } else { return(nTable.IndexOfRows(itemList)); } }
public List <string> Expression(List <string> selectedIds, double threshold) { const short maxExprIndex = 13; // number of expressed levels. const short indexShift = 38; double[][] M = (double[][])(NumTable.Matrix); int cellNr = NumTable.Rows; int geneNr = NumTable.Columns; IList <int> selectedCells = NumTable.IndexOfRows(selectedIds); IList <int> selectedGenes = NumTable.IndexOfColumns(selectedIds); List <string> expressedId = new List <string>(); if (threshold == 0) // mark the genes/cells which have higher expression than the average of selected genes/cells. { if (GeneToCell) // Finding expressed cells for selected genes. Gene->Cell operation. { double[] expression = new double[cellNr]; // total expression of each cell. MT.Loop(0, cellNr, row => { foreach (int col in selectedGenes) { expression[row] += M[row][col]; } }); double meanExp = expression.Sum() / cellNr; double maxExp = expression.Max(); double step = (maxExp - meanExp) / maxExprIndex; this.Genes = selectedGenes.Count; this.Cells = 0; for (int i = 0; i < cellNr; i++) // for each cells. { double delta = expression[i] - meanExp; int bIdx = row2bodyIdx[i]; if (delta > 0) { short v = Math.Min(maxExprIndex, (short)(delta / step)); BodyList[bIdx].Type = (short)(indexShift + v); this.Cells++; expressedId.Add(BodyList[bIdx].Id); } else { BodyList[bIdx].Type = OrgBodies[bIdx].Type; } } } else { double[] expression = new double[geneNr]; // total expression of each gene. MT.Loop(0, geneNr, col => { foreach (int row in selectedCells) { expression[col] += M[row][col]; } }); double meanExp = expression.Sum() / geneNr; double maxExp = expression.Max(); double step = (maxExp - meanExp) / maxExprIndex; // Finding expressed genes for selected cells. Cell->Gene operation. this.Cells = selectedCells.Count; this.Genes = 0; for (int i = 0; i < geneNr; i++) // for each gene. { double delta = expression[i] - meanExp; int bIdx = col2bodyIdx[i]; if (delta > 0) { int v = Math.Min(maxExprIndex, (int)(delta / step)); BodyList[bIdx].Type = (short)(indexShift + v); this.Genes++; expressedId.Add(BodyList[bIdx].Id); } else { BodyList[bIdx].Type = OrgBodies[bIdx].Type; } } } } else // Mark all those cells/genes which have above global average expressions. { if (GeneToCell) { short[] expressed = new short[cellNr]; // count of expressed genes for each cell. MT.Loop(0, cellNr, row => { foreach (int col in selectedGenes) { if (M[row][col] > threshold) { expressed[row] += 1; } } }); this.Genes = selectedGenes.Count; this.Cells = expressed.Count(v => v > 0); for (int i = 0; i < cellNr; i++) { short v = Math.Min(maxExprIndex, expressed[i]); int bIdx = row2bodyIdx[i]; BodyList[bIdx].Type = (short)((v > 0) ? (indexShift + v) : OrgBodies[bIdx].Type); if (v > 0) { expressedId.Add(BodyList[bIdx].Id); } } } else { short[] expressed = new short[geneNr]; // count of expressed cells for each gene. MT.Loop(0, geneNr, col => { foreach (int row in selectedCells) { if (M[row][col] > threshold) { expressed[col] += 1; } } }); this.Cells = selectedCells.Count; this.Genes = expressed.Count(v => v > 0); for (int i = 0; i < geneNr; i++) { short v = Math.Min(maxExprIndex, expressed[i]); int bIdx = col2bodyIdx[i]; BodyList[bIdx].Type = (short)((v > 0) ? (indexShift + v) : OrgBodies[bIdx].Type); if (v > 0) { expressedId.Add(BodyList[bIdx].Id); } } } } return(expressedId); }