]> ruin.nu Git - germs.git/blob - src/main.cpp
fe581385516cc1133f80a49a9e338211a7a69279
[germs.git] / src / main.cpp
1 #include <iostream>
2 #include <vector>
3 #include <queue>
4 #include <iterator>
5 #include <fstream>
6
7 using namespace std;
8
9 #include <unistd.h>
10
11 #include "geneorder.h"
12 #include "modelidentifier.h"
13 #include "genesorter.h"
14 #include "sortaction.h"
15 #include "genealgorithms.h"
16 #include "model.h"
17
18 int main(int argc, char** argv){
19
20         string ann = "default.ann";
21         Model model(0);
22         bool detectModel = true;
23         bool onlyIdentify = false;
24
25         //Parse command line arguments
26         int opt;
27         while ((opt = getopt(argc, argv, "im:n:h")) != -1) {
28                 switch (opt) {
29                         case 'm':
30                                 model = Model::modelFactory(optarg);
31                                 detectModel = false;
32                                 break;
33                         case 'n':
34                                 ann = optarg;
35                                 break;
36                         case 'i':
37                                 onlyIdentify = true;
38                                 break;
39                         case 'h':
40                                 cout << "Usage: " << argv[0] << " [OPTION] [FILE]" << endl
41                                         << endl << "  -m <model> Specifies which model to use for sorting: Whirl, X, Zipper, FatX or Cloud "
42                                         << endl << "  -n <ann>   Specifies which artificial neural network to use for identification. '" << ann << "' is used by default"
43                                         << endl << "  -h         Prints this help message"
44                                         << endl << endl
45                                         << "With no FILE, or if FILE is '-', stdin will be used"
46                                         << endl;
47                                 exit(EXIT_SUCCESS);
48                                 break;
49                         default: /* '?' */
50                                 cerr << "Usage:  " << argv[0] << " [-n <ann>] [-h] [FILE]" << endl;
51                                 exit(EXIT_FAILURE);
52                 }
53         }
54
55         //Open file, or stdin
56         istream* in;
57         ifstream file;
58         if (optind == argc || *argv[optind] == '-'){
59                 in = &cin;
60         }else{
61                 file.open(argv[optind]);
62                 if (file.fail()){
63                         cerr << "Could not open file: '" << argv[optind] << "'" << endl;
64                         exit(EXIT_FAILURE);
65                 }
66                 in = &file;
67         }
68
69         //Parse the gene order permutation
70         vector<Gene> g;
71         copy(istream_iterator<int>(*in), istream_iterator<int>(),
72              back_inserter(g));
73         GeneOrder go(g.begin(),g.end());
74
75         //Identify the model
76         ModelIdentifier mi(ann);
77         priority_queue<pair<double,Model> > pq = mi.identify(go);
78         if (detectModel){
79                 model = pq.top().second;
80         }
81         while (pq.size() > 0){
82                 cout << "Model: " << pq.top().second.name() << " score: " << pq.top().first << endl;
83                 pq.pop();
84         }
85         cout << "Using model: " << model.name() << endl;
86
87         cout << "Distance: " << inversionDistance(go) << endl;
88         //copy(go.begin(), go.end(), ostream_iterator<int>(cout, " "));
89         //cout << endl;
90
91         if (onlyIdentify){
92                 return EXIT_SUCCESS;
93         }
94         //Sort
95         GeneSorter so;
96         GeneSorter::ActionList al = so.sort(go,model);
97
98         //Print the result
99         double score = 0;
100         for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){
101                 cout << "Action: " << sa->toString() << " model score: " << model.score(*sa,go) << endl;
102                 score += model.score(*sa,go);
103         }
104         cout << "Avg score: " << score / al.size() << endl;
105
106         return EXIT_SUCCESS;
107 }