示例#1
0
bool StrainHMObject::setGraph(std::string title, std::string patientName, int patientid, int taxid, osg::Vec4 color)
{
    std::stringstream queryss;

    queryss << "select value, unix_timestamp(timestamp) as timestamp from EcoliShigella_Measurement where patient_id = " << patientid << " and taxonomy_id = " << taxid << " order by timestamp;";

    struct StrainData
    {
	float value;
	time_t timestamp;
    };

    StrainData * sdata = NULL;
    int dataSize = 0;

    if(ComController::instance()->isMaster())
    {
	if(_dbm)
	{
	    DBMQueryResult result;

	    _dbm->runQuery(queryss.str(),result);

	    dataSize = result.numRows();
	    if(dataSize)
	    {
		sdata = new struct StrainData[dataSize];

		for(int i = 0; i < dataSize; ++i)
		{
		    sdata[i].value = atof(result(i,"value").c_str());
		    sdata[i].timestamp = atol(result(i,"timestamp").c_str());
		}
	    }
	}
	else
	{
	    std::cerr << "No Database connection." << std::endl;
	}

	ComController::instance()->sendSlaves(&dataSize,sizeof(int));
	if(dataSize)
	{
	    ComController::instance()->sendSlaves(sdata,dataSize*sizeof(struct StrainData));
	}
    }
    else
    {
	ComController::instance()->readMaster(&dataSize,sizeof(int));
	if(dataSize)
	{
	    sdata = new struct StrainData[dataSize];
	    ComController::instance()->readMaster(sdata,dataSize*sizeof(struct StrainData));
	}
    }

    bool graphValid = false;

    if(dataSize)
    {
	std::vector<std::string> labels;
	std::vector<float> values;
	std::vector<osg::Vec4> colors;

	float minValue = FLT_MAX;
	float maxValue = FLT_MIN;
	for(int i = 0; i < dataSize; ++i)
	{
	    char temp[1024];

	    strftime(temp,1023,"%F",localtime(&sdata[i].timestamp));

	    labels.push_back(temp);
	    values.push_back(sdata[i].value);
	    colors.push_back(color);

	    if(sdata[i].value > maxValue)
	    {
		maxValue = sdata[i].value;
	    }
	    if(sdata[i].value < minValue)
	    {
		minValue = sdata[i].value;
	    }
	}

	if(minValue < 0.00003)
	{
	    std::cerr << "Graph discarded for low range." << std::endl;
	    if(sdata)
	    {
		delete[] sdata;
	    }
	    return false;
	}

	std::stringstream titless;
	titless << patientName << " : " << title;

	graphValid = _graph->setGraph(titless.str(),labels,values,minValue,maxValue,0.1,1.0,colors);
    }

    PluginHelper::registerSceneObject(this,"FuturePatient");
    attachToScene();

    if(sdata)
    {
	delete[] sdata;
    }

    return graphValid;
}
示例#2
0
bool MicrobePointLineObject::setGraph(std::string microbeTableSuffix, std::string measureTableSuffix, bool expandAxis)
{
    std::vector<std::string> phylumOrder;
    // larry order
    /*phylumOrder.push_back("Bacteroidetes");
    phylumOrder.push_back("Firmicutes");
    phylumOrder.push_back("Verrucomicrobia");
    phylumOrder.push_back("Proteobacteria");
    phylumOrder.push_back("Actinobacteria");
    phylumOrder.push_back("Fusobacteria");
    phylumOrder.push_back("Euryarchaeota");*/

    phylumOrder.push_back("Bacteroidetes");
    phylumOrder.push_back("Firmicutes");
    phylumOrder.push_back("Actinobacteria");
    phylumOrder.push_back("Proteobacteria");
    phylumOrder.push_back("Verrucomicrobia");
    phylumOrder.push_back("Euryarchaeota");
    phylumOrder.push_back("Fusobacteria");

    int numPatients = 0;
    std::vector<patientData> data;

    std::string measurementTable = "Microbe_Measurement";
    measurementTable += measureTableSuffix;

    std::string microbesTable = "Microbes";
    microbesTable += microbeTableSuffix;

    std::stringstream queryss;
    queryss << "SELECT Patient.p_condition, Patient.last_name, unix_timestamp(" << measurementTable << ".timestamp) as timestamp, " << microbesTable << ".phylum, sum(" << measurementTable << ".value) as value from " << measurementTable << " inner join " << microbesTable << " on " << measurementTable << ".taxonomy_id = " << microbesTable << ".taxonomy_id inner join Patient on Patient.patient_id = " << measurementTable << ".patient_id where Patient.region = \"US\" group by Patient.last_name, " << measurementTable << ".timestamp, " << microbesTable << ".phylum;";

    std::string querystr = queryss.str();

    //std::cerr << querystr << std::endl;

    if(ComController::instance()->isMaster())
    {
	if(_dbm)
	{
	    DBMQueryResult result;

	    _dbm->runQuery(querystr,result);

	    std::map<patType,std::map<std::string,std::map<time_t,std::map<std::string,float> > > > sortMap;
	    for(int i = 0; i < result.numRows(); ++i)
	    {
		std::string condition = result(i,"p_condition").c_str();
		patType type;
		if(condition == "CD" || condition == "crohn's disease")
		{
		    type = CROHNS;
		}
		else if(condition == "healthy")
		{
		    type = HEALTHY;
		}
		else if(condition == "Larry")
		{
		    type = SMARR;
		}
		else if(condition == "UC" || condition == "ulcerous colitis")
		{
		    type = UC;
		}
		else
		{
		    continue;
		}
		sortMap[type][result(i,"last_name").c_str()][atol(result(i,"timestamp").c_str())][result(i,"phylum").c_str()] = atof(result(i,"value").c_str());
	    }

	    for(std::map<patType,std::map<std::string,std::map<time_t,std::map<std::string,float> > > >::iterator git = sortMap.begin(); git != sortMap.end(); ++git)
	    {
		for(std::map<std::string,std::map<time_t,std::map<std::string,float> > >::iterator pit = git->second.begin(); pit != git->second.end(); ++pit)
		{
		    for(std::map<time_t,std::map<std::string,float> >::iterator tsit = pit->second.begin(); tsit != pit->second.end(); ++tsit)
		    {
			patientData pd;
			pd.type = git->first;
			strncpy(pd.name,pit->first.c_str(),511);
			pd.timestamp = tsit->first;
			for(int i = 0; i < phylumOrder.size(); ++i)
			{
			    pd.data[i] = tsit->second[phylumOrder[i]];
			}
			data.push_back(pd);
		    }
		}
	    }
	    numPatients = data.size();
	}

	ComController::instance()->sendSlaves(&numPatients,sizeof(int));
	if(numPatients)
	{
	    ComController::instance()->sendSlaves(data.data(),numPatients*sizeof(struct patientData));
	}
    }
    else
    {
	ComController::instance()->readMaster(&numPatients,sizeof(int));
	if(numPatients)
	{
	    data.resize(numPatients);
	    ComController::instance()->readMaster(data.data(),numPatients*sizeof(struct patientData));
	}
    }

    std::cerr << "Got " << numPatients << " patients." << std::endl;

    if(!numPatients)
    {
	return false;
    }

    std::vector<std::string> groupNames;
    groupNames.push_back("LS");
    groupNames.push_back("Crohns");
    groupNames.push_back("UC");
    groupNames.push_back("Healthy");

    std::vector<std::vector<std::string> > patientNames;
    patientNames.push_back(std::vector<std::string>());
    patientNames.push_back(std::vector<std::string>());
    patientNames.push_back(std::vector<std::string>());
    patientNames.push_back(std::vector<std::string>());

    std::vector<std::vector<std::vector<float> > > values;
    values.push_back(std::vector<std::vector<float> >());
    values.push_back(std::vector<std::vector<float> >());
    values.push_back(std::vector<std::vector<float> >());
    values.push_back(std::vector<std::vector<float> >());

    for(int i = 0; i < data.size(); ++i)
    {
	int index = (int)data[i].type;

	char timestamp[512];
	std::string name = data[i].name;
	if(name == "Smarr")
	{
	    name = "LS";
	}
	name += " - ";
	strftime(timestamp,511,"%F",localtime(&data[i].timestamp));
	name += timestamp;

	patientNames[index].push_back(name);
	values[index].push_back(std::vector<float>());
	for(int j = 0; j < 7; ++j)
	{
	    values[index].back().push_back(data[i].data[j]);
	}
    }

    bool tret = _graph->setGraph("Phylum Line Chart",groupNames,phylumOrder,patientNames,values,expandAxis);

    if(tret)
    {
	addChild(_graph->getRootNode());
    }

    return tret;
}
示例#3
0
bool StrainGraphObject::setGraph(std::string title, int taxId, bool larryOnly)
{
    std::stringstream ss;
    if(!larryOnly)
    {
	ss << "select Patient.last_name, Patient.p_condition, EcoliShigella_Measurement.value, unix_timestamp(EcoliShigella_Measurement.timestamp) as timestamp from EcoliShigella_Measurement inner join Patient on Patient.patient_id = EcoliShigella_Measurement.patient_id where EcoliShigella_Measurement.taxonomy_id = " << taxId << " order by Patient.p_condition, EcoliShigella_Measurement.value desc;";
    }
    else
    {
	ss << "select \"Smarr\" as last_name, \"Larry\" as p_condition, value, unix_timestamp(timestamp) as timestamp from EcoliShigella_Measurement where patient_id = 1 and taxonomy_id = " << taxId << " order by timestamp;";
    }

    struct StrainData
    {
	char name[1024];
	char group[1024];
	float value;
	time_t timestamp;
    };

    StrainData * sdata = NULL;
    int dataSize = 0;

    if(ComController::instance()->isMaster())
    {
	if(_dbm)
	{
	    DBMQueryResult result;

	    _dbm->runQuery(ss.str(),result);

	    dataSize = result.numRows();
	    if(dataSize)
	    {
		sdata = new struct StrainData[dataSize];

		for(int i = 0; i < dataSize; ++i)
		{
		    strncpy(sdata[i].name,result(i,"last_name").c_str(),1023);
		    strncpy(sdata[i].group,result(i,"p_condition").c_str(),1023);
		    sdata[i].value = atof(result(i,"value").c_str());
		    sdata[i].timestamp = atol(result(i,"timestamp").c_str());
		}
	    }
	}

	/*if(_conn)
	{
	    mysqlpp::Query strainQuery = _conn->query(ss.str().c_str());
	    mysqlpp::StoreQueryResult strainResult = strainQuery.store();

	    dataSize = strainResult.num_rows();
	    if(dataSize)
	    {
		sdata = new struct StrainData[dataSize];

		for(int i = 0; i < dataSize; ++i)
		{
		    strncpy(sdata[i].name,strainResult[i]["last_name"].c_str(),1023);
		    strncpy(sdata[i].group,strainResult[i]["p_condition"].c_str(),1023);
		    sdata[i].value = atof(strainResult[i]["value"].c_str());
		    sdata[i].timestamp = atol(strainResult[i]["timestamp"].c_str());
		}
	    }
	}
	else
	{
	    std::cerr << "No Database connection." << std::endl;
	}*/

	ComController::instance()->sendSlaves(&dataSize,sizeof(int));
	if(dataSize)
	{
	    ComController::instance()->sendSlaves(sdata,dataSize*sizeof(struct StrainData));
	}
    }
    else
    {
	ComController::instance()->readMaster(&dataSize,sizeof(int));
	if(dataSize)
	{
	    sdata = new struct StrainData[dataSize];
	    ComController::instance()->readMaster(sdata,dataSize*sizeof(struct StrainData));
	}
    }

    std::map<std::string,std::string> condition2Group;
    condition2Group["crohn's disease"] = "Crohns";
    condition2Group["healthy"] = "Healthy";
    condition2Group["Larry"] = "LS";
    condition2Group["ulcerous colitis"] = "UC";

    std::map<std::string, std::vector<std::pair<std::string, float> > > dataMap;
    std::vector<std::string> orderVec;

    for(int i = 0; i < dataSize; ++i)
    {
	char timestamp[512];
	std::string name = sdata[i].name;
	name += " - ";
	strftime(timestamp,511,"%F",localtime(&sdata[i].timestamp));
	name += timestamp;

	if(!larryOnly)
	{
	    if(sdata[i].value > 0.0)
	    {
		dataMap[condition2Group[sdata[i].group]].push_back(std::pair<std::string,float>(name,sdata[i].value));
	    }
	}
	else
	{
	    std::stringstream ss;
	    ss << "LS" << (i+1);
	    if(sdata[i].value > 0.0)
	    {
		dataMap[ss.str()].push_back(std::pair<std::string,float>(name,sdata[i].value));
	    }
	}
    }

    for(std::map<std::string, std::vector<std::pair<std::string, float> > >::iterator it = dataMap.begin(); it != dataMap.end(); ++it)
    {
	orderVec.push_back(it->first);
    }

    bool graphValid = false;

    if(orderVec.size())
    {
	graphValid = _graph->setGraph(title, dataMap, orderVec, BGAT_LOG, "Relative Abundance", "", "condition / patient", osg::Vec4());
    }

    if(graphValid)
    {
	addChild(_graph->getRootNode());
	std::vector<std::pair<std::string,int> > customOrder;

	if(!larryOnly)
	{
	    int totalEntries = 0;
	    for(std::map<std::string, std::vector<std::pair<std::string, float> > >::iterator it = dataMap.begin(); it != dataMap.end(); ++it)
	    {
		totalEntries += it->second.size();
	    }

	    std::map<std::string,int> groupIndexMap;

	    while(customOrder.size() < totalEntries)
	    {
		float maxVal = FLT_MIN;
		std::string group;
		for(std::map<std::string, std::vector<std::pair<std::string, float> > >::iterator it = dataMap.begin(); it != dataMap.end(); ++it)
		{
		    if(groupIndexMap[it->first] >= it->second.size())
		    {
			continue;
		    }

		    if(it->second[groupIndexMap[it->first]].second > maxVal)
		    {
			group = it->first;
			maxVal = it->second[groupIndexMap[it->first]].second;
		    }
		}

		customOrder.push_back(std::pair<std::string,int>(group,groupIndexMap[group]));
		groupIndexMap[group]++;
	    }
	}
	else
	{
	    for(std::map<std::string, std::vector<std::pair<std::string, float> > >::iterator it = dataMap.begin(); it != dataMap.end(); ++it)
	    {
		for(int i = 0; i < it->second.size(); ++i)
		{
		    customOrder.push_back(std::pair<std::string,int>(it->first,i));
		}
	    }

	    std::map<std::string,osg::Vec4> larryColors;

	    float step = 0.75 / ((float)customOrder.size());

	    for(int i = 0; i < customOrder.size(); ++i)
	    {
		std::stringstream ss;
		ss << "LS" << (i+1);
		larryColors[ss.str()] = osg::Vec4(1.0-((float)i)*step,0.0,0.0,1.0);
	    }
	    _graph->setColorMapping(osg::Vec4(0.0,0.0,0.0,1.0),larryColors);
	}

	_graph->setCustomOrder(customOrder);
	_graph->setDisplayMode(BGDM_CUSTOM);
	_graph->setShowLabels(false);
    }

    if(sdata)
    {
	delete[] sdata;
    }

    return graphValid;
}