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 SparseMatrix();
59 overlapFile = m->getRootName(blastfile) + "overlap.dist";
60 distFile = m->getRootName(blastfile) + "hclusterDists.dist";
62 m->openOutputFile(overlapFile, outOverlap);
63 m->openOutputFile(distFile, outDist);
66 if (m->control_pressed) {
68 if (!hclusterWanted) { delete matrix; }
69 else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
73 Progress* reading = new Progress("Reading blast: ", nseqs * nseqs);
75 //this is used to quickly find if we already have a distance for this combo
76 vector< map<int,float> > dists; dists.resize(nseqs); //dists[0][1] = distance from seq0 to seq1
77 map<int, float> thisRowsBlastScores;
79 if (!fileHandle.eof()) {
80 //read in line from file
81 fileHandle >> firstName >> secondName >> percentId >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
82 m->gobble(fileHandle);
84 currentRow = firstName;
85 lengthThisSeq = numBases;
86 repeatName = firstName + secondName;
88 if (firstName == secondName) { refScore = score; }
90 //convert name to number
91 map<string,int>::iterator itA = nameMap->find(firstName);
92 map<string,int>::iterator itB = nameMap->find(secondName);
93 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
94 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
96 thisRowsBlastScores[itB->second] = score;
99 thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
101 //if there is a valid overlap, add it
102 if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
103 if (!hclusterWanted) {
104 seqDist overlapValue(itA->second, itB->second, thisoverlap);
105 overlap.push_back(overlapValue);
107 outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
111 }else { m->mothurOut("Error in your blast file, cannot read."); m->mothurOutEndLine(); exit(1); }
115 while(!fileHandle.eof()){
117 if (m->control_pressed) {
119 if (!hclusterWanted) { delete matrix; }
120 else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
125 //read in line from file
126 fileHandle >> firstName >> secondName >> percentId >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
127 //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;
128 m->gobble(fileHandle);
130 string temp = firstName + secondName; //to check if this file has repeat lines, ie. is this a blast instead of a blscreen file
132 //if this is a new pairing
133 if (temp != repeatName) {
136 if (currentRow == firstName) {
137 //cout << "first = " << firstName << " second = " << secondName << endl;
138 if (firstName == secondName) {
140 reading->update((count + nseqs));
143 //convert name to number
144 map<string,int>::iterator itA = nameMap->find(firstName);
145 map<string,int>::iterator itB = nameMap->find(secondName);
146 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
147 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
150 thisRowsBlastScores[itB->second] = score;
153 thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
155 //if there is a valid overlap, add it
156 if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
157 if (!hclusterWanted) {
158 seqDist overlapValue(itA->second, itB->second, thisoverlap);
159 //cout << "overlap = " << itA->second << '\t' << itB->second << '\t' << thisoverlap << endl;
160 overlap.push_back(overlapValue);
162 outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
167 //convert blast scores to distance and add cell to sparse matrix if we can
168 map<int, float>::iterator it;
169 map<int, float>::iterator itDist;
170 for(it=thisRowsBlastScores.begin(); it!=thisRowsBlastScores.end(); it++) {
171 distance = 1.0 - (it->second / refScore);
174 //do we already have the distance calculated for b->a
175 map<string,int>::iterator itA = nameMap->find(currentRow);
176 itDist = dists[it->first].find(itA->second);
178 //if we have it then compare
179 if (itDist != dists[it->first].end()) {
181 //if you want the minimum blast score ratio, then pick max distance
182 if(minWanted) { distance = max(itDist->second, distance); }
183 else{ distance = min(itDist->second, distance); }
185 //is this distance below cutoff
186 if (distance < cutoff) {
187 if (!hclusterWanted) {
188 PCell value(itA->second, it->first, distance);
189 matrix->addCell(value);
191 outDist << itA->first << '\t' << nameMap->get(it->first) << '\t' << distance << endl;
194 //not going to need this again
195 dists[it->first].erase(itDist);
196 }else { //save this value until we get the other ratio
197 dists[itA->second][it->first] = distance;
200 //clear out last rows info
201 thisRowsBlastScores.clear();
203 currentRow = firstName;
204 lengthThisSeq = numBases;
206 //add this row to thisRowsBlastScores
207 if (firstName == secondName) { refScore = score; }
208 else{ //add this row to thisRowsBlastScores
210 //convert name to number
211 map<string,int>::iterator itA = nameMap->find(firstName);
212 map<string,int>::iterator itB = nameMap->find(secondName);
213 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
214 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
216 thisRowsBlastScores[itB->second] = score;
219 thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
221 //if there is a valid overlap, add it
222 if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
223 if (!hclusterWanted) {
224 seqDist overlapValue(itA->second, itB->second, thisoverlap);
225 overlap.push_back(overlapValue);
227 outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
231 }//end if current row
235 //get last rows info stored
236 //convert blast scores to distance and add cell to sparse matrix if we can
237 map<int, float>::iterator it;
238 map<int, float>::iterator itDist;
239 for(it=thisRowsBlastScores.begin(); it!=thisRowsBlastScores.end(); it++) {
240 distance = 1.0 - (it->second / refScore);
242 //do we already have the distance calculated for b->a
243 map<string,int>::iterator itA = nameMap->find(currentRow);
244 itDist = dists[it->first].find(itA->second);
246 //if we have it then compare
247 if (itDist != dists[it->first].end()) {
248 //if you want the minimum blast score ratio, then pick max distance
249 if(minWanted) { distance = max(itDist->second, distance); }
250 else{ distance = min(itDist->second, distance); }
252 //is this distance below cutoff
253 if (distance < cutoff) {
254 if (!hclusterWanted) {
255 PCell value(itA->second, it->first, distance);
256 matrix->addCell(value);
258 outDist << itA->first << '\t' << nameMap->get(it->first) << '\t' << distance << endl;
261 //not going to need this again
262 dists[it->first].erase(itDist);
263 }else { //save this value until we get the other ratio
264 dists[itA->second][it->first] = distance;
268 thisRowsBlastScores.clear();
271 if (m->control_pressed) {
273 if (!hclusterWanted) { delete matrix; }
274 else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
279 if (!hclusterWanted) {
280 sort(overlap.begin(), overlap.end(), compareOverlap);
286 if (m->control_pressed) {
288 if (!hclusterWanted) { delete matrix; }
289 else { m->mothurRemove(overlapFile); m->mothurRemove(distFile); }
300 catch(exception& e) {
301 m->errorOut(e, "ReadBlast", "read");
305 /*********************************************************************************************/
306 int ReadBlast::readNames(NameAssignment* nameMap) {
308 m->mothurOut("Reading names... "); cout.flush();
310 string name, hold, prevName;
314 m->openInputFile(blastfile, in);
317 //m->openOutputFile((blastfile + ".tempOutNames"), outName);
322 for (int i = 0; i < 11; i++) { in >> hold; }
325 //save name in nameMap
326 nameMap->push_back(prevName);
329 if (m->control_pressed) { in.close(); return 0; }
334 for (int i = 0; i < 11; i++) { in >> hold; }
337 //is this a new name?
338 if (name != prevName) {
340 nameMap->push_back(name);
347 //write out names file
348 //string outNames = m->getRootName(blastfile) + "names";
350 //m->openOutputFile(outNames, out);
351 //nameMap->print(out);
354 if (m->control_pressed) { return 0; }
356 m->mothurOut(toString(num) + " names read."); m->mothurOutEndLine();
361 catch(exception& e) {
362 m->errorOut(e, "ReadBlast", "readNames");
366 /*********************************************************************************************/