5 * Created by westcott on 12/10/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "readblast.h"
11 #include "progress.hpp"
13 //********************************************************************************************************************
14 //sorts lowest to highest
15 inline bool compareOverlap(seqDist left, seqDist right){
16 return (left.dist < right.dist);
18 /*********************************************************************************************/
19 ReadBlast::ReadBlast(string file, float c, float p, int l, bool ms, bool h) : blastfile(file), cutoff(c), penalty(p), length(l), minWanted(ms), hclusterWanted(h) {
21 m = MothurOut::getInstance();
25 m->errorOut(e, "ReadBlast", "ReadBlast");
29 /*********************************************************************************************/
30 //assumptions about the blast file:
31 //1. if duplicate lines occur the first line is always best and is chosen
32 //2. blast scores are grouped together, ie. a a .... score, a b .... score, a c ....score...
33 int ReadBlast::read(NameAssignment* nameMap) {
36 //if the user has not given a names file read names from blastfile
37 if (nameMap->size() == 0) { readNames(nameMap); }
38 int nseqs = nameMap->size();
40 if (m->control_pressed) { return 0; }
43 m->openInputFile(blastfile, fileHandle);
45 string firstName, secondName, eScore, currentRow;
46 string repeatName = "";
48 float distance, thisoverlap, refScore;
50 float numBases, mismatch, gap, startQuery, endQuery, startRef, endRef, score, lengthThisSeq;
55 //create objects needed for read
56 if (!hclusterWanted) {
57 matrix = new SparseDistanceMatrix();
58 matrix->resize(nseqs);
60 overlapFile = m->getRootName(blastfile) + "overlap.dist";
61 distFile = m->getRootName(blastfile) + "hclusterDists.dist";
63 m->openOutputFile(overlapFile, outOverlap);
64 m->openOutputFile(distFile, outDist);
67 if (m->control_pressed) {
69 if (!hclusterWanted) { delete matrix; }
70 else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
74 Progress* reading = new Progress("Reading blast: ", nseqs * nseqs);
76 //this is used to quickly find if we already have a distance for this combo
77 vector< map<int,float> > dists; dists.resize(nseqs); //dists[0][1] = distance from seq0 to seq1
78 map<int, float> thisRowsBlastScores;
80 if (!fileHandle.eof()) {
81 //read in line from file
82 fileHandle >> firstName >> secondName >> percentId >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
83 m->gobble(fileHandle);
85 currentRow = firstName;
86 lengthThisSeq = numBases;
87 repeatName = firstName + secondName;
89 if (firstName == secondName) { refScore = score; }
91 //convert name to number
92 map<string,int>::iterator itA = nameMap->find(firstName);
93 map<string,int>::iterator itB = nameMap->find(secondName);
94 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
95 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
97 thisRowsBlastScores[itB->second] = score;
100 thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
102 //if there is a valid overlap, add it
103 if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
104 if (!hclusterWanted) {
105 seqDist overlapValue(itA->second, itB->second, thisoverlap);
106 overlap.push_back(overlapValue);
108 outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
112 }else { m->mothurOut("Error in your blast file, cannot read."); m->mothurOutEndLine(); exit(1); }
116 while(!fileHandle.eof()){
118 if (m->control_pressed) {
120 if (!hclusterWanted) { delete matrix; }
121 else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
126 //read in line from file
127 fileHandle >> firstName >> secondName >> percentId >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
128 //cout << firstName << '\t' << secondName << '\t' << percentId << '\t' << numBases << '\t' << mismatch << '\t' << gap << '\t' << startQuery << '\t' << endQuery << '\t' << startRef << '\t' << endRef << '\t' << eScore << '\t' << score << endl;
129 m->gobble(fileHandle);
131 string temp = firstName + secondName; //to check if this file has repeat lines, ie. is this a blast instead of a blscreen file
133 //if this is a new pairing
134 if (temp != repeatName) {
137 if (currentRow == firstName) {
138 //cout << "first = " << firstName << " second = " << secondName << endl;
139 if (firstName == secondName) {
141 reading->update((count + nseqs));
144 //convert name to number
145 map<string,int>::iterator itA = nameMap->find(firstName);
146 map<string,int>::iterator itB = nameMap->find(secondName);
147 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
148 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
151 thisRowsBlastScores[itB->second] = score;
154 thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
156 //if there is a valid overlap, add it
157 if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
158 if (!hclusterWanted) {
159 seqDist overlapValue(itA->second, itB->second, thisoverlap);
160 //cout << "overlap = " << itA->second << '\t' << itB->second << '\t' << thisoverlap << endl;
161 overlap.push_back(overlapValue);
163 outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
168 //convert blast scores to distance and add cell to sparse matrix if we can
169 map<int, float>::iterator it;
170 map<int, float>::iterator itDist;
171 for(it=thisRowsBlastScores.begin(); it!=thisRowsBlastScores.end(); it++) {
172 distance = 1.0 - (it->second / refScore);
175 //do we already have the distance calculated for b->a
176 map<string,int>::iterator itA = nameMap->find(currentRow);
177 itDist = dists[it->first].find(itA->second);
179 //if we have it then compare
180 if (itDist != dists[it->first].end()) {
182 //if you want the minimum blast score ratio, then pick max distance
183 if(minWanted) { distance = max(itDist->second, distance); }
184 else{ distance = min(itDist->second, distance); }
186 //is this distance below cutoff
187 if (distance < cutoff) {
188 if (!hclusterWanted) {
189 if (itA->second < it->first) {
190 PDistCell value(it->first, distance);
191 matrix->addCell(itA->second, value);
193 PDistCell value(itA->second, distance);
194 matrix->addCell(it->first, value);
197 outDist << itA->first << '\t' << nameMap->get(it->first) << '\t' << distance << endl;
200 //not going to need this again
201 dists[it->first].erase(itDist);
202 }else { //save this value until we get the other ratio
203 dists[itA->second][it->first] = distance;
206 //clear out last rows info
207 thisRowsBlastScores.clear();
209 currentRow = firstName;
210 lengthThisSeq = numBases;
212 //add this row to thisRowsBlastScores
213 if (firstName == secondName) { refScore = score; }
214 else{ //add this row to thisRowsBlastScores
216 //convert name to number
217 map<string,int>::iterator itA = nameMap->find(firstName);
218 map<string,int>::iterator itB = nameMap->find(secondName);
219 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
220 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
222 thisRowsBlastScores[itB->second] = score;
225 thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
227 //if there is a valid overlap, add it
228 if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
229 if (!hclusterWanted) {
230 seqDist overlapValue(itA->second, itB->second, thisoverlap);
231 overlap.push_back(overlapValue);
233 outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
237 }//end if current row
241 //get last rows info stored
242 //convert blast scores to distance and add cell to sparse matrix if we can
243 map<int, float>::iterator it;
244 map<int, float>::iterator itDist;
245 for(it=thisRowsBlastScores.begin(); it!=thisRowsBlastScores.end(); it++) {
246 distance = 1.0 - (it->second / refScore);
248 //do we already have the distance calculated for b->a
249 map<string,int>::iterator itA = nameMap->find(currentRow);
250 itDist = dists[it->first].find(itA->second);
252 //if we have it then compare
253 if (itDist != dists[it->first].end()) {
254 //if you want the minimum blast score ratio, then pick max distance
255 if(minWanted) { distance = max(itDist->second, distance); }
256 else{ distance = min(itDist->second, distance); }
258 //is this distance below cutoff
259 if (distance < cutoff) {
260 if (!hclusterWanted) {
261 if (itA->second < it->first) {
262 PDistCell value(it->first, distance);
263 matrix->addCell(itA->second, value);
265 PDistCell value(itA->second, distance);
266 matrix->addCell(it->first, value);
269 outDist << itA->first << '\t' << nameMap->get(it->first) << '\t' << distance << endl;
272 //not going to need this again
273 dists[it->first].erase(itDist);
274 }else { //save this value until we get the other ratio
275 dists[itA->second][it->first] = distance;
279 thisRowsBlastScores.clear();
282 if (m->control_pressed) {
284 if (!hclusterWanted) { delete matrix; }
285 else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
290 if (!hclusterWanted) {
291 sort(overlap.begin(), overlap.end(), compareOverlap);
297 if (m->control_pressed) {
299 if (!hclusterWanted) { delete matrix; }
300 else { m->mothurRemove(overlapFile); m->mothurRemove(distFile); }
311 catch(exception& e) {
312 m->errorOut(e, "ReadBlast", "read");
316 /*********************************************************************************************/
317 int ReadBlast::readNames(NameAssignment* nameMap) {
319 m->mothurOut("Reading names... "); cout.flush();
321 string name, hold, prevName;
325 m->openInputFile(blastfile, in);
328 //m->openOutputFile((blastfile + ".tempOutNames"), outName);
333 for (int i = 0; i < 11; i++) { in >> hold; }
336 //save name in nameMap
337 nameMap->push_back(prevName);
340 if (m->control_pressed) { in.close(); return 0; }
345 for (int i = 0; i < 11; i++) { in >> hold; }
348 //is this a new name?
349 if (name != prevName) {
352 if (nameMap->get(name) != -1) { m->mothurOut("[ERROR]: trying to exact names from blast file, and I found dups. Are you sequence names unique? quitting.\n"); m->control_pressed = true; }
354 nameMap->push_back(name);
356 //outName << name << '\t' << name << endl;
363 //write out names file
364 //string outNames = m->getRootName(blastfile) + "names";
366 //m->openOutputFile(outNames, out);
367 //nameMap->print(out);
370 if (m->control_pressed) { return 0; }
372 m->mothurOut(toString(num) + " names read."); m->mothurOutEndLine();
377 catch(exception& e) {
378 m->errorOut(e, "ReadBlast", "readNames");
382 /*********************************************************************************************/