| 15 | | # BioPerl |
| 16 | | import subprocess |
| 17 | | import os |
| 18 | | from Bio import SeqIO |
| | 17 | logger = logging.getLogger('plone4bio.base') |
| | 18 | |
| | 19 | try: |
| | 20 | from plone4bio.graphics.seqrecord import SeqRecordDrawer |
| | 21 | has_plone4bio_graphics = True |
| | 22 | except ImportError: |
| | 23 | logger.warning("missing plone4bio.graphics") |
| | 24 | # BioPerl |
| | 25 | import subprocess |
| | 26 | import os |
| | 27 | has_plone4bio_graphics = False |
| | 28 | |
| | 29 | |
| | 30 | @adapter(ISeqRecord, IRequest) |
| | 31 | @implementer(IImagemapPresentation) |
| | 32 | def seqrecordImagemap(context, request): |
| | 33 | # FIXME: XXX |
| | 34 | context.Description() |
| | 35 | seqrecord = context.seqrecord |
| | 36 | if has_plone4bio_graphics: |
| | 37 | imagemap = """<map name="graphicsmap" id="graphicsmap">\n""" |
| | 38 | for box in SeqRecordDrawer(seqrecord, fig_width=1500).boxes(): |
| | 39 | box['start'] = box['feature'].start |
| | 40 | box['end'] = box['feature'].end |
| | 41 | box['tag'] = box['feature'].name |
| | 42 | # my $tag = eval {$feature->method} || $feature->primary_tag; |
| | 43 | # $left += $pad_left; |
| | 44 | # $right += $pad_left; |
| | 45 | # next unless $tag; |
| | 46 | imagemap = imagemap + \ |
| | 47 | """<area class="tips" shape="rect" """ \ |
| | 48 | """coords="%(left)i,%(top)i,%(right)i,%(bottom)i" href="#" """ \ |
| | 49 | """rel="#%(tag)sX%(start)sX$%(end)s" title="%(tag)s %(start)s:%(end)s" alt="" />""" \ |
| | 50 | % box |
| | 51 | imagemap = imagemap + "</map>\n" |
| | 52 | return imagemap |
| | 53 | else: |
| | 54 | # BIOPERL |
| | 55 | perlpath = os.sep.join((os.path.dirname(__file__), "perl")) |
| | 56 | cmd = ["perl", "-I"+ perlpath, os.sep.join((perlpath, "graphics-imagemap-cmd.pl")), "-"] |
| | 57 | graphics = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
| | 58 | genbank=StringIO() |
| | 59 | seqrecord.name = seqrecord.name[:16] |
| | 60 | for f in seqrecord.features: |
| | 61 | f.type = f.type.replace(" ", "_") |
| | 62 | SeqIO.write([seqrecord, ], genbank, "genbank") |
| | 63 | (stdoutdata, stderrdata) = graphics.communicate(genbank.getvalue()) |
| | 64 | return stdoutdata |
| | 65 | |
| | 66 | @adapter(ISeqRecord, IRequest) |
| | 67 | @implementer(IPNGPresentation) |
| | 68 | def seqrecordPNG(context, request): |
| | 69 | # FIXME: XXX |
| | 70 | context.Description() |
| | 71 | seqrecord = context.seqrecord |
| | 72 | if has_plone4bio_graphics: |
| | 73 | imgdata=StringIO() |
| | 74 | SeqRecordDrawer(seqrecord, fig_width=1500).save(imgdata, format='PNG') |
| | 75 | return imgdata.getvalue() |
| | 76 | else: |
| | 77 | # BioPerl |
| | 78 | # prediction.getDataCharts() = |
| | 79 | # [{'chart':(line,bar,...),'data':[(x,y),(x,y),...}, ...] |
| | 80 | perlpath = os.sep.join((os.path.dirname(__file__), "perl")) |
| | 81 | cmd = ["perl", "-I"+ perlpath, os.sep.join((perlpath, "graphics-cmd.pl")), "-"] |
| | 82 | graphics = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
| | 83 | genbank=StringIO() |
| | 84 | seqrecord.name = seqrecord.name[:16] |
| | 85 | for f in seqrecord.features: |
| | 86 | f.type = f.type.replace(" ", "_") |
| | 87 | SeqIO.write([seqrecord, ], genbank, "genbank") |
| | 88 | (stdoutdata, stderrdata) = graphics.communicate(genbank.getvalue()) |
| | 89 | return stdoutdata |
| 31 | | # TODO: refactoring with genometools (?) |
| 32 | | @adapter(ISeqRecord, IRequest) |
| 33 | | @implementer(IImagemapPresentation) |
| 34 | | def seqrecordImagemap(context, request): |
| 35 | | perlpath = os.sep.join((os.path.dirname(__file__), "perl")) |
| 36 | | cmd = ["perl", "-I"+ perlpath, os.sep.join((perlpath, "graphics-imagemap-cmd.pl")), "-"] |
| 37 | | graphics = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
| 38 | | # FIXME: XXX |
| 39 | | context.Description() |
| 40 | | genbank=StringIO() |
| 41 | | seqrecord = context.seqrecord |
| 42 | | seqrecord.name = seqrecord.name[:16] |
| 43 | | for f in seqrecord.features: |
| 44 | | f.type = f.type.replace(" ", "_") |
| 45 | | SeqIO.write([seqrecord, ], genbank, "genbank") |
| 46 | | (stdoutdata, stderrdata) = graphics.communicate(genbank.getvalue()) |
| 47 | | return stdoutdata |
| 48 | | |
| 49 | | # TODO: refactoring with genometools (?) |
| 50 | | @adapter(ISeqRecord, IRequest) |
| 51 | | @implementer(IPNGPresentation) |
| 52 | | def seqrecordPNG(context, request): |
| 53 | | # prediction.getDataCharts() = [{'chart':(line,bar,...),'data':[(x,y),(x,y),...}, ...] |
| 54 | | |
| 55 | | # BioPerl |
| 56 | | perlpath = os.sep.join((os.path.dirname(__file__), "perl")) |
| 57 | | cmd = ["perl", "-I"+ perlpath, os.sep.join((perlpath, "graphics-cmd.pl")), "-"] |
| 58 | | graphics = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
| 59 | | # FIXME: XXX |
| 60 | | context.Description() |
| 61 | | genbank=StringIO() |
| 62 | | seqrecord = context.seqrecord |
| 63 | | seqrecord.name = seqrecord.name[:16] |
| 64 | | for f in seqrecord.features: |
| 65 | | f.type = f.type.replace(" ", "_") |
| 66 | | SeqIO.write([seqrecord, ], genbank, "genbank") |
| 67 | | (stdoutdata, stderrdata) = graphics.communicate(genbank.getvalue()) |
| 68 | | return stdoutdata |