def draw_annotation_maps(seq, annot_maps, default_colors=None, picture_class=BitmapSequencePicture, wrappers=None): # make sure the list of default colors is the same length as the list # of input annotation maps. if default_colors is None: default_colors = [ 'black' ] * len(annot_maps) else: default_colors = list(default_colors) for i in range(len(default_colors), len(annot_maps)): default_colors.append('black') # make sure the list of feature wrappers is the same length as the # list of input annotation maps. if wrappers is None: wrappers = [ None ] * len(annot_maps) else: wrappers = list(wrappers) for i in range(len(wrappers), len(annot_maps)): wrappers.append(None) # create an instance of the picture class, whatever it may be. p = picture_class(len(seq)) ### underneath, draw each set of annotations l = [] maxmax_text_length = 0 for n, annot_map in enumerate(annot_maps): default_color = default_colors[n] try: annots = annot_map[seq] except (KeyError, TypeError): # sequence not in map, or map is None continue new_map, max_text_length = \ convert_to_image_coords(seq, annots, p, default_color, wrappers[n]) if new_map: l.append(new_map) maxmax_text_length = max(maxmax_text_length, max_text_length) p.set_left_margin_offset(maxmax_text_length) ### draw the basic sequence line p.draw_sequence_line() start_slot = 0 for new_map in l: n_slots = p.draw_annotations(new_map, start_slot=start_slot) start_slot += n_slots return p
def draw(self, seq): picture = draw_annotation_maps(seq, self.maps, default_colors=self.default_colors, picture_class=self.picture_class, wrappers=self.wrappers) return picture # def draw_annotation_maps(seq, annot_maps, # default_colors=None, # picture_class=None, # wrappers=None): annot_maps = self.maps default_colors=self.default_colors picture_class=self.picture_class wrappers=self.wrappers if picture_class is None: picture_class = get_picture_class # make sure the list of default colors is the same length as the list # of input annotation maps. if default_colors is None: default_colors = [ 'black' ] * len(annot_maps) else: default_colors = list(default_colors) for i in range(len(default_colors), len(annot_maps)): default_colors.append('black') # make sure the list of feature wrappers is the same length as the # list of input annotation maps. if wrappers is None: wrappers = [ None ] * len(annot_maps) else: wrappers = list(wrappers) for i in range(len(wrappers), len(annot_maps)): wrappers.append(None) # create an instance of the picture class, whatever it may be. p = picture_class(seq) self.p = p ### underneath, draw each set of annotations l = [] maxmax_text_length = 0 # keep track of left margin offset for n, annot_map in enumerate(annot_maps): default_color = default_colors[n] if isinstance(annot_map, SpanMap): try: annot_map.nlmsa[seq] l.append(annot_map) except KeyError: pass # either way, no further processing needed continue try: annots = annot_map[seq] except (KeyError, TypeError): # sequence not in map, or map is None continue new_map, max_text_length = \ convert_to_image_coords(seq, annots, p, default_color, wrappers[n]) if new_map: l.append(new_map) maxmax_text_length = max(maxmax_text_length, max_text_length) p.set_left_margin_offset(maxmax_text_length) ### draw the basic sequence line p.draw_sequence_line() start_slot = 0 for new_map in l: if isinstance(new_map, SpanMap): color = None if new_map.line_color: color = getattr(p.colors, new_map.line_color) fill = None if new_map.fill_color: fill = getattr(p.colors, new_map.fill_color) pairs = new_map.transform_coords_to_picture(seq, p) n_slots = p._draw_xy_plot(start_slot, seq.start, seq.stop, pairs, new_map.height, color=color, fill=fill) n_slots = new_map.height else: n_slots = p.draw_annotations(new_map, start_slot=start_slot) start_slot += n_slots ### return p