1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*==========================================================================
SeqAn - The Library for Sequence Analysis
http://www.seqan.de
============================================================================
Copyright (C) 2007
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
==========================================================================*/
#include <seqan/basic.h>
#include <seqan/graph_msa.h>
#include <seqan/modifier.h>
#include <seqan/misc/misc_cmdparser.h>
#include <iostream>
#include <fstream>
using namespace seqan;
//////////////////////////////////////////////////////////////////////////////////
inline void
_addVersion(CommandLineParser& parser) {
::std::string rev = "$Revision: 4692 $";
addVersionLine(parser, "Version 1.01 (18. August 2009) Revision: " + rev.substr(11, 4) + "");
}
//////////////////////////////////////////////////////////////////////////////////
template<typename TFile, typename TMat, typename TNames>
inline void
_readPhylipMatrix(TFile& file,
TMat& matrix,
TNames& names)
{
typedef typename Value<TFile>::Type TValue;
typedef typename Value<TMat>::Type TDistance;
typedef typename Size<TMat>::Type TSize;
typedef typename Value<TNames>::Type TName;
typedef typename Iterator<TMat, Standard>::Type TMatIter;
// Parse the file and convert the internal ids
TValue c;
if (_streamEOF(file)) return;
else c = _streamGet(file);
while (!_streamEOF(file)) {
if (_streamEOF(file)) break;
_parse_skipWhitespace(file, c);
TSize nseq = _parse_readNumber(file, c);
_parse_skipWhitespace(file, c);
resize(matrix, nseq * nseq);
resize(names, nseq);
TMatIter it = begin(matrix, Standard());
for(TSize row = 0; row<nseq; ++row) {
_parse_readIdentifier(file, names[row], c);
_parse_skipWhitespace(file, c);
for(TSize col = 0; col<nseq; ++col, ++it) {
*it = _parse_readDouble(file, c);
_parse_skipWhitespace(file, c);
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
int main(int argc, const char *argv[]) {
// Command line parsing
CommandLineParser parser;
_addVersion(parser);
addTitleLine(parser, "***************************************");
addTitleLine(parser, "* Tree reconstrucion - TreeRecon *");
addTitleLine(parser, "* (c) Copyright 2009 by Tobias Rausch *");
addTitleLine(parser, "***************************************");
addUsageLine(parser, "-m <Phylip distance matrix> [Options]");
addSection(parser, "Main Options:");
addOption(parser, addArgumentText(CommandLineOption("m", "matrix", "file with distance matrix", OptionType::String), "<Phylip distance matrix>"));
addHelpLine(parser, "At least 3 species required.");
addOption(parser, addArgumentText(CommandLineOption("b", "build", "tree building method", (int)OptionType::String, "nj"), "[nj, min, max, avg, wavg]"));
addHelpLine(parser, "nj = Neighbor-joining");
addHelpLine(parser, "min = UPGMA single linkage");
addHelpLine(parser, "max = UPGMA complete linkage");
addHelpLine(parser, "avg = UPGMA average linkage");
addHelpLine(parser, "wavg = UPGMA weighted average linkage");
addHelpLine(parser, "/*Neighbor-joining creates an");
addHelpLine(parser, " unrooted tree. We root that tree");
addHelpLine(parser, " at the last joined pair.*/");
addOption(parser, addArgumentText(CommandLineOption("o", "outfile", "output filename", (int)OptionType::String, "tree.dot"), "<Filename>"));
addOption(parser, addArgumentText(CommandLineOption("f", "format", "output format", (int)OptionType::String, "dot"), "[dot | newick]"));
if (argc == 1)
{
shortHelp(parser, std::cerr); // print short help and exit
return 0;
}
if (!parse(parser, argc, argv, ::std::cerr)) return 1;
if (isSetLong(parser, "help") || isSetLong(parser, "version")) return 0; // print help or version and exit
// Tree reconstruction
typedef double TDistanceValue;
typedef String<char> TName;
typedef Size<TName>::Type TSize;
// Read the options
::std::string infile;
getOptionValueLong(parser, "matrix", infile);
::std::string outfile;
getOptionValueLong(parser, "outfile", outfile);
TSize build = 0;
String<char> meth;
getOptionValueLong(parser, "build", meth);
if (meth == "nj") build = 0;
else if (meth == "min") build = 1;
else if (meth == "max") build = 2;
else if (meth == "avg") build = 3;
else if (meth == "wavg") build = 4;
String<char> format;
getOptionValueLong(parser, "format", format);
// Read the distance matrix
String<TName> names;
String<TDistanceValue> matrix;
FILE* strmMat = fopen(infile.c_str(), "rb");
_readPhylipMatrix(strmMat, matrix, names);
fclose(strmMat);
// Create the tree
Graph<Tree<TDistanceValue> > tree;
if (build == 0) njTree(matrix, tree);
else if (build == 1) upgmaTree(matrix, tree, UpgmaMin());
else if (build == 2) upgmaTree(matrix, tree, UpgmaMax());
else if (build == 3) upgmaTree(matrix, tree, UpgmaAvg());
else if (build == 4) upgmaTree(matrix, tree, UpgmaWeightAvg());
if (format == "dot") {
TSize nameLen = length(names);
resize(names, numVertices(tree));
// Add the label prefix for leaves
for(TSize i = 0;i < nameLen; ++i) {
TName tmpName = "label = \"";
append(tmpName, names[i], Generous());
append(tmpName, '"');
names[i] = tmpName;
}
// Append emty names for internal vertices
for(;nameLen < length(names); ++nameLen) {
names[nameLen] = "label = \"\"";
}
// Write the result
FILE* strmDot;
strmDot = fopen(outfile.c_str(), "w");
write(strmDot, tree, names, DotDrawing());
fclose(strmDot);
} else if (format == "newick") {
FILE* strmDot;
strmDot = fopen(outfile.c_str(), "w");
// If nj tree collapse the root
if (build == 0) write(strmDot, tree, names, true, NewickFormat());
else write(strmDot, tree, names, false, NewickFormat());
fclose(strmDot);
}
return 0;
}
|