00001 #ifndef MST_CPP
00002 #define MST_CPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "MST.h"
00038 #include "Utilities.h"
00039 #include "arg_parser.h"
00040 #include <iterator>
00041 #include <fstream>
00042
00043 MST::MST(const int maxNodes, const bool alignData) :
00044 haveAlignmentMetric(alignData) {
00045
00046
00047 nodeList.reserve(maxNodes);
00048 }
00049
00050 void
00051 MST::addNode(const int parentIdx, const int estIdx,
00052 const float similarity, const int alignmentInfo,
00053 const int directionInfo) {
00054 nodeList.push_back(MSTNode(parentIdx, estIdx, similarity, alignmentInfo,
00055 directionInfo));
00056 }
00057
00058 void
00059 MST::serialize(const char *fileName, const char *srcFile,
00060 const float threshold) const {
00061
00062 std::ofstream outFile(fileName);
00063 if (!outFile.good()) {
00064 std::cerr << "Unable to open " << fileName << " for writing."
00065 << std::endl;
00066 return;
00067 }
00068
00069 char now[128], srcTimeStr[128];
00070 outFile << "# MST Data\n"
00071 << "# Node count: " << nodeList.size() << "\n"
00072 << "# Generated on: " << getTime(now)
00073 << "# Generated from source file: " << srcFile << "\n"
00074 << "# Source file timestamp: " << getTimeStamp(srcFile, srcTimeStr)
00075 << "# Data format: <parentESTidx>,<estIdx>,<similarityMetric>,"
00076 << "<alignmentMetric>,<direction>\n"
00077 << "# Total MST distance: " << getMSTDistance() << "\n"
00078 << "# Clustering threshold: " << threshold << "\n"
00079 << "# Command line: " << arg_parser::get_global_args() << "\n";
00080
00081
00082 for(size_t i = 0; (i < nodeList.size()); i++) {
00083 nodeList[i].serialize(outFile, haveAlignmentMetric);
00084 }
00085
00086
00087 outFile.close();
00088 }
00089
00090 MST*
00091 MST::deSerialize(const char *fileName) {
00092
00093 std::ifstream inFile(fileName);
00094 if (!inFile.good()) {
00095 std::cerr << "Unable to open " << fileName << " for reading MST data."
00096 << std::endl;
00097 return NULL;
00098 }
00099
00100 MST *mst = new MST(20000, false);
00101 int result = 0;
00102 do {
00103
00104 MSTNode node;
00105 if ((result = MSTNode::deSerialize(inFile, node,
00106 mst->haveAlignmentMetric)) == 0) {
00107
00108 mst->nodeList.push_back(node);
00109 }
00110 } while (result == 0);
00111
00112
00113 if (result != -1) {
00114
00115 delete mst;
00116 mst = NULL;
00117 }
00118
00119 inFile.close();
00120
00121 return mst;
00122 }
00123
00124 float
00125 MST::getMSTDistance() const {
00126 float distance = 0;
00127 for(size_t idx = 0; (idx < nodeList.size()); idx++) {
00128 distance += nodeList[idx].getMetric();
00129 }
00130
00131 return distance;
00132 }
00133
00134 std::ostream&
00135 operator<<(std::ostream& os, const MST& mst) {
00136
00137 std::copy(mst.nodeList.begin(), mst.nodeList.end(),
00138 std::ostream_iterator<MSTNode>(os, "\n"));
00139 return os;
00140 }
00141
00142 #endif