Ejemplo n.º 1
0
def test_enable_import_with_ext():
    """Using exts any file extension can be importable."""
    ext = ".html,.kid.html"
    sys.path.insert(0, tmpdir)
    try:
        raises(ImportError, "import test_suffixes1")
        raises(ImportError, "import test_suffixes2")
        raises(ImportError, "import test_suffixes3")
        kid.enable_import(ext=ext)
        dest = joinpath(tmpdir, "test_suffixes1.kid")
        copyfile(tfile, dest)
        import test_suffixes1  # *.kid files are always importable

        dest = joinpath(tmpdir, "test_suffixes2.html")
        copyfile(tfile, dest)
        import test_suffixes2

        dest = joinpath(tmpdir, "test_suffixes3.kid.html")
        copyfile(tfile, dest)
        import test_suffixes3

        dest = joinpath(tmpdir, "test_suffixes4.xhtml")
        copyfile(tfile, dest)
        raises(ImportError, "import test_suffixes4")
        kid.disable_import()
    finally:
        sys.path.remove(tmpdir)
Ejemplo n.º 2
0
def test_import_hook():
    kid.enable_import()
    import test.test_if

    assert_template_interface(test.test_if)
    assert sys.modules.has_key("test.test_if")
    kid.disable_import()
Ejemplo n.º 3
0
def test_pyc_generation():
    # if this exists, the test is worthless. make sure this test runs before
    # anything else import test_content
    from kid.test import template_dir
    kid.enable_import()
    assert not exists(joinpath(template_dir, 'test_content.pyc'))
    import test.test_content
    assert exists(joinpath(template_dir, 'test_content.pyc'))
    assert sys.modules.has_key('test.test_content')
Ejemplo n.º 4
0
def test_pyc_generation():
    # if this exists, the test is worthless. make sure this test runs before
    # anything else import test_content
    from kid.test import template_dir
    kid.enable_import()
    assert not exists(joinpath(template_dir, 'test_content.pyc'))
    import test.test_content
    assert exists(joinpath(template_dir, 'test_content.pyc'))
    assert sys.modules.has_key('test.test_content')
Ejemplo n.º 5
0
    def _create_report(self, pwm, background, stats={}, best_id={}):
        self.logger.info("Creating graphical report")
        class ReportMotif:
            pass
        
        motifs = pwmfile_to_motifs(pwm)
        for m,match in self.closest_match.items():
            match[0].to_img(os.path.join(self.imgdir,"%s.png" % match[0].id), format="PNG")

        sort_key = background[0]
        if "gc" in background:
            sort_key = "gc"

        roc_img_file = "%s_%s_roc"
        report_motifs = []
        sorted_motifs = sorted(motifs, 
                cmp= lambda x,y: cmp(self.mncp[sort_key][y.id], self.mncp[sort_key][x.id])
                )

        for motif in sorted_motifs:
            rm = ReportMotif()
            rm.id = motif.id
            rm.id_href = {"href": "#%s" % motif.id}
            rm.id_name = {"name": motif.id}
            rm.img = {"src":  os.path.join("images", "%s.png" % motif.id)}
            
            rm.best = best_id[motif.id]
            
            rm.consensus = motif.to_consensus()
            rm.stars = stats["%s_%s" % (motif.id, motif.to_consensus())]["stars"]

            rm.bg = {}
            for bg in background:
                rm.bg[bg] = {}
                rm.bg[bg]["e"] = "%0.2f" % self.e[bg].setdefault(motif.id, 0.0)
                rm.bg[bg]["p"] = "%0.2f" % self.p[bg].setdefault(motif.id, 1.0)
                rm.bg[bg]["auc"] = "%0.3f" % self.auc[bg][motif.id]
                rm.bg[bg]["mncp"] = "%0.3f" % self.mncp[bg][motif.id]
                rm.bg[bg]["roc_img"] = {"src": "images/" + os.path.basename(roc_img_file % (motif.id, bg)) + ".png"}
                rm.bg[bg]["roc_img_link"] = {"href": "images/" + os.path.basename(roc_img_file % (motif.id, bg)) + ".png"}
            
            rm.histogram_img = {"data":"images/%s_histogram.svg" % motif.id}
            rm.histogram_link= {"href":"images/%s_histogram.svg" % motif.id}
            rm.match_img = {"src":  "images/%s.png" % self.closest_match[motif.id][0].id}
            rm.match_id = self.closest_match[motif.id][0].id
            rm.match_pval = "%0.2e" % self.closest_match[motif.id][1] 
            
            report_motifs.append(rm)
        
        total_report = self.motif_report 
        kid.enable_import()
        template_file = os.path.join(self.config.get_template_dir(), "report_template_v2.kid") 
        template = kid.Template(file=template_file, expname=self.name, motifs=report_motifs, inputfile=self.inputfile, date=datetime.today().strftime("%d/%m/%Y"), version=GM_VERSION)
        f = open(total_report, "w")
        f.write(template.serialize())
        f.close()
Ejemplo n.º 6
0
	def _cluster_motifs(self, pfm_file, cluster_pwm, dir, threshold):
		self.logger.info("Clustering significant motifs.")
		
		trim_ic = 0.2
		clusters = []
		motifs = pwmfile_to_motifs(pfm_file)
		if len(motifs) == 1:
			clusters = [[motifs[0], motifs]]
		else:
			tree = cluster_motifs(pfm_file, "total", "wic", "mean", True, threshold=float(threshold), include_bg=True)
			clusters = tree.getResult()

		ids = []
		mc = MotifComparer()

		for cluster,members in clusters:
			cluster.trim(trim_ic)
			cluster.to_img(os.path.join(self.imgdir,"%s.png" % cluster.id), format="PNG")
			ids.append([cluster.id, {"src":"images/%s.png" % cluster.id},[]])
			if len(members) > 1:
				scores = {}
				for motif in members:
					scores[motif] =  mc.compare_motifs(cluster, motif, "total", "wic", "mean", pval=True)	
				add_pos = sorted(scores.values(),cmp=lambda x,y: cmp(x[1], y[1]))[0][1]
				for motif in members:
					score, pos, strand = scores[motif]
					add = pos - add_pos
						
					if strand in [1,"+"]:
						pass
					else:
						#print "RC %s" % motif.id
						rc = motif.rc()
						rc.id = motif.id
						motif = rc
					#print "%s\t%s" % (motif.id, add)	
					motif.to_img(os.path.join(self.imgdir, "%s.png" % motif.id.replace(" ", "_")), format="PNG", add_left=add)
			ids[-1][2] = [dict([("src", "images/%s.png" % motif.id.replace(" ", "_")), ("alt", motif.id.replace(" ", "_"))]) for motif in members]
		
		kid.enable_import()
		template_file = os.path.join(self.config.get_template_dir(), "cluster_template_v2.kid")
		template = kid.Template(file=template_file, expname=self.name, motifs=ids, inputfile=self.inputfile, date=datetime.today().strftime("%d/%m/%Y"), version=GM_VERSION)
		f = open(self.cluster_report, "w")
		f.write(template.serialize())
		f.close()
		
		f = open(cluster_pwm, "w")
		if len(clusters) == 1 and len(clusters[0][1]) == 1:
			f.write("%s\n" % clusters[0][0].to_pwm())
		else:
			for motif in tree.get_clustered_motifs():
				f.write("%s\n" % motif.to_pwm())
		f.close()
	
		self.logger.info("Clustering done. See the result in %s" % self.cluster_report)
		return clusters
Ejemplo n.º 7
0
    def _cluster_motifs(self, pfm_file, cluster_pwm, dir, threshold):
        self.logger.info("Clustering significant motifs.")
        
        trim_ic = 0.2
        clusters = []
        motifs = pwmfile_to_motifs(pfm_file)
        if len(motifs) == 1:
            clusters = [[motifs[0], motifs]]
        else:
            tree = cluster_motifs(pfm_file, "total", "wic", "mean", True, threshold=float(threshold), include_bg=True)
            clusters = tree.getResult()

        ids = []
        mc = MotifComparer()

        for cluster,members in clusters:
            cluster.trim(trim_ic)
            cluster.to_img(os.path.join(self.imgdir,"%s.png" % cluster.id), format="PNG")
            ids.append([cluster.id, {"src":"images/%s.png" % cluster.id},[]])
            if len(members) > 1:
                scores = {}
                for motif in members:
                    scores[motif] =  mc.compare_motifs(cluster, motif, "total", "wic", "mean", pval=True)    
                add_pos = sorted(scores.values(),cmp=lambda x,y: cmp(x[1], y[1]))[0][1]
                for motif in members:
                    score, pos, strand = scores[motif]
                    add = pos - add_pos
                        
                    if strand in [1,"+"]:
                        pass
                    else:
                        #print "RC %s" % motif.id
                        rc = motif.rc()
                        rc.id = motif.id
                        motif = rc
                    #print "%s\t%s" % (motif.id, add)    
                    motif.to_img(os.path.join(self.imgdir, "%s.png" % motif.id.replace(" ", "_")), format="PNG", add_left=add)
            ids[-1][2] = [dict([("src", "images/%s.png" % motif.id.replace(" ", "_")), ("alt", motif.id.replace(" ", "_"))]) for motif in members]
        
        kid.enable_import()
        template_file = os.path.join(self.config.get_template_dir(), "cluster_template_v2.kid")
        template = kid.Template(file=template_file, expname=self.name, motifs=ids, inputfile=self.inputfile, date=datetime.today().strftime("%d/%m/%Y"), version=GM_VERSION)
        f = open(self.cluster_report, "w")
        f.write(template.serialize())
        f.close()
        
        f = open(cluster_pwm, "w")
        if len(clusters) == 1 and len(clusters[0][1]) == 1:
            f.write("%s\n" % clusters[0][0].to_pwm())
        else:
            for motif in tree.get_clustered_motifs():
                f.write("%s\n" % motif.to_pwm())
        f.close()
    
        self.logger.info("Clustering done. See the result in %s" % self.cluster_report)
        return clusters
Ejemplo n.º 8
0
	def _create_report(self, pwm, background):
		class ReportMotif:
			pass
		
		motifs = pwmfile_to_motifs(pwm)
		for m,match in self.closest_match.items():
			match[0].to_img(os.path.join(self.imgdir,"%s.png" % match[0].id), format="PNG")

		random = "random" in background
		genomic = "genomic_matched" in background
		sort_key = "random"
		if genomic:
			sort_key = "genomic_matched"

		roc_img_file = "%s_%s_roc"
		report_motifs = []
		for motif in sorted(motifs, cmp=lambda x,y: cmp(self.mncp[sort_key][y.id], self.mncp[sort_key][x.id])):
			rm = ReportMotif()
			rm.id = motif.id
			rm.id_href = {"href": "#%s" % motif.id}
			rm.id_name = {"name": motif.id}
			rm.img = {"src":  os.path.join("images", "%s.png" % motif.id)}
			
			rm.consensus = motif.to_consensus()
			
			if random:
				rm.random_e = "%0.2f" % self.e["random"][motif.id]
				rm.random_p = "%0.2f" % self.p["random"][motif.id]
				rm.random_auc = "%0.3f" % self.auc["random"][motif.id]
				rm.random_mncp = "%0.3f" % self.mncp["random"][motif.id]
				rm.random_roc_img = {"src": "images/" + os.path.basename(roc_img_file % (motif.id, "random")) + ".png"}
				rm.random_roc_img_link = {"href": "images/" + os.path.basename(roc_img_file % (motif.id, "random")) + ".png"}
			if genomic:
				rm.genomic_e = "%0.2f" % self.e["genomic_matched"][motif.id]
				rm.genomic_p = "%0.2f" % self.p["genomic_matched"][motif.id]
				rm.genomic_auc = "%0.3f" % self.auc["genomic_matched"][motif.id]
				rm.genomic_mncp = "%0.3f" % self.mncp["genomic_matched"][motif.id]
				rm.genomic_roc_img = {"src": "images/" + os.path.basename(roc_img_file % (motif.id, "genomic_matched")) + ".png"}
				rm.genomic_roc_img_link = {"href": "images/" + os.path.basename(roc_img_file % (motif.id, "genomic_matched")) + ".png"}
			rm.histogram_img = {"data":"images/%s_histogram.svg" % motif.id}
			rm.histogram_link= {"href":"images/%s_histogram.svg" % motif.id}
			rm.match_img = {"src":  "images/%s.png" % self.closest_match[motif.id][0].id}
			rm.match_id = self.closest_match[motif.id][0].id
			rm.match_pval = "%0.2e" % self.closest_match[motif.id][1] 
			
			report_motifs.append(rm)
		
		total_report = self.motif_report 
		kid.enable_import()
		template_file = os.path.join(self.config.get_template_dir(), "report_template_v2.kid") 
		template = kid.Template(file=template_file, expname=self.name, motifs=report_motifs, random=random, genomic=genomic, inputfile=self.inputfile, date=datetime.today().strftime("%d/%m/%Y"), version=self.VERSION)
		f = open(total_report, "w")
		f.write(template.serialize())
		f.close()
Ejemplo n.º 9
0
def test_import_and_expand():
    from kid.test import output_dir
    kid.enable_import()
    import test.context as c
    C = c.Template
    out = joinpath(output_dir, 'context.out')
    t = C(foo=10, bar='bla bla')
    it = t.pull()
    for e in it:
        pass
    t.write(out)
    check_xml_file(out)
Ejemplo n.º 10
0
def test_enable_import_empty():
    """By default *.kid files are imported."""
    sys.path.insert(0, tmpdir)
    try:
        kid.disable_import()
        raises(ImportError, "import test_suffixes0")
        kid.enable_import()
        import test_suffixes0
        raises(ImportError, "import test_suffixes1")
        kid.disable_import()
    finally:
        sys.path.remove(tmpdir)
Ejemplo n.º 11
0
def test_enable_import_with_path():
    """Using path any template directory can be importable."""
    assert tmpdir not in sys.path
    raises(ImportError, "import test_suffixes4")
    kid.enable_import(path=tmpdir)
    dest = joinpath(tmpdir, "test_suffixes4.kid")
    copyfile(tfile, dest)
    import test_suffixes4
    kid.disable_import(path=tmpdir)
    dest = joinpath(tmpdir, "test_suffixes5.kid")
    copyfile(tfile, dest)
    raises(ImportError, "import test_suffixes5")
Ejemplo n.º 12
0
def test_import_and_expand():
    from kid.test import output_dir
    kid.enable_import()
    import test.context as c
    C = c.Template
    out = joinpath(output_dir, 'context.out')
    t = C(foo=10, bar='bla bla')
    it = t.pull()
    for e in it:
        pass
    t.write(out)
    check_xml_file(out)
Ejemplo n.º 13
0
def test_enable_import_with_path():
    """Using path any template directory can be importable."""
    assert tmpdir not in sys.path
    raises(ImportError, "import test_suffixes4")
    kid.enable_import(path=tmpdir)
    dest = joinpath(tmpdir, "test_suffixes4.kid")
    copyfile(tfile, dest)
    import test_suffixes4

    kid.disable_import(path=tmpdir)
    dest = joinpath(tmpdir, "test_suffixes5.kid")
    copyfile(tfile, dest)
    raises(ImportError, "import test_suffixes5")
Ejemplo n.º 14
0
def test_enable_import_empty():
    """By default *.kid files are imported."""
    sys.path.insert(0, tmpdir)
    try:
        kid.disable_import()
        raises(ImportError, "import test_suffixes0")
        kid.enable_import()
        import test_suffixes0

        raises(ImportError, "import test_suffixes1")
        kid.disable_import()
    finally:
        sys.path.remove(tmpdir)
Ejemplo n.º 15
0
	def _create_report(self, pwm, background):
		self.logger.info("Creating graphical report")
		class ReportMotif:
			pass
		
		motifs = pwmfile_to_motifs(pwm)
		for m,match in self.closest_match.items():
			match[0].to_img(os.path.join(self.imgdir,"%s.png" % match[0].id), format="PNG")

		sort_key = background[0]
		if "genomic_matched" in background:
			sort_key = "genomic_matched"

		roc_img_file = "%s_%s_roc"
		report_motifs = []
		for motif in sorted(motifs, cmp=lambda x,y: cmp(self.mncp[sort_key][y.id], self.mncp[sort_key][x.id])):
			rm = ReportMotif()
			rm.id = motif.id
			rm.id_href = {"href": "#%s" % motif.id}
			rm.id_name = {"name": motif.id}
			rm.img = {"src":  os.path.join("images", "%s.png" % motif.id)}
			
			rm.consensus = motif.to_consensus()
			
			rm.bg = {}
			for bg in background:
				rm.bg[bg] = {}
				rm.bg[bg]["e"] = "%0.2f" % self.e[bg][motif.id]
				rm.bg[bg]["p"] = "%0.2f" % self.p[bg][motif.id]
				rm.bg[bg]["auc"] = "%0.3f" % self.auc[bg][motif.id]
				rm.bg[bg]["mncp"] = "%0.3f" % self.mncp[bg][motif.id]
				rm.bg[bg]["roc_img"] = {"src": "images/" + os.path.basename(roc_img_file % (motif.id, bg)) + ".png"}
				rm.bg[bg]["roc_img_link"] = {"href": "images/" + os.path.basename(roc_img_file % (motif.id, bg)) + ".png"}
			
			rm.histogram_img = {"data":"images/%s_histogram.svg" % motif.id}
			rm.histogram_link= {"href":"images/%s_histogram.svg" % motif.id}
			rm.match_img = {"src":  "images/%s.png" % self.closest_match[motif.id][0].id}
			rm.match_id = self.closest_match[motif.id][0].id
			rm.match_pval = "%0.2e" % self.closest_match[motif.id][1] 
			
			report_motifs.append(rm)
		
		total_report = self.motif_report 
		kid.enable_import()
		template_file = os.path.join(self.config.get_template_dir(), "report_template_v2.kid") 
		template = kid.Template(file=template_file, expname=self.name, motifs=report_motifs, inputfile=self.inputfile, date=datetime.today().strftime("%d/%m/%Y"), version=GM_VERSION)
		f = open(total_report, "w")
		f.write(template.serialize())
		f.close()
Ejemplo n.º 16
0
def test_import_and_expand():
    try:
        del sys.modules['test.context']
    except KeyError:
        pass
    kid.disable_import()
    try:
        import test.context as c
    except ImportError:
        c = None
    assert c is None
    kid.enable_import()
    import test.context as c
    kid.disable_import()
    C = c.Template
    out = joinpath(output_dir, 'context.out')
    t = C(foo=10, bar='bla bla')
    it = t.pull()
    for e in it:
        pass
    t.write(out)
    check_xml_file(out)
Ejemplo n.º 17
0
def test_import_and_expand():
    try:
        del sys.modules['test.context']
    except KeyError:
        pass
    kid.disable_import()
    try:
        import test.context as c
    except ImportError:
        c = None
    assert c is None
    kid.enable_import()
    import test.context as c
    kid.disable_import()
    C = c.Template
    out = joinpath(output_dir, 'context.out')
    t = C(foo=10, bar='bla bla')
    it = t.pull()
    for e in it:
        pass
    t.write(out)
    check_xml_file(out)
Ejemplo n.º 18
0
def test_enable_import_with_ext():
    """Using exts any file extension can be importable."""
    ext = ".html,.kid.html"
    sys.path.insert(0, tmpdir)
    try:
        raises(ImportError, "import test_suffixes1")
        raises(ImportError, "import test_suffixes2")
        raises(ImportError, "import test_suffixes3")
        kid.enable_import(ext=ext)
        dest = joinpath(tmpdir, "test_suffixes1.kid")
        copyfile(tfile, dest)
        import test_suffixes1  # *.kid files are always importable
        dest = joinpath(tmpdir, "test_suffixes2.html")
        copyfile(tfile, dest)
        import test_suffixes2
        dest = joinpath(tmpdir, "test_suffixes3.kid.html")
        copyfile(tfile, dest)
        import test_suffixes3
        dest = joinpath(tmpdir, "test_suffixes4.xhtml")
        copyfile(tfile, dest)
        raises(ImportError, "import test_suffixes4")
        kid.disable_import()
    finally:
        sys.path.remove(tmpdir)
Ejemplo n.º 19
0
#
# Copyright (c) SAS Institute Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import kid
kid.enable_import()
Ejemplo n.º 20
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
Filename:   index.py
Authors:    Han Lin Yap and Alexander Östman
Course:     TDP003 - Projekt: Egna datormiljön
Date:       HT09   - 2009-10-07
Program:    LiU/IP1
Desciption: This is the startpage. 
"""


import kid
import os
from config import *
kid.enable_import(path=os.path.dirname(__file__) + "/" + "templates")

def index():
    template = kid.Template(name='home', 
                            headline=get_text('startpage'),
                            title=get_text('sitename'),
                            **common_language[DEFAULT_LANGUAGE])
    template.cache = False

    return template.serialize(output='xhtml-strict')
Ejemplo n.º 21
0
#! -*- coding: utf-8 -*- 		 
# Returns a project with specified id 
# Whenever exceptions is caught the user is redirected to a page telling a error has occured.
import site
import kid
import os
import sys
import data
path = os.getcwd() + "/" 
sys.path.append(path)
kid.enable_import(path=path)    

def index(id):
    try:
        id = int(id)
    except ValueError:
        pass        
    project = data.lookup_project(id)[1]
    if project is not None:
        template = kid.Template(file=os.path.dirname(__file__) + os.sep + 'templates' + os.sep + 'project',project_id=project)

    else:
        template = kid.Template(file=os.path.dirname(__file__) + os.sep + 'templates' + os.sep + 'error',project_id=project)
    template.cache = False
    site.cache = False
    return template.serialize(output='xhtml-strict')
Ejemplo n.º 22
0
		add_pos = sorted(scores.values(),cmp=lambda x,y: cmp(x[1], y[1]))[0][1]
		for motif in members:
			score, pos, strand = scores[motif]
			add = pos - add_pos
				
			if strand in [1,"+"]:
				pass
			else:
				#print "RC %s" % motif.id
				rc = motif.rc()
				rc.id = motif.id
				motif = rc
			#print "%s\t%s" % (motif.id, add)	
			motif.to_img(os.path.join(outdir, "%s.png" % motif.id.replace(" ", "_")), format="PNG", add_left=add)
	ids[-1][2] = [dict([("src", "%s.png" % motif.id.replace(" ", "_")), ("alt", motif.id.replace(" ", "_"))]) for motif in members]

kid.enable_import()
template_file = os.path.join(sys.prefix, "share/gimmemotifs/templates/cluster_template.kid")
template = kid.Template(file=template_file, motifs=ids)
f = open(os.path.join(outdir, "cluster_report.html"), "w")
f.write(template.serialize())
f.close()

f = open(os.path.join(outdir, "clustered_motifs.pwm"), "w")
if len(clusters) == 1 and len(clusters[0][1]) == 1:
	f.write("%s\n" % clusters[0][0].to_pwm())
else:
	for motif in tree.get_clustered_motifs():
		f.write("%s\n" % motif.to_pwm())
f.close()
Ejemplo n.º 23
0
def test_import_hook():
    kid.enable_import()
    import test.test_if
    assert_template_interface(test.test_if)
    assert sys.modules.has_key('test.test_if')
    kid.disable_import()
Ejemplo n.º 24
0
def cluster(args):

    revcomp = not args.single

    outdir = os.path.abspath(args.outdir)
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    trim_ic = 0.2
    clusters = []
    motifs = pwmfile_to_motifs(args.inputfile)
    if len(motifs) == 1:
        clusters = [[motifs[0], motifs]]
    else:
        tree = cluster_motifs(args.inputfile, "total", "wic", "mean", True, threshold=args.threshold, include_bg=True)
        clusters = tree.getResult()
    
    ids = []
    mc = MotifComparer()

    sys.stderr.write("Creating images\n")
    for cluster,members in clusters:
        cluster.trim(trim_ic)
        cluster.to_img(os.path.join(outdir,"%s.png" % cluster.id), format="PNG")
        ids.append([cluster.id, {"src":"%s.png" % cluster.id},[]])
        if len(members) > 1:
            scores = {}
            for motif in members:
                scores[motif] =  mc.compare_motifs(cluster, motif, "total", "wic", "mean", pval=True)    
            add_pos = sorted(scores.values(),cmp=lambda x,y: cmp(x[1], y[1]))[0][1]
            for motif in members:
                score, pos, strand = scores[motif]
                add = pos - add_pos
                
                if strand in [1,"+"]:
                    pass
                else:
                    #print "RC %s" % motif.id
                    rc = motif.rc()
                    rc.id = motif.id
                    motif = rc
                #print "%s\t%s" % (motif.id, add)    
                motif.to_img(os.path.join(outdir, "%s.png" % motif.id.replace(" ", "_")), format="PNG", add_left=add)
        ids[-1][2] = [dict([("src", "%s.png" % motif.id.replace(" ", "_")), ("alt", motif.id.replace(" ", "_"))]) for motif in members]
    
    kid.enable_import()
    template_file = os.path.join(sys.prefix, "share/gimmemotifs/templates/cluster_template.kid")
    template = kid.Template(file=template_file, motifs=ids)
    f = open(os.path.join(outdir, "cluster_report.html"), "w")
    f.write(template.serialize())
    f.close()

    f = open(os.path.join(outdir, "cluster_key.txt"), "w")
    for id in ids:
        f.write("%s\t%s\n" % (id[0], ",".join([x["alt"] for x in id[2]])))
    f.close()

    f = open(os.path.join(outdir, "clustered_motifs.pwm"), "w")
    if len(clusters) == 1 and len(clusters[0][1]) == 1:
        f.write("%s\n" % clusters[0][0].to_pwm())
    else:
        for motif in tree.get_clustered_motifs():
            f.write("%s\n" % motif.to_pwm())
    f.close()