]> git.donarmstrong.com Git - bamtools.git/blobdiff - bamtools_fasta.cpp
Added the <cctype> include to BamAux to explicitly support the toupper() fxn. It...
[bamtools.git] / bamtools_fasta.cpp
index 74cde03a7e88d5bbb9c56465057c343b3b4d32cf..ef1c742e57073fe95b5343257bfc14c8922c4c94 100644 (file)
@@ -1,3 +1,13 @@
+// ***************************************************************************
+// bamtools_fasta.cpp (c) 2010 Derek Barnett, Erik Garrison
+// Marth Lab, Department of Biology, Boston College
+// All rights reserved.
+// ---------------------------------------------------------------------------
+// Last modified: 13 July 2010
+// ---------------------------------------------------------------------------
+// Provides FASTA reading/indexing functionality.
+// ***************************************************************************
+
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
@@ -157,12 +167,26 @@ bool Fasta::FastaPrivate::CreateIndex(const string& indexFilename) {
     string sequence = "";
     while ( GetNextHeader(header) ) {
         
+        // ---------------------------
         // build index entry data
         FastaIndexData data;
-        GetNameFromHeader(header, data.Name);
+        
+        // store file offset of beginning of DNA sequence (after header)
         data.Offset = ftello(Stream);
         
-        GetNextSequence(sequence);
+        // parse header, store sequence name in data.Name
+        if ( !GetNameFromHeader(header, data.Name) ) {
+            cerr << "FASTA error : could not parse read name from FASTA header" << endl;
+            return false;
+        }
+        
+        // retrieve FASTA sequence
+        if ( !GetNextSequence(sequence) ) {
+            cerr << "FASTA error : could not read in next sequence from FASTA file" << endl;
+            return false;
+        }
+        
+        // store sequence length & line/byte lengths
         data.Length = sequence.length();
         data.LineLength = lineLength;
         data.ByteLength = byteLength;
@@ -176,7 +200,7 @@ bool Fasta::FastaPrivate::CreateIndex(const string& indexFilename) {
     
     // open index file
     if ( !indexFilename.empty() ) {
-        IndexStream = fopen64(indexFilename.c_str(), "wb");
+        IndexStream = fopen(indexFilename.c_str(), "wb");
         if ( !IndexStream ) {
             cerr << "FASTA error : Could not open " << indexFilename << " for writing." << endl;
             return false;
@@ -305,7 +329,7 @@ bool Fasta::FastaPrivate::GetNameFromHeader(const string& header, string& name)
     }
 
     if ( start == stop ) {
-        cout << "FASTA error : could not parse read name from FASTA header." << endl;
+        cerr << "FASTA error : could not parse read name from FASTA header" << endl;
         return false;
     }
 
@@ -507,7 +531,7 @@ bool Fasta::FastaPrivate::Open(const string& filename, const string& indexFilena
     bool success = true;
   
     // open FASTA filename
-    Stream = fopen64(filename.c_str(), "rb");
+    Stream = fopen(filename.c_str(), "rb");
     if ( !Stream ) {
         cerr << "FASTA error: Could not open " << filename << " for reading" << endl;
         return false;
@@ -517,7 +541,7 @@ bool Fasta::FastaPrivate::Open(const string& filename, const string& indexFilena
     
     // open index file if it exists
     if ( !indexFilename.empty() ) {
-        IndexStream = fopen64(indexFilename.c_str(), "rb");
+        IndexStream = fopen(indexFilename.c_str(), "rb");
         if ( !IndexStream ) {
             cerr << "FASTA error : Could not open " << indexFilename << " for reading." << endl;
             return false;
@@ -582,7 +606,7 @@ Fasta::~Fasta(void) {
     d = 0;
 }
 
-bool Fasta::Close(void) {
+bool Fasta::Close(void) { 
     return d->Close();
 }
 
@@ -600,4 +624,4 @@ bool Fasta::GetSequence(const int& refId, const int& start, const int& stop, str
 
 bool Fasta::Open(const string& filename, const string& indexFilename) {
     return d->Open(filename, indexFilename);
-}
\ No newline at end of file
+}