Show
Ignore:
Timestamp:
08/05/10 18:28:38 (22 months ago)
Author:
mauro
Message:

plone4

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plone4bio.base/trunk/src/plone4bio/base/png/seqrecord.py

    r119 r149  
    1111from plone4bio.base.png.interfaces import IImagemapPresentation 
    1212 
    13 from StringIO import StringIO 
     13from Bio import SeqIO 
     14from cStringIO import StringIO 
     15import logging 
    1416 
    15 # BioPerl 
    16 import subprocess 
    17 import os 
    18 from Bio import SeqIO 
     17logger = logging.getLogger('plone4bio.base') 
     18 
     19try: 
     20    from plone4bio.graphics.seqrecord import SeqRecordDrawer 
     21    has_plone4bio_graphics = True 
     22except 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) 
     32def 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) 
     68def 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 
    1990 
    2091class PNGFeaturesView(BrowserPage): 
     
    2798class ImagemapFeaturesView(BrowserView): 
    2899    def __call__(self): 
    29         return getMultiAdapter((self.context, self.request), IImagemapPresentation) 
     100        return seqrecordImagemap(self.context, self.request) 
     101        # return getMultiAdapter((self.context, self.request), IImagemapPresentation) 
    30102 
    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