Slice Tools libSlice |
ReadData.ccGo to the documentation of this file.00001 #include "Exceptions.hh" 00002 #include "ReadData.hh" 00003 00004 #include <stdlib.h> // for atol 00005 00010 namespace libSlice 00011 { 00012 using std::string; 00013 00015 00024 ReadData::ReadData(const char * id, 00025 const char * seqname, 00026 const char * dir, 00027 const char * chem) 00028 { 00029 if (!id) 00030 { 00031 throw Exception("Cannot have NULL read id", "ReadData::ReadData"); 00032 } 00033 00034 m_id = id; 00035 00036 m_dir = (dir) ? dir : "0"; 00037 m_chem = (chem) ? chem : ""; 00038 m_seqname = (seqname) ? seqname : ""; 00039 00040 m_clrleft = m_seqleft = 0; 00041 m_clrright = m_seqright = 0; 00042 00043 parseDirection(); 00044 } 00045 00047 00054 void ReadData::addClrData(const char * left, 00055 const char * right) 00056 { 00057 setSeqData((left) ? atol(left) : 0, 00058 (right) ? atol(right) : 0); 00059 } 00060 00061 void ReadData::setSeqData(long int seqleft, 00062 long int seqright) 00063 { 00064 m_seqleft = seqleft; 00065 m_seqright = seqright; 00066 00067 m_clrleft = (m_rc) ? m_seqright : m_seqleft; 00068 m_clrright = (m_rc) ? m_seqleft : m_seqright; 00069 00070 if (m_clrright < m_clrleft) 00071 { 00072 throw Exception("Invalid Clrdata for id " + m_id, 00073 "ReadData::addClrData"); 00074 } 00075 } 00076 00077 void ReadData::setClrData(long int clrleft, 00078 long int clrright) 00079 { 00080 if (m_rc) 00081 { 00082 setSeqData(clrright, clrleft); 00083 } 00084 else 00085 { 00086 setSeqData(clrleft, clrright); 00087 00088 } 00089 } 00090 00091 00093 00098 void ReadData::parseDirection() 00099 { 00100 if (m_dir == "0" || m_dir[0] == 'F') 00101 { 00102 m_rc = 0; 00103 } 00104 else if (m_dir == "1" || m_dir[0] == 'R') 00105 { 00106 m_rc = 1; 00107 } 00108 else 00109 { 00110 throw Exception("Read Direction is unknown in " + m_dir, 00111 "ReadData::ReadData"); 00112 } 00113 } 00114 00116 00119 char ReadData::isReverseCompliment() const 00120 { 00121 return m_rc; 00122 } 00123 00125 00131 void ReadData::printXML(ostream & out, const string & prefix) const 00132 { 00133 out << prefix 00134 << "<Read Id=\"" << m_id 00135 << "\" Seqname=\"" << m_seqname 00136 << "\" Dir=\"" << m_dir 00137 << "\" Chem=\"" << m_chem 00138 << "\">" << endl; 00139 00140 if (m_seqright) 00141 { 00142 out << prefix 00143 << " <Clr Left=\"" << m_seqleft 00144 << "\" Right=\"" << m_seqright 00145 << "\" />" << endl; 00146 } 00147 00148 if (m_leftnuc.length()) 00149 { 00150 out << prefix << " <TrimmedSeq position=\"left\">" << endl 00151 << prefix << " <Nuc>" << m_leftnuc << "</Nuc>" << endl 00152 << prefix << " <Qualval>" << m_leftqual << "</Qualval>" << endl 00153 << prefix << " </TrimmedSeq>" << endl; 00154 } 00155 00156 if (m_rightnuc.length()) 00157 { 00158 out << prefix << " <TrimmedSeq position=\"right\">" << endl 00159 << prefix << " <Nuc>" << m_rightnuc << "</Nuc>" << endl 00160 << prefix << " <Qualval>" << m_rightqual << "</Qualval>" << endl 00161 << prefix << " </TrimmedSeq>" << endl; 00162 } 00163 00164 out << prefix << "</Read>" << endl; 00165 } 00166 00168 00170 void ReadData::reverseCompliment() 00171 { 00172 if (m_dir == "Forward") 00173 { 00174 m_dir = "Reversed"; 00175 } 00176 else if (m_dir == "0") 00177 { 00178 m_dir = "1"; 00179 } 00180 else if (m_dir == "Reversed") 00181 { 00182 m_dir = "Forward"; 00183 } 00184 else if (m_dir == "1") 00185 { 00186 m_dir = "0"; 00187 } 00188 00189 long swap = m_seqleft; 00190 m_seqleft = m_seqright; 00191 m_seqright = swap; 00192 } 00193 00195 00203 void ReadData::resetId(const string & id) 00204 { 00205 m_id = id; 00206 } 00207 00208 00209 00211 const string & ReadData::getId() const 00212 { 00213 return m_id; 00214 } 00215 00217 const string & ReadData::getSeqname() const 00218 { 00219 return m_seqname; 00220 } 00221 00223 const string & ReadData::getChem() const 00224 { 00225 return m_chem; 00226 } 00227 00229 long ReadData::getSeqleft() const 00230 { 00231 return m_seqleft; 00232 } 00233 00235 long ReadData::getSeqright() const 00236 { 00237 return m_seqright; 00238 } 00239 00241 long ReadData::getClrleft() const 00242 { 00243 return m_clrleft; 00244 } 00245 00247 long ReadData::getClrright() const 00248 { 00249 return m_clrright; 00250 } 00251 00253 void ReadData::addNucData(const char * buffer, bool isLeft) 00254 { 00255 if (buffer) 00256 { 00257 if (isLeft) 00258 { 00259 m_leftnuc += buffer; 00260 } 00261 else 00262 { 00263 m_rightnuc += buffer; 00264 } 00265 } 00266 } 00267 00269 void ReadData::addQualData(const char * buffer, bool isLeft) 00270 { 00271 if (buffer) 00272 { 00273 if (isLeft) 00274 { 00275 m_leftqual += buffer; 00276 } 00277 else 00278 { 00279 m_rightqual += buffer; 00280 } 00281 } 00282 } 00283 00285 const string & ReadData::getRawNucData(bool getLeft) const 00286 { 00287 if (getLeft) return m_leftnuc; 00288 return m_rightnuc; 00289 } 00290 00291 00293 const string & ReadData::getRawQualData(bool getLeft) const 00294 { 00295 if (getLeft) return m_leftqual; 00296 return m_rightqual; 00297 } 00298 00300 void ReadData::validateSeqdata() const 00301 { 00302 //TODO: Implement this 00303 00304 } 00305 00306 00308 00311 const string ReadData::getNucData(bool getLeft) const 00312 { 00313 validateSeqdata(); 00314 00315 string retval; 00316 00317 if (getLeft) 00318 { 00319 retval = m_leftnuc; 00320 00321 if (!retval.size()) 00322 { 00323 for (long i = 0; i < m_clrleft-1; i++) 00324 { 00325 retval += "?"; 00326 } 00327 } 00328 } 00329 else 00330 { 00331 retval = m_rightnuc; 00332 00333 // Can't pad here, because length is unknown 00334 } 00335 00336 return retval; 00337 } 00338 00340 00343 const string ReadData::getQualData(bool getLeft) const 00344 { 00345 validateSeqdata(); 00346 00347 string retval; 00348 00349 if (getLeft) 00350 { 00351 retval = m_leftqual; 00352 00353 if (!retval.size()) 00354 { 00355 bool isFirst = true; 00356 for (long i = 0; i < m_clrleft-1; i++) 00357 { 00358 if (!isFirst) retval += " "; 00359 retval += "00"; 00360 isFirst = false; 00361 } 00362 } 00363 } 00364 else 00365 { 00366 retval = m_rightqual; 00367 00368 // Can't pad here, because length is unknown 00369 } 00370 00371 return retval; 00372 } 00373 00374 00376 void ReadData::flushTrimmedSeq() 00377 { 00378 m_leftnuc = ""; 00379 m_leftqual = ""; 00380 00381 m_rightnuc = ""; 00382 m_rightqual = ""; 00383 } 00384 00386 void ReadData::flushTrimmedSeqSide(bool flushLeft) 00387 { 00388 if (flushLeft) 00389 { 00390 m_leftnuc = ""; 00391 m_leftqual = ""; 00392 } 00393 else 00394 { 00395 m_rightnuc = ""; 00396 m_rightqual = ""; 00397 } 00398 } 00399 00400 00402 00404 ReadManager::ReadManager() 00405 { 00406 m_readIterator = m_readMap.end(); 00407 } 00408 00410 ReadManager::~ReadManager() 00411 { 00412 ReadMap::iterator i; 00413 00414 for (i = m_readMap.begin(); 00415 i != m_readMap.end(); 00416 i++) 00417 { 00418 delete i->second; 00419 } 00420 } 00421 00423 00429 ReadData * ReadManager::addReadData(const char * id, 00430 const char * seqname, 00431 const char * dir, 00432 const char * chem) 00433 { 00434 ReadData * newRead = new ReadData(id, seqname, dir, chem); 00435 00436 insertRead(newRead); 00437 00438 return newRead; 00439 } 00440 00442 ReadData * ReadManager::getNextRead() 00443 { 00444 ReadData * retval = NULL; 00445 00446 if (m_readIterator != m_readMap.end()) 00447 { 00448 retval = m_readIterator->second; 00449 m_readIterator++; 00450 } 00451 00452 return retval; 00453 } 00454 00456 void ReadManager::resetReadIterator() 00457 { 00458 m_readIterator = m_readMap.begin(); 00459 } 00460 00462 00465 ReadData * ReadManager::find(const string & readId) const 00466 { 00467 ReadData * retval = NULL; 00468 ReadMap::const_iterator i = m_readMap.find(readId); 00469 00470 if (i != m_readMap.end()) 00471 { 00472 retval = i->second; 00473 } 00474 00475 return retval; 00476 } 00477 00479 int ReadManager::size() const 00480 { 00481 return m_readMap.size(); 00482 } 00483 00485 void ReadManager::removeRead(const string & id) 00486 { 00487 m_readMap.erase(id); 00488 } 00489 00491 00494 void ReadManager::insertRead(ReadData * rd) 00495 { 00496 const string & id = rd->getId(); 00497 00498 if (find(id)) 00499 { 00500 throw Exception(id + " is already in readMap", 00501 "ReadManager::insertRead"); 00502 } 00503 00504 m_readMap[id] = rd; 00505 } 00506 00508 00512 void ReadManager::resetReadId(ReadData * rd, const string & id) 00513 { 00514 const string & origId = rd->getId(); 00515 00516 if (find(origId)) 00517 { 00518 removeRead(origId); 00519 rd->resetId(id); 00520 insertRead(rd); 00521 } 00522 } 00523 00525 void ReadManager::printXML(ostream & out, const string & prefix) const 00526 { 00527 out << prefix << "<ReadCollection>" << endl; 00528 00529 ReadMap::const_iterator i; 00530 for (i = m_readMap.begin(); 00531 i != m_readMap.end(); 00532 i++) 00533 { 00534 i->second->printXML(out, prefix + " "); 00535 } 00536 00537 out << prefix << "</ReadCollection>" << endl; 00538 } 00539 00540 } // namespace |