
//cmake -G "Eclipse CDT4 - Unix Makefiles" ~/MyC++/ElutionProfile



#include <OpenMS/FORMAT/ConsensusXMLFile.h>
#include <OpenMS/FORMAT/FeatureXMLFile.h>
#include <OpenMS/FORMAT/MzMLFile.h>
#include <OpenMS/KERNEL/ConsensusMap.h>

#include <OpenMS/DATASTRUCTURES/Map.h>

#include <iostream>
#include <fstream>

using namespace OpenMS;
using namespace std;


Int main(){

//---BEGIN OF read out the consensus features from a .consensusXML data

 ConsensusMap consensMap;
 ConsensusXMLFile consensus_file;
 consensus_file.load("dataset_picked.consensusXML", consensMap);

 ofstream outputFile_ConFea;
 outputFile_ConFea.open("out_CoFea.txt", ios::out);


	for(ConsensusMap::Iterator  it_0=consensMap.begin(); it_0!=consensMap.end(); ++it_0){
		ConsensusFeature::HandleSetType cm_hst = it_0->getFeatures();

		//outputFile_ConsensusFeatures << "  ConsensusFeature with children: ";
		for (ConsensusFeature::HandleSetType::iterator it_cf = cm_hst.begin(); it_cf!=cm_hst.end(); ++it_cf){
			outputFile_ConFea << it_cf->getUniqueId() << ",";
		}
		outputFile_ConFea << endl;
	 }

	outputFile_ConFea.close();
 //---END OF read out the consensus features from a .consensusXML data




//---BEGIN OF build up and read out of elution profiles for each feature

  MSExperiment<> expRaw;
  MzMLFile mzml_file;
  mzml_file.load("dataset_picked.mzML", expRaw);

  FeatureMap<> featMap;
  FeatureXMLFile feature_file;
  feature_file.load("dataset_picked.featureXML",featMap);

  map<DoubleReal,DoubleReal>::iterator mapIt;

  DoubleReal minMZ;
  DoubleReal maxMZ;
  DoubleReal minRT;
  DoubleReal maxRT;
  DoubleReal myRT;


  //--- open a file to write the results into
  ofstream outputFile_I;
  outputFile_I.open("out_fea_I.txt", ios::out);
  ofstream outputFile_RT;
  outputFile_RT.open("out_fea_RT.txt", ios::out);
  ofstream outputFile_UnID;
  outputFile_UnID.open("out_fea_UnID.txt", ios::out);


	  //--- iterate along the features in the feature map
	  for (FeatureMap<>::Iterator it_1=featMap.begin(); it_1!=featMap.end(); ++it_1){
		  map<DoubleReal,DoubleReal> ProfileMap;
		  outputFile_UnID<< (*it_1).getUniqueId() << endl;

		  //--- bounderies for the mass traces of a given feature
		  for(Size i=0; i<(*it_1).getConvexHulls().size(); i++){
			  minRT = (*it_1).getConvexHulls()[i].getBoundingBox().minX();
			  maxRT = (*it_1).getConvexHulls()[i].getBoundingBox().maxX();
			  minMZ = (*it_1).getConvexHulls()[i].getBoundingBox().minY();
			  maxMZ = (*it_1).getConvexHulls()[i].getBoundingBox().maxY();

			  //--- elution profile
			  for(MSExperiment<>::AreaIterator it_2=expRaw.areaBegin(minRT,maxRT,minMZ,maxMZ);it_2!=expRaw.areaEnd();++it_2){

				  //cout << (*it_2).getIntensity() << " - "<< (*it_2).getMZ() << " - "<< it_2.getRT() <<  endl;
				  //--- if key in map ProfileMap already exist, the intensity for the key sum up
				  ProfileMap[it_2.getRT()] += (*it_2).getIntensity();
			  }
		  }
	 //--- read out the map; the ProfileMap contain the elution profile
	 for ( mapIt=ProfileMap.begin() ; mapIt != ProfileMap.end(); mapIt++ ){

		 //--- read the intensity values and the corresponding RT in the two different files
		 outputFile_I << (*mapIt).second << ",";
		 outputFile_RT << (*mapIt).first << ",";
	 }
	 outputFile_I << endl;
	 outputFile_RT << endl;

	 }
	 outputFile_I.close();
	 outputFile_RT.close();
	 outputFile_UnID.close();
	 //---END OF build up and read out of elution profiles for each feature

 return 0;
} //end of main
