| | 21 | def draw_motifs_on_sequence(dna_seq, motifs, picture_class, |
|---|
| | 22 | seq_start, seq_stop): |
|---|
| | 23 | sequencedb = dict(sequence=pygr.sequence.Sequence(dna_seq.seq, |
|---|
| | 24 | 'sequence')) |
|---|
| | 25 | colors = picture_class.colors |
|---|
| | 26 | |
|---|
| | 27 | map_list = [] |
|---|
| | 28 | for i, motif in enumerate(motifs): |
|---|
| | 29 | color_name = color_rotation[i % len(color_rotation)] |
|---|
| | 30 | color = getattr(colors, color_name) |
|---|
| | 31 | |
|---|
| | 32 | d = {} |
|---|
| | 33 | matches = motif.search(dna_seq) |
|---|
| | 34 | for n, (start, stop, _, _) in enumerate(matches): |
|---|
| | 35 | name = "match%d" % (n,) |
|---|
| | 36 | d[name] = Annotation(name, 'sequence', start, stop, |
|---|
| | 37 | color=color) |
|---|
| | 38 | |
|---|
| | 39 | map = pygr_draw.create_annotation_map(d, sequencedb) |
|---|
| | 40 | |
|---|
| | 41 | map_list.append(map) |
|---|
| | 42 | |
|---|
| | 43 | sequence = sequencedb['sequence'][seq_start:seq_stop] |
|---|
| | 44 | p = pygr_draw.draw_annotation_maps(sequence, map_list, |
|---|
| | 45 | picture_class=picture_class) |
|---|
| | 46 | |
|---|
| | 47 | image = p.finalize() |
|---|
| | 48 | return image |
|---|
| | 49 | |
|---|
| 107 | | map_list = [] |
|---|
| 108 | | for i, motif in enumerate(motifs): |
|---|
| 109 | | color_name = color_rotation[i % len(color_rotation)] |
|---|
| 110 | | color = getattr(colors, color_name) |
|---|
| 111 | | |
|---|
| 112 | | motif = self.manager.load(motif.id) |
|---|
| 113 | | |
|---|
| 114 | | d = {} |
|---|
| 115 | | matches = motif.search(self.seq) |
|---|
| 116 | | for n, (start, stop, _, _) in enumerate(matches): |
|---|
| 117 | | name = "match%d" % (n,) |
|---|
| 118 | | d[name] = Annotation(name, 'sequence', start, stop, |
|---|
| 119 | | color=color) |
|---|
| 120 | | |
|---|
| 121 | | map = pygr_draw.create_annotation_map(d, sequencedb) |
|---|
| 122 | | |
|---|
| 123 | | map_list.append(map) |
|---|
| 124 | | |
|---|
| 125 | | sequence = sequencedb['sequence'][seq_start:seq_stop] |
|---|
| 126 | | p = pygr_draw.draw_annotation_maps(sequence, map_list, |
|---|
| 127 | | picture_class=picture_class) |
|---|
| 128 | | |
|---|
| 129 | | image = p.finalize() |
|---|
| | 130 | image = draw_motifs_on_sequence(self.seq, motifs, |
|---|
| | 131 | pygr_draw.BitmapSequencePicture, |
|---|
| | 132 | seq_start, seq_stop) |
|---|
| | 133 | |
|---|
| 131 | | |
|---|
| | 135 | return image |
|---|
| | 136 | |
|---|
| | 137 | def draw_pdf(self, request): |
|---|
| | 138 | form = request.form |
|---|
| | 139 | seq_start = int(form.get('start', 0)) |
|---|
| | 140 | seq_start = max(seq_start, 0) |
|---|
| | 141 | |
|---|
| | 142 | seq_stop = int(form.get('stop', len(self.seq.seq))) |
|---|
| | 143 | seq_stop = min(len(self.seq.seq), seq_stop) |
|---|
| | 144 | |
|---|
| | 145 | (folder, motifs) = get_folder_motifs(self.manager, request) |
|---|
| | 146 | |
|---|
| | 147 | image = draw_motifs_on_sequence(self.seq, motifs, |
|---|
| | 148 | pygr_draw.PDFSequencePicture, |
|---|
| | 149 | seq_start, seq_stop) |
|---|
| | 150 | |
|---|
| | 151 | # force a download to a filename |
|---|
| | 152 | filename = 'motifs-seq%d.pdf' % (self.seq.id,) |
|---|
| | 153 | request.response.set_header('Content-Disposition', |
|---|
| | 154 | 'attachment; filename=%s' % (filename,)) |
|---|
| | 155 | request.response.set_header('Content-Length', len(image)) |
|---|
| | 156 | request.response.set_header('Content-Type', 'binary/octet-stream') |
|---|