def main(*argv): "Demostration program number 5." print Demo5.main.__doc__ GeneralTree.main(*argv) BinaryTree.main(*argv) NaryTree.main(*argv) BinarySearchTree.main(*argv) AVLTree.main(*argv) MWayTree.main(*argv) BTree.main(*argv) return 0
def parseCoordFile(coord_file): """ Returns two dictionaries: chr2coord2count: Holds the counts for each coordinate chr2coordTree: Holds each search tree for each chr in the coord file """ chr2coord2count = {} chr2coordTree = {} for line in coord_file: line = formatLine(line) line_list = line.split("\t") chr = line_list[0] start = int(line_list[1]) end = int(line_list[2]) coord = (start, end) if chr.startswith("chr"): chr = chr.replace("chr", "") if chr in chr2coord2count: chr2coord2count[chr][coord] = 0 else: chr2coord2count[chr] = {coord: 0} if chr in chr2coordTree: chr2coordTree[chr].insert(coord) else: chr2coordTree[chr] = AVLTree() chr2coordTree[chr].insert(coord) return chr2coord2count, chr2coordTree
def getCoordFromDB(db, db_name, table): """ Returns two dictionaries: chr2coord2count: Holds the counts for each coordinate chr2coordTree: Holds each search tree for each chr in the coord file """ chr2coord2count = {} chr2coordTree = {} select_state = "SELECT chr, start, end FROM %s" % table records = db.getDBRecords_Dict(select_state, db_name) for row in records: chr = row["chr"] start = int(row["start"]) end = int(row["end"]) coord = (start, end) if chr.startswith("chr"): chr = chr.replace("chr", "") if chr in chr2coord2count: chr2coord2count[chr][coord] = 0 else: chr2coord2count[chr] = {coord: 0} if chr in chr2coordTree: chr2coordTree[chr].insert(coord) else: chr2coordTree[chr] = AVLTree() chr2coordTree[chr].insert(coord) return chr2coord2count, chr2coordTree
def translate(dictionary, input, output): """ (File, File, File) -> None Reads all the word pairs from the dictionary file and then reads words from the input file, translates the words (if possible), and writes them to the output file. """ searchTree = AVLTree() for line in dictionary.readlines(): words = line.split() assert len(words) == 2 searchTree.insert(Association(words[0], words[1])) for line in input.readlines(): for word in line.split(): assoc = searchTree.find(Association(word)) if assoc is None: output.write(word + " ") else: output.write(assoc.value + " ") output.write("\n")
def getSearchTree(coord_list): """ coord_list is a list of (start, end) positions. """ coordTree = AVLTree() # Make sure coords are unique coord_list_set = set(coord_list) for coord in coord_list_set: # ERROR CHECKING if type(coord) != type((1, 2)): raise TypeError(("List should be of tuples")) if len(coord) != 2: raise ValueError(("Tuple should only have two elements)")) # Will raise exception if the coordinates are not numbers val_check = coord[1] - coord[0] coordTree.insert(coord) return coordTree
def getSearchTree(coord_list): """ coord_list is a list of (start, end) positions. """ coordTree = AVLTree() # Make sure coords are unique coord_list_set = set(coord_list) for coord in coord_list_set: # ERROR CHECKING if type(coord) != type((1,2)): raise TypeError, ("List should be of tuples") if len(coord) != 2: raise ValueError, ("Tuple should only have two elements)") # Will raise exception if the coordinates are not numbers val_check = coord[1] - coord[0] coordTree.insert(coord) return coordTree