// This file is a part of Framsticks SDK. http://www.framsticks.com/ // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. // See LICENSE.txt for details. #include #include #include "common/loggers/loggertostdout.h" #include "frams/_demos/genotypeloader.h" #include "frams/genetics/preconfigured.h" #include "common/virtfile/stdiofile.h" #include "frams/model/similarity/measure-distribution.h" #include "frams/model/similarity/measure-greedy.h" #include "frams/model/similarity/measure-hungarian.h" using namespace std; int add_double_param(std::vector *args, int pos, std::vector *params, std::vector *params_names) { for (unsigned int i = 0; i < params_names->size(); i++) { try { params->push_back(std::stod(args->at(pos))); pos++; } catch (const std::invalid_argument&) { printf("%s should be a number\n", params_names->at(i).c_str()); return -1; } catch (const std::out_of_range&) { printf("%s should be inside double range\n", params_names->at(i).c_str()); return -1; } } return 0; } /** Computes a matrix of distances between all genotypes in the specified .gen file, using the matching and measure weights as specified in the command line. */ int main(int argc, char *argv[]) { typedef double *pDouble; LoggerToStdout messages_to_stdout(LoggerBase::Enable); SimilMeasure *simil_measure = nullptr; if (argc < 5) { printf("Too few parameters!\n"); printf("Command line: [-names] \n\n"); printf("Command line: [-names] \n\n"); printf("Parameters:\n"); printf(" name of a file with genotypes\n"); printf(" similarity measure name (greedy/hungarian/distribution)\n"); printf("\n"); printf("Parameters of greedy and hungarian measures:\n"); printf(" weight of the difference in the number of parts\n"); printf(" weight of the difference in degrees of matched parts\n"); printf(" weight of the difference in neurons of matched parts\n"); printf(" weight of the distance of matched parts\n"); printf(" should the 'z' (vertical) coordinate be fixed during the alignment? (0 or 1)\n\n"); printf("Parameters of distribution measure:\n"); printf(" sampling density\n"); printf(" number of histogram bins\n"); printf(" number of samples taken\n\n"); printf("Switches:\n"); printf(" -names specifies that the number and names of genotypes are to be printed to output\n"); printf(" before the distance matrix; by default the number and names are not printed\n\n"); printf("Outputs a symmetric distance matrix in the format:\n"); printf(" (columns in a row are separated by TABs)\n"); printf(" ...\n"); printf(" \n"); return -1; } std::vector args; for (int i = 1; i < argc; i++) args.push_back(std::string(argv[i])); bool print_names = false; int pos = 1; if (args.at(0).compare("-names")==0) { print_names = true; pos = 2; } string measure_name = args.at(pos); pos++; std::vector params; if (measure_name.compare("greedy")==0 || measure_name.compare("hungarian")==0) { std::vector params_names{ "", "", "", "", "" }; if (add_double_param(&args, pos, ¶ms, ¶ms_names) == -1) return -1; if (measure_name.compare("greedy")==0) simil_measure = new SimilMeasureGreedy(); else simil_measure = new SimilMeasureHungarian(); } else if (measure_name.compare("distribution")==0) { std::vector params_names{ "", "", "" }; if (add_double_param(&args, pos, ¶ms, ¶ms_names)==-1) return -1; simil_measure = new SimilMeasureDistribution(); } else { printf("Measure type should be greedy (flexible criteria order and optimal matching), hungarian (vertex degree order and greedy matching) or distribution!\n"); return -1; } simil_measure->setParams(params); // read the input file // prepare loading of genotypes from a .gen file // create some basic genotype converters PreconfiguredGenetics genetics; StdioFileSystem_autoselect stdiofilesys; // prepare output parameters from .gen file vector pvGenos; vector pvNames; long count = 0, totalsize = 0; GenotypeMiniLoader loader(args.at(0).c_str()); GenotypeMini *loaded; while (loaded = loader.loadNextGenotype()) { // while a valid genotype was loaded count++; totalsize += loaded->genotype.length(); // create a Geno object based on the MiniGenotype Geno *pNextGenotype = new Geno(loaded->genotype); if ((pNextGenotype != NULL) && (pNextGenotype->isValid())) { pvGenos.push_back(pNextGenotype); char *szNewName = new char[loaded->name.length() + 1]; strcpy(szNewName, loaded->name.c_str()); pvNames.push_back(szNewName); } else { printf("Genotype %2li is not valid\n", count); if (pNextGenotype != NULL) delete pNextGenotype; } } if (loader.getStatus() == GenotypeMiniLoader::OnError) { printf("Error: %s", loader.getError().c_str()); } double dSimilarity = 0.0; double **aaSimil = NULL; // array of similarities // create the empty array of similarities aaSimil = new pDouble[pvGenos.size()]; for (unsigned int k = 0; k < pvGenos.size(); k++) { aaSimil[k] = new double[pvGenos.size()]; for (unsigned int l = 0; l < pvGenos.size(); l++) aaSimil[k][l] = 0.0; } // compute and remember similarities for (unsigned int i = 0; i < pvGenos.size(); i++) { for (unsigned int j = 0; j < pvGenos.size(); j++) { dSimilarity = simil_measure->evaluateDistance(pvGenos.operator[](i), pvGenos.operator[](j)); aaSimil[i][j] = dSimilarity; } } if (print_names) { // if "-names" switch was given, print the number of genotypes and their names printf("%li\n", pvGenos.size()); for (unsigned int iGen = 0; iGen < pvNames.size(); iGen++) { printf("%s\n", pvNames.at(iGen)); } } // print out the matrix of similarities for (unsigned int i = 0; i < pvGenos.size(); i++) { for (unsigned int j = 0; j < pvGenos.size(); j++) { printf("%.2lf\t", aaSimil[i][j]); } printf("\n"); } // delete vectors and arrays for (unsigned int i = 0; i < pvGenos.size(); i++) { delete pvGenos.operator[](i); delete[] pvNames.operator[](i); delete[] aaSimil[i]; } delete[] aaSimil; delete simil_measure; return 0; }