// // Programmer: Craig Stuart Sapp // Creation Date: Sat Jul 1 02:48:55 PDT 2006 // Last Modified: Mon Jul 3 06:25:05 PDT 2006 (converted from hicor) // Last Modified: Thu Oct 5 21:39:33 PDT 2006 (added color from input file) // Last Modified: Wed Jul 11 23:12:42 PDT 2007 (added smoothing option) // Filename: ...sig/examples/all/polyhicor.cpp // Web Address: http://sig.sapp.org/examples/museinfo/polyhicor/polyhicor.cpp // Syntax: C++; museinfo // // Description: Generate a comparison between multiple sets of // number sequences. // #include "Array.h" #include "Options.h" #include "PixelColor.h" #include #include #include #ifndef OLDCPP using namespace std; #include #include #include #else #include #include #include #endif #define COLOR_BW 0 #define COLOR_HUE 1 #define SHAPE_TRIANGLE 0 #define SHAPE_RECTANGLE 1 // function declarations: void checkOptions (Options& opts, int argc, char** argv); void example (void); void usage (const char* command); void calculateCorrelations (Array >& correlations, Array& a, Array& b); double pearsonCorrelation (int size, double* a, double* b); double getMean (int size, double* a); void printPolyImageTriangle (Array > >& correlations); void printPolyImageRectangle (Array > >& correlations); void printCorrelationPixel (int valueindex, int count, double correlation, int style); int getBestIndex (Array > >& correlations, int i, int j, int targetindex); void printArea (Array& pids, Array > >& correlationset); void readData (Array >& data, const char* hfile); void smoothData (Array >& data, double gain); void unsmoothData (Array >& data, double gain); void smoothSequence (Array& sequence, double gain); void unsmoothSequence (Array& sequence, double gain); // User interface variables: Options options; int dataq = 0; // debug: display input data only with -d int colortype = COLOR_HUE; // used with various color options int plotshape = SHAPE_TRIANGLE; // used with -r option int targetindex = 0; // used with -n option int scaleq = 0; // used with -s option int areaQ = 0; // used with -A option int lowlimit = 0; // used with -A option double smooth = 0.0; // used with -S option int unsmoothq = 0; // used with -U option Array colorindex; char pids[50000] = {0}; char labels[50000] = {0}; Array pidindex; Array labelindex; const char null[2] = "."; ////////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { // process the command-line options checkOptions(options, argc, argv); colorindex.setSize(1000); colorindex.setSize(0); Array > data; data.setSize(100); data.setSize(0); data.setGrowth(100); data.allowGrowth(1); string filename = ""; if (options.getArgCount() <= 0) { filename = ""; } else { filename = options.getArg(1); } readData(data, filename.c_str()); if (smooth != 0.0) { if (!unsmoothq) { smoothData(data, smooth); } else { unsmoothData(data, smooth); } } /* int inputsize = 0; double inputvalues[200]; int ii; char *ptr; double value; const char* blanks = " \t,:"; char templine[4096]; while (!infile.eof()) { infile.getline(templine, 4096, '\n'); if (infile.eof() && (strcmp(templine, "") == 0)) { break; } else if (isdigit(templine[0]) || templine[0] == '-' || templine[0] == '+') { ptr = strtok(templine, blanks); inputsize = 0; while (ptr != NULL && sscanf(ptr, "%lf", &value) == 1) { inputvalues[inputsize++] = value; if (inputsize >= 100) { break; } ptr = strtok(NULL, blanks); } if (inputsize > 0 && data.getSize() == 0) { data.setSize(inputsize); for (ii=0; ii > > correlationset; correlationset.setSize(data.getSize()); // check the bounds of target index: if (targetindex < 0) { targetindex = 0; } else if (targetindex >= data.getSize()) { targetindex = 0; } for (i=0; i >& data, double gain) { int i; for (i=0; i >& data, double gain) { int i; for (i=0; i& sequence, double gain) { double oneminusgain = 1.0 - gain; int i; int ssize = sequence.getSize(); // reverse filtering first for (i=ssize-2; i>=0; i--) { sequence[i] = gain*sequence[i] + oneminusgain*sequence[i+1]; } // then forward filtering for (i=1; i& sequence, double gain) { Array smoothed = sequence; smoothSequence(smoothed, gain); int i; for (i=0; i& pidindex, Array > >& correlationset) { int setcount = correlationset.getSize(); int mastercount = 0; Array localcount; localcount.allowGrowth(0); localcount.setSize(setcount); localcount.setAll(0); int nontarget = targetindex + 1; if (nontarget >= correlationset.getSize()) { nontarget = 0; } int length = correlationset[0].getSize(); if (length == 0) { length = correlationset[1].getSize(); } int i; int j; int datawidth; int bestindex; for (i=0; i > >& correlations) { string background = options.getString("background"); int nontarget = targetindex + 1; if (nontarget >= correlations.getSize()) { nontarget = 0; } int seqcount = correlations.getSize(); int length = correlations[0].getSize(); if (length == 0) { length = correlations[1].getSize(); } int maxcolumn = length * 2; int maxrow = (length - 1) * 2; cout << "P3\n"; cout << maxcolumn << " " << maxrow << "\n"; cout << "255\n"; int i, j; int datawidth; int bgwidth; int bestindex; for (i=0; i > >& correlations, int i, int j, int targetindex) { // int doubled = 0; int bestindex; bestindex = 0; if (targetindex == 0) { bestindex = 1; } int index = 0; if (targetindex == 0) { index = 1; } if (index == bestindex) { index++; } if (index == targetindex) { index++; } for ( ; index < correlations.getSize(); index++) { if (index == targetindex) { continue; } if (correlations[index][i][j] > correlations[bestindex][i][j]) { bestindex = index; } } return bestindex; } ////////////////////////////// // // printPolyImageRectangle -- // void printPolyImageRectangle(Array > >& correlations) { return; /* int maxcolumn = correlation[0].getSize() * 2; int maxrow = (correlation[0].getSize()-1) * 2; cout << "P3\n"; cout << maxcolumn << " " << maxrow << "\n"; cout << "255\n"; int i, j; int datawidth; int jstretch; for (i=0; i >& correlations, Array& a, Array& b) { int i, j; correlations.setSize(a.getSize()); for (i=0; i >& data, const char* filename) { data.setSize(500); data.setSize(0); data.setGrowth(500); PixelColor newcolor; data.allowGrowth(1); ifstream infile; #ifndef OLDCPP infile.open(filename, ios::in); #else infile.open(filename, ios::in | ios::nocreate); #endif if (!infile.is_open()) { cerr << "Cannot open file for reading: " << filename << endl; exit(1); } int inputsize = 0; double inputvalues[500]; int ii; char *ptr; double value; const char* blanks = " \t,:"; int foundlabels = 0; char templine[100000]; while (!infile.eof()) { infile.getline(templine, 90000, '\n'); if (!foundlabels) { if (!((strncmp(templine, "pid", 3) == 0) || (strncmp(templine, "!!", 2) == 0) || (strncmp(templine, "*", 1) == 0) || (strncmp(templine, "!pid", 4) == 0))) { foundlabels = 1; strcpy(labels, templine); continue; } } if (infile.eof() && (strcmp(templine, "") == 0)) { break; } else if (isdigit(templine[0]) || templine[0] == '-' || templine[0] == '+') { ptr = strtok(templine, blanks); inputsize = 0; while (ptr != NULL && sscanf(ptr, "%lf", &value) == 1) { inputvalues[inputsize++] = value; if (inputsize >= 100) { break; } ptr = strtok(NULL, blanks); } if (inputsize > 0 && data.getSize() == 0) { data.setSize(inputsize); for (ii=0; ii