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

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

Similarity modified and expanded to include both old (greedy) and new (hungarian) measures.

File size: 6.2 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2016  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
15using namespace std;
16
17/** Computes a matrix of distances between all genotypes in the specified
18        .gen file, using the matching and measure weights as specified in the
19        command line. */
20int main(int argc, char *argv[])
21{
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
29
30        if (argc < 8)
31        {
32                printf("Too few parameters!\n");
33                printf("Command line: [-names] <genotypesFile> <measure> <w_dP> <w_dDEG> <w_dNEU> <w_dGEO> <fixZaxis?>\n\n");
34
35                printf("Parameters:\n");
36                printf("  <genotypesFile> name of a file with genotypes\n");
37                printf("  <measure> similarity measure\n");
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");
43
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");
47
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");
52
53                return -1;
54        }
55
56        // prepare output parameters from .gen file
57        vector<Geno *> pvGenos;
58        vector<char *> pvNames;
59
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        }
70
71        // check the parameters
72        // get <genotypesFile> name from command line
73        char *szFileName = argv[iCurrParam];
74
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        }
80       
81        iCurrParam++;
82        szCurrParam = argv[iCurrParam];
83        int measure_type = -1;
84        nResult = sscanf(szCurrParam, " %d ", &measure_type);
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;
98
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];
104                nResult = sscanf(szCurrParam, " %lf ", &M.m_adFactors[i]);
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        }
123
124        iCurrParam++;
125        szCurrParam = argv[iCurrParam];
126        nResult = sscanf(szCurrParam, " %d", &M.fixedZaxis);
127        if (nResult != 1)
128        {
129                // <isZFixed> is not a number -- error
130                printf("<isZFixed> should be a number\n");
131                return -1;
132        }
133        else if (M.fixedZaxis != 0 && M.fixedZaxis != 1)
134        {
135                printf("<isZFixed>=%d. <isZFixed> should be equal to 0 or 1\n", M.fixedZaxis);
136                return -1;
137        }
138
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;
146        GenotypeMiniLoader loader(szFileName);
147        GenotypeMini *loaded;
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        }
168        if (loader.getStatus() == GenotypeMiniLoader::OnError)
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()];
181                for (unsigned int l = 0; l < pvGenos.size(); l++)
182                        aaSimil[k][l] = 0.0;
183        }
184
185        // compute and remember similarities
186        for (unsigned int i = 0; i < pvGenos.size(); i++)
187        {
188                for (unsigned int j = 0; j < pvGenos.size(); j++)
189                {
190                        dSimilarity = M.EvaluateDistance(pvGenos.operator[](i), pvGenos.operator[](j));
191                        aaSimil[i][j] = dSimilarity;
192                }
193        }
194
195        if (bPrintNames)
196        {
197                // if "-names" switch was given, print the number of genotypes and their names
198                printf("%li\n", pvGenos.size());
199                for (unsigned int iGen = 0; iGen < pvNames.size(); iGen++)
200                {
201                        printf("%s\n", pvNames.at(iGen));
202                }
203        }
204
205        // print out the matrix of similarities
206        for (unsigned int i = 0; i < pvGenos.size(); i++)
207        {
208                for (unsigned int j = 0; j < pvGenos.size(); j++)
209                {
210                        printf("%.2lf\t", aaSimil[i][j]);
211                }
212                printf("\n");
213        }
214
215        // delete vectors and arrays
216        for (unsigned int i = 0; i < pvGenos.size(); i++)
217        {
218                delete pvGenos.operator[](i);
219                delete[] pvNames.operator[](i);
220                delete[] aaSimil[i];
221        }
222
223        delete[] aaSimil;
224
225        return 0;
226}
Note: See TracBrowser for help on using the repository browser.