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