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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
/**
* Author: Mark Larkin
*
* Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
*/
/**
* Changes:
*
* 10-02-07,Nigel Brown(EMBL): changed ifstream to InFileStream to handle
* cross-platform end-of-lines.
*
* 23-03-07,Nigel Brown(EMBL): added call to Alignment::testUniqueNames() to
* test sequence names prior to appending; moved
* Alignment::checkAllNamesDifferent() test into block handling loading of new
* sequences.
*
* 02-04-07,Nigel Brown(EMBL): commented out the "No sequences in file..."
* warnings in seqInput() and profileInput()and enabled the
* higher-up-the-stack counterpart in MainWindow::errorHandler(), since the
* former was causing a crash during the repaint after the user accepts the
* message, because some state (what exactly?) is unstable at this depth after
* a sequence load error, specifically when loading an empty file after
* already loading some sequences.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "FileReader.h"
namespace clustalw
{
FileReader::FileReader()
{
fileIn = new InFileStream;
structPenalties = NONE; // I think this should be ok.
}
FileReader::~FileReader()
{
delete fileIn;
}
/* check if we've read sequences without any information, i.e. header
* but no sequence at all
*/
bool FileReader::noEmptySequence(vector<Sequence> seqVector, string *offendingSeq)
{
vector<Sequence>::iterator si;
for (si = seqVector.begin(); si != seqVector.end(); si++) {
if (si->isEmpty()) {
offendingSeq->assign(si->getName());
return false;
}
}
return true;
}
/*
* The function seqInput is called from the interactive menu only. This is because it
* allows us to append seqs. But this would not happen on command line.
* It is called twice in the interactive menu, both times with append as false. It calls
* the readseqs function to do the work.
*/
int FileReader::seqInput(Alignment *alignPtr, bool append, string *offendingSeq)
{
int code;
if(userParameters->getMenuFlag())
{
cout << "\n\nSequences should all be in 1 file.\n";
cout << "\n7 formats accepted: \n";
cout << "NBRF/PIR, EMBL/SwissProt, Pearson (Fasta), GDE, Clustal, GCG/MSF, \
RSF.\n\n\n";
}
if (append)
{
int numSeqsAlready = alignPtr->getNumSeqs();
code = readSeqs(alignPtr, numSeqsAlready + 1, offendingSeq);
}
else
{
code = readSeqs(alignPtr, 1, offendingSeq); // 1 is the first seq to be read
}
if(code == OK)
{
userParameters->setStructPenalties1(false);
userParameters->setStructPenalties2(false);
alignPtr->clearSecStruct1();
alignPtr->clearSecStruct2();
string typeOfAlign = userParameters->getDNAFlag() ? "DNA" : "PROTEIN";
cout << "Sequences assumed to be "
<< typeOfAlign << endl;
if (userParameters->getMenuFlag())
{
cout << "\n\n";
alignPtr->printSequencesAddedInfo();
}
if(userParameters->getDNAFlag())
{
userParameters->setDNAMultiGap();
}
else
{
userParameters->setProtMultiGap();
}
userParameters->setEmpty(false);
}
else if(code == NOSEQUENCESINFILE) // no sequences
{
/* 02-04-07,nige: this causes a fatal repaint: let
* MainWindow::errorHandler deal with it.
*/
//utilityObject->error("No sequences in file! Bad format?\n");
return code;
}
else
{
return code;
}
return code;
}
/*
* The function readSeqs will create the FileParser that is needed and
* it will read in all of the sequences. Depending on the filetype, it
* will also check for secondary structure information.
*
* If there is a problem with one of the sequences its name will be
* assigned to offendingSeq
*/
int FileReader::readSeqs(Alignment *alignPtr, int firstSeq, string *offendingSeq)
{
string line;
string fileName;
string linuxFilePath = "file://";
// static char *seq1, sname1[MAXNAMES + 1], title[MAXTITLES + 1];
//int i, j;
int noSeqs;
//static int l1;
static bool DNAFlag1;
vector<Sequence> seqVector;
vector<Sequence> seqRangeVector;
auto_ptr<FileParser> fileParser; // Means we dont need to delete it!
vector<int> _outputIndex; // Local version of outputIndex.
if (userParameters->getMenuFlag())
{
utilityObject->getStr(string("Enter the name of the sequence file "), line);
}
else
{
line = userParameters->getSeqName();
}
if (line.length() == 0)
{
// We have no fileName, so return -1
return -1;
}
// If we have file:// in the string, remove it!!
string::size_type loc = line.find(linuxFilePath, 0);
if(loc != string::npos)
{
fileName = line.substr(loc + linuxFilePath.length());
line = fileName;
}
// Now we have the file name, we must open the file.
fileIn->open(line.c_str()); //nige
if (fileIn->fail())
return CANNOTOPENFILE;
sequenceFileName = line;
// Check if the file exists!
if (!fileIn->is_open())
{
utilityObject->error("Cannot open input file (%s)\n", line.c_str());
return CANNOTOPENFILE;
}
userParameters->setSeqName(line);
// NOTE I made a change here because the profile2Name was not stored!
if(userParameters->getProfileNum() == 2 && userParameters->getProfile2Name().empty())
{
userParameters->setProfile2Name(line);
}
else if(userParameters->getProfileNum() == 1 && userParameters->getProfile1Name().empty())
{
userParameters->setProfile1Name(line);
}
noSeqs = 0;
checkInfile(&noSeqs, fileParser);
if (noSeqs == 0)
{
return NOSEQUENCESINFILE;
}
seqRangeVector = fileParser->getSeqRange(1, noSeqs, offendingSeq);
if (seqRangeVector.size()==0)
return fileParser->getParseExitCode();
// FIXME Andreas Wilm (UCD): noEmptySequence check should be done
// internally by FileParser instances
if (noEmptySequence(seqRangeVector, offendingSeq) == false) {
return EMPTYSEQUENCE; // Error there are same names.
}
for (int i=0; i < (int)seqRangeVector.size(); i++) {
// Andreas Wilm (UCD): fixed wrong default output order
// which is important when no alignment (convert!) is done
// _outputIndex.push_back(i); // default output order
_outputIndex.push_back(i+1); // default output order
Sequence tempSeq = seqRangeVector[i];
if (!userParameters->getExplicitDNAFlag())
{
DNAFlag1 = tempSeq.checkDNAFlag(); // check DNA/Prot
if (i == 1)
{
userParameters->setDNAFlag(DNAFlag1);
}
} // type decided by first seq
else
{
DNAFlag1 = userParameters->getDNAFlag();
}
seqVector.push_back(tempSeq);
}
if(firstSeq == 1) // New mulitple alignment, or else profile1
{
int prfNum = userParameters->getProfileNum();
alignPtr->addSequences(&seqVector);
//test names after saving
if(alignPtr->checkAllNamesDifferent(offendingSeq) == false) { //nige moved
return ALLNAMESNOTDIFFERENT; // Error there are same names.
}
userParameters->setProfileNum(prfNum);
if(!alignPtr->addOutputIndex(&_outputIndex))
{
return OTHERERROR;
}
}
else // profile2
{
//test names before appending
if (! alignPtr->testUniqueNames(&seqVector, offendingSeq)) { //nige added
return ALLNAMESNOTDIFFERENT;
}
alignPtr->appendSequences(&seqVector);
if(!alignPtr->appendOutputIndex(&_outputIndex))
{
return OTHERERROR;
}
}
// Then need to pass the length to the parser to get the secondary structure info.
int maxAlnLength; // Get values from the Alignment object.
maxAlnLength = alignPtr->getMaxAlnLength();
// look for a feature table / gap penalty mask (only if this is a profile)
if (userParameters->getProfileNum() > 0)
{
structPenalties = NONE;
secStructMask.clear();
gapPenaltyMask.clear();
secStructName = "";
fileParser->getSecStructure(gapPenaltyMask, secStructMask, secStructName,
structPenalties, maxAlnLength);
// Andreas Wilm (UCD): bug 114
// after calling getSecStructure gapPenaltyMask needs to be
// allocated (it's copied/linked to later)
if (gapPenaltyMask.empty())
{
gapPenaltyMask.resize(secStructMask.size());
}
}
if (fileIn->is_open())
{
fileIn->close();
}
return OK; // return the number of seqs. read in this call
}
/*
* The function profileInput is used to read profiles into the Alignment. If the first
* profile is already there it will read in the second profile. It returns the number
* of seqs. If it returns -1, couldnt open file.
*/
int FileReader::profileInput(Alignment *alignPtr)
{
int code;
//int i, totalNumOfSeqs = 0;
string offendingSeq;
if(userParameters->getProfileNum() == 2 && userParameters->getProfile1Empty())
{
utilityObject->error("You must read in profile number 1 first\n");
return MUSTREADINPROFILE1FIRST;
}
if(userParameters->getProfileNum() == 1) // for the 1st profile
{
code = readSeqs(alignPtr, 1, &offendingSeq);
if (code == OK)
{
// success; found some seqs.
userParameters->setStructPenalties1(NONE);
alignPtr->clearSecStruct1(); // Clear old info
if (structPenalties != NONE) // feature table / mask in alignment
{
userParameters->setStructPenalties1(structPenalties);
if (structPenalties == SECST)
{
alignPtr->addSecStructMask1(&secStructMask);
}
alignPtr->addGapPenaltyMask1(&gapPenaltyMask);
alignPtr->addSecStructName1(secStructName);
}
alignPtr->setProfile1NumSeqs(alignPtr->getNumSeqs());
userParameters->setProfile1Empty(false);
userParameters->setProfile2Empty(true);
cout << "No. of seqs = " << alignPtr->getNumSeqs() << endl;
}
else
{
return code; // FIXME and offendingSeq?
}
}
else
{
// first seq to be read = profile1_nseqs + 1
int profile1NumOfSeqs = alignPtr->getNumSeqs();
code = readSeqs(alignPtr, profile1NumOfSeqs + 1, &offendingSeq);
if(code == OK)
{
userParameters->setStructPenalties2(NONE);
alignPtr->clearSecStruct2(); // Clear old info
if (structPenalties != NONE) // feature table / mask in alignment
{
userParameters->setStructPenalties2(structPenalties);
if (structPenalties == SECST)
{
alignPtr->addSecStructMask2(&secStructMask);
}
alignPtr->addGapPenaltyMask2(&gapPenaltyMask);
alignPtr->addSecStructName2(secStructName);
}
cout << "No. of seqs in profile=" << alignPtr->getNumSeqs() - profile1NumOfSeqs
<< endl;
cout << "Total no. of seqs =" << alignPtr->getNumSeqs() << endl;
userParameters->setProfile2Empty(false);
userParameters->setEmpty(false);
}
else
{
return code; // FIXME and offendingSeq?
}
}
// Clear out the masks used in this class. This is important!!!!
secStructMask.clear();
gapPenaltyMask.clear();
secStructName = "";
string typeOfAlign = userParameters->getDNAFlag() ? "DNA" : "PROTEIN";
cout << "Sequences assumed to be "
<< typeOfAlign << endl;
if (userParameters->getMenuFlag())
{
cout<< "\n\n";
}
alignPtr->printSequencesAddedInfo();
if(userParameters->getDNAFlag())
{
userParameters->setDNAMultiGap();
}
else
{
userParameters->setProtMultiGap();
}
return OK;
}
/*
* The function checkInfile is used to find out which format the file is in, and it
* also calls the appropriate function to count the seqences.
*/
void FileReader::checkInfile(int *nseqs, auto_ptr<FileParser>& fileParser)
{
char _lineIn[MAXLINE + 1];
int i;
int lengthLine = 0;
*nseqs = 0;
while (fileIn->getline(_lineIn, MAXLINE + 1))
{
if (!utilityObject->blankLine(_lineIn))
{
break;
}
}
lengthLine = strlen(_lineIn) - 1; // Mark Change 20-6-07
for (i = lengthLine; i >= 0; i--)
{
if (isgraph(_lineIn[i]))
{
break;
}
}
_lineIn[i + 1] = EOS;
// Put first 7 characters into upper case!
for (i = 0; i <= 6 && i <= lengthLine; i++)
{
_lineIn[i] = toupper(_lineIn[i]);
}
// Create the parser to read the file.
if (utilityObject->lineType(_lineIn, "ID"))
{
// EMBL/Swiss-Prot format ?
fileParser.reset(new EMBLFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is EMBL\n";
}
else if (utilityObject->lineType(_lineIn, "CLUSTAL"))
{
fileParser.reset(new ClustalFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is CLUSTAL\n";
}
else if (utilityObject->lineType(_lineIn, "PILEUP")) // MSF
{
fileParser.reset(new MSFFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is MSF\n";
}
else if (utilityObject->lineType(_lineIn, "!!AA_MULTIPLE_ALIGNMENT")) // MSF
{
fileParser.reset(new MSFFileParser(sequenceFileName));
userParameters->setDNAFlag(false);
if(userParameters->getDisplayInfo())
cout << "Sequence format is MSF\n";
}
else if (utilityObject->lineType(_lineIn, "!!NA_MULTIPLE_ALIGNMENT")) // MSF
{
fileParser.reset(new MSFFileParser(sequenceFileName));
userParameters->setDNAFlag(true);
if(userParameters->getDisplayInfo())
cout << "Sequence format is MSF\n";
}
else if (strstr(_lineIn, "MSF") && _lineIn[strlen(_lineIn) - 1] == '.' &&
_lineIn[strlen(_lineIn) - 2] == '.') // MSF
{
fileParser.reset(new MSFFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is MSF\n";
}
else if (utilityObject->lineType(_lineIn, "!!RICH_SEQUENCE")) // RSF
{
fileParser.reset(new RSFFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is RSF\n";
}
else if (utilityObject->lineType(_lineIn, "#NEXUS"))
{
//utilityObject->error("Cannot read NEXUS format\n");
return;
}
else if (*_lineIn == '>')
{
if ((lengthLine>=3) && _lineIn[3] == ';') {
//if(_lineIn[3] == ';') // distinguish PIR and Pearson
{
// PIR
fileParser.reset(new PIRFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is PIR\n";
}
}
else
{
// PEARSON
fileParser.reset(new PearsonFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is Pearson\n";
}
}
else if ((*_lineIn == '"') || (*_lineIn == '%') || (*_lineIn == '#'))
{
// GDE format
fileParser.reset(new GDEFileParser(sequenceFileName));
if(userParameters->getDisplayInfo())
cout << "Sequence format is GDE\n";
if (*_lineIn == '%')
{
userParameters->setDNAFlag(false);
}
else if (*_lineIn == '#')
{
userParameters->setDNAFlag(true);
}
}
else
{
return ;
}
try
{
// Get the number of sequences. This is passed back as a pointer!
*nseqs = fileParser->countSeqs();
// no output in 1.83: cout << "number of seqs is: " << *nseqs << "\n";
}
catch(exception ex)
{
*nseqs = 0;
}
}
}
|