Main Links
Documentation
Source Code

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.

  1. using System;
  2. using BioSharp.Core.Bio;
  3. using BioSharp.Core.Bio.Symbol;
  4. using BioSharp.Core.Utils;
  5. namespace Cookbook.Alphabets
  6. {
  7. class CustomSymbols
  8. {
  9. public static void Main(string[] args)
  10. {
  11. // Make the "zero" symbol with no annotation
  12. ISymbol zero = AlphabetManager.CreateSymbol("zero", Annotation.EMPTY_ANNOTATION);
  13. // Make the "one" Symbol
  14. ISymbol one = AlphabetManager.CreateSymbol("one", Annotation.EMPTY_ANNOTATION);
  15. // Collect the Symbols in a Set
  16. SymbolSet symbols = new SymbolSet();
  17. symbols.Add(zero);
  18. symbols.Add(one);
  19. // Make the Binary Alphabet
  20. IFiniteAlphabet binary = new SimpleAlphabet(symbols, "Binary");
  21. // Iterate through the symbols to show everything works
  22. foreach (ISymbol sym in binary)
  23. {
  24. Console.WriteLine(sym.Name);
  25. }
  26. // It is usual to register newly created Alphabets with the AlphabetManager
  27. AlphabetManager.RegisterAlphabet(binary.Name, binary);
  28. // The newly created Alphabet will have been registered with the
  29. // AlphabetManager under the name "Binary". If you retreive an instance
  30. // of it using this name it should be canonical with the previous instance
  31. IAlphabet alpha = AlphabetManager.AlphabetForName("Binary");
  32. // Check canonical status
  33. Console.WriteLine(alpha == binary);
  34. }
  35. }
  36. }