d00f76667c0ea91810a6eac1e748abdbee2fe208
[mussa.git] / mussa_class.cc
1 //                        ----------------------------------------
2 //                          ---------- mussa_class.cc -----------
3 //                        ----------------------------------------
4
5 #include "mussa_class.hh"
6
7
8 Mussa::Mussa()
9 {
10 }
11
12 void
13 Mussa::setup(char * para_file_path)
14 {
15   ifstream para_file;
16   string file_data_line;
17   string param, value, annot_file;
18   int split_index, fasta_index;
19   bool seq_params, did_seq;
20   int bogo;
21
22
23   win_append = false;
24   thres_append = false;
25
26   para_file.open(para_file_path, ios::in);
27
28   getline(para_file,file_data_line);
29   split_index = file_data_line.find(" ");
30   param = file_data_line.substr(0,split_index);
31   value = file_data_line.substr(split_index+1);
32     
33
34   while (!para_file.eof())
35   {
36     did_seq = false;
37     if (param == "ANA_NAME")
38       ana_name = value;
39     else if (param == "APPEND_WIN")
40       win_append = true;
41     else if (param == "APPEND_THRES")
42       thres_append = true;
43     else if (param == "SEQUENCE_NUM")
44       seq_num = atoi(value.c_str());
45     else if (param == "WINDOW")
46       window = atoi(value.c_str());
47     else if (param == "THRESHOLD")
48       threshold = atoi(value.c_str());
49     else if (param == "SEQUENCE")
50     {
51       seq_files.push_back(value);
52       fasta_index = 1;
53       annot_file = "";
54       seq_params = true;
55
56       while ((!para_file.eof()) && seq_params)
57       {
58         getline(para_file,file_data_line);
59         split_index = file_data_line.find(" ");
60         param = file_data_line.substr(0,split_index);
61         value = file_data_line.substr(split_index+1);
62
63         if (param == "FASTA_INDEX")
64           fasta_index = atoi(value.c_str());
65         else if (param == "ANNOTATION")
66           annot_file = value;
67         //ignore empty lines or that start with '#'
68         else if ((param == "") || (param == "#")) {}
69         else seq_params = false;
70       }
71
72
73       fasta_indices.push_back(fasta_index);
74       annot_files.push_back(annot_file);
75       did_seq = true;
76       
77     }
78     //ignore empty lines or that start with '#'
79     else if ((param == "") || (param == "#")) {}
80     else
81     {
82       cout << "Illegal/misplaced mussa parameter in file\n";
83       cout << param << "\n";
84     }
85
86     if (!did_seq)
87     {
88       getline(para_file,file_data_line);
89       split_index = file_data_line.find(" ");
90       param = file_data_line.substr(0,split_index);
91       value = file_data_line.substr(split_index+1);
92       did_seq = false;
93     }
94   }
95
96   para_file.close();
97
98   cout << "ana_name = " << ana_name << win_append << win_append << "\n";
99   cout << "window = " << window << " threshold = " << threshold << "\n";
100 }
101
102
103 void
104 Mussa::get_Seqs()
105 {
106   list<string>::iterator seq_files_i, annot_files_i;
107   list<int>::iterator fasta_indices_i;
108   Sequence aSeq;
109
110   seq_files_i = seq_files.begin();
111   fasta_indices_i = fasta_indices.begin();
112   annot_files_i = annot_files.begin();
113
114   while ( (seq_files_i != seq_files.end()) &&
115           (fasta_indices_i != fasta_indices.end()) &&
116           (annot_files_i != annot_files.end()) )
117   {
118     aSeq.load_fasta(*seq_files_i, *fasta_indices_i);
119     the_Seqs.push_back(aSeq);
120     cout << aSeq.hdr() << endl;
121     aSeq.clear();
122     ++seq_files_i;
123     ++fasta_indices_i;
124     ++annot_files_i;
125   }
126 }
127
128 /*
129     aSeq.seq();
130     cout << "\n";
131     aSeq.hdr();
132     cout << "\n";
133 */
134
135 void
136 Mussa::seqcomp()
137 {
138   int i, i2;     // loop vars over sequences to analyze
139   vector<int> seq_lens;
140   vector<FLPs> empty_FLP_vector;
141   FLPs dummy_comp;
142   string save_file_string;
143
144   empty_FLP_vector.clear();
145   for(i = 0; i < seq_num; i++)
146   {
147     all_comps.push_back(empty_FLP_vector); 
148     for(i2 = 0; i2 < seq_num; i2++)
149       all_comps[i].push_back(dummy_comp);
150   }
151
152   for(i = 0; i < seq_num; i++)
153     seq_lens.push_back(the_Seqs[i].len());
154
155   for(i = 0; i < seq_num; i++)
156     for(i2 = i+1; i2 < seq_num; i2++)
157     {
158       all_comps[i][i2].setup("m", window, threshold, seq_lens[i],seq_lens[i2]);
159       all_comps[i][i2].seqcomp(the_Seqs[i].seq(), the_Seqs[i2].seq(), false);
160       all_comps[i][i2].seqcomp(the_Seqs[i].seq(),the_Seqs[i2].rev_comp(),true);
161       save_file_string = "ana";
162       if (i == 0)
163         save_file_string += "0";
164       else if (i == 1)
165         save_file_string += "1";
166       if (i2 == 1)
167         save_file_string += "1";
168       else if (i2 == 2)
169         save_file_string += "2";
170
171       all_comps[i][i2].file_save(save_file_string);
172     }
173 }
174
175
176 void
177 Mussa::nway()
178 {
179   the_paths.setup(seq_num);
180   the_paths.find_paths_r(all_comps);
181 }
182
183
184 void
185 Mussa::save_old()
186 {
187   fstream save_file;
188   int i;
189
190   save_file.open(ana_name.c_str(), ios::out);
191
192   for(i = 0; i < seq_num; i++)
193     save_file << the_Seqs[i].seq() << endl;
194
195   save_file << window << endl;
196   save_file.close();
197   //note more complex eventually since ana_name may need to have
198   //window size, threshold and other stuff to modify it...
199   the_paths.save_old(ana_name);
200 }
201
202
203 void
204 Mussa::load_old(char * load_file_path, int s_num)
205 {
206   fstream save_file;
207   string file_data_line; 
208   int i, space_split_i, comma_split_i;
209   vector<int> loaded_path;
210   string node_pair, node;
211   Sequence a_seq;
212
213   seq_num = s_num;
214   the_paths.setup(seq_num);
215   save_file.open(load_file_path, ios::in);
216
217   // currently loads old mussa format
218
219   // get sequences
220   for(i = 0; i < seq_num; i++)
221   {
222     getline(save_file, file_data_line);
223     a_seq.set_seq(file_data_line);
224     the_Seqs.push_back(a_seq);
225   }
226
227   // get window size
228   getline(save_file, file_data_line);
229   window = atoi(file_data_line.c_str());
230   // get paths
231
232   while (!save_file.eof())
233   {
234     loaded_path.clear();
235     getline(save_file, file_data_line);
236     if (file_data_line != "")
237     for(i = 0; i < seq_num; i++)
238     {
239       space_split_i = file_data_line.find(" ");
240       node_pair = file_data_line.substr(0,space_split_i);
241       //cout << "np= " << node_pair;
242       comma_split_i = node_pair.find(",");
243       node = node_pair.substr(comma_split_i+1);
244       //cout << "n= " << node << " ";
245       loaded_path.push_back(atoi (node.c_str()));
246       file_data_line = file_data_line.substr(space_split_i+1);
247     }
248     //cout << endl;
249     the_paths.add_path(loaded_path);
250   }
251   save_file.close();
252
253   //the_paths.save("tmp.save");
254 }
255
256
257 // In Memorial to Everything that's gone wrong in the last week
258 // and Everything that will go wrong in the next 2 weeks 03/02/2004 - Tristan
259 void
260 Mussa::FuckingPieceOfShit(int x_max, int y_max)
261 {
262
263   Fl_Window *conn_window = new Fl_Window(x_max, y_max, "Mussa Connections");
264   ConnView *conn_box = new ConnView(0, 0, x_max, y_max);
265   conn_box->setup(ana_name, seq_num, window, &the_Seqs, &the_paths);
266   conn_box->scale_paths();
267   conn_box->spawnSeq();
268   conn_window->end();
269   conn_window->show();
270
271   Fl::run();
272 }
273
274 /*
275       cout << "fee\n";
276       cout << "fie\n";
277       cout << "foe\n";
278       cout << "fum\n";
279 */
280
281