BioInfWeb - TreeGraph / Version 1 / Show File - NEXUS2tgf.cc

TreeGraph subversion repository

sventon subversion web client - http://www.sventon.org
[show recent changes]
 
  Help
Rev: HEAD (2) - https://secure.bioinfweb.info/Code/svn/TreeGraph / trunk / main / NEXUS2tgf.cc
Show File - NEXUS2tgf.cc  [show properties]
spinner
/*
 NEXUS2tgf.cc, part of
 treegraph
 Tree formatting program
 Generates vector graphics (SVG,EPS) from .tgf-tree description files.
10   Copyright (c) 2003-04 by Joern Mueller
11 
12 
13   This program is free software; you can redistribute it and/or
14   modify it under the terms of the GNU General Public License
15   as published by the Free Software Foundation; either version 2
16   of the License, or (at your option) any later version.
17 
18   This program is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21   GNU General Public License for more details.
22 
23   You should have received a copy of the GNU General Public License
24   along with this program (GPL.html); if not, write to the Free Software
25   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26   (also: http://www.gnu.org)
27  */
28 
29 
30 
31 
32 
33 
34 
35  #include "tgf-main.h"
36 
37  #include <iostream>
38  #include <fstream>
39  #include <string>
40  #include <list>
41 
42  #include <cmath>
43  #include <cctype>
44  #include <cstdio>
45  #include <vector>
46 
47  #include "NEXUS2tgf.h"
48 
49  using namespace std;
50 
51  void skipcomment(ifstream& ein);
52  void mspace(ofstream& aus, int m);
53  void readTreeLine (ifstream& ein, ofstream& aus, bool& intLabels, bool& brlens, bool& altNexus, vector<string>& taxa);
54  void skipspaces(ifstream& f);
55  void writesettings (ofstream& aus, vector<string>& taxa);
56 
57  inline bool letter(char ch){
58   return (isalnum(ch) || ch=='_');
59  }
60 
61  void tolower(string& s)
62  {
63   for(int i=0; i<s.length(); i++)
64    s[i]=tolower(s[i]);
65  }
66 
67  void replace_with_space(string& s)
68  {
69      string tmp="";
70      tmp.reserve(s.length()+1);
71   for(int i=0; i<s.length(); i++){
72    if(s[i]=='_') tmp.push_back(' ');
73          else if(s[i]!=',') tmp.push_back(s[i]);
74   }
75   s=tmp;
76  }
77 
78  void translateNEXUS(ifstream& ein, ofstream& aus)
79  {
80   int ch='0';
81   vector<string> taxa;
82 
83   bool altNexus=true, brlens=false, intLabels=false, mega=false;
84   string token="", nr="", name="";
85   while (token!="tree" && altNexus && !ein.eof()) {
86    ein >> token;
87    tolower(token);
88    if (token=="#mega") {
89     mega=true;
90     altNexus=false;
91     while (nr!="[1]") ein >> nr;
92     while (!ein.eof() ) {
93                  skipspaces(ein);
94      ein.ignore();
95      ein >> name;
96      taxa.push_back(name);
97      skipspaces(ein);
98      ch=ein.get();
99      if (ch=='#') break;
100      ein >> nr;
101     }
102     break; //"tree" muss ja in MEGA nicht gesucht werden
103          }
104    if (token=="translate") {
105     altNexus=false;
106              while (!ein.eof()) {
107                  ein >> nr;
108                  if(nr==";") break;
109                  ein >> name;
110                  taxa.push_back(name);
111              }
112              ein >> token; //reads "tree"
113              //replace "_ AND ," in taxon names
114              break;
115    }
116    if (ein.eof()) {
117     cout << "no tree command found";
118     return;
119    }
120   }
121 
122      if (altNexus) {
123          ein >> token; //reads treename
124          aus << "\\begindef\n\\enddef\n";
125   } else {
126          if(!mega) ein >> token; //reads treename
127       for (int i=0; i<taxa.size(); i++)
128        replace_with_space(taxa[i]);
129          writesettings(aus, taxa);
130   }
131   aus << "%tree " << token << "\n\n";
132      readTreeLine (ein, aus, intLabels, brlens, altNexus, taxa);
133 
134      cout << "Import from ";
135   if (mega) cout << "MEGA tree";
136   else if (altNexus) cout << "altNEXUS"; else cout << "NEXUS";
137   cout << " file with";
138   if (!brlens) cout << "out";
139   cout << " branchlengths and with";
140   if (!intLabels) cout << "out";
141   cout << "\n internal node labels.\n";
142   cout << taxa.size()-1 << " taxa read.\n";
143  }
144 
145  void readTreeLine (ifstream& ein, ofstream& aus, bool& intLabels, bool& brlens,
146    bool& altNexus, vector<string>& taxa)
147  {
148 
149   ein.ignore(32000,'(');
150   if (!ein || ein.eof()) {
151    cout << "readTreeLine: found no tree in first 32K of file.\n";
152    return;
153   }
154   ein.putback('(');
155 
156   int ch;
157 
158 
159   //beginnt bei (
160   int dp=0; //dp = depth/level in tree
161 
162   while((ch=ein.get())!=EOF){
163   switch(ch){
164   case EOF:
165    cout << "unexpected end of tree";
166    cin >> ch;
167    break;
168   case '(':
169    mspace(aus,dp);
170    aus << "(\n";
171    ++dp;
172    break;
173   case ')':
174    --dp;
175    mspace(aus,dp);
176    aus << ")\n";
177    break;
178   case '\'':
179    intLabels=true;
180          mspace(aus,dp);
181          aus << "\"";
182       while((ch=ein.get())!='\''){
183          aus << (char)ch;
184       }
185       aus << "\"\n";
186       break;
187   case ':':
188    brlens=true;
189          float f;
190    ein >> f;
191    mspace(aus,dp);
192    aus << "\\len{" << f << "}\n";
193    break;
194   case ',':
195    mspace(aus,dp);
196    aus << ",\n";
197    break;
198   case ';':
199          return;
200       break;
201   default:
202    char buf[256];
203    buf[0]=ch;
204    int i;
205    for(i=1;i<256;i++){
206     ch=ein.get();
207     if(!letter(ch)){
208      ein.unget();
209      break;
210     }
211     if (ch=='_') ch=' ';
212     buf[i]=ch;
213    }
214    buf[i]=0;
215    mspace(aus,dp);
216    aus << "\"";
217          if (altNexus) {
218           aus << buf;
219           taxa.push_back(buf);
220          } else {
221              int txnr=atoi(buf)-1;
222              if(txnr<0 || txnr>=taxa.size()){
223                          cout << buf << ": Out of range.\n";
224                          return;
225              }
226     aus << taxa[txnr];
227 
228          }
229          aus << "\"\n";
230    break;
231   }//switch
232   }
233  }
234 
235 
236  void mspace(ofstream& aus, int m)
237  {
238   if(m>30) m=30;
239   for(int i=0; i<m; i++)
240   aus << ' ';
241  }
242 
243 
244  void skipcomment(ifstream& ein)
245  {
246   int ch;
247   while((ch=ein.get())!=']' && ch!=EOF){
248   }
249  }
250 
251  void skipspaces(ifstream& f)
252  {
253   int ch;
254   do {
255    ch=f.get();
256    if(!isspace(ch)){
257     f.unget();
258     ch=EOF;
259    }
260   } while(ch!=EOF);
261  }
262 
263 
264  void writesettings (ofstream& aus, vector<string>& taxa)
265  {
266      double bl=(2.8346457*250)/(double)taxa.size();
267      if (bl>18) bl=18;
268      int defaultWidth=150, defaultHeight=250;
269      aus << " \\begindef\n";
270   aus << "%\n% tree geometry\n%\n";
271      aus << " \\width{" << defaultWidth << "}\n";
272      aus << " \\height{" << defaultHeight << "}\n";
273  // aus << "% \\scale{1.0}\n";
274      aus << " \\autolength\n";
275      aus << "% \\variable\n";
276   aus << "%\n% layout\n%\n";
277      aus << " \\roundness{0.2}\n";
278      aus << " \\thickness{0.3}\n";
279      aus << " \\margin{0}{0}{0}{0} % left top right bottom\n";
280   aus << " \\style{r}{plain}{"<<bl<<"}\n";
281   aus << " \\style{u}{plain}{"<<bl*0.8<<"}\n";
282    aus << "% \\proof\n";
283    aus << "%\n% brackets\n%\n";
284   aus << "% \\style{u}{plain}{"<<bl*0.8<<"}\n";
285      aus << "% \\bracket{0}{n1}{n2}{example text}\n";
286      aus << "% \\brace*{1.5}{root}{root}{example text}\n";
287      aus << " \\enddef\n\n";
288      aus.flush();
289  }


feed icon

sventon 2.5.1

Valid XHTML 1.0 Strict   CSS ist valide!
TreeGraph icon
bioinfweb RSS feed TreeGraph 2 on ResearchGate bioinfweb on twitter bioinfweb on GitHub
bioinfweb - Biology & Informatics Website