// // Copyright 2009 Illumina, Inc. // // This software is covered by the "Illumina Genome Analyzer Software // License Agreement" and the "Illumina Source Code License Agreement", // and certain third party copyright/licenses, and any user of this // source file is bound by the terms therein (see accompanying files // Illumina_Genome_Analyzer_Software_License_Agreement.pdf and // Illumina_Source_Code_License_Agreement.pdf and third party // copyright/license notices). // // /// \file /// \author Ivan Mikoulitch /// #include "samReader.h" #include "SamMD2Export/utils.h" SamReader::SamReader(const string& sam_file_name) { sam_file_name_=sam_file_name; export_file_name_ = sam_file_name_+".export"; export_log_file_name_ = sam_file_name_+".export.log"; BUFFER = new char[BUFFER_SIZE]; has_data_=false; export_line_count=0; num_invalid_reads_=0; } SamReader::~SamReader(void) { Close(); } bool SamReader::Open() { try { sam_ifs_.open(sam_file_name_.c_str()); if (!sam_ifs_.is_open()) { WriteError("Cannot open sam file: '" + sam_file_name_ + "'"); return false; } export_ofs_.open(export_file_name_.c_str()); if(!export_ofs_.is_open()) { WriteError("Cannot open output file: '" + export_file_name_ + "'"); return false; } export_log_.open(export_log_file_name_.c_str()); if(!export_log_.is_open()) { WriteError("Cannot open log file: '" + export_log_file_name_ + "'"); return false; } WriteInfo("Reading sam file: '" + sam_file_name_ + "' ..."); WriteInfo("Writing export file: '" + export_file_name_ + "' ..."); WriteInfo(""); ReadHeader(); return true; } catch(...) { WriteError("Exception occured while opening sam file: " + sam_file_name_); return false; } } void SamReader::Close() { Flush(); sam_ifs_.close(); export_ofs_.close(); export_log_.close(); } bool SamReader::GetNextSamLine() { if (has_data_) { has_data_=false; return true; } else { if(sam_ifs_.getline(BUFFER, BUFFER_SIZE)) { return true; } else { return false; } } } void SamReader::ReadHeader() { while(sam_ifs_.getline(BUFFER, BUFFER_SIZE)) { if (BUFFER[0]!='@') { has_data_=true; break; } } } bool SamReader::WriteExportLine(const Export& exr) { export_buffer.append( exr.Machine ); export_buffer.append( "\t"); // 1 export_buffer.append( exr.RunNumber ); export_buffer.append( "\t"); // 2 export_buffer.append( exr.Lane ); export_buffer.append( "\t"); // 3 export_buffer.append( exr.Tile ); export_buffer.append( "\t"); // 4 export_buffer.append( exr.XCoordinateCluster ); export_buffer.append( "\t"); // 5 export_buffer.append( exr.YCoordinateCluster ); export_buffer.append( "\t"); // 6 export_buffer.append( exr.Index ); export_buffer.append( "\t"); // 7 export_buffer.append( exr.ReadNumber ); export_buffer.append( "\t"); // 8 export_buffer.append( exr.Read ); export_buffer.append( "\t"); // 9 export_buffer.append( exr.QualityString ); export_buffer.append( "\t"); // 10 export_buffer.append( exr.MatchChromosome ); export_buffer.append( "\t"); // 11 export_buffer.append( exr.MatchContig ); export_buffer.append( "\t"); // 12 export_buffer.append( exr.MatchPosition ); export_buffer.append( "\t"); // 13 export_buffer.append( exr.MatchStrand ); export_buffer.append( "\t"); // 14 export_buffer.append( exr.MatchDescriptor ); export_buffer.append( "\t"); // 15 export_buffer.append( exr.SingleReadAlignmentScore ); export_buffer.append( "\t"); // 16 export_buffer.append( exr.PairedReadAlignmentScore ); export_buffer.append( "\t"); // 17 export_buffer.append( exr.PartnerChromosome ); export_buffer.append( "\t"); // 18 export_buffer.append( exr.PartnerContig ); export_buffer.append( "\t"); // 19 export_buffer.append( exr.PartnerOffset ); export_buffer.append( "\t"); // 20 export_buffer.append( exr.PartnerStrand ); export_buffer.append( "\t"); // 21 export_buffer.append( exr.FilteringYN ); export_buffer.append( "\n"); // 22 export_line_count++; if(export_line_count==MAX_EXPORT_LINE_COUNT) { export_ofs_ << export_buffer; export_buffer.clear(); export_buffer.reserve(MAX_EXPORT_LINE_COUNT*512); export_line_count=0; } return true; } void SamReader::Flush() { if (export_line_count>0) { export_ofs_ << export_buffer; export_buffer.clear(); export_buffer.reserve(MAX_EXPORT_LINE_COUNT*512); export_line_count=0; } } void SamReader::WriteInfo(const string& str) { cout << str << endl; if(export_log_.is_open()) { export_log_ << str << endl; } return; } void SamReader::WriteWarning(const string& str) { cout << "Warning! " << str << endl; if(export_log_.is_open()) { export_log_ << "Warning! " << str << endl; } return; } void SamReader::WriteError(const string& str) { cout << "Error!!! " << str << endl; if(export_log_.is_open()) { export_log_ << "Error!!! " << str << endl; } return; } void SamReader::WriteFailedRead(const string& message, const string& read) { num_invalid_reads_++; cout << "! Invalid Read: " << message << endl; cout << read << endl << endl; if(export_log_.is_open()) { export_log_ << "! Invalid Read: " << message << endl; export_log_ << read << endl << endl; } return; } bool SamReader::WriteWarningErrorSummary() { if (num_invalid_reads_==0) return true; WriteInfo("!!! "+to_string(num_invalid_reads_)+" Invalid Reads were detected and excluded from export file."); WriteInfo("The log file has details: "+export_log_file_name_); return false; }