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