# __ __ ___ ___ __ __ ____ ____ ____ _ _ ____ ___ # /__\ ( ) / __) ___ / __)( )( )( ___)( ___)(_ _)( \/ )( ___)/ __) # /(__)\ )(__( (_-.(___)\__ \ )(__)( )__) )__) _)(_ ) ( )__) \__ \ # (__)(__)(____)\___/ (___/(______)(__) (__) (____)(_/\_)(____)(___/ # # Template by Leo Ackermann # Code by PUT YOUR NAME HERE # ============================================================================== # C L A S S for Trie node # ============================================================================== class TrieNode: def __init__(self): # Whether the node is marked self.marked = False # A dictionnary to store auxiliary information at the level of node self.misc = {} # The tree structure is stored as a dictionnary self.children = {} def pretty_print(self, indent=''): ''' Pretty print the subtree of the node ''' children = list(self.children.items()) if len(children) == 0: # This is a leaf, marked by construction print(f'#') elif len(children) == 1: # This is a non-branching node (label, child) = children[0] if self.marked: print(f'#──({label})──', end='') else: print(f'━──({label})──', end='') child.pretty_print(indent + " ") else: # This is a branching node (at least two children) # First child (label, child) = children[0] if self.marked: print(f'#──({label})──', end='') else: print(f'┬──({label})──', end='') child.pretty_print(indent + "│ ") # Mid children (possibly skipretty_printed) for i_children in range(1, len(children)-1): (label, child) = children[i_children] print(f'{indent}├──({label})──', end='') child.pretty_print(indent + "│ ") # Last child (label, child) = children[-1] print(f'{indent}└──({label})──', end='') child.pretty_print(indent + " ") # ============================================================================== # C L A S S for Trie # ============================================================================== class Trie: def __init__(self, words=[]): self.root = TrieNode() for word in words: self.insert(word) def pretty_print(self): self.root.pretty_print() def insert(self, word): pass # TODO # ============================================================================== # U T I L I T I E S # ============================================================================== def open_fasta_as_string(filepath): ''' Retrieve the genome stored in a (simple) fasta file, as a string ''' with open(filepath) as fastafile: return ''.join(fastafile.read().splitlines()[1:])