73527d2c37905c96c8c7e9615a2942453b18f194
[mussa.git] / flp.cc
1 //  This file is part of the Mussa source distribution.
2 //  http://mussa.caltech.edu/
3 //  Contact author: Tristan  De Buysscher, tristan@caltech.edu
4
5 // This program and all associated source code files are Copyright (C) 2005
6 // the California Institute of Technology, Pasadena, CA, 91125 USA.  It is
7 // under the GNU Public License; please see the included LICENSE.txt
8 // file for more information, or contact Tristan directly.
9
10
11 //                        ----------------------------------------
12 //                            ---------- flp.cc  -----------
13 //                        ----------------------------------------
14
15 #include "flp.hh"
16
17 #include <fstream>
18 #include <iostream>
19
20 using namespace std;
21
22 FLPs::FLPs()
23 {
24 }
25
26 void
27 FLPs::setup(string type, int win_size, int hard_thres, int len1, int len2)
28 {
29   list<match> empty_match_list;
30   int i;
31
32   window_size = win_size;
33   hard_threshold = hard_thres;
34   seq1_length = len1;
35   seq2_length = len2;
36   ana_type = type;
37   seq1_win_num = seq1_length - win_size + 1;
38   seq2_win_num = seq2_length - win_size + 1;
39   all_matches.reserve(seq1_win_num);
40   
41   empty_match_list.clear();
42   for(i = 0; i < seq1_win_num; i++)
43     all_matches.push_back(empty_match_list);
44 }
45
46 int
47 FLPs::win_num()
48 {
49   return seq1_win_num;
50 }
51
52 /*
53 bool
54 FLPs::match_less(match *match1, match *match2)
55 {
56   if (match1->score < match2->score)
57     return true;
58   else if ( (match1->score == match2->score) &&
59             (match1->index < match2->index) )
60     return true;
61   else
62     return false;
63 }
64
65 void
66 FLPs::sort()
67 {
68   int i;
69
70   for(i = 0; i < seq1_win_num; i++)
71     if (!all_matches[i].empty())
72       all_matches[i].sort(&FLPs::match_less);
73 }
74 */
75
76 list<int>
77 FLPs::matches(int index)
78 {
79   list<int> these_matches;
80   list<match>::iterator list_i, list_end;
81
82   index = abs(index);
83   list_i = all_matches[index].begin();
84   list_end = all_matches[index].end();
85   //if (list_i == list_end)
86   //cout << "its fuckin empty!!!!";
87   while (list_i != list_end)
88   {
89     these_matches.push_back(list_i->index);
90     //cout << list_i->index << " ";
91     ++list_i;
92   }
93   //cout << endl;
94
95   return these_matches;
96 }
97
98 list<int>
99 FLPs::thres_matches(int index, int thres)
100 {
101   list<int> thres_matches;
102   list<match>::iterator list_i, list_end;
103
104   index = abs(index);
105   list_i = all_matches[index].begin();
106   list_end = all_matches[index].end();
107   thres_matches.clear();
108
109   //if (list_i == list_end)
110   //cout << "its fuckin empty!!!!";
111   while (list_i != list_end)
112   {
113     if (list_i->score >= thres)
114       thres_matches.push_back(list_i->index);
115     //cout << list_i->index << " ";
116
117     ++list_i;
118   }
119   //cout << endl;
120
121   return thres_matches;
122 }
123
124
125 void
126 FLPs::file_save(string save_file_path)
127 {
128   fstream save_file;
129   list<match>::iterator match_i, match_list_end;
130   int i;
131
132   save_file.open(save_file_path.c_str(), ios::out);
133
134   save_file << "<Seqcomp type=" << ana_type << " win=" << window_size;
135   save_file << " thres=" << hard_threshold << ">\n";
136
137   for(i = 0; i < seq1_win_num; i++)
138   {
139     if (!all_matches[i].empty())
140     {
141       match_i = all_matches[i].begin();
142       match_list_end = all_matches[i].end();
143       while (match_i != match_list_end)
144       {
145         save_file << match_i->index << "," << match_i->score << " ";
146         ++match_i;
147       }
148     }
149     save_file << endl;
150   }
151
152   save_file << "</Seqcomp>\n";
153
154   save_file.close();
155 }
156
157 void
158 FLPs::file_load(string file_path)
159 {
160   fstream data_file;
161   string file_data, file_data_line, pair_data, index_data, score_data;
162   int type, window, hard_thres;
163   match a_match;
164   string::size_type split_index, comma_index;
165   bool tag_open = false;
166   list<match> a_match_list;
167   int i;
168
169
170
171
172
173
174   data_file.open(file_path.c_str(), ios::in);
175
176   getline(data_file,file_data_line);
177   // parse seqcomp open tag and parameters
178   // eg <Seqcomp type=mussa win=30 thres=21>
179   // if parse successful...
180   tag_open = true;
181
182   while ((!data_file.eof()) && tag_open)
183   {
184     // intialize list to empty
185     a_match_list.clear();
186
187     getline(data_file,file_data_line);
188     
189     if (file_data_line == "</Seqcomp>")
190       tag_open = false;
191     // parse line of matches
192     else if (file_data_line == "")
193     {
194       //cout << "empty line\n";
195       all_matches.push_back(a_match_list); 
196     }
197     else
198     {
199       split_index = file_data_line.find(" ");
200       
201       while (split_index != string::npos)
202       {
203         pair_data = file_data_line.substr(0,split_index); 
204         file_data_line = file_data_line.substr(split_index+1);
205         //cout << "pair_data = " << pair_data << "...";
206         // parse out the 2 pieces of data, index and score of pair match
207         comma_index = pair_data.find(",");
208         index_data = pair_data.substr(0, comma_index);
209         a_match.index = atoi(index_data.c_str() );
210         score_data = pair_data.substr(comma_index+1); 
211         a_match.score = atoi(score_data.c_str() );
212
213         //cout << a_match.index << "," << a_match.score << " ";
214
215         a_match_list.push_back(a_match);
216
217         split_index = file_data_line.find(" ");
218       }
219       //cout << "\n";
220       all_matches.push_back(a_match_list); 
221     }
222   }
223   seq1_win_num = all_matches.size();
224   cout << "windows in flp = " << all_matches.size() << endl;
225   data_file.close();
226 }
227
228 /*
229       cout << "fee\n";
230       cout << "fie\n";
231       cout << "foe  ";
232       cout << "fum\n";
233 */
234
235