#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>

using namespace std;

//reads std input where it expects the quadruples:
//timePoint of the scheduler, timePoint of the cell, cell phase, cell ID
//also expects one cmd line parameter with filename, whose name
//it just prints on the screen as a part of an example of
//a gnuplot command
//
//with the meaning that at the given scheduler time the cell of
//given ID is actually at its respective timePoint and going
//to conduct given phase after which it ends up in a new timePoint,
//which we can learn only after we spot another quadruple of
//this cell
//
//the formated gnuplot command file is printer on std output

class Pair {
  public:
	int sTP;
	int cTP;
	int P;
};

int main(int argc,char **argv) {
	int sTP, cTP, ID;
	string phase;
	int counter=0;

	map< int,vector< Pair > > plot;

	while (cin >> sTP >> cTP >> phase >> ID) {
		Pair point;

		//convert phase into number
		int P=0;
		switch (phase[0]) {
			case 'P': P=0; break;
			case 'M': P=1; break;
			case 'A': P=2; break;
			case 'T': P=3; break;
			case 'C': P=4; break;
			case 'G': if (phase[1]=='1') P=5; else P=7; break;
			case 'S': P=6; break;
		}
		
		//first, read the last point and update the cTP
		//to show that the cell has moved forward in its
		//time during the given scheduler time (and the
		//cell really has moved since we have a record here)
		point.cTP=cTP;
		if (!plot[ID].empty()) {
			point.sTP=plot[ID].back().sTP;
			point.P=plot[ID].back().P; //shows finished phase...
			//point.P=P;		//shows comming phase...
						//...in the horizontal line segment
			plot[ID].push_back(point);

			point.sTP=sTP;
			plot[ID].push_back(point);
		}

		point.sTP=sTP;
		point.P=P;
		plot[ID].push_back(point);

		if (P == 4) {
			//if this phase is Cytokinesis, we need to draw
			//a connecting line to both daughters
			int dID,dTP;
			cin >> dID >> dTP;
			cin >> dID >> dTP;
			//both daughters have the same dTP
			//(a time point since which they started existing)

			point.cTP=dTP;
			plot[ID].push_back(point); //vert. line

			point.sTP=dTP;
			plot[ID].push_back(point); //hor. line
		}

		++counter;
	}

	cout << "# processed " << counter << " tripplets\n\n";
	cout << "# columns: scheduler timePoint, cell timePoint, starting phase\n";
	cout << "# phases: \n\
# 0 - Prophase\n\
# 1 - Metaphase\n\
# 2 - Anaphase\n\
# 3 - Telophase\n\
# 4 - Cytokinesis\n\
# 5 - G1-phase\n\
# 6 - S-phase\n\
# 7 - G2-phase\n\n";

	map< int,vector< Pair > >::const_iterator cell=plot.begin();
	int maxID=-1;
	while (cell != plot.end()) {
		cout << "# ID=" << cell->first << "\n";
		maxID=cell->first;
		
		vector<Pair>::const_iterator points=cell->second.begin();
		while (points != cell->second.end()) {
			cout << points->sTP << "  " << points->cTP <<  "  " << points->P << "\n";
			points++;
		}
		cout << "\n\n";
		cell++;
	}

	cout << "# sample commands to show everything:\n\n";

	cout << "# avoids plotting line titles over the lines\n";
	cout << "# set key rmargin\n\n";

	cout << "# line colour+style -> cell phase; point style -> cell ID\n";
	cout << "# plot [:][0:] for [ID=1:" << maxID << "] for [p=0:7] '" << argv[1] << "' u 1:($3==p ? $2 : 1/0) index -1+ID w lp lw 2 lt p pt ID notitle;\n";
	cout << "# plot [:][0:] for [ID in \"1 16 17\"] for [p=0:7] '" << argv[1] << "' u 1:($3==p ? $2 : 1/0) index -1+ID w lp lw 2 lt p pt ID notitle;\n\n";

	cout << "# append the next line to provide titles for lines...\n";
	cout << "# , for [ID=1:" << maxID << "] -1 w p ps 1.3 pt ID title \"ID=\".ID;\n";
	//cout << "# , for [ID in \"1 16 17\"] -1 w p ps 1.3 pt ID title \"ID=\".ID;\n";

	return(0);
}
