Skip to content

Commit

Permalink
Merge pull request #40 from hybridLambda/36_network_reading
Browse files Browse the repository at this point in the history
36 network reading
  • Loading branch information
shajoezhu authored Dec 8, 2020
2 parents b8cf3cf + 8a8c485 commit 8041607
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 264 deletions.
14 changes: 10 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ os:
compiler:
- gcc
- clang

addons:
apt:
packages: git-core libcppunit-dev graphviz valgrind r-base-core texlive-latex-extra texlive-fonts-recommended doxygen
homebrew:
packages:
- cppunit
update: true

before_install:
- echo $LANG
- echo $LC_ALL
- if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update -qq; sudo apt-get install -qq git-core libcppunit-dev graphviz valgrind r-base-core texlive-latex-extra doxygen; fi
- if [ $TRAVIS_OS_NAME == osx ]; then rvm get 1.29.3; brew update; brew doctor; rvm install 2.3.3; fi
- if [ $TRAVIS_OS_NAME == osx ]; then brew update ; fi
- if [ $TRAVIS_OS_NAME == osx ]; then brew install llvm cppunit zlib ; fi
- if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update -qq; sudo apt-get install -qq git-core libcppunit-dev graphviz valgrind r-base-core texlive-latex-extra texlive-fonts-recommended doxygen; fi

before_script:
- ./bootstrap
Expand Down
Binary file removed doc/manual.pdf
Binary file not shown.
177 changes: 112 additions & 65 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,69 +36,13 @@ Tree::Tree(string old_string /*! input (extended) newick form string */){
this->check_labeled( old_string );
// check & sign, this should be illigal for hybrid-Lambda,

vector<string> labels;
vector<string> node_contents;
vector<string> brchlens;
size_t found_bl = net_str.find(':');
for (size_t i_str_len=1;i_str_len<net_str.size();){
if (net_str[i_str_len]=='e' && (net_str[i_str_len+1]=='-' || net_str[i_str_len+1]=='+')){
i_str_len++;
}
else{
if ( start_of_tax_name(net_str,i_str_len) ){
size_t str_start_index = i_str_len;
string label = extract_label(net_str,i_str_len);
labels.push_back(label);

string node_content;
if ( net_str[str_start_index-1]==')' ){
size_t rev_dummy_i = Parenthesis_balance_index_backwards( net_str, str_start_index-1 );
size_t substr_len = str_start_index-rev_dummy_i;
node_content = net_str.substr(rev_dummy_i, substr_len );
}
else {
node_content=label;
}
i_str_len += label.size();

node_contents.push_back(node_content);
string brchlen;
if ( found_bl != string::npos ){
size_t found=min(min(net_str.find(",",i_str_len+1),net_str.find(")",i_str_len+1)),net_str.size());
brchlen = net_str.substr(i_str_len+1,found-i_str_len-1);
}
found_bl = net_str.find(":", found_bl+1);
brchlens.push_back(brchlen);
}
else {
i_str_len++;
}
}
}

//int label_counter = brchlens.size();
for ( size_t new_i_label=0 ; new_i_label < brchlens.size(); new_i_label++ ){
Node empty_node;
NodeContainer.push_back(empty_node);
NodeContainer[new_i_label].label = labels[new_i_label];
NodeContainer[new_i_label].node_content = node_contents[new_i_label];
NodeContainer[new_i_label].set_brchlen1( strtod(brchlens[new_i_label].c_str(), NULL) );
}

for ( size_t i = 1; i < NodeContainer.size()-1; i++ ){
size_t j;
for ( j = i+1; j < NodeContainer.size()-1; j++ ){
if ( NodeContainer[j].label==NodeContainer[i].label ){
if ( NodeContainer[j].node_content[0] == '(' ){
NodeContainer[i].node_content = NodeContainer[j].node_content;
}
NodeContainer[i].set_brchlen2 ( NodeContainer[j].brchlen1() );
break;
}
}
if ( NodeContainer[j].label == NodeContainer[i].label ) NodeContainer.erase(NodeContainer.begin()+j);
}

vector<string> labels;
vector<string> node_contents;
vector<string> brchlens;
vector<size_t> label_starts_at;
vector<size_t> content_starts_at;
this->initialize_nodes_label_and_content(labels, node_contents, brchlens, label_starts_at, content_starts_at);
this->initialize_NodeContainer(labels, node_contents, brchlens, label_starts_at, content_starts_at);
this->extract_tax_and_tip_names();

this->connect_graph();
Expand All @@ -116,6 +60,89 @@ Tree::Tree(string old_string /*! input (extended) newick form string */){
}


void Tree::initialize_nodes_label_and_content(vector<string> & labels,
vector<string> & node_contents,
vector<string> & brchlens,
vector<size_t> & label_starts_at,
vector<size_t> & content_starts_at) {
size_t found_bl = net_str.find(':');
for (size_t i_str_len=1;i_str_len<net_str.size();){
if (net_str[i_str_len]=='e' && (net_str[i_str_len+1]=='-' || net_str[i_str_len+1]=='+')){
i_str_len++;
}
else{
if ( start_of_tax_name(net_str,i_str_len) ){
size_t str_start_index = i_str_len;
string label = extract_label(net_str,i_str_len);
labels.push_back(label);
label_starts_at.push_back(str_start_index);

string node_content;
if ( net_str[str_start_index-1]==')' ){
size_t rev_dummy_i = Parenthesis_balance_index_backwards( net_str, str_start_index-1 );
size_t substr_len = str_start_index-rev_dummy_i;
node_content = net_str.substr(rev_dummy_i, substr_len );
content_starts_at.push_back(rev_dummy_i);
}
else {
node_content=label;
content_starts_at.push_back(str_start_index);
}
i_str_len += label.size();

node_contents.push_back(node_content);
string brchlen;
if ( found_bl != string::npos ){
size_t found=min(min(net_str.find(",",i_str_len+1),net_str.find(")",i_str_len+1)),net_str.size());
brchlen = net_str.substr(i_str_len+1,found-i_str_len-1);
}
found_bl = net_str.find(":", found_bl+1);
brchlens.push_back(brchlen);
}
else {
i_str_len++;
}
}
}
}


void Tree::initialize_NodeContainer(vector<string> & labels,
vector<string> & node_contents,
vector<string> & brchlens,
vector<size_t> & label_starts_at,
vector<size_t> & content_starts_at){
//int label_counter = brchlens.size();
for ( size_t new_i_label=0 ; new_i_label < brchlens.size(); new_i_label++ ){
Node empty_node;
NodeContainer.push_back(empty_node);
NodeContainer[new_i_label].label = labels[new_i_label];
NodeContainer[new_i_label].node_content = node_contents[new_i_label];
NodeContainer[new_i_label].set_brchlen1( strtod(brchlens[new_i_label].c_str(), NULL) );
//cout << new_i_label << " " << labels[new_i_label] << " " << brchlens[new_i_label] << endl;
NodeContainer[new_i_label].set_label1_starts_at(label_starts_at[new_i_label]);
NodeContainer[new_i_label].set_node_content_starts_at(content_starts_at[new_i_label]);
}

for ( size_t i = 1; i < NodeContainer.size()-1; i++ ){
size_t j;
for ( j = i+1; j < NodeContainer.size()-1; j++ ){
if ( NodeContainer[j].label==NodeContainer[i].label ){
if ( NodeContainer[j].node_content[0] == '(' ){
NodeContainer[i].node_content = NodeContainer[j].node_content;
}
NodeContainer[i].set_brchlen2 ( NodeContainer[j].brchlen1() );
NodeContainer[i].set_label2_starts_at ( NodeContainer[j].label1_starts_at() );
NodeContainer[i].set_node_content_starts_at ( NodeContainer[j].node_content_starts_at() );
break;
}
}
if ( NodeContainer[j].label == NodeContainer[i].label ) NodeContainer.erase(NodeContainer.begin()+j);
}

}


void Tree::init_descendant(){
for ( size_t i = 0; i < NodeContainer.size(); i++){
valarray <int> descndnt_dummy(0,tax_name.size());
Expand Down Expand Up @@ -200,6 +227,7 @@ void Tree::connect_graph(){
for ( size_t i = 0; i < NodeContainer.size(); i++ ){
if ( NodeContainer[i].node_content[0] != '(' ) continue;

// cout << NodeContainer[i].label << " " << NodeContainer[i].node_content <<endl;
char child_node1[NodeContainer[i].node_content.length()];
for ( size_t i_content_len = 1; i_content_len < NodeContainer[i].node_content.length(); ){
if (NodeContainer[i].node_content[i_content_len]=='(' || start_of_tax_name(NodeContainer[i].node_content,i_content_len) ){
Expand All @@ -215,10 +243,27 @@ void Tree::connect_graph(){
}
child1_node_content_i++;
}

string child_node1_str = child_node1;
i_content_len = j_content_len + 2;
for ( size_t j = 0; j < NodeContainer.size(); j++){
if (child_node1_str == NodeContainer[j].label) NodeContainer[i].add_child( &NodeContainer[j] );

size_t adding_to_parent = 1;

if (child_node1_str == NodeContainer[j].label) {
// cout<< NodeContainer[i].label << " " << NodeContainer[i].node_content_starts_at() + child1_node_content_i << " " << NodeContainer[j].label << " " << NodeContainer[j].label1_starts_at() <<endl;
// Checking node content label index ...
if (NodeContainer[j].label2_starts_at() == 0){
assert(adding_to_parent = 1);
} else {
if (NodeContainer[i].node_content_starts_at() > NodeContainer[j].label1_starts_at()){
adding_to_parent = 2;
}
}


NodeContainer[i].add_child( &NodeContainer[j], adding_to_parent );
}
}
}
else { i_content_len++;}
Expand Down Expand Up @@ -552,7 +597,9 @@ void Tree::rewrite_descendant(){ //check for coaleased tips(& sign in the tip

string Tree::print_newick( Node * node ){
string tree_str;
if ( node->tip_bool ) tree_str = node->label ;
if ( node->tip_bool ){
tree_str = node->label ;
}
else {
tree_str = "(";
for ( size_t i = 0 ; i < node->child.size() ; i++ ){
Expand Down
127 changes: 69 additions & 58 deletions src/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,70 +40,81 @@ class Tree{
friend class simTree;
friend class Frequency;
friend class Figure;
private:
string label_interior_node(string in_str);
void enumerate_internal_branch( Node &node );

size_t first_coal_rank();
size_t current_enum_;
void init(){
this->current_enum_ = 0;
this->is_Net = false;
this->is_ultrametric = true;
}

bool start_of_tax_name(string in_str, size_t i);
size_t Parenthesis_balance_index_backwards( string &in_str, size_t i );
size_t Parenthesis_balance_index_forwards( string &in_str, size_t i );

void check_Parenthesis(string &in_str);
void check_labeled( string in_str );

void check_isNet(); /*!< \brief To determin if a Net is network or not. \return is_Net */
void check_isUltrametric(); /*!< \brief To determin if a Net is ultrametric or not. \return is_ultrametric */

size_t first_coal_index ();

string rewrite_internal_node_content( size_t i);
void connect_graph();
void extract_tax_and_tip_names();

void init_descendant();
void init_node_clade();
void rewrite_descendant();
void rewrite_node_clade();

string net_str; /*!< \brief species network string \todo this is new!!!*/
size_t max_rank;
vector< valarray <int> > descndnt;
vector< valarray <int> > samples_below;
vector<string> tip_name;
bool is_Net_() const { return this->is_Net ; }
string extract_label(string in_str, size_t i);
void print_all_node();
bool print_all_node_dout();
Tree (){ this->init(); }
Tree(string Tree_str);
~Tree(){};

vector <string> tax_name;
bool is_ultrametric; /*!< \brief true if the distances between tips and root are equal; false, otherwise */
bool is_Net; /*!< \brief true if Net is a network; false if it's a tree */
public:
vector < Node > NodeContainer; /*!< \brief vector of nodes */
void rewrite_node_content();
string print_newick( Node * node );
private:
string label_interior_node(string in_str);
void enumerate_internal_branch( Node &node );

size_t first_coal_rank();
size_t current_enum_;
void init(){
this->current_enum_ = 0;
this->is_Net = false;
this->is_ultrametric = true;
}

bool start_of_tax_name(string in_str, size_t i);
size_t Parenthesis_balance_index_backwards( string &in_str, size_t i );
size_t Parenthesis_balance_index_forwards( string &in_str, size_t i );

void check_Parenthesis(string &in_str);
void check_labeled( string in_str );

void check_isNet(); /*!< \brief To determin if a Net is network or not. \return is_Net */
void check_isUltrametric(); /*!< \brief To determin if a Net is ultrametric or not. \return is_ultrametric */

size_t first_coal_index ();

string rewrite_internal_node_content( size_t i);
void connect_graph();
void extract_tax_and_tip_names();

void initialize_nodes_label_and_content(vector<string> & labels,
vector<string> & node_contents,
vector<string> & brchlens,
vector<size_t> & label_starts_at,
vector<size_t> & content_starts_at);
void initialize_NodeContainer(vector<string> & labels,
vector<string> & node_contents,
vector<string> & brchlens,
vector<size_t> & label_starts_at,
vector<size_t> & content_starts_at);
void init_descendant();
void init_node_clade();
void rewrite_descendant();
void rewrite_node_clade();

string net_str; /*!< \brief species network string \todo this is new!!!*/
size_t max_rank;
vector< valarray <int> > descndnt;
vector< valarray <int> > samples_below;
vector<string> tip_name;
bool is_Net_() const { return this->is_Net ; }
string extract_label(string in_str, size_t i);
void print_all_node();
bool print_all_node_dout();
Tree (){ this->init(); }
Tree(string Tree_str);
~Tree(){};

vector <string> tax_name;
bool is_ultrametric; /*!< \brief true if the distances between tips and root are equal; false, otherwise */
bool is_Net; /*!< \brief true if Net is a network; false if it's a tree */

public:
vector < Node > NodeContainer; /*!< \brief vector of nodes */
void rewrite_node_content();
string print_newick( Node * node );
};

class Net: public Tree {
//friend class Figure;
//friend class HybridLambda;
//friend class simTree;
public:
Net (string Net_str) : Tree ( Net_str){};
Net () { this->init();}
~Net(){};
};
public:
Net (string Net_str) : Tree ( Net_str){};
Net () { this->init();}
~Net(){};
};

string remove_interior_label(string in_str);
size_t end_of_label_or_bl( string &in_str, size_t i);
Expand Down
Loading

0 comments on commit 8041607

Please sign in to comment.