Line 72: |
Line 72: |
| | Get the Status Message of the last call that sets status. | | | Get the Status Message of the last call that sets status. |
| |- | | |- |
− | | '''DEPRECATED: <code>SamStatus::Status SamFile::GetFailure()</code>''' | + | | '''DEPRECATED: '''<code>SamStatus::Status SamFile::GetFailure()</code> |
| | Get the type of failure that occurred on a method failure. | | | Get the type of failure that occurred on a method failure. |
| |- | | |- |
Line 105: |
Line 105: |
| | | |
| <source lang="cpp"> | | <source lang="cpp"> |
| + | #include "SamFile.h" |
| + | #include "SamValidation.h" |
| + | |
| int main(int argc, char ** argv) | | int main(int argc, char ** argv) |
| { | | { |
| + | // Check for the appropriate number of arguments. |
| if(argc != 3) | | if(argc != 3) |
| { | | { |
Line 113: |
Line 117: |
| } | | } |
| | | |
− | | + | // Open the input file for reading. |
| SamFile samIn; | | SamFile samIn; |
− |
| + | if(!samIn.OpenForRead(argv[1])) |
− | samIn.OpenForRead(argv[1]); | + | { |
| + | fprintf(stderr, "%s\n", samIn.GetStatusMessage()); |
| + | return(samIn.GetStatus()); |
| + | } |
| | | |
| + | // Open the output file for writing. |
| SamFile samOut; | | SamFile samOut; |
− | | + | if(!samOut.OpenForWrite(argv[2])) |
− | samOut.OpenForWrite(argv[2]); | + | { |
| + | fprintf(stderr, "%s\n", samOut.GetStatusMessage()); |
| + | return(samOut.GetStatus()); |
| + | } |
| | | |
| // Read the sam header. | | // Read the sam header. |
| SamFileHeader samHeader; | | SamFileHeader samHeader; |
− | samIn.ReadHeader(samHeader); | + | if(!samIn.ReadHeader(samHeader)) |
| + | { |
| + | fprintf(stderr, "%s\n", samIn.GetStatusMessage()); |
| + | return(samIn.GetStatus()); |
| + | } |
| | | |
− | samOut.WriteHeader(samHeader); | + | // Write the sam header. |
| + | if(!samOut.WriteHeader(samHeader)) |
| + | { |
| + | fprintf(stderr, "%s\n", samOut.GetStatusMessage()); |
| + | return(samOut.GetStatus()); |
| + | } |
| | | |
− | // Read the first sam record.
| |
| SamRecord samRecord; | | SamRecord samRecord; |
| + | SamValidationErrors samInvalidErrors; |
| | | |
− | // Track if any of the records are invalid. Initialize to 0, it will | + | // Set writeStatus to success. It will be changed |
− | // be set to -1 on an invalid record. | + | // to the failure reason if any of the writes fail. |
− | int status = 0; | + | SamStatus::Status writeStatus = SamStatus::SUCCESS; |
| | | |
− | // Keep reading records until the end of the file is reached. | + | // Keep reading records until ReadRecord returns false. |
− | int numValidRecords = 0;
| + | while(samIn.ReadRecord(samHeader, samRecord)) |
− | while(!samIn.IsEOF()) | |
| { | | { |
− | if(samIn.ReadRecord(samHeader, samRecord) == true) | + | // Successfully read a record from the file, so write it. |
| + | if(!samOut.WriteRecord(samHeader, samRecord)) |
| { | | { |
− | // Successfully read a record from the file, so check to see | + | // Failed to write a record. |
− | // if it is valid.
| + | fprintf(stderr, "%s\n", samOut.GetStatusMessage()); |
− | if(samRecord.isValid(samHeader)) | + | writeStatus = samOut.GetStatus(); |
− | {
| |
− | // It is valid, so write it.
| |
− | numValidRecords++;
| |
− | samOut.WriteRecord(samHeader, samRecord);
| |
− | } | |
− | else
| |
− | {
| |
− | // There is at least one invalid record, return -1.
| |
− | status = -1;
| |
− | }
| |
− | }
| |
− | else
| |
− | {
| |
− | // Failed to read the record. Check to see if it failed due to
| |
− | // there being no more records, which is not a failure. Any
| |
− | // other failure reason counts as a failure.
| |
− | if(samIn.GetFailure() != SamStatus::NO_MORE_RECS)
| |
− | {
| |
− | // Read failed, invalid.
| |
− | status = -1;
| |
− | }
| |
| } | | } |
| } | | } |
− | std::cout << "Number of records read = " << | + | |
| + | if(samIn.GetStatus() != SamStatus::NO_MORE_RECS) |
| + | { |
| + | // Failed to read a record. |
| + | fprintf(stderr, "%s\n", samIn.GetStatusMessage()); |
| + | } |
| + | |
| + | std::cout << std::endl << "Number of records read = " << |
| samIn.GetCurrentRecordCount() << std::endl; | | samIn.GetCurrentRecordCount() << std::endl; |
| std::cout << "Number of records written = " << | | std::cout << "Number of records written = " << |
| samOut.GetCurrentRecordCount() << std::endl; | | samOut.GetCurrentRecordCount() << std::endl; |
− | std::cout << "Number of valid records = " << numValidRecords << std::endl;
| |
| | | |
− | return(status); | + | if(samIn.GetStatus() != SamStatus::NO_MORE_RECS) |
| + | { |
| + | // Failed reading a record. |
| + | return(samIn.GetStatus()); |
| + | } |
| + | |
| + | // Since the reads were successful, return the status based |
| + | // on the status of the writes. If any failed, return |
| + | // their failure status. |
| + | return(writeStatus); |
| } | | } |
| </source> | | </source> |
Line 184: |
Line 198: |
| const char* indexFilename) | | const char* indexFilename) |
| { | | { |
| + | // Open the input file for reading. |
| SamFile samIn; | | SamFile samIn; |
− |
| + | if(!samIn.OpenForRead(inputFilename)) |
− | samIn.OpenForRead(inputFilename); | + | { |
− | samIn.ReadBamIndex(indexFilename); | + | fprintf(stderr, "%s\n", samIn.GetStatusMessage()); |
| + | return(samIn.GetStatus()); |
| + | } |
| + | |
| + | // Open the bam index file for reading. |
| + | if(!samIn.ReadBamIndex(indexFilename)) |
| + | { |
| + | fprintf(stderr, "%s\n", samIn.GetStatusMessage()); |
| + | return(samIn.GetStatus()); |
| + | } |
| | | |
| + | // Open the output file for writing. |
| SamFile samOut; | | SamFile samOut; |
− | | + | if(!samOut.OpenForWrite(outputFilename)) |
− | samOut.OpenForWrite(outputFilename); | + | { |
| + | fprintf(stderr, "%s\n", samOut.GetStatusMessage()); |
| + | return(samOut.GetStatus()); |
| + | } |
| | | |
| // Read the sam header. | | // Read the sam header. |
| SamFileHeader samHeader; | | SamFileHeader samHeader; |
− | samIn.ReadHeader(samHeader); | + | if(!samIn.ReadHeader(samHeader)) |
| + | { |
| + | fprintf(stderr, "%s\n", samIn.GetStatusMessage()); |
| + | return(samIn.GetStatus()); |
| + | } |
| + | |
| // Write the sam header. | | // Write the sam header. |
− | samOut.WriteHeader(samHeader); | + | if(!samOut.WriteHeader(samHeader)) |
| + | { |
| + | fprintf(stderr, "%s\n", samOut.GetStatusMessage()); |
| + | return(samOut.GetStatus()); |
| + | } |
| | | |
| SamRecord samRecord; | | SamRecord samRecord; |
− | | + | SamValidationErrors samInvalidErrors; |
− | int numValidRecords = 0;
| |
− | int numRecords = 0;
| |
| | | |
| // Loop through each Reference. | | // Loop through each Reference. |
Line 209: |
Line 244: |
| int numSectionRecords = 0; | | int numSectionRecords = 0; |
| samIn.SetReadSection(i); | | samIn.SetReadSection(i); |
− | // Keep reading records until they aren't read successfully. | + | // Keep reading records until they aren't more. |
− | while(samIn.ReadRecord(samHeader, samRecord) == true) | + | while(samIn.ReadRecord(samHeader, samRecord)) |
| { | | { |
| numSectionRecords++; | | numSectionRecords++; |
− | numRecords++;
| + | // Successfully read a record from the file, so write it. |
− | // Successfully read a record from the file, so check to see | + | if(!samOut.WriteRecord(samHeader, samRecord)) |
− | // if it is valid.
| |
− | if(samRecord.isValid(samHeader)) | |
| { | | { |
− | // It is valid, so write it. | + | // Failed to write a record. |
− | numValidRecords++; | + | fprintf(stderr, "%s\n", samOut.GetStatusMessage()); |
− | samOut.WriteRecord(samHeader, samRecord);
| |
| } | | } |
| } | | } |
| + | |
| + | if(samIn.GetStatus() != SamStatus::NO_MORE_RECS) |
| + | { |
| + | // Failed to read a record. |
| + | fprintf(stderr, "%s\n", samIn.GetStatusMessage()); |
| + | } |
| + | |
| std::cout << "Reference ID " << i << " has " << numSectionRecords | | std::cout << "Reference ID " << i << " has " << numSectionRecords |
| << " records" << std::endl; | | << " records" << std::endl; |
| } | | } |
− |
| + | |
− | std::cout << "Number of records = " << numRecords << std::endl; | + | std::cout << "Number of records = " << samIn.GetCurrentRecordCount() << std::endl; |
− | std::cout << "Number of valid records = " << numValidRecords << std::endl; | + | |
− | | |
| return(0); | | return(0); |
| } | | } |
| </source> | | </source> |