source: cpp/frams/_demos/simil_test.cpp @ 918

Last change on this file since 918 was 895, checked in by oriona, 5 years ago

Unnecessary spaces removed.

File size: 6.2 KB
RevLine 
[349]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[605]2// Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
[349]3// See LICENSE.txt for details.
4
5
6#include <vector>
[391]7#include "common/loggers/loggertostdout.h"
[349]8#include "frams/_demos/genotypeloader.h"
9#include "frams/genetics/preconfigured.h"
[382]10#include "common/virtfile/stdiofile.h"
[349]11#include "frams/model/similarity/simil_model.h"
12
13
14
15using namespace std;
16
17/** Computes a matrix of distances between all genotypes in the specified
[606]18        .gen file, using the matching and measure weights as specified in the
19        command line. */
[349]20int main(int argc, char *argv[])
21{
[606]22        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
23        typedef double *pDouble;
24        int iCurrParam = 0; // index of the currently processed parameter
25        char *szCurrParam = NULL;
26        ModelSimil M; // similarity computing object
27        bool bPrintNames = false; // specifies if names of genotypes are to be printed
28        int nResult = 0; // a temporary result
[349]29
[893]30        if (argc < 8)
[606]31        {
32                printf("Too few parameters!\n");
[893]33                printf("Command line: [-names] <genotypesFile> <measure> <w_dP> <w_dDEG> <w_dNEU> <w_dGEO> <fixZaxis?>\n\n");
[349]34
[606]35                printf("Parameters:\n");
36                printf("  <genotypesFile> name of a file with genotypes\n");
[893]37                printf("  <measure> similarity measure\n");
[606]38                printf("  <w_dP> weight of the difference in the number of parts\n");
39                printf("  <w_dDEG> weight of the difference in degrees of matched parts\n");
40                printf("  <w_dNEU> weight of the difference in neurons of matched parts\n");
41                printf("  <w_dGEO> weight of the distance of matched parts\n");
42                printf("  <fixZaxis?> should the 'z' (vertical) coordinate be fixed during the alignment? (0 or 1)\n\n");
[349]43
[606]44                printf("Switches:\n");
45                printf("  -names specifies that the number and names of genotypes are to be printed to output\n");
46                printf("  before the distance matrix; by default the number and names are not printed\n\n");
[349]47
[606]48                printf("Outputs a symmetric distance matrix in the format:\n");
49                printf("  <row_1> (columns in a row are separated by TABs)\n");
50                printf("  ...\n");
51                printf("  <row_n>\n");
[349]52
[606]53                return -1;
54        }
[349]55
[606]56        // prepare output parameters from .gen file
57        vector<Geno *> pvGenos;
58        vector<char *> pvNames;
[349]59
[606]60        // check if there is a switch
61        iCurrParam = 1;
62        szCurrParam = argv[iCurrParam];
63        if (strcmp(szCurrParam, "-names") == 0)
64        {
65                // switch "-names" was given; print names also
66                bPrintNames = true;
67                // pass to the next parameter
68                iCurrParam++;
69        }
[349]70
[606]71        // check the parameters
72        // get <genotypesFile> name from command line
73        char *szFileName = argv[iCurrParam];
[349]74
[606]75        // initially set measure components' weights to invalid values (negative)
76        for (int i = 0; i < M.GetNOFactors(); i++)
77        {
78                M.m_adFactors[i] = -1.0;
79        }
[893]80       
81        iCurrParam++;
82        szCurrParam = argv[iCurrParam];
83        int measure_type = -1;
[895]84        nResult = sscanf(szCurrParam, "%d", &measure_type);
[893]85        if (nResult != 1)
86        {
87                printf("Measure type should be a number!\n");
88                return -1;
89        }
90       
91        if (measure_type != 0 && measure_type != 1)
92        {
93                printf("Measure type should be 0 (flexible criteria order and optimal matching) or 1 (vertex degree order and greedy matching)!\n");
94                return -1;             
95        }
96       
97        M.matching_method = measure_type;
[349]98
[606]99        const char *params[] = { "<w_dP>", "<w_dDEG>", "<w_dNEU>", "<w_dGEO>" };
100        for (int i = 0; i < M.GetNOFactors(); i++)
101        {
102                iCurrParam++;
103                szCurrParam = argv[iCurrParam];
[895]104                nResult = sscanf(szCurrParam, "%lf", &M.m_adFactors[i]);
[606]105                if (nResult != 1)
106                {
107                        // <w_dX> is not a number -- error
108                        printf("%s", params[i]);
109                        printf(" should be a number\n");
110                        return -1;
111                }
112                else
113                {
114                        // <w_dX> is a number; check if nonnegative
115                        if (M.m_adFactors[i] < 0.0)
116                        {
117                                printf("%s", params[i]);
118                                printf(" should be a nonnegative number\n");
119                                return -1;
120                        }
121                }
122        }
[349]123
[606]124        iCurrParam++;
125        szCurrParam = argv[iCurrParam];
[895]126        nResult = sscanf(szCurrParam, "%d", &M.fixedZaxis);
[606]127        if (nResult != 1)
128        {
129                // <isZFixed> is not a number -- error
130                printf("<isZFixed> should be a number\n");
131                return -1;
132        }
[612]133        else if (M.fixedZaxis != 0 && M.fixedZaxis != 1)
[606]134        {
[612]135                printf("<isZFixed>=%d. <isZFixed> should be equal to 0 or 1\n", M.fixedZaxis);
[606]136                return -1;
137        }
[349]138
[606]139        // read the input file
140        // prepare loading of genotypes from a .gen file
141        // create some basic genotype converters
142        PreconfiguredGenetics genetics;
143        StdioFileSystem_autoselect stdiofilesys;
144
145        long count = 0, totalsize = 0;
[732]146        GenotypeMiniLoader loader(szFileName);
147        GenotypeMini *loaded;
[606]148        while (loaded = loader.loadNextGenotype())
149        {
150                // while a valid genotype was loaded
151                count++;
152                totalsize += loaded->genotype.len();
153                // create a Geno object based on the MiniGenotype
154                Geno *pNextGenotype = new Geno(loaded->genotype);
155                if ((pNextGenotype != NULL) && (pNextGenotype->isValid()))
156                {
157                        pvGenos.push_back(pNextGenotype);
158                        char *szNewName = new char[loaded->name.len() + 1];
159                        strcpy(szNewName, loaded->name.c_str());
160                        pvNames.push_back(szNewName);
161                }
162                else
163                {
164                        printf("Genotype %2li is not valid\n", count);
165                        if (pNextGenotype != NULL) delete pNextGenotype;
166                }
167        }
[732]168        if (loader.getStatus() == GenotypeMiniLoader::OnError)
[606]169        {
170                printf("Error: %s", loader.getError().c_str());
171        }
172
173        double dSimilarity = 0.0;
174        double **aaSimil = NULL; // array of similarities
175
176        // create the empty array of similarities
177        aaSimil = new pDouble[pvGenos.size()];
178        for (unsigned int k = 0; k < pvGenos.size(); k++)
179        {
180                aaSimil[k] = new double[pvGenos.size()];
[455]181                for (unsigned int l = 0; l < pvGenos.size(); l++)
[606]182                        aaSimil[k][l] = 0.0;
183        }
[349]184
[606]185        // compute and remember similarities
[455]186        for (unsigned int i = 0; i < pvGenos.size(); i++)
[606]187        {
[455]188                for (unsigned int j = 0; j < pvGenos.size(); j++)
[606]189                {
190                        dSimilarity = M.EvaluateDistance(pvGenos.operator[](i), pvGenos.operator[](j));
191                        aaSimil[i][j] = dSimilarity;
192                }
193        }
[349]194
[606]195        if (bPrintNames)
196        {
197                // if "-names" switch was given, print the number of genotypes and their names
198                printf("%li\n", pvGenos.size());
[455]199                for (unsigned int iGen = 0; iGen < pvNames.size(); iGen++)
[606]200                {
201                        printf("%s\n", pvNames.at(iGen));
202                }
203        }
[349]204
[606]205        // print out the matrix of similarities
[455]206        for (unsigned int i = 0; i < pvGenos.size(); i++)
[606]207        {
[455]208                for (unsigned int j = 0; j < pvGenos.size(); j++)
[606]209                {
210                        printf("%.2lf\t", aaSimil[i][j]);
211                }
212                printf("\n");
213        }
[349]214
[606]215        // delete vectors and arrays
[455]216        for (unsigned int i = 0; i < pvGenos.size(); i++)
[606]217        {
218                delete pvGenos.operator[](i);
219                delete[] pvNames.operator[](i);
220                delete[] aaSimil[i];
221        }
[349]222
[606]223        delete[] aaSimil;
[349]224
[606]225        return 0;
[372]226}
Note: See TracBrowser for help on using the repository browser.