📄 nexus.jj
字号:
/* Copyright (c) 2002 Compaq Computer Corporation SOFTWARE RELEASE Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the names of Compaq Research, Compaq Computer Corporation nor the names of its contributors may be used to endorse or promote products derived from this Software without specific prior written permission. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL COMPAQ COMPUTER CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/options{ FORCE_LA_CHECK = true; LOOKAHEAD = 2;}PARSER_BEGIN(Nexus)package Parser.nexus;//import Parse.*;import AccordionTreeDrawer.Tree;import AccordionTreeDrawer.TreeNode;import java.util.*;/** * A static class used for parsing a Nexus format file containing multiple trees. * Given a Nexus tree file and an index number, the parser will find the corresponding * tree with the given index number and initiate a tree object for it. * @author Yunhong Zhou */public class Nexus { /** the target tree to be initiated by the parser */ private static Tree tree; /** the current TreeNode that the parser is visiting and initiating */ private static TreeNode current_node; /** a helper TreeNode field */ private static TreeNode tn; /** the index of the tree to be initiated from the collection of trees in the nexus file */ private static int index; /** flag is true if the parser is visiting the target tree */ private static boolean flag; private static int cnt; /** * transflag is true if the nexus file has a translation header. * If transflag is true, then we need to translate the leaf label to its real name. */ private static boolean transflag = false; /** the hashtable ht_trans stores entries for label translation process */ private static Hashtable ht_trans; /** * function parseTree initiates field tree, call function Input to parse the input. * @param t Tree t will be initiated throught the parsing process * @param index The index of the tree to be initiated from parsing * @author Yunhong Zhou */ public void parseTree(Tree t, int index) { tree = t; this.index = index; try{ Input(); } catch( ParseException e ){ if(t.getName() == null) System.out.println("Parsing error with input!"); else System.out.println("Parsing error with input file " + t.getName()); e.printStackTrace(); } }} PARSER_END(Nexus)void Input():{ cnt = 0; }{ ( <token_tree> tree_newick() | <translate> trans_statement() | <unquoted_string> other_statement() )+ <EOF>}void other_statement() :{}{ ( <unquoted_string> | <quoted_string> | <double_number> | <token_tree> | <equal> | <lparen> | <rparen> | <comma> | <colon> )* <semicolon> }/** * function trans_statement generates a hash table for label translation */void trans_statement() :{ String s_key, s_value; transflag = true; ht_trans = new Hashtable(); Hashtable ht_values = new Hashtable();}{ ( s_key = label_text() s_value = label_text() ( <comma> )? { String prev_key = (String)(ht_values.get(s_value)); if(prev_key != null){ StringBuffer sb = new StringBuffer(s_value); sb.append(" ").append(s_key); ht_trans.put(s_key, sb.toString()); if(!prev_key.equals("-1")){ sb = new StringBuffer(s_value); sb.append(" ").append(prev_key); ht_trans.put(prev_key, sb.toString()); ht_values.put(s_value, "-1"); } }else{ ht_trans.put(s_key, s_value); ht_values.put(s_value, s_key); } } )+ <semicolon> { ht_values = null; }}/** this function is responsible for parsing one tree */void tree_newick() :{ String s; double len;}{ <unquoted_string> <equal> { cnt++; flag = (cnt == index)?true:false; if(flag)current_node = tree.getRoot(); } descendant_list() ( s = label() { if(flag)current_node.setName(s); //System.out.println("Name: " + s); } )? ( <colon> len = branch_length() { if(flag)current_node.setWeight(len); //System.out.println("weight: " + len); } )? <semicolon>}void descendant_list():{}{ <lparen> { if(flag){ tn = new TreeNode(); current_node.addChild(tn); current_node = tn; } } subtree() ( <comma> { if(flag){ tn = new TreeNode(); current_node.addChild(tn); current_node = tn; } } subtree() )* <rparen>}/** function subtree will set name, length and weight for each tree node */void subtree():{ String s; double len; }{ descendant_list() {} ( s = label() { if(flag)current_node.setName(s); //System.out.println("Name: " + s); } )? ( <colon> len = branch_length() { if(flag)current_node.setWeight(len); //System.out.println("Weight: " + len); } )? { if(flag)current_node = current_node.parent(); }| ( s = label() { if(flag)current_node.setName(s); //System.out.println("Name: " + s); } )? ( <colon> len = branch_length() { if(flag)current_node.setWeight(len); //System.out.println("Weight: " + len); } )? { if(flag)current_node = current_node.parent(); }}/** get the real name for each label, translate if necessary */String label(): { String s, s_trans = null; Token t; }{ s = label_text() { if(transflag && flag){ s_trans = (String)ht_trans.get(s); if(s_trans != null){ return s_trans; }else return s; }else return s; // TODO : integer index }}/** * for each unquoted label, we need to replace '_' by ' '. * for each quoted label, we remove double quotes from the string. */String label_text() :{ String s; Token t; }{ t = <unquoted_string> { s = new String(t.toString()); return s.replace('_', ' '); }| t = <quoted_string> { s = new String(t.toString()); return s.substring(1, s.length()-1).replaceAll("[\n\r\f]", ""); }| t = <double_number> { return new String(t.toString()); }}double branch_length():{ Token t; }{ t = <double_number> { return Double.parseDouble(t.toString()); } }<*>SKIP :{ " "| "\t"| "\n"| "\r"| "\f"| <comment: "[" ( ~["]"] )* "]"> }<DEFAULT>SKIP [IGNORE_CASE] :{ "#NEXUS" : NexusBody | < ~[] > }<NexusBody>SKIP [IGNORE_CASE] :{ "BEGIN" : BlockStart| < ~[] >}<BlockStart>SKIP [IGNORE_CASE] :{ "TREES;" : TreesBlock| < ~[] > : OtherBlock}<OtherBlock>SKIP [IGNORE_CASE] :{ "END;" : NexusBody| < ~[] > }/*TOKEN [IGNORE_CASE]:{ <end: "END">| <trees: "TREES">| <token_tree: "TREE">| <otherblock: "TAXA" | "CHARACTERS" | "UNALIGNED", "DISTANCES" | "SETS" | "ASSUMPTIONS" | "CODONS" | "NOTES" >}*/<TreesBlock>TOKEN [IGNORE_CASE] :{ <token_tree: "TREE"> | <equal: "="> | <lparen: "(">| <rparen: ")">| <comma: ",">| <colon: ":">| <semicolon: ";">| <translate: "TRANSLATE">}<TreesBlock>SKIP [IGNORE_CASE] :{ "END;" : EOF_STATE }<TreesBlock>TOKEN:{ <#digit: ["0"-"9"] >| <#number: ( <digit> )+ ("." ( <digit> )* )? | "." ( <digit> )+ >| <#exponent: ["e", "E"] ("+"|"-")? (<digit>)+ >| <double_number: ("+"|"-")? <number> (<exponent>)? >| <#alpha: ["a"-"z", "A"-"Z"] >| <#only_quote_char: [ "(", ")", "," ]>| <#single_quote: "''">| <#both_char: [ "~", "`", "!", "@", "$", "%", "^", "&", "*", "-", "_", "+", "{", "}", "|", ".", "?" ] > | <#whitespace: [ " " , "\t" , "\n" , "\r", "\f" ] >| <#unquoted_char: ( <digit> | <alpha> | <both_char> ) >| <#quoted_char: ( <unquoted_char> | <whitespace> | <only_quote_char> )>| <unquoted_string: ( <unquoted_char> )+ >| <quoted_string: "'" ( <quoted_char> | <single_quote> )+ "'" >}<EOF_STATE>SKIP :{ < ~[] > }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -