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

Last change on this file since 1004 was 1004, checked in by Maciej Komosinski, 4 years ago

Cosmetic

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