BioJava:Cookbook:Alphabets:Component

How do I break Symbols from CrossProductAlphabets into their component Symbols?

CrossProductAlphabets are used to represent groups of Symbols as a single Symbol. This is very useful for treating things like codons as single Symbols. Sometimes however, you might want to covert the Symbols back into their component Symbols. The following recipe demonstrates how this can be done.

The Symbols from a CrossProductAlphabet are implementations of the AtomicSymbol interface. The prefix ‘Atomic’ suggests that the Symbols cannot be divided so one might ask, ‘how can an indivisible Symbol be divided into it’s component parts?’. The full definition of the AtomicSymbol is that it cannot be divided into a simpler Symbol that is still part of the same Alphabet. The component parts of an AtomicSymbol from a CrossProductAlphabet are not members of the same Alphabet so the ‘Atomic’ definition still stands. A codon would be from the (DNA x DNA x DNA) Alphabet whereas the components of the codon Symbol are from the DNA alphabet.

Contrast this with the definition of a BasisSymbol. A BasisSymbol can be validly divided into components that are still part of the same Alphabet. In this way a BasisSymbol can be ambiguous. For further discussion of BasisSymbol follow this link.

```java package biojava_in_anger;

import java.util.*; import org.biojava.bio.seq.*; import org.biojava.bio.symbol.*;

public class BreakingComponents {

 public static void main(String[] args) {
   //make the 'codon' alphabet
   List l = Collections.nCopies(3, DNATools.getDNA());
   Alphabet alpha = AlphabetManager.getCrossProductAlphabet(l);

   //get the first symbol in the alphabet
   Iterator iter = ((FiniteAlphabet)alpha).iterator();
   AtomicSymbol codon = (AtomicSymbol)iter.next();
   System.out.print(codon.getName()+" is made of: ");

   //break it into a list its components
   List symbols = codon.getSymbols();
   for(int i = 0; i < symbols.size(); i++){
     if(i != 0)
       System.out.print(", ");
     Symbol sym = (Symbol)symbols.get(i);
     System.out.print(sym.getName());
   }
 }

} ```