1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
* readblast.cpp
* Mothur
*
* Created by westcott on 12/10/09.
* Copyright 2009 Schloss Lab. All rights reserved.
*
*/
#include "readblast.h"
#include "progress.hpp"
//********************************************************************************************************************
//sorts lowest to highest
inline bool compareOverlap(seqDist left, seqDist right){
return (left.dist < right.dist);
}
/*********************************************************************************************/
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) {
try {
m = MothurOut::getInstance();
matrix = NULL;
}
catch(exception& e) {
m->errorOut(e, "ReadBlast", "ReadBlast");
exit(1);
}
}
/*********************************************************************************************/
//assumptions about the blast file:
//1. if duplicate lines occur the first line is always best and is chosen
//2. blast scores are grouped together, ie. a a .... score, a b .... score, a c ....score...
int ReadBlast::read(NameAssignment* nameMap) {
try {
//if the user has not given a names file read names from blastfile
if (nameMap->size() == 0) { readNames(nameMap); }
int nseqs = nameMap->size();
if (m->control_pressed) { return 0; }
ifstream fileHandle;
m->openInputFile(blastfile, fileHandle);
string firstName, secondName, eScore, currentRow;
string repeatName = "";
int count = 1;
float distance, thisoverlap, refScore;
float percentId;
float numBases, mismatch, gap, startQuery, endQuery, startRef, endRef, score, lengthThisSeq;
ofstream outDist;
ofstream outOverlap;
//create objects needed for read
if (!hclusterWanted) {
matrix = new SparseDistanceMatrix();
matrix->resize(nseqs);
}else{
overlapFile = m->getRootName(blastfile) + "overlap.dist";
distFile = m->getRootName(blastfile) + "hclusterDists.dist";
m->openOutputFile(overlapFile, outOverlap);
m->openOutputFile(distFile, outDist);
}
if (m->control_pressed) {
fileHandle.close();
if (!hclusterWanted) { delete matrix; }
else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
return 0;
}
Progress* reading = new Progress("Reading blast: ", nseqs * nseqs);
//this is used to quickly find if we already have a distance for this combo
vector< map<int,float> > dists; dists.resize(nseqs); //dists[0][1] = distance from seq0 to seq1
map<int, float> thisRowsBlastScores;
if (!fileHandle.eof()) {
//read in line from file
fileHandle >> firstName >> secondName >> percentId >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
m->gobble(fileHandle);
currentRow = firstName;
lengthThisSeq = numBases;
repeatName = firstName + secondName;
if (firstName == secondName) { refScore = score; }
else{
//convert name to number
map<string,int>::iterator itA = nameMap->find(firstName);
map<string,int>::iterator itB = nameMap->find(secondName);
if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
thisRowsBlastScores[itB->second] = score;
//calc overlap score
thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
//if there is a valid overlap, add it
if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
if (!hclusterWanted) {
seqDist overlapValue(itA->second, itB->second, thisoverlap);
overlap.push_back(overlapValue);
}else {
outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
}
}
}
}else { m->mothurOut("Error in your blast file, cannot read."); m->mothurOutEndLine(); exit(1); }
//read file
while(!fileHandle.eof()){
if (m->control_pressed) {
fileHandle.close();
if (!hclusterWanted) { delete matrix; }
else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
delete reading;
return 0;
}
//read in line from file
fileHandle >> firstName >> secondName >> percentId >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
//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;
m->gobble(fileHandle);
string temp = firstName + secondName; //to check if this file has repeat lines, ie. is this a blast instead of a blscreen file
//if this is a new pairing
if (temp != repeatName) {
repeatName = temp;
if (currentRow == firstName) {
//cout << "first = " << firstName << " second = " << secondName << endl;
if (firstName == secondName) {
refScore = score;
reading->update((count + nseqs));
count++;
}else{
//convert name to number
map<string,int>::iterator itA = nameMap->find(firstName);
map<string,int>::iterator itB = nameMap->find(secondName);
if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
//save score
thisRowsBlastScores[itB->second] = score;
//calc overlap score
thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
//if there is a valid overlap, add it
if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
if (!hclusterWanted) {
seqDist overlapValue(itA->second, itB->second, thisoverlap);
//cout << "overlap = " << itA->second << '\t' << itB->second << '\t' << thisoverlap << endl;
overlap.push_back(overlapValue);
}else {
outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
}
}
} //end else
}else { //end row
//convert blast scores to distance and add cell to sparse matrix if we can
map<int, float>::iterator it;
map<int, float>::iterator itDist;
for(it=thisRowsBlastScores.begin(); it!=thisRowsBlastScores.end(); it++) {
distance = 1.0 - (it->second / refScore);
//do we already have the distance calculated for b->a
map<string,int>::iterator itA = nameMap->find(currentRow);
itDist = dists[it->first].find(itA->second);
//if we have it then compare
if (itDist != dists[it->first].end()) {
//if you want the minimum blast score ratio, then pick max distance
if(minWanted) { distance = max(itDist->second, distance); }
else{ distance = min(itDist->second, distance); }
//is this distance below cutoff
if (distance < cutoff) {
if (!hclusterWanted) {
if (itA->second < it->first) {
PDistCell value(it->first, distance);
matrix->addCell(itA->second, value);
}else {
PDistCell value(itA->second, distance);
matrix->addCell(it->first, value);
}
}else{
outDist << itA->first << '\t' << nameMap->get(it->first) << '\t' << distance << endl;
}
}
//not going to need this again
dists[it->first].erase(itDist);
}else { //save this value until we get the other ratio
dists[itA->second][it->first] = distance;
}
}
//clear out last rows info
thisRowsBlastScores.clear();
currentRow = firstName;
lengthThisSeq = numBases;
//add this row to thisRowsBlastScores
if (firstName == secondName) { refScore = score; }
else{ //add this row to thisRowsBlastScores
//convert name to number
map<string,int>::iterator itA = nameMap->find(firstName);
map<string,int>::iterator itB = nameMap->find(secondName);
if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
thisRowsBlastScores[itB->second] = score;
//calc overlap score
thisoverlap = 1.0 - (percentId * (lengthThisSeq - startQuery) / endRef / 100.0 - penalty);
//if there is a valid overlap, add it
if ((startRef <= length) && ((endQuery+length) >= lengthThisSeq) && (thisoverlap < cutoff)) {
if (!hclusterWanted) {
seqDist overlapValue(itA->second, itB->second, thisoverlap);
overlap.push_back(overlapValue);
}else {
outOverlap << itA->first << '\t' << itB->first << '\t' << thisoverlap << endl;
}
}
}
}//end if current row
}//end if repeat
}//end while
//get last rows info stored
//convert blast scores to distance and add cell to sparse matrix if we can
map<int, float>::iterator it;
map<int, float>::iterator itDist;
for(it=thisRowsBlastScores.begin(); it!=thisRowsBlastScores.end(); it++) {
distance = 1.0 - (it->second / refScore);
//do we already have the distance calculated for b->a
map<string,int>::iterator itA = nameMap->find(currentRow);
itDist = dists[it->first].find(itA->second);
//if we have it then compare
if (itDist != dists[it->first].end()) {
//if you want the minimum blast score ratio, then pick max distance
if(minWanted) { distance = max(itDist->second, distance); }
else{ distance = min(itDist->second, distance); }
//is this distance below cutoff
if (distance < cutoff) {
if (!hclusterWanted) {
if (itA->second < it->first) {
PDistCell value(it->first, distance);
matrix->addCell(itA->second, value);
}else {
PDistCell value(itA->second, distance);
matrix->addCell(it->first, value);
}
}else{
outDist << itA->first << '\t' << nameMap->get(it->first) << '\t' << distance << endl;
}
}
//not going to need this again
dists[it->first].erase(itDist);
}else { //save this value until we get the other ratio
dists[itA->second][it->first] = distance;
}
}
//clear out info
thisRowsBlastScores.clear();
dists.clear();
if (m->control_pressed) {
fileHandle.close();
if (!hclusterWanted) { delete matrix; }
else { outOverlap.close(); m->mothurRemove(overlapFile); outDist.close(); m->mothurRemove(distFile); }
delete reading;
return 0;
}
if (!hclusterWanted) {
sort(overlap.begin(), overlap.end(), compareOverlap);
}else {
outDist.close();
outOverlap.close();
}
if (m->control_pressed) {
fileHandle.close();
if (!hclusterWanted) { delete matrix; }
else { m->mothurRemove(overlapFile); m->mothurRemove(distFile); }
delete reading;
return 0;
}
reading->finish();
delete reading;
fileHandle.close();
return 0;
}
catch(exception& e) {
m->errorOut(e, "ReadBlast", "read");
exit(1);
}
}
/*********************************************************************************************/
int ReadBlast::readNames(NameAssignment* nameMap) {
try {
m->mothurOut("Reading names... "); cout.flush();
string name, hold, prevName;
int num = 1;
ifstream in;
m->openInputFile(blastfile, in);
//ofstream outName;
//m->openOutputFile((blastfile + ".tempOutNames"), outName);
//read first line
in >> prevName;
for (int i = 0; i < 11; i++) { in >> hold; }
m->gobble(in);
//save name in nameMap
nameMap->push_back(prevName);
while (!in.eof()) {
if (m->control_pressed) { in.close(); return 0; }
//read line
in >> name;
for (int i = 0; i < 11; i++) { in >> hold; }
m->gobble(in);
//is this a new name?
if (name != prevName) {
prevName = name;
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; }
else {
nameMap->push_back(name);
}
//outName << name << '\t' << name << endl;
num++;
}
}
in.close();
//write out names file
//string outNames = m->getRootName(blastfile) + "names";
//ofstream out;
//m->openOutputFile(outNames, out);
//nameMap->print(out);
//out.close();
if (m->control_pressed) { return 0; }
m->mothurOut(toString(num) + " names read."); m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "ReadBlast", "readNames");
exit(1);
}
}
/*********************************************************************************************/
|