Пример #1
0
void printExplinedGenesFrequencyAndPhonotype(vector<int>* explainedGenesFrequencyRealUpDown, vector<double>* pValues, vector<bool>* isPhenotypeGenes,
		vector<string>* geneIdToSymbol, TIntAdjList* network, TDoubleMatrix* originalGeneExpressionMatrix, vector<int>* genesEx, double F, string filename){
	int totalGenesUpDown = explainedGenesFrequencyRealUpDown->size();
	int totalGenes = totalGenesUpDown / 2;
	int totalSamples = originalGeneExpressionMatrix->at(0).size();

	list<ExplainedGeneDetail> explainedGenesList;

	for (int i = 0; i < totalGenesUpDown; ++i) {
		//only print explained genes
		if(explainedGenesFrequencyRealUpDown->at(i) > 0){
			ExplainedGeneDetail exGene;
			if(i < totalGenes){
				exGene.gene = geneIdToSymbol->at(i) + "_UP";
				exGene.degree = network->at(i).size();
			}else{
				exGene.gene = geneIdToSymbol->at(i-totalGenes) + "_DOWN";
				exGene.degree = network->at(i-totalGenes).size();
			}

			exGene.isPhenotype = isPhenotypeGenes->at(i);
			exGene.numSampleExplained = explainedGenesFrequencyRealUpDown->at(i);
			exGene.numSampleDeregulated = getNumSamplesOfDeregulatedGene(originalGeneExpressionMatrix, genesEx, F, i, totalGenesUpDown);
			exGene.pValue = pValues->at(i);

			explainedGenesList.push_back(exGene);
		}
	}

	//sorting

	//save to file
	vector<string>* outputStr = new vector<string>;
//	string filename = "output/exp_gene_freq.dat";
	//outputStr->push_back("GENE\tDEGREE\tNUM_SAMPLE_DEREGULATED\tFREQUENCY_SAMPLE_DEREGULATED\tNUM_SAMPLE_EXPLAINED\tFREQUENCY_SAMPLE_EXPLAINED\tEXPLAINED\\DEREGULATED\tIS_PHENOTYPE");

	for (list<ExplainedGeneDetail>::iterator it = explainedGenesList.begin();
			it != explainedGenesList.end(); it++) {
		string str = it->gene + "\t" + intToStr(it->degree) + "\t"
				+ intToStr(it->numSampleDeregulated) + "\t"
				+ doubleToStr(1.0 * it->numSampleDeregulated / totalSamples, 2) + "\t"
				+ intToStr(it->numSampleExplained) + "\t"
				+ doubleToStr(1.0 * it->numSampleExplained / totalSamples, 2) + "\t"
				+ doubleToStr(1.0 * it->numSampleExplained / it->numSampleDeregulated, 2) + "\t";

		//note that the p-values can be different for up and down, but if the gene is a phenotype gene, it will say Y
		if(it->isPhenotype){
			str += "Y\t" + doubleToStr(it->pValue, 5);
		}else{
			str += "N\t" + doubleToStr(it->pValue, 5);
		}
		outputStr->push_back(str);
	}

	writeStrVector(filename.c_str(), outputStr);
	delete outputStr;
}
Пример #2
0
 Str* Parser::evaluateToString(Expr* e)
 {
     switch (e->tag()) {
         case TAG_literalUndefined: return compiler->intern("undefined");
         case TAG_literalNull:      return compiler->intern("null");
         case TAG_literalBoolean:   return ((LiteralBoolean*)e)->value ? compiler->intern("true") : compiler->intern("false");
         case TAG_literalDouble:    return doubleToStr(((LiteralDouble*)e)->value);
         case TAG_literalInt:       return doubleToStr(((LiteralInt*)e)->value);
         case TAG_literalUInt:      return doubleToStr(((LiteralUInt*)e)->value);
         case TAG_literalString:    return ((LiteralString*)e)->value;
         default:
             failNonConstant(e);
             return 0;
     }
 }
Пример #3
0
/**
 * Convert scpi_number_t to string
 * @param context
 * @param value number value
 * @param str target string
 * @param len max length of string
 * @return number of chars written to string
 */
size_t SCPIParser::SCPI_NumberToStr(scpi_number_t * value, char * str, size_t len) {
    const char * type;
    const char * unit;
    size_t result;

    if (!value || !str) {
        return 0;
    }

    type = translateSpecialNumberInverse(context.special_numbers, value->type);

    if (type) {
        strncpy(str, type, len);
        return min(strlen(type), len);
    }

    result = doubleToStr(value->value, str, len);

    unit = translateUnitInverse(context.units, value->unit);

    if (unit) {
        strncat(str, " ", len);
        strncat(str, unit, len);
        result += strlen(unit) + 1;
    }

    return result;
}
Пример #4
0
/**
 * Write double walue to the result
 * @param context
 * @param val
 * @return 
 */
size_t SCPI_ResultDouble(scpi_t * context, double val) {
    char buffer[32];
    size_t result = 0;
    size_t len = doubleToStr(val, buffer, sizeof (buffer));
    result += writeDelimiter(context);
    result += writeData(context, buffer, len);
    context->output_count++;
    return result;

}
Пример #5
0
/**
 * Write double walue to the result
 * @param context
 * @param val
 * @return
 */
size_t SCPIParser::SCPI_ResultDouble(double val) {
    char buffer[32];
    size_t result = 0;
    size_t len = doubleToStr(val, buffer, sizeof (buffer));
    result += writeDelimiter();
    result += writeData(buffer, len);
    context.output_count++;
    return result;

}
Пример #6
0
void saveJSDivergences(vector<JSDivergence>* jsDivergences, string filename){
	vector<string> outputStr;
	outputStr.push_back("L\tD\tF\tJS Divergence");

	int total = jsDivergences->size();
	int D, L;
	double F, divergence;
	for (int i = 0; i < total; ++i) {
		D = jsDivergences->at(i).D;
		L = jsDivergences->at(i).L;
		F = jsDivergences->at(i).F;
		divergence = jsDivergences->at(i).divergence;

		string str = intToStr(L) + "\t" + intToStr(D) + "\t" + doubleToStr(F, 1) + "\t" + doubleToStr(divergence, 5);
		outputStr.push_back(str);
	}

	writeStrVector(filename.c_str(), &outputStr);
}
Пример #7
0
string Classifier::classifResultsToHTML(ClassifResults& clRes, int resNum)
{
    string HTMLrep = "";

    // Print header
    HTMLrep += "<table border=\"1\" cellpadding=\"5\" style=\"border-collapse:collapse\">\n";

    HTMLrep += "<tr>\n";

        HTMLrep += "\t<th>Img ID</th>\n";    
        HTMLrep += "\t<th>Class name</th>\n";

        for (int i = 0; i < resNum; i++)
            HTMLrep += "\t<th>" + intToStr(i + 1) + "</th>\n";

    HTMLrep += "</tr>\n";

    // Print table row for each result
    for (int resIdx = 0; resIdx < clRes.size(); resIdx++)
    {
        // Extract data
        string className = clRes[resIdx].first;
        vector< pair<string, double> > results = clRes[resIdx].second;

        HTMLrep += "<tr>\n";

            // Color correct ones in green
            string thColor = "white";
            if (inPredClasses(className, results)) thColor = "#85FF5C";

            // Print idx of image
            HTMLrep += "\t<th style=\"background-color:"+thColor+"\"" + ">" + intToStr(resIdx + 1) + "</th>\n";

            // Print class of image
            HTMLrep += "\t<td>" + className + "</td>\n";

            // Print results of classification
            for (int i = 0; i < results.size(); i++)
            {
                string predClassName = results[i].first;
                double predClassVal  = results[i].second;

                HTMLrep += "\t<td align=\"center\">" + predClassName + " : " +
                           "<strong>" + doubleToStr(predClassVal) + " </strong>" + " </td>\n";
                
            }
        
        HTMLrep += "</tr>\n";
    }

    // Print closing stuff
    HTMLrep += "</table>";

    return HTMLrep;
}
Пример #8
0
QString Tools::roundNumber(qreal value, int precision)
{
    double fractpart, intpart;
    fractpart = modf(value, &intpart);

    // round number when fraction part is really small
    // when fraction part is smaller than 1% of integer part
    // 24.2008 -> 24.2
    // 2.01738 -> 2.02
    // 3.004   -> 3
    if (qAbs(fractpart/intpart*100) < 1.1f) {
        qreal v = pow(10, (precision-1));
        qreal fractpart2 = qRound(fractpart * v) / v;
        value = intpart + fractpart2;
    }
    return doubleToStr(value, precision);
}
std::string stringCalc(std::string expression)
{
    std::string subExpression, subExpression2, result, result2;
    int startPos, endPos;
    double val1, val2;

    if (expression.compare(varNotFound) == 0)
        return "";

    do // Searches for (), calculates the expression inside, then
    {   // replaces the () with the result. Will start from the
        startPos = -1; // innermost (). Repeats until there are no more ().
        endPos = -1;
        for(register int i = 0 ; unsigned(i) < expression.length() ; i++)
        {
            if (expression[i] == '(')
            {
                startPos = i;
                for (register int j = i; unsigned(j) < expression.length() ; j++)
                {
                    if (expression[j] == ')')
                    {
                        endPos = j;
                        break;
                    }
                }
            }
        }
        if (startPos != -1) // () found
        {
            subExpression = expression.substr(startPos+1, endPos-startPos-1);
            result = stringCalc(subExpression); // Calls same function with expression inside ()
            expression.erase(startPos, endPos-startPos+1);
            expression.insert(startPos, result);
        }
    }
    while(startPos != -1);

    // At this point the expression will have no ( or ) and only numbers, +, -, *, /

    do // Searches for + and - then calculates the rest of the expression
    {   // Will calculate last the + or - that is most to the right
        startPos = -1;
        for(register int i = 0 ; unsigned(i) < expression.length() ; i++)
        {
            if ((expression[i] == '+')||(expression[i] == '-'))
            {
                startPos = i;
            }
        }
        // Expression[startPos] is the last + or - found in the entire string
        if (startPos != -1)
        {

            if(expression[startPos]=='-') // Special case for negative numbers
            {
                if(startPos > 0)
                {
                    if(!isdigit(expression[startPos-1]))
                    {
                        endPos = startPos;
                        for(register int i = 0 ; unsigned(i) < startPos ; i++)
                        {
                            if ((expression[i] == '+')||(expression[i] == '-'))
                            {
                                startPos = i;
                            }
                        }
                        if (startPos == endPos)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    break;
                }
            }
            // Separates the 2 expressions before and after the + or -
            subExpression = expression.substr(0, startPos);
            subExpression2 = expression.substr(startPos+1, expression.length()-startPos);
            result = stringCalc(subExpression); // Calls the same function for
            result2 = stringCalc(subExpression2); // each of the new expressions
            val1 = strToDouble(result);
            val2 = strToDouble(result2);
            if (expression[startPos] == '+')
            {
                expression = doubleToStr(val1 + val2);
            }
            else
            {
                expression = doubleToStr(val1 - val2);
            }

        }
    }
    while(startPos != -1);

    do // Searches for * and / then calculates the rest of the expression
    {   // Same logic as the +- block
        startPos = -1;
        for(register int i = 0 ; unsigned(i) < expression.length() ; i++)
        {
            if ((expression[i] == '*')||(expression[i] == '/'))
            {
                startPos = i;
            }
        }
        if (startPos != -1)
        {
            subExpression = expression.substr(0, startPos);
            subExpression2 = expression.substr(startPos+1, expression.length()-startPos);
            result = stringCalc(subExpression);
            result2 = stringCalc(subExpression2);
            val1 = strToDouble(result);
            val2 = strToDouble(result2);
            if(expression[startPos] == '*')
            {
                expression = doubleToStr(val1 * val2);
            }
            else
            {
                expression = doubleToStr(val1 / val2);
            }

        }
    }
    while(startPos != -1);

    return expression;
}
Пример #10
0
void calculateImpactScoresForAllSamples(vector< list<Module> >* modulesListOfAllSamples,
		vector< vector<Driver> >* driversOfAllSamples, TDoubleMatrix* originalGeneExpressionMatrix, vector<int>* GenesEx,
		int totalGenes, double F, vector<string>* geneIdToSymbol, string filename){

	int totalSamples = modulesListOfAllSamples->size();

	//map the gene id to the row id in the gene expression matrix
	vector<int> rowId(totalGenes);
	for (int i = 0; i < totalGenes; ++i) {
		rowId[i] = findIndex(GenesEx, i);
	}

	//OUTPUT: print drivers and impact scores for all samples
	vector<string>* outputDrivers = new vector<string>;
	outputDrivers->push_back("SAMPLE_ID\tDRIVER\tIMPACT_SCORE\tIS_DEREGULATED\tMODULE_SIZE\tNUM_DRIVERS");

	//for each sample i
	for (int i = 0; i < totalSamples; ++i) {

		list<Module> modules = modulesListOfAllSamples->at(i);

		//for each module
		for (list<Module>::iterator it = modules.begin(); it != modules.end(); it++) {
			Module module = *it;
			double score = 0;

			int moduleSize = 0;
			int numDrivers = module.driverGeneIds.size();

			//sum up the fold change

			for(list<int>::iterator git = module.explainedGeneIdsUpDown.begin(); git != module.explainedGeneIdsUpDown.end(); git++){
				int currentExplainedGeneId = *git;
				if(currentExplainedGeneId < totalGenes){	//up
					score += fabs(originalGeneExpressionMatrix->at(rowId[currentExplainedGeneId])[i]);
				}else{										//down
					score += fabs(originalGeneExpressionMatrix->at(rowId[currentExplainedGeneId - totalGenes])[i]);
				}
				moduleSize++;
			}

			for(list<int>::iterator git = module.phenotypeGeneIdsUpDown.begin(); git != module.phenotypeGeneIdsUpDown.end(); git++){
				int currentPhenotypeGeneId = *git;
				if(currentPhenotypeGeneId < totalGenes){	//up
					score += fabs(originalGeneExpressionMatrix->at(rowId[currentPhenotypeGeneId])[i]);
				}else{										//down
					score += fabs(originalGeneExpressionMatrix->at(rowId[currentPhenotypeGeneId - totalGenes])[i]);
				}
				moduleSize++;
			}

			//save the drivers and their scores
			for(list<int>::iterator git = module.driverGeneIds.begin(); git != module.driverGeneIds.end(); git++){
				Driver driver;
				driver.geneId = *git;
				driver.sampleId = i;
				driver.impactScore = score;

				//OUTPUT: print drivers and impact scores for all samples (cont.)
				string str = intToStr(i) + "\t" + geneIdToSymbol->at(*git) + "\t" + doubleToStr(score, 3) + "\t";

				//check if the driver gene is also a deregulated gene
				if(rowId[*git] != -1 and fabs(originalGeneExpressionMatrix->at(rowId[*git])[i]) >= F){
					driver.isDeregulated = true;
					str = str + "1\t" + intToStr(moduleSize) + "\t" + intToStr(numDrivers);
				}else{
					driver.isDeregulated = false;
					str = str + "0\t" + intToStr(moduleSize) + "\t" + intToStr(numDrivers);
				}

				driversOfAllSamples->at(i).push_back(driver);

				outputDrivers->push_back(str);
			}

		}	//end for each module

	}

	//OUTPUT: print drivers and impact scores for all samples (cont.)
	writeStrVector(filename.c_str(), outputDrivers);
	delete outputDrivers;
}
Пример #11
0
// Set Widget data as an double (entry, textview)
bool
GtkAReViWidget::setWidgetDouble(const StlString &propName,double propValue)
{
  return setWidgetString(propName,doubleToStr(propValue));
}
Пример #12
0
template<> std::string stringify<Double>(State const& state, Double::Element a) {
	return Double::isNA(a) ? "NA" : doubleToStr(a);
}  
Пример #13
0
template<> std::string deparse<Double>(State const& state, Double::Element a) {
	return Double::isNA(a) ? "NA_real_" : doubleToStr(a);
}  
Пример #14
0
std::string stringify(State const& state, Double::Element a, Format f) {
	return Double::isNA(a) ? "NA" : doubleToStr(a, f.scientific ? f.sdecimals : f.fdecimals, !f.scientific);
}  
Пример #15
0
void printAggregatedDriverList(vector<DriverGene>* driverGenes, string filename,
		vector<string>* geneIdToSymbol, vector<string>* sampleIdToName,
		vector<double>* driverAggregatedScores, vector<int>* driversFrequency,
		vector<int>* mutationFrequency,
		vector<int>* pointMutationDriversFrequency,
		vector<int>* deletionDriversFrequency,
		vector<int>* amplificationDriversFrequency,
		vector<int>* pointMutationFrequency, vector<int>* deletionFrequency,
		vector<int>* amplificationFrequency, vector<bool>* isCancerBenchmarkGenes) {

	vector<string> outputStr;
	outputStr.push_back(
			"GENE\tDRIVER_FREQUENCY\tDRIVER_SNV_FREQUENCY\tDRIVER_DELTION_FREQUENCY\tDRIVER_AMPLIFICATION_FREQUENCY\tCANCER_CENSUS\tPAN_CANCER\t"
					"IMPACT\tMUTATION_FREQUENCY\tSNV_FREQUENCY\tDELTION_FREQUENCY\tAMPLIFICATION_FREQUENCY");

	int totalSamples = sampleIdToName->size();
	int totalDrivers = driverGenes->size();

	list<AggregatedDriver> aggregatedDriversList;

	for (int i = 0; i < totalDrivers; ++i) {

		AggregatedDriver driver;
		int currentDriverGeneId = driverGenes->at(i).geneId;

		driver.gene = geneIdToSymbol->at(currentDriverGeneId);
		driver.driverFrequency = 1.0 * driversFrequency->at(currentDriverGeneId)
				/ totalSamples;
		driver.driverPointMutationFrequency = 1.0
				* pointMutationDriversFrequency->at(currentDriverGeneId)
				/ totalSamples;
		driver.driverDeletionFrequency = 1.0
				* deletionDriversFrequency->at(currentDriverGeneId)
				/ totalSamples;
		driver.driverAmplificationFrequency = 1.0
				* amplificationDriversFrequency->at(currentDriverGeneId)
				/ totalSamples;
		if(isCancerBenchmarkGenes->at(currentDriverGeneId)){
			driver.cancerCensus = "Y";
		}else{
			driver.cancerCensus = "N";
		}
		driver.panCancer = "NA";
		driver.aggregatedImpactScore = driverAggregatedScores->at(
				currentDriverGeneId);
		driver.mutationFrequency = 1.0
				* mutationFrequency->at(currentDriverGeneId) / totalSamples;
		driver.pointMutationFrequency = 1.0
				* pointMutationFrequency->at(currentDriverGeneId)
				/ totalSamples;
		driver.deletionFrequency = 1.0
				* deletionFrequency->at(currentDriverGeneId) / totalSamples;
		driver.amplificationFrequency = 1.0
				* amplificationFrequency->at(currentDriverGeneId)
				/ totalSamples;

		aggregatedDriversList.push_back(driver);

	}

	aggregatedDriversList.sort(sortByAggregatedImpactScore);

	for (list<AggregatedDriver>::iterator it = aggregatedDriversList.begin();
			it != aggregatedDriversList.end(); it++) {
		string str = it->gene + "\t" + doubleToStr(it->driverFrequency, 3) + "\t"
				+ doubleToStr(it->driverPointMutationFrequency, 3) + "\t"
				+ doubleToStr(it->driverDeletionFrequency, 3) + "\t"
				+ doubleToStr(it->driverAmplificationFrequency, 3) + "\t"
				+ it->cancerCensus + "\t" + it->panCancer + "\t"
				+ doubleToStr(it->aggregatedImpactScore, 10) + "\t"
				+ doubleToStr(it->mutationFrequency, 3) + "\t"
				+ doubleToStr(it->pointMutationFrequency, 3) + "\t"
				+ doubleToStr(it->deletionFrequency, 3) + "\t"
				+ doubleToStr(it->amplificationFrequency, 3);
		outputStr.push_back(str);
	}

	writeStrVector(filename.c_str(), &outputStr);

}
Пример #16
0
void printSampleDriverList(vector<vector<Driver> >* driversOfAllSamples,
		string pathname, vector<string>* geneIdToSymbol,
		vector<string>* sampleIdToName,
		TIntegerMatrix* originaloriginalPointMutationsMatrix,
		TIntegerMatrix* originalCNVsMatrix, vector<int>* genesPointMut,
		vector<int>* genesCNV, vector<double>* driverAggregatedScores,
		vector<int>* driversFrequency, vector<int>* mutationFrequency, vector<bool>* isCancerBenchmarkGenes) {

	int totalSamples = driversOfAllSamples->size();

	for (int i = 0; i < totalSamples; ++i) {
		vector<Driver> drivers = driversOfAllSamples->at(i);
		int numDrivers = drivers.size();

		list<SampleDriver> sampleDriversList;

		//for each driver
		for (int j = 0; j < numDrivers; ++j) {
			SampleDriver driver;
			driver.gene = geneIdToSymbol->at(drivers[j].geneId);
			driver.type = getDriverType(drivers[j].geneId, i,
					originaloriginalPointMutationsMatrix, originalCNVsMatrix,
					genesPointMut, genesCNV);
			driver.impactScore = drivers[j].impactScore;
			driver.aggregatedImpactScore = driverAggregatedScores->at(
					drivers[j].geneId);
			driver.driverFrequency = 1.0
					* driversFrequency->at(drivers[j].geneId) / totalSamples;
			driver.mutationFrequency = 1.0
					* mutationFrequency->at(drivers[j].geneId) / totalSamples;
			if(isCancerBenchmarkGenes->at(drivers[j].geneId)){
				driver.cancerCensus = "Y";
			}else{
				driver.cancerCensus = "N";
			}
			driver.panCancer = "NA";

			sampleDriversList.push_back(driver);
		}

		string filename = pathname + sampleIdToName->at(i) + ".tsv";
		vector<string> outputStr;
		outputStr.push_back(
				"GENE\tTYPE\tSAMPLE_IMPACT\tDATA_SET_IMPACT\tDRIVER_FREQUENCY\tMUTATION_FREQUENCY\tCANCER_CENSUS\tPAN_CANCER");

		sampleDriversList.sort(sortByImpactScore);
		for (list<SampleDriver>::iterator it = sampleDriversList.begin();
				it != sampleDriversList.end(); it++) {
			string str = it->gene + "\t" + it->type + "\t"
					+ doubleToStr(it->impactScore, 3) + "\t"
					+ doubleToStr(it->aggregatedImpactScore, 3) + "\t"
					+ doubleToStr(it->driverFrequency, 3) + "\t"
					+ doubleToStr(it->mutationFrequency, 3) + "\t"
					+ it->cancerCensus + "\t" + it->panCancer;
			outputStr.push_back(str);
		}

		writeStrVector(filename.c_str(), &outputStr);

	}
}