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