Skip to content

Commit

Permalink
import exceptions and reference track permanence
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ondov committed Jul 17, 2014
1 parent ade57b9 commit c1601b4
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/harvest/HarvestIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
48 changes: 43 additions & 5 deletions src/harvest/LcbList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ void LcbList::initFromMfa(const char * file, ReferenceList * referenceList, Trac
ifstream in(file);
char line[1 << 20];
vector<string> seqs;
bool oldTags = trackList->getTrackCount();
const bool oldTags = phylogenyTree->getRoot();
string refTag;
int * trackIndecesNew;

if ( oldTags )
{
trackIndecesNew = new int[trackList->getTrackCount()];
}
else
{
trackList->clear();
}

while ( ! in.eof() )
{
Expand All @@ -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
{
Expand Down Expand Up @@ -141,7 +155,7 @@ void LcbList::initFromXmfa(const char * file, ReferenceList * referenceList, Tra
char line[1 << 20];
int trackIndex = 0;
vector<string> seqs;
bool oldTags = trackList->getTrackCount();
const bool oldTags = phylogenyTree->getRoot();
int * trackIndecesNew;
bool mauve = false;
string ref;
Expand All @@ -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;

Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down
11 changes: 10 additions & 1 deletion src/harvest/PhylogenyTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/harvest/ReferenceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ int ReferenceList::getReferenceSequenceFromGi(long int gi) const
}
}

throw GiNotFoundException(std::to_string(gi));

return undef;
}

Expand Down
14 changes: 14 additions & 0 deletions src/harvest/ReferenceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 22 additions & 1 deletion src/harvest/TrackList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -55,6 +69,11 @@ void TrackList::initFromProtocolBuffer(const Harvest::TrackList & msg)
}

setTracksByFile();

if ( msg.has_variantreference() )
{
trackReference = msg.variantreference();
}
}

void TrackList::setTracksByFile()
Expand Down Expand Up @@ -93,4 +112,6 @@ void TrackList::writeToProtocolBuffer(Harvest * msg)
msgTrack->set_type((Harvest::TrackList::Track::Type)track.type);
}
}

msg->mutable_tracks()->set_variantreference(trackReference);
}
26 changes: 23 additions & 3 deletions src/harvest/TrackList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
13 changes: 12 additions & 1 deletion src/harvest/VariantList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/harvest/pb/harvest.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ message Harvest
}

repeated Track tracks = 1;
optional uint32 variantReference = 2;
}

message Tree
Expand Down

0 comments on commit c1601b4

Please sign in to comment.