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
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5
6#include <vector>
7#include "common/loggers/loggertostdout.h"
8#include "frams/_demos/genotypeloader.h"
9#include "frams/genetics/preconfigured.h"
10#include "common/virtfile/stdiofile.h"
11#include "frams/model/similarity/simil_model.h"
12
13
14
15/** Computes a matrix of distances between all genotypes in the specified
16        .gen file, using the matching and measure weights as specified in the
17        command line. */
18int main(int argc, char *argv[])
19{
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
27
28        if (argc < 8)
29        {
30                printf("Too few parameters!\n");
31                printf("Command line: [-names] <genotypesFile> <measure> <w_dP> <w_dDEG> <w_dNEU> <w_dGEO> <fixZaxis?>\n\n");
32
33                printf("Parameters:\n");
34                printf("  <genotypesFile> name of a file with genotypes\n");
35                printf("  <measure> similarity measure\n");
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");
41
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");
45
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");
50
51                return -1;
52        }
53
54        // prepare output parameters from .gen file
55        vector<Geno *> pvGenos;
56        vector<char *> pvNames;
57
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        }
68
69        // check the parameters
70        // get <genotypesFile> name from command line
71        char *szFileName = argv[iCurrParam];
72
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        }
78
79        iCurrParam++;
80        szCurrParam = argv[iCurrParam];
81        int measure_type = -1;
82        nResult = sscanf(szCurrParam, "%d", &measure_type);
83        if (nResult != 1)
84        {
85                printf("Measure type should be a number!\n");
86                return -1;
87        }
88
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");
92                return -1;
93        }
94
95        M.matching_method = measure_type;
96
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];
102                nResult = sscanf(szCurrParam, "%lf", &M.m_adFactors[i]);
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        }
121
122        iCurrParam++;
123        szCurrParam = argv[iCurrParam];
124        nResult = sscanf(szCurrParam, "%d", &M.fixedZaxis);
125        if (nResult != 1)
126        {
127                // <isZFixed> is not a number -- error
128                printf("<isZFixed> should be a number\n");
129                return -1;
130        }
131        else if (M.fixedZaxis != 0 && M.fixedZaxis != 1)
132        {
133                printf("<isZFixed>=%d. <isZFixed> should be equal to 0 or 1\n", M.fixedZaxis);
134                return -1;
135        }
136
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;
144        GenotypeMiniLoader loader(szFileName);
145        GenotypeMini *loaded;
146        while (loaded = loader.loadNextGenotype())
147        {
148                // while a valid genotype was loaded
149                count++;
150                totalsize += loaded->genotype.length();
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);
156                        char *szNewName = new char[loaded->name.length() + 1];
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        }
166        if (loader.getStatus() == GenotypeMiniLoader::OnError)
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()];
179                for (unsigned int l = 0; l < pvGenos.size(); l++)
180                        aaSimil[k][l] = 0.0;
181        }
182
183        // compute and remember similarities
184        for (unsigned int i = 0; i < pvGenos.size(); i++)
185        {
186                for (unsigned int j = 0; j < pvGenos.size(); j++)
187                {
188                        dSimilarity = M.EvaluateDistance(pvGenos.operator[](i), pvGenos.operator[](j));
189                        aaSimil[i][j] = dSimilarity;
190                }
191        }
192
193        if (bPrintNames)
194        {
195                // if "-names" switch was given, print the number of genotypes and their names
196                printf("%li\n", pvGenos.size());
197                for (unsigned int iGen = 0; iGen < pvNames.size(); iGen++)
198                {
199                        printf("%s\n", pvNames.at(iGen));
200                }
201        }
202
203        // print out the matrix of similarities
204        for (unsigned int i = 0; i < pvGenos.size(); i++)
205        {
206                for (unsigned int j = 0; j < pvGenos.size(); j++)
207                {
208                        printf("%.2lf\t", aaSimil[i][j]);
209                }
210                printf("\n");
211        }
212
213        // delete vectors and arrays
214        for (unsigned int i = 0; i < pvGenos.size(); i++)
215        {
216                delete pvGenos.operator[](i);
217                delete[] pvNames.operator[](i);
218                delete[] aaSimil[i];
219        }
220
221        delete[] aaSimil;
222
223        return 0;
224}
Note: See TracBrowser for help on using the repository browser.