Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

After downloading the source files make sure you can compile PhyloNet when linked against the above mentioned libraries.

Step 2. Create Your Command and extend CommandBase

In our example we will create a new command CountNodes that computes the number of nodes in a given network.

...

2.3 Implement executeCommandHelp.

The actual execution of our the command takes place within the executeCommandHelp method. In our example we will put the command execution code in this method directly; however, it is advisable not to implement the core logic of larger commands here but instead to delegate that computation to a helper class. The single displayResult parameter is a function that we can call every time we want to show text results to the usersfor displaying text results to the user. The first call to displayResult should begin with a newline character to preserve the tool's output formatting. These results will be automatically displayed on the console or sent to an output file based on the user's preference.

Code Block

protected void executeCommandHelp(Proc<String> displayResult) throws IOException
{
    String eNewickNetwork = NetworkTransformer.toENewick(_networkToScan); // convert NEXUS network to extended newick string
    
    /*
     * convert extended network string to Network
     */
    edu.rice.cs.bioinfo.programs.phylonet.structs.network.io.ExNewickReader<String> enr = 
      new edu.rice.cs.bioinfo.programs.phylonet.structs.network.io.ExNewickReader<String>(
          newStringReader(eNewickNetwork));
    
    edu.rice.cs.bioinfo.programs.phylonet.structs.network.Network<String> net;
    try
    {
      net = enr.readNetwork();
    }
    catch(Exception e)
    {
      throw new RuntimeException(e);
    }
    
    /*
     * count and display number of nodes in network
     */
    Iterable<edu.rice.cs.bioinfo.programs.phylonet.structs.network.NetNode<String>> allNodes = net.dfs();
    int nodeCount = 0;
    for(Object node : net.dfs())
    {
      nodeCount++;
    }
    
    displayResult.execute("\nNetwork contains " + nodeCount + " nodes.");
}

Step 3. Update CommandFactory

The next step is to update edu.rice.cs.bioinfo.programs.phylonet.commands.CommandFactory to construct instances of the CountNodes command. When examining the existing structure of the CommandFactory you will discover an if/else if chain within the make method:

Code Block


public class CommandFactory {



    public static Command make(SyntaxCommand directive, Map<String,NetworkNonEmpty> sourceIdentToNetwork, 
                               Proc3<String, Integer, Integer> errorDetected, Random rand)
    {
        ...

        if(lowerCommandName.equals("symmetricdifference") || lowerCommandName.equals("rf"))
        {
            return new SymmetricDifference(directive, params, sourceIdentToNetwork, errorDetected);
        }
        else if(lowerCommandName.equals("lca"))
        {
            return new LCA(directive, params, sourceIdentToNetwork, errorDetected);
        }
        ...
    }

To add a new command to the factory, select a lower case string name to represent the command in NEXUS and append an entry of the command in the chain. In our example we will name the CountNodes command simply "CountNodes":

Code Block

public class CommandFactory {



    public static Command make(SyntaxCommand directive, Map<String,NetworkNonEmpty> sourceIdentToNetwork, 
                               Proc3<String, Integer, Integer> errorDetected, Random rand)
    {
        ...
        else if(lowerCommandName.equals("nexus_out"))
        {
            return new NexusOut(directive, params, sourceIdentToNetwork, errorDetected);
        }
        else if(lowerCommandName.equals("countnodes"))
        {
           return new CountNodes(directive, params, sourceIdentToNetwork, errorDetected);
        }
        ...

This will instruct PhyloNet to create instances of the new command when named within a PHYLONET block.

Step 4. Compile and Test

At this point inclusion of a new command is complete. After recompiling PhyloNet the new command should be available.