How do I make a custom Alphabet from custom Symbols? - Cookbook - BioSharp
This example demonstrates the creation of a 'binary' alphabet that will have two symbols, zero and one. The custom made ISymbol and IAlphabet can then be used to make ISymbolList, ISequence, Distributions, etc.
- using System;
- using BioSharp.Core.Bio;
- using BioSharp.Core.Bio.Symbol;
- using BioSharp.Core.Utils;
- namespace Cookbook.Alphabets
- {
- class CustomSymbols
- {
- public static void Main(string[] args)
- {
- // Make the "zero" symbol with no annotation
- ISymbol zero = AlphabetManager.CreateSymbol("zero", Annotation.EMPTY_ANNOTATION);
- // Make the "one" Symbol
- ISymbol one = AlphabetManager.CreateSymbol("one", Annotation.EMPTY_ANNOTATION);
- // Collect the Symbols in a Set
- SymbolSet symbols = new SymbolSet();
- symbols.Add(zero);
- symbols.Add(one);
- // Make the Binary Alphabet
- IFiniteAlphabet binary = new SimpleAlphabet(symbols, "Binary");
- // Iterate through the symbols to show everything works
- foreach (ISymbol sym in binary)
- {
- Console.WriteLine(sym.Name);
- }
- // It is usual to register newly created Alphabets with the AlphabetManager
- AlphabetManager.RegisterAlphabet(binary.Name, binary);
- // The newly created Alphabet will have been registered with the
- // AlphabetManager under the name "Binary". If you retreive an instance
- // of it using this name it should be canonical with the previous instance
- IAlphabet alpha = AlphabetManager.AlphabetForName("Binary");
- // Check canonical status
- Console.WriteLine(alpha == binary);
- }
- }
- }