From c1601b4fec5a6aeefee10b80d3bd404d3addd80d Mon Sep 17 00:00:00 2001 From: Brian Ondov Date: Thu, 17 Jul 2014 18:44:50 -0400 Subject: [PATCH] import exceptions and reference track permanence --- src/harvest/HarvestIO.cpp | 10 ++++++++ src/harvest/LcbList.cpp | 48 +++++++++++++++++++++++++++++++---- src/harvest/PhylogenyTree.cpp | 11 +++++++- src/harvest/ReferenceList.cpp | 2 ++ src/harvest/ReferenceList.h | 14 ++++++++++ src/harvest/TrackList.cpp | 23 ++++++++++++++++- src/harvest/TrackList.h | 26 ++++++++++++++++--- src/harvest/VariantList.cpp | 13 +++++++++- src/harvest/pb/harvest.proto | 1 + 9 files changed, 137 insertions(+), 11 deletions(-) diff --git a/src/harvest/HarvestIO.cpp b/src/harvest/HarvestIO.cpp index 0d78280..7bb693e 100644 --- a/src/harvest/HarvestIO.cpp +++ b/src/harvest/HarvestIO.cpp @@ -47,6 +47,11 @@ bool HarvestIO::loadHarvest(const char * file) { int fd = open(file, O_RDONLY); + if ( fd < 0 ) + { + return false; + } + FileInputStream raw_input(fd); GzipInputStream gz(&raw_input); CodedInputStream coded_input(&gz); @@ -100,6 +105,11 @@ void HarvestIO::loadMFA(const char * file, bool findVariants) void HarvestIO::loadNewick(const char * file) { + if ( lcbList.getLcbCount() == 0 ) + { + trackList.clear(); + } + phylogenyTree.initFromNewick(file, &trackList); } diff --git a/src/harvest/LcbList.cpp b/src/harvest/LcbList.cpp index 0f8833f..52c52b9 100644 --- a/src/harvest/LcbList.cpp +++ b/src/harvest/LcbList.cpp @@ -32,7 +32,7 @@ void LcbList::initFromMfa(const char * file, ReferenceList * referenceList, Trac ifstream in(file); char line[1 << 20]; vector seqs; - bool oldTags = trackList->getTrackCount(); + const bool oldTags = phylogenyTree->getRoot(); string refTag; int * trackIndecesNew; @@ -40,6 +40,10 @@ void LcbList::initFromMfa(const char * file, ReferenceList * referenceList, Trac { trackIndecesNew = new int[trackList->getTrackCount()]; } + else + { + trackList->clear(); + } while ( ! in.eof() ) { @@ -64,7 +68,17 @@ void LcbList::initFromMfa(const char * file, ReferenceList * referenceList, Trac if ( oldTags ) { track = &trackList->getTrackMutable(seqs.size()); - trackIndecesNew[trackList->getTrackIndexByFile(tag.c_str())] = seqs.size(); + + try + { + trackIndecesNew[trackList->getTrackIndexByFile(tag.c_str())] = seqs.size(); + } + catch ( const TrackList::TrackNotFoundException & e ) + { + delete [] trackIndecesNew; + throw; + return; + } } else { @@ -141,7 +155,7 @@ void LcbList::initFromXmfa(const char * file, ReferenceList * referenceList, Tra char line[1 << 20]; int trackIndex = 0; vector seqs; - bool oldTags = trackList->getTrackCount(); + const bool oldTags = phylogenyTree->getRoot(); int * trackIndecesNew; bool mauve = false; string ref; @@ -156,6 +170,10 @@ void LcbList::initFromXmfa(const char * file, ReferenceList * referenceList, Tra { trackIndecesNew = new int[trackList->getTrackCount()]; } + else + { + trackList->clear(); + } LcbList::Lcb * lcb = 0; @@ -191,7 +209,17 @@ void LcbList::initFromXmfa(const char * file, ReferenceList * referenceList, Tra if ( oldTags ) { track = &trackList->getTrackMutable(trackIndex); - trackIndecesNew[trackList->getTrackIndexByFile(suffix)] = trackIndex; + + try + { + trackIndecesNew[trackList->getTrackIndexByFile(suffix)] = trackIndex; + } + catch ( const TrackList::TrackNotFoundException & e ) + { + delete [] trackIndecesNew; + throw; + return; + } } else { @@ -217,7 +245,17 @@ void LcbList::initFromXmfa(const char * file, ReferenceList * referenceList, Tra if ( oldTags ) { track = &trackList->getTrackMutable(trackIndex); - trackIndecesNew[trackList->getTrackIndexByFile(suffix)] = trackIndex; + + try + { + trackIndecesNew[trackList->getTrackIndexByFile(suffix)] = trackIndex; + } + catch ( const TrackList::TrackNotFoundException & e ) + { + delete [] trackIndecesNew; + throw; + return; + } } else { diff --git a/src/harvest/PhylogenyTree.cpp b/src/harvest/PhylogenyTree.cpp index 30bb00a..8b38215 100644 --- a/src/harvest/PhylogenyTree.cpp +++ b/src/harvest/PhylogenyTree.cpp @@ -59,7 +59,16 @@ void PhylogenyTree::initFromNewick(const char * file, TrackList * trackList) while ( in.getline(line, (1 << 20) - 1) ) { char * token = line; - root = new PhylogenyTreeNode(token, trackList, useNames); + + try + { + root = new PhylogenyTreeNode(token, trackList, useNames); + } + catch ( const TrackList::TrackNotFoundException & e ) + { + root = 0; + throw; + } } in.close(); diff --git a/src/harvest/ReferenceList.cpp b/src/harvest/ReferenceList.cpp index c85df8c..b6013c6 100644 --- a/src/harvest/ReferenceList.cpp +++ b/src/harvest/ReferenceList.cpp @@ -55,6 +55,8 @@ int ReferenceList::getReferenceSequenceFromGi(long int gi) const } } + throw GiNotFoundException(std::to_string(gi)); + return undef; } diff --git a/src/harvest/ReferenceList.h b/src/harvest/ReferenceList.h index 9d8ea50..eeff12e 100644 --- a/src/harvest/ReferenceList.h +++ b/src/harvest/ReferenceList.h @@ -20,6 +20,20 @@ class ReferenceList { public: + class GiNotFoundException : public std::exception + { + public: + + GiNotFoundException(const std::string & giNew) + { + gi = giNew; + } + + virtual ~GiNotFoundException() throw() {} + + std::string gi; + }; + void addReference(std::string name, std::string sequence); void clear(); int getPositionFromConcatenated(int sequence, long int position) const; diff --git a/src/harvest/TrackList.cpp b/src/harvest/TrackList.cpp index 8e099e2..330b7da 100644 --- a/src/harvest/TrackList.cpp +++ b/src/harvest/TrackList.cpp @@ -2,6 +2,11 @@ using namespace::std; +TrackList::TrackList() +{ + trackReference = 0; +} + int TrackList::addTrack(const char * file, int size, const char * name, TrackType type) { tracks.resize(tracks.size() + 1); @@ -20,11 +25,20 @@ void TrackList::clear() { tracks.clear(); tracksByFile.clear(); + trackReference = 0; } int TrackList::getTrackIndexByFile(const char * file) const { - return tracksByFile.at(file); + try + { + return tracksByFile.at(file); + } + catch ( const out_of_range & e ) + { + throw TrackNotFoundException(file); + return -1; + } } void TrackList::initFromProtocolBuffer(const Harvest::TrackList & msg) @@ -55,6 +69,11 @@ void TrackList::initFromProtocolBuffer(const Harvest::TrackList & msg) } setTracksByFile(); + + if ( msg.has_variantreference() ) + { + trackReference = msg.variantreference(); + } } void TrackList::setTracksByFile() @@ -93,4 +112,6 @@ void TrackList::writeToProtocolBuffer(Harvest * msg) msgTrack->set_type((Harvest::TrackList::Track::Type)track.type); } } + + msg->mutable_tracks()->set_variantreference(trackReference); } diff --git a/src/harvest/TrackList.h b/src/harvest/TrackList.h index 45da53f..2524e9f 100644 --- a/src/harvest/TrackList.h +++ b/src/harvest/TrackList.h @@ -35,13 +35,31 @@ class TrackList TrackType type; }; + class TrackNotFoundException : public std::exception + { + public: + + TrackNotFoundException(const std::string & nameNew) + { + name = nameNew; + } + + virtual ~TrackNotFoundException() throw() {} + + std::string name; + }; + + TrackList(); + int addTrack(const char * file, int size = 0, const char * name = "", TrackType type = NONE); void clear(); const Track & getTrack(int index) const; - Track & getTrackMutable(int index); - int getTrackIndexByFile(const char * file) const; int getTrackCount() const; + int getTrackIndexByFile(const char * file) const; + Track & getTrackMutable(int index); + int getTrackReference() const; void initFromProtocolBuffer(const Harvest::TrackList & msg); + void setTrackReference(int trackReferenceNew); void setTracksByFile(); void writeToProtocolBuffer(Harvest * msg); @@ -53,6 +71,8 @@ class TrackList }; inline const TrackList::Track & TrackList::getTrack(int index) const { return tracks[index]; } -inline TrackList::Track & TrackList::getTrackMutable(int index) { return tracks[index]; } inline int TrackList::getTrackCount() const { return tracks.size(); } +inline TrackList::Track & TrackList::getTrackMutable(int index) { return tracks[index]; } +inline int TrackList::getTrackReference() const { return trackReference; } +inline void TrackList::setTrackReference(int trackReferenceNew) { trackReference = trackReferenceNew; } #endif diff --git a/src/harvest/VariantList.cpp b/src/harvest/VariantList.cpp index a2058e3..5983e47 100644 --- a/src/harvest/VariantList.cpp +++ b/src/harvest/VariantList.cpp @@ -366,7 +366,18 @@ void VariantList::initFromVcf(const char * file, const ReferenceList & reference if ( oldTags ) { track = &trackList->getTrackMutable(n); // TODO: clear track - trackIndecesNew[trackList->getTrackIndexByFile(token)] = n; + + try + { + trackIndecesNew[trackList->getTrackIndexByFile(token)] = n; + } + catch ( const TrackList::TrackNotFoundException & e ) + { + delete [] trackIndecesNew; + throw; + return; + } + n++; } else diff --git a/src/harvest/pb/harvest.proto b/src/harvest/pb/harvest.proto index 9f9b6f3..dc6b7ca 100644 --- a/src/harvest/pb/harvest.proto +++ b/src/harvest/pb/harvest.proto @@ -31,6 +31,7 @@ message Harvest } repeated Track tracks = 1; + optional uint32 variantReference = 2; } message Tree