#ifndef PARSER_H
#define PARSER_H

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <math.h>

#include <sys/stat.h>
#include <stdlib.h>
#include <fstream>
#include <map>
#include <algorithm>

using namespace std;

struct binner {
        double p_value;
        string state;
        double value;
        vector<string> genes;
        int bin;
};

struct color {
        int color[3];
};

struct colorbar {
        color max_color;
        color max_center;
        color min_color;
        color min_center;
        double max;
        double center;
        double min;
        string max_label;
        string min_label;
};

struct metabin {
        vector<string> genes;
        vector<string> evalues;
        double min;
        double max;
        int bin;
};

struct nucleotide_motif {
        string candidate;
        vector<binner> bins;
        string location;
        double mi;
        double z_score;
        int robustness;
        int robustness_max;
        int position_bias;
        int orientation_bias;
        double conservation_index;
        string seed;
        vector<string> names;
        vector<string> protein_array;
        int cluster;
};

struct fire_general {
        colorbar scalebar;
        int bincount;
        vector<metabin> metabins;
        vector<nucleotide_motif> motifs;
        int clusters;
        string exptype;
        string querysettings;
        int status;
};

struct fire_interaction {
        colorbar scalebar;
        vector<nucleotide_motif> motifs;
        vector< vector<double> > interaction_matrix_r0;
        vector< vector<double> > interaction_matrix_r1;
        vector< vector<double> > interaction_matrix_r3;
        vector< vector<double> > interaction_matrix_r4;
        string querysettings;
        int status;
};

struct protein_motif {
        string candidate;
        vector<binner> bins;
        double mi;
        double z_score;
        int robustness;
        int robustness_max;
        vector<string> names;
        vector<string> details;
        int cluster;
};

struct firepro_general {
        colorbar scalebar;
        int bincount;
        vector<metabin> metabins;
        vector<protein_motif> motifs;
        int clusters;
        string exptype;
        string querysettings;
        int status;
};

struct firepro_interaction {
        colorbar scalebar;
        vector<protein_motif> motifs;
        vector< vector<double> > interaction_matrix_r0;
        vector< vector<double> > interaction_matrix_r1;
        vector< vector<double> > interaction_matrix_r3;
        vector< vector<double> > interaction_matrix_r4;
        string querysettings;
        int status;
};

struct pathway {
        vector<binner> bins;
        string pathway;
};

struct ipage_general {
        colorbar scalebar;
        int bincount;
        vector<metabin> metabins;
        vector<pathway> pathways;
        string exptype;
        map<string, string> querysettings;
        int status;
};

struct prmg_general {
        colorbar scalebar;
        vector<pathway> pathways;
        vector<nucleotide_motif> motifs;
        vector< vector<double> > interaction_matrix;
        int status;
};

struct prmg_general_protein {
        colorbar scalebar;
        vector<pathway> pathways;
        vector<protein_motif> motifs;
        vector< vector<double> > interaction_matrix;
        int status;
};

struct fire_results {
        fire_general general;
        fire_interaction interaction;
};

struct firepro_results {
        firepro_general general;
        firepro_interaction interaction;
};

bool file_exists(string filename);
void verbose_output(string output); // unused
void error_message(string error); // unused
ipage_general parse_ipage(string filename);
vector<string> split_whitespace (string line);
vector<string> split_slash (string line);
vector<string> split_tab (string line);
vector<string> split_semicolon (string line);
vector<string> split_comma (string line);
void dump_ipage(ipage_general results);
void dump_fire(fire_results results);
void dump_firepro(firepro_results results);
void dump_prmg(prmg_general results);
void dump_prmg_protein(prmg_general_protein results);
void dump_all(string filename);
fire_results parse_fire(string filename, string type);
firepro_results parse_firepro(string filename);
prmg_general parse_prmg(fire_results fire, ipage_general ipage, string filename);
prmg_general_protein parse_prmg_protein(firepro_results fire, ipage_general ipage, string filename);
string determine_results (string filename);

#endif // PARSER_H