diff --git a/gtpython/gt/__init__.py b/gtpython/gt/__init__.py index d332946..e37c160 100644 --- a/gtpython/gt/__init__.py +++ b/gtpython/gt/__init__.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -18,11 +20,11 @@ import sys try: - import ctypes + import ctypes except ImportError: - sys.stderr.write("The ctypes package could not be found. ") - sys.stderr.write("Please make sure it can be imported by Python.") - sys.exit(1) + sys.stderr.write("The ctypes package could not be found. ") + sys.stderr.write("Please make sure it can be imported by Python.") + sys.exit(1) from annotationsketch import * from core import * diff --git a/gtpython/gt/annotationsketch/__init__.py b/gtpython/gt/annotationsketch/__init__.py index 2a15c0e..243c409 100644 --- a/gtpython/gt/annotationsketch/__init__.py +++ b/gtpython/gt/annotationsketch/__init__.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -32,7 +34,9 @@ Block.register(gtlib) CanvasCairoFile.register(gtlib) CustomTrack.register(gtlib) Diagram.register(gtlib) + #DiagramFromArray.register(gtlib) + FeatureIndex.register(gtlib) Graphics.register(gtlib) ImageInfo.register(gtlib) diff --git a/gtpython/gt/annotationsketch/block.py b/gtpython/gt/annotationsketch/block.py index 0f4609a..069c41f 100644 --- a/gtpython/gt/annotationsketch/block.py +++ b/gtpython/gt/annotationsketch/block.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -22,81 +24,90 @@ from gt.core.gtstr import Str from gt.extended.strand import strandchars from gt.extended.feature_node import FeatureNode + class Block(object): - def __init__(self, ptr): - if ptr == 0 or ptr == None: - gterror("Block pointer cannot be NULL (was: " + str(ptr) + ")") - self.block = gtlib.gt_block_ref(ptr) - self._as_parameter_ = self.block - - def __del__(self): - try: - gtlib.gt_block_delete(self.block) - except AttributeError: - pass - - def get_range(self): - r = gtlib.gt_block_get_range(self.block) - return (r.start, r.end) - - def get_type(self): - return gtlib.gt_block_get_type(self.block) - - def has_only_one_fullsize_element(self): - return (gtlib.gt_block_has_only_one_fullsize_element(self.block) == 1) - - def merge(self, block2): - gtlib.gt_block_merge(self.block, block2) - - def clone(self): - return Block(gtlib.gt_block_clone(self.block)) - - def get_caption(self): - s = Str(gtlib.gt_block_get_caption(self.block)) - return str(s) - - def set_strand(self, strand): - if not strand in strandchars: - gterror("Invalid strand '%s' -- must be one of %s" \ - % (strand, strandchars)) - gtlib.gt_block_set_strand(self.block, strandchars.index(strand)) - - def get_strand(self): - return strandchars[gtlib.gt_block_get_strand(self.block)] - - def get_top_level_feature(self): - f = gtlib.gt_block_get_top_level_feature(self.block) - if f != 0: - return FeatureNode.create_from_ptr(f, True) - else: - return None - - def get_size(self): - return gtlib.gt_block_get_size(self.block) - - def from_param(cls, obj): - if not isinstance(obj, Block): - raise TypeError, "argument must be a Block" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def register(cls, gtlib): - from ctypes import c_char_p, c_void_p, c_int - gtlib.gt_block_get_range.restype = Range - gtlib.gt_block_get_range.argtypes = [c_void_p] - gtlib.gt_block_get_type.restype = c_char_p - gtlib.gt_block_get_type.argtypes = [c_void_p] - gtlib.gt_block_has_only_one_fullsize_element.argtypes = [c_void_p] - gtlib.gt_block_merge.argtypes = [c_void_p, Block] - gtlib.gt_block_clone.restype = c_void_p - gtlib.gt_block_clone.argtypes = [c_void_p] - gtlib.gt_block_set_caption_visibility.argtypes = [c_void_p, c_int] - gtlib.gt_block_caption_is_visible.argtypes = [c_void_p] - gtlib.gt_block_get_caption.restype = c_void_p - gtlib.gt_block_get_caption.argtypes = [c_void_p] - gtlib.gt_block_set_strand.argtypes = [c_void_p, c_int] - gtlib.gt_block_get_strand.restype = c_int - gtlib.gt_block_get_strand.argtypes = [c_void_p] - gtlib.gt_block_get_top_level_feature.restype = c_void_p - gtlib.gt_block_get_top_level_feature.argtypes = [c_void_p] - register = classmethod(register) + + def __init__(self, ptr): + if ptr == 0 or ptr == None: + gterror("Block pointer cannot be NULL (was: " + str(ptr) + + ")") + self.block = gtlib.gt_block_ref(ptr) + self._as_parameter_ = self.block + + def __del__(self): + try: + gtlib.gt_block_delete(self.block) + except AttributeError: + pass + + def get_range(self): + r = gtlib.gt_block_get_range(self.block) + return (r.start, r.end) + + def get_type(self): + return gtlib.gt_block_get_type(self.block) + + def has_only_one_fullsize_element(self): + return gtlib.gt_block_has_only_one_fullsize_element(self.block) == \ + 1 + + def merge(self, block2): + gtlib.gt_block_merge(self.block, block2) + + def clone(self): + return Block(gtlib.gt_block_clone(self.block)) + + def get_caption(self): + s = Str(gtlib.gt_block_get_caption(self.block)) + return str(s) + + def set_strand(self, strand): + if not strand in strandchars: + gterror("Invalid strand '%s' -- must be one of %s" % (strand, + strandchars)) + gtlib.gt_block_set_strand(self.block, strandchars.index(strand)) + + def get_strand(self): + return strandchars[gtlib.gt_block_get_strand(self.block)] + + def get_top_level_feature(self): + f = gtlib.gt_block_get_top_level_feature(self.block) + if f != 0: + return FeatureNode.create_from_ptr(f, True) + else: + return None + + def get_size(self): + return gtlib.gt_block_get_size(self.block) + + def from_param(cls, obj): + if not isinstance(obj, Block): + raise TypeError, "argument must be a Block" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def register(cls, gtlib): + from ctypes import c_char_p, c_void_p, c_int + gtlib.gt_block_get_range.restype = Range + gtlib.gt_block_get_range.argtypes = [c_void_p] + gtlib.gt_block_get_type.restype = c_char_p + gtlib.gt_block_get_type.argtypes = [c_void_p] + gtlib.gt_block_has_only_one_fullsize_element.argtypes = [c_void_p] + gtlib.gt_block_merge.argtypes = [c_void_p, Block] + gtlib.gt_block_clone.restype = c_void_p + gtlib.gt_block_clone.argtypes = [c_void_p] + gtlib.gt_block_set_caption_visibility.argtypes = [c_void_p, + c_int] + gtlib.gt_block_caption_is_visible.argtypes = [c_void_p] + gtlib.gt_block_get_caption.restype = c_void_p + gtlib.gt_block_get_caption.argtypes = [c_void_p] + gtlib.gt_block_set_strand.argtypes = [c_void_p, c_int] + gtlib.gt_block_get_strand.restype = c_int + gtlib.gt_block_get_strand.argtypes = [c_void_p] + gtlib.gt_block_get_top_level_feature.restype = c_void_p + gtlib.gt_block_get_top_level_feature.argtypes = [c_void_p] + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/canvas.py b/gtpython/gt/annotationsketch/canvas.py index 2092cf2..ef65c0a 100644 --- a/gtpython/gt/annotationsketch/canvas.py +++ b/gtpython/gt/annotationsketch/canvas.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -21,53 +23,65 @@ from gt.annotationsketch.image_info import ImageInfo from gt.core.error import Error, gterror from gt.core.gtstr import Str + class Canvas: - def __init__(self, *args): - raise NotImplementedError, "Please call the constructor of a " \ - "Canvas implementation." - - def __del__(self): - try: - gtlib.gt_canvas_delete(self.canvas) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, Canvas): - raise TypeError, "argument must be a Canvas" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, *args): + raise NotImplementedError, \ + 'Please call the constructor of a Canvas implementation.' + + def __del__(self): + try: + gtlib.gt_canvas_delete(self.canvas) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, Canvas): + raise TypeError, "argument must be a Canvas" + return obj._as_parameter_ + + from_param = classmethod(from_param) + class CanvasCairoFile(Canvas): - def __init__(self, style, width, height, ii = None): - self.canvas = gtlib.gt_canvas_cairo_file_new(style, 1, width, height, ii) - self._as_parameter_ = self.canvas - - def from_param(cls, obj): - if not isinstance(obj, CanvasCairoFile): - raise TypeError, "argument must be a CanvasCairoFile" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def to_file(self, filename): - err = Error() - rval = gtlib.gt_canvas_cairo_file_to_file(self.canvas, filename, err) - if rval != 0: - gterror(err) - - def to_stream(self): - from ctypes import string_at - str = Str(None) - gtlib.gt_canvas_cairo_file_to_stream(self.canvas, str) - return string_at(str.get_mem(), str.length()) - - def register(cls, gtlib): - from ctypes import c_char_p, c_void_p, c_ulong, c_int - gtlib.gt_canvas_cairo_file_to_file.restype = c_int - gtlib.gt_canvas_cairo_file_to_file.argtypes = [c_void_p, c_char_p, Error] - gtlib.gt_canvas_cairo_file_to_stream.restype = c_char_p - gtlib.gt_canvas_cairo_file_to_stream.argtypes = [c_void_p, Str] - gtlib.gt_canvas_cairo_file_new.restype = c_void_p - gtlib.gt_canvas_cairo_file_new.argtypes = [Style, c_int, c_ulong, c_ulong, \ - ImageInfo] - register = classmethod(register) + + def __init__(self, style, width, height, ii=None): + self.canvas = gtlib.gt_canvas_cairo_file_new(style, 1, width, + height, ii) + self._as_parameter_ = self.canvas + + def from_param(cls, obj): + if not isinstance(obj, CanvasCairoFile): + raise TypeError, "argument must be a CanvasCairoFile" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def to_file(self, filename): + err = Error() + rval = gtlib.gt_canvas_cairo_file_to_file(self.canvas, filename, + err) + if rval != 0: + gterror(err) + + def to_stream(self): + from ctypes import string_at + str = Str(None) + gtlib.gt_canvas_cairo_file_to_stream(self.canvas, str) + return string_at(str.get_mem(), str.length()) + + def register(cls, gtlib): + from ctypes import c_char_p, c_void_p, c_ulong, c_int + gtlib.gt_canvas_cairo_file_to_file.restype = c_int + gtlib.gt_canvas_cairo_file_to_file.argtypes = [c_void_p, + c_char_p, Error] + gtlib.gt_canvas_cairo_file_to_stream.restype = c_char_p + gtlib.gt_canvas_cairo_file_to_stream.argtypes = [c_void_p, Str] + gtlib.gt_canvas_cairo_file_new.restype = c_void_p + gtlib.gt_canvas_cairo_file_new.argtypes = [Style, c_int, c_ulong, + c_ulong, ImageInfo] + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/color.py b/gtpython/gt/annotationsketch/color.py index d50531c..d4388a1 100644 --- a/gtpython/gt/annotationsketch/color.py +++ b/gtpython/gt/annotationsketch/color.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -18,8 +20,10 @@ from gt.dlload import gtlib from ctypes import Structure, c_double + class Color(Structure): - _fields_ = [("red", c_double), - ("green", c_double), - ("blue", c_double), - ("alpha", c_double)] + + _fields_ = [("red", c_double), ("green", c_double), ("blue", + c_double), ("alpha", c_double)] + + diff --git a/gtpython/gt/annotationsketch/custom_track.py b/gtpython/gt/annotationsketch/custom_track.py index 40d9c9c..2e2d2b0 100644 --- a/gtpython/gt/annotationsketch/custom_track.py +++ b/gtpython/gt/annotationsketch/custom_track.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,52 +25,64 @@ from gt.annotationsketch.graphics import Graphics from gt.core.error import Error, gterror from gt.core.gtrange import Range -RenderFunc = CFUNCTYPE(c_int, c_void_p, c_ulong, Range, c_void_p, c_void_p) -TitleFunc = CFUNCTYPE(c_char_p, c_void_p) +RenderFunc = CFUNCTYPE(c_int, c_void_p, c_ulong, Range, c_void_p, + c_void_p) +TitleFunc = CFUNCTYPE(c_char_p, c_void_p) HeightFunc = CFUNCTYPE(c_ulong, c_void_p) -FreeFunc = CFUNCTYPE(c_void_p, c_void_p) +FreeFunc = CFUNCTYPE(c_void_p, c_void_p) + class CustomTrack(object): - def __init__(self): + + def __init__(self): + # create callbacks to C script wrapper - def get_title_w(ptr): - return self.get_title() - self.get_title_cb = TitleFunc(get_title_w) - def get_height_w(ptr): - return self.get_height() - self.get_height_cb = HeightFunc(get_height_w) - def render_w(graphics, ypos, rng, sty, err): - g = Graphics(graphics) - s = Style(sty) - e = Error(err) - return self.render(g, ypos, rng, s, e) - self.render_cb = RenderFunc(render_w) - def free_w(ptr): - self.free() - self.free_cb = FreeFunc(free_w) - self.ctt = gtlib.gt_custom_track_script_wrapper_new(self.render_cb, \ - self.get_height_cb, \ - self.get_title_cb, \ - self.free_cb) - self._as_parameter_ = self.ctt - - def __del__(self): - try: - gtlib.gt_custom_track_delete(self.ctt) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, CustomTrack): - raise TypeError, "argument must be a CustomTrack" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def register(cls, gtlib): - from ctypes import c_char_p, c_void_p, c_int, POINTER - gtlib.gt_custom_track_script_wrapper_new.restype = c_void_p - gtlib.gt_custom_track_script_wrapper_new.argtypes = [RenderFunc, \ - HeightFunc, \ - TitleFunc, \ - FreeFunc] - register = classmethod(register) + + def get_title_w(ptr): + return self.get_title() + + self.get_title_cb = TitleFunc(get_title_w) + + def get_height_w(ptr): + return self.get_height() + + self.get_height_cb = HeightFunc(get_height_w) + + def render_w(graphics, ypos, rng, sty, err): + g = Graphics(graphics) + s = Style(sty) + e = Error(err) + return self.render(g, ypos, rng, s, e) + + self.render_cb = RenderFunc(render_w) + + def free_w(ptr): + self.free() + + self.free_cb = FreeFunc(free_w) + self.ctt = gtlib.gt_custom_track_script_wrapper_new(self.render_cb, + self.get_height_cb, self.get_title_cb, self.free_cb) + self._as_parameter_ = self.ctt + + def __del__(self): + try: + gtlib.gt_custom_track_delete(self.ctt) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, CustomTrack): + raise TypeError, "argument must be a CustomTrack" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def register(cls, gtlib): + from ctypes import c_char_p, c_void_p, c_int, POINTER + gtlib.gt_custom_track_script_wrapper_new.restype = c_void_p + gtlib.gt_custom_track_script_wrapper_new.argtypes = [RenderFunc, + HeightFunc, TitleFunc, FreeFunc] + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/custom_track_example.py b/gtpython/gt/annotationsketch/custom_track_example.py index 3ba8614..2fda7a6 100644 --- a/gtpython/gt/annotationsketch/custom_track_example.py +++ b/gtpython/gt/annotationsketch/custom_track_example.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -19,21 +21,25 @@ from gt.annotationsketch.custom_track import CustomTrack from gt.annotationsketch.color import Color from gt.core.gtrange import Range + class CustomTrackExample(CustomTrack): - def __init__(self): - super(CustomTrackExample, self).__init__() - - def get_height(self): - return 50 - - def get_title(self): - return "Sample track drawn by a Python script" - - def render(self, graphics, ypos, rng, style, error): - from random import random - data = [] - for i in range(0, 120): - data.append(random()) - graphics.draw_curve_data(graphics.get_xmargins(), ypos, Color(0,0,1,.6), \ - data, 120, Range(0, 1), 40) - return 0 + + def __init__(self): + super(CustomTrackExample, self).__init__() + + def get_height(self): + return 50 + + def get_title(self): + return "Sample track drawn by a Python script" + + def render(self, graphics, ypos, rng, style, error): + from random import random + data = [] + for i in range(0, 120): + data.append(random()) + graphics.draw_curve_data(graphics.get_xmargins(), ypos, Color(0, + 0, 1, .6), data, 120, Range(0, 1), 40) + return 0 + + diff --git a/gtpython/gt/annotationsketch/diagram.py b/gtpython/gt/annotationsketch/diagram.py index 4175ddd..b6c97a5 100644 --- a/gtpython/gt/annotationsketch/diagram.py +++ b/gtpython/gt/annotationsketch/diagram.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -30,73 +32,84 @@ from gt.extended.feature_node import FeatureNode TrackSelectorFunc = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_void_p) + class Diagram: - def from_array(arr, rng, style): - from ctypes import byref, sizeof - if rng.start > rng.end: - gterror("range.start > range.end") - gtarr = Array.create(sizeof(c_void_p), False) - for i in arr: - if not isinstance(i, FeatureNode): - gterror("Diagram array must only contain FeatureNodes!") - gtarr.add(i) - diagram = gtlib.gt_diagram_new_from_array(gtarr, byref(rng), \ - style) - return Diagram(diagram) - from_array = staticmethod(from_array) - - def from_index(feature_index, seqid, rng, style): - from ctypes import byref - err = Error() - if rng.start > rng.end: - gterror("range.start > range.end") - diagram = gtlib.gt_diagram_new(feature_index, seqid, byref(rng), \ - style, err) - if err.is_set(): - gterror(err) - return Diagram(diagram) - from_index = staticmethod(from_index) - - def __init__(self, ptr): - self.diagram = ptr - self._as_parameter_ = self.diagram - - def __del__(self): - try: - gtlib.gt_diagram_delete(self.diagram) - except AttributeError: - pass - - def set_track_selector_func(self, func): - def trackselector(block_ptr, string_ptr, data_ptr): - b = Block(block_ptr) - string = Str(string_ptr) - ret = func(b) - if not ret: - gterror("Track selector callback function must return a string!") - string.append_cstr(ret) - self.tsf_cb = TrackSelectorFunc(trackselector) - self.tsf = trackselector - gtlib.gt_diagram_set_track_selector_func(self.diagram, self.tsf_cb) - - def add_custom_track(self, ct): - gtlib.gt_diagram_add_custom_track(self.diagram, ct) - - def from_param(cls, obj): - if not isinstance(obj, Diagram): - raise TypeError, "argument must be a Diagram" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def register(cls, gtlib): - from ctypes import c_char_p, c_void_p, POINTER - gtlib.gt_diagram_new.restype = c_void_p - gtlib.gt_diagram_new.argtypes = [FeatureIndex, c_char_p, POINTER(Range), \ - Style, Error] - gtlib.gt_diagram_set_track_selector_func.argtypes = [c_void_p, \ - TrackSelectorFunc] - gtlib.gt_diagram_add_custom_track.argtypes = [c_void_p, \ - CustomTrack] - gtlib.gt_diagram_new_from_array.restype = c_void_p - gtlib.gt_diagram_new_from_array.argtypes = [Array, POINTER(Range), Style] - register = classmethod(register) + + def from_array(arr, rng, style): + from ctypes import byref, sizeof + if rng.start > rng.end: + gterror("range.start > range.end") + gtarr = Array.create(sizeof(c_void_p), False) + for i in arr: + if not isinstance(i, FeatureNode): + gterror("Diagram array must only contain FeatureNodes!") + gtarr.add(i) + diagram = gtlib.gt_diagram_new_from_array(gtarr, byref(rng), + style) + return Diagram(diagram) + + from_array = staticmethod(from_array) + + def from_index(feature_index, seqid, rng, style): + from ctypes import byref + err = Error() + if rng.start > rng.end: + gterror("range.start > range.end") + diagram = gtlib.gt_diagram_new(feature_index, seqid, byref(rng), + style, err) + if err.is_set(): + gterror(err) + return Diagram(diagram) + + from_index = staticmethod(from_index) + + def __init__(self, ptr): + self.diagram = ptr + self._as_parameter_ = self.diagram + + def __del__(self): + try: + gtlib.gt_diagram_delete(self.diagram) + except AttributeError: + pass + + def set_track_selector_func(self, func): + + def trackselector(block_ptr, string_ptr, data_ptr): + b = Block(block_ptr) + string = Str(string_ptr) + ret = func(b) + if not ret: + gterror("Track selector callback function must return a string!") + string.append_cstr(ret) + + self.tsf_cb = TrackSelectorFunc(trackselector) + self.tsf = trackselector + gtlib.gt_diagram_set_track_selector_func(self.diagram, self.tsf_cb) + + def add_custom_track(self, ct): + gtlib.gt_diagram_add_custom_track(self.diagram, ct) + + def from_param(cls, obj): + if not isinstance(obj, Diagram): + raise TypeError, "argument must be a Diagram" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def register(cls, gtlib): + from ctypes import c_char_p, c_void_p, POINTER + gtlib.gt_diagram_new.restype = c_void_p + gtlib.gt_diagram_new.argtypes = [FeatureIndex, c_char_p, POINTER(Range), + Style, Error] + gtlib.gt_diagram_set_track_selector_func.argtypes = [c_void_p, + TrackSelectorFunc] + gtlib.gt_diagram_add_custom_track.argtypes = [c_void_p, + CustomTrack] + gtlib.gt_diagram_new_from_array.restype = c_void_p + gtlib.gt_diagram_new_from_array.argtypes = [Array, POINTER(Range), + Style] + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/feature_index.py b/gtpython/gt/annotationsketch/feature_index.py index f75a35d..9c7d8a8 100644 --- a/gtpython/gt/annotationsketch/feature_index.py +++ b/gtpython/gt/annotationsketch/feature_index.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -22,100 +24,107 @@ from gt.core.gtrange import Range from gt.core.str_array import StrArray from gt.extended.feature_node import FeatureNode + class FeatureIndex: - def __init__(self, *args): - raise NotImplementedError, "Please call the constructor of a " \ - "FeatureIndex implementation." - - def __del__(self): - try: - gtlib.gt_feature_index_delete(self.fi) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, FeatureIndex): - raise TypeError, "argument must be a FeatureIndex" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def get_features_for_seqid(self, seqid): - rval = gtlib.gt_feature_index_get_features_for_seqid(self.fi, seqid) - if rval: - a = Array(rval, True) - result = [] - for i in range(a.size()): - fptr = gtlib.gt_genome_node_ref(a.get(i)) - result.append(FeatureNode.create_from_ptr(fptr)) - return result - else: - return None - - def add_gff3file(self, filename): - err = Error() - rval = gtlib.gt_feature_index_add_gff3file(self.fi, filename, err) - if rval != 0: - gterror(err) - - def get_first_seqid(self): - return gtlib.gt_feature_index_get_first_seqid(self.fi) - - def get_seqids(self): - result = [] - stra = StrArray(gtlib.gt_feature_index_get_seqids(self.fi)) - for i in range(stra.size()): - result.append(stra.get(i)) - return result - - def get_range_for_seqid(self, seqid): - from ctypes import byref - if (gtlib.gt_feature_index_has_seqid(self.fi, seqid) == 0): - gterror("feature_index does not contain seqid") - range = Range() - gtlib.gt_feature_index_get_range_for_seqid(self.fi, byref(range), seqid) - return range - - def get_features_for_range(self, start, end, seqid): - from ctypes import byref - a = Array.create() - err = Error() - rng = Range(start, end) - rval = gtlib.gt_feature_index_get_features_for_range(self.fi, a, \ - seqid, byref(rng),\ - err) - if rval != 0: - gterror(err) - result = [] - for i in range(a.size()): - fptr = gtlib.gt_genome_node_ref(a.get(i)) - result.append(FeatureNode.create_from_ptr(fptr)) - return result - - def register(cls, gtlib): - from ctypes import c_char_p, c_void_p, c_int, POINTER - gtlib.gt_feature_index_get_features_for_seqid.restype = c_void_p - gtlib.gt_feature_index_add_gff3file.argtypes = [c_void_p, c_char_p, \ - Error] - gtlib.gt_feature_index_get_first_seqid.restype = c_char_p - gtlib.gt_feature_index_get_seqids.restype = c_void_p - gtlib.gt_feature_index_has_seqid.argtypes = [c_void_p, c_char_p] - gtlib.gt_feature_index_get_range_for_seqid.argtypes = [c_void_p, \ - POINTER(Range), \ - c_char_p] - gtlib.gt_feature_index_get_features_for_range.argtypes = [c_void_p, Array, \ - c_char_p, \ - POINTER(Range), \ - Error] - register = classmethod(register) + + def __init__(self, *args): + raise NotImplementedError, \ + 'Please call the constructor of a FeatureIndex implementation.' + + def __del__(self): + try: + gtlib.gt_feature_index_delete(self.fi) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, FeatureIndex): + raise TypeError, "argument must be a FeatureIndex" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def get_features_for_seqid(self, seqid): + rval = gtlib.gt_feature_index_get_features_for_seqid(self.fi, + seqid) + if rval: + a = Array(rval, True) + result = [] + for i in range(a.size()): + fptr = gtlib.gt_genome_node_ref(a.get(i)) + result.append(FeatureNode.create_from_ptr(fptr)) + return result + else: + return None + + def add_gff3file(self, filename): + err = Error() + rval = gtlib.gt_feature_index_add_gff3file(self.fi, filename, + err) + if rval != 0: + gterror(err) + + def get_first_seqid(self): + return gtlib.gt_feature_index_get_first_seqid(self.fi) + + def get_seqids(self): + result = [] + stra = StrArray(gtlib.gt_feature_index_get_seqids(self.fi)) + for i in range(stra.size()): + result.append(stra.get(i)) + return result + + def get_range_for_seqid(self, seqid): + from ctypes import byref + if gtlib.gt_feature_index_has_seqid(self.fi, seqid) == 0: + gterror("feature_index does not contain seqid") + range = Range() + gtlib.gt_feature_index_get_range_for_seqid(self.fi, byref(range), + seqid) + return range + + def get_features_for_range(self, start, end, seqid): + from ctypes import byref + a = Array.create() + err = Error() + rng = Range(start, end) + rval = gtlib.gt_feature_index_get_features_for_range(self.fi, a, + seqid, byref(rng), err) + if rval != 0: + gterror(err) + result = [] + for i in range(a.size()): + fptr = gtlib.gt_genome_node_ref(a.get(i)) + result.append(FeatureNode.create_from_ptr(fptr)) + return result + + def register(cls, gtlib): + from ctypes import c_char_p, c_void_p, c_int, POINTER + gtlib.gt_feature_index_get_features_for_seqid.restype = c_void_p + gtlib.gt_feature_index_add_gff3file.argtypes = [c_void_p, + c_char_p, Error] + gtlib.gt_feature_index_get_first_seqid.restype = c_char_p + gtlib.gt_feature_index_get_seqids.restype = c_void_p + gtlib.gt_feature_index_has_seqid.argtypes = [c_void_p, c_char_p] + gtlib.gt_feature_index_get_range_for_seqid.argtypes = [c_void_p, + POINTER(Range), c_char_p] + gtlib.gt_feature_index_get_features_for_range.argtypes = [c_void_p, + Array, c_char_p, POINTER(Range), Error] + + register = classmethod(register) class FeatureIndexMemory(FeatureIndex): - def __init__(self): - self.fi = gtlib.gt_feature_index_memory_new() - self._as_parameter_ = self.fi - - def from_param(cls, obj): - if not isinstance(obj, FeatureIndexMemory): - raise TypeError, "argument must be a FeatureIndexMemory" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self): + self.fi = gtlib.gt_feature_index_memory_new() + self._as_parameter_ = self.fi + + def from_param(cls, obj): + if not isinstance(obj, FeatureIndexMemory): + raise TypeError, "argument must be a FeatureIndexMemory" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + diff --git a/gtpython/gt/annotationsketch/feature_stream.py b/gtpython/gt/annotationsketch/feature_stream.py index 2b8d8c8..44ead78 100644 --- a/gtpython/gt/annotationsketch/feature_stream.py +++ b/gtpython/gt/annotationsketch/feature_stream.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -19,18 +21,26 @@ from gt.dlload import gtlib from gt.annotationsketch.feature_index import FeatureIndex from gt.extended.genome_stream import GenomeStream + class FeatureStream(GenomeStream): - def __init__(self, genome_stream, feature_index): - self.gs = gtlib.gt_feature_stream_new(genome_stream, feature_index) - self._as_parameter_ = self.gs - - def from_param(cls, obj): - if not isinstance(obj, FeatureStream): - raise TypeError, "argument must be a FeatureStream" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def register(cls, gtlib): - gtlib.gt_feature_stream_new.restype = c_void_p - gtlib.gt_feature_stream_new.argtypes = [GenomeStream, FeatureIndex] - register = classmethod(register) + + def __init__(self, genome_stream, feature_index): + self.gs = gtlib.gt_feature_stream_new(genome_stream, + feature_index) + self._as_parameter_ = self.gs + + def from_param(cls, obj): + if not isinstance(obj, FeatureStream): + raise TypeError, "argument must be a FeatureStream" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def register(cls, gtlib): + gtlib.gt_feature_stream_new.restype = c_void_p + gtlib.gt_feature_stream_new.argtypes = [GenomeStream, + FeatureIndex] + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/graphics.py b/gtpython/gt/annotationsketch/graphics.py index d6ac8c4..b88131c 100644 --- a/gtpython/gt/annotationsketch/graphics.py +++ b/gtpython/gt/annotationsketch/graphics.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -24,7 +26,7 @@ from gt.core.gtstr import Str GRAPHICS_PDF = 0 GRAPHICS_PNG = 1 -GRAPHICS_PS = 2 +GRAPHICS_PS = 2 GRAPHICS_SVG = 3 ARROW_LEFT = 0 @@ -32,234 +34,374 @@ ARROW_RIGHT = 1 ARROW_BOTH = 2 ARROW_NONE = 3 + class Graphics: - def __init__(self, p): - self.g = p - self.own = False - self._as_parameter_ = self.g - - def __del__(self): - if self.own: - try: - gtlib.gt_graphics_delete(self.g) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, Graphics): - raise TypeError, "argument must be a Graphics" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def draw_text(self, x, y, text): - gtlib.gt_graphics_draw_text(self.g, x, y, text) - - def draw_text_centered(self, x, y, text): - gtlib.gt_graphics_draw_text_centered(self.g, x, y, text) - - def draw_text_right(self, x, y, text): - gtlib.gt_graphics_draw_text_right(self.g, x, y, text) - - def draw_colored_text(self, x, y, color, text): - gtlib.gt_graphics_draw_colored_text(self.g, x, y, color, text) - - def get_image_height(self): - return gtlib.gt_graphics_get_image_height(self.g) - - def get_image_width(self): - return gtlib.gt_graphics_get_image_width(self.g) - - def get_text_height(self): - return gtlib.gt_graphics_get_text_height(self.g) - - def get_text_width(self, txt): - return gtlib.gt_graphics_get_text_width(self.g, txt) - - def set_margins(self, xmargs, ymargs): - gtlib.gt_graphics_set_margins(self.g, xmargs, ymargs) - - def get_xmargins(self): - return gtlib.gt_graphics_get_xmargins(self.g) - - def get_ymargins(self): - return gtlib.gt_graphics_get_ymargins(self.g) - - def draw_line(self, x, y, xto, yto, color, stroke_width): - gtlib.gt_graphics_draw_line(self.g, x, y, xto, yto, color, stroke_width) - - def draw_horizontal_line(self, x, y, color, width, stroke_width): - gtlib.gt_graphics_draw_horizontal_line(self.g, x, y, color, width, \ - stroke_width) - - def draw_vertical_line(self, x, y, color, length, stroke_width): - gtlib.gt_graphics_draw_vertical_line(self.g, x, y, color, length, \ - stroke_width) - - def draw_box(self, x, y, width, height, fillcolor, astatus, awidth, swidth, \ - scolor, dashed): - if dashed: - d_int = 1 - else: - d_int = 0 - gtlib.gt_graphics_draw_box(self.g, x, y, width, height, fillcolor, astatus,\ - awidth, swidth, scolor, d_int) - - def draw_dashes(self, x, y, width, height, astatus, awidth, swidth, scolor): - gtlib.gt_graphics_draw_dashes(self.g, x, y, width, height, astatus, awidth,\ - swidth, scolor) - - def draw_caret(self, x, y, width, height, astatus, awidth, swidth, scolor): - gtlib.gt_graphics_draw_caret(self.g,x, y, width, height, astatus, awidth, \ - swidth, scolor) - - def draw_rectangle(self, x, y, filled, fcolor, stroked, scolor, swidth, \ - width, height): - if filled: - f_int = 1 - else: - f_int = 0 - if stroked: - s_int = 1 - else: - s_int = 0 - gtlib.gt_graphics_draw_rectangle(self.g, x, y, f_int, fcolor, s_int, \ - scolor, swidth, width, height) - - def draw_arrowhead(self, x, y, color, status): - gtlib.gt_graphics_draw_arrowhead(self.g, x, y, color, status) - - def draw_curve_data(self, x, y, color, data, ndata, valrange, height): - from ctypes import c_double - NDblArr = c_double * ndata - cdata = NDblArr() - for i in range(0, ndata): - cdata[i] = data[i] - gtlib.gt_graphics_draw_curve_data(self.g, x, y, color, cdata, ndata, \ - valrange, height) - - def to_file(self, filename): - err = Error() - if gtlib.gt_graphics_save_to_file(self.g, filename, err) < 0: - gterror(err) - - def to_stream(self): - from ctypes import string_at - s = Str(None) - gtlib.gt_graphics_save_to_stream(self.g, s) - return string_at(s.get_mem(), s.length()) - - def get_text_height(self): - return gtlib.gt_graphics_get_text_height(self.g) - - def get_text_width(self, text): - return gtlib.gt_graphics_get_text_width(self.g, text) - - def register(cls, gtlib): - from ctypes import c_char_p, c_void_p, c_int, POINTER, c_double, c_ulong - gtlib.gt_graphics_draw_text.argtypes = [c_void_p, c_double, c_double, \ - c_char_p] - gtlib.gt_graphics_draw_text_right.argtypes = [c_void_p, c_double, c_double,\ - c_char_p] - gtlib.gt_graphics_draw_text_centered.argtypes = [c_void_p, c_double, \ - c_double, c_char_p] - gtlib.gt_graphics_draw_colored_text.argtypes = [c_void_p, c_double, \ - c_double, Color, c_char_p] - gtlib.gt_graphics_get_image_height.restype = c_double - gtlib.gt_graphics_get_image_height.argtypes = [c_void_p] - gtlib.gt_graphics_get_image_width.restype = c_double - gtlib.gt_graphics_get_image_width.argtypes = [c_void_p] - gtlib.gt_graphics_get_text_height.restype = c_double - gtlib.gt_graphics_get_text_height.argtypes = [c_void_p] - gtlib.gt_graphics_get_text_width.restype = c_double - gtlib.gt_graphics_get_text_width.argtypes = [c_void_p, c_char_p] - gtlib.gt_graphics_set_margins.argtypes = [c_void_p, c_double, c_double] - gtlib.gt_graphics_get_xmargins.restype = c_double - gtlib.gt_graphics_get_xmargins.argtypes = [c_void_p] - gtlib.gt_graphics_get_ymargins.restype = c_double - gtlib.gt_graphics_get_ymargins.argtypes = [c_void_p] - gtlib.gt_graphics_draw_vertical_line.argtypes = [c_void_p, c_double, \ - c_double, Color, c_double,\ - c_double] - gtlib.gt_graphics_draw_line.argtypes = [c_void_p, c_double, c_double, \ - c_double, c_double, Color, c_double] - gtlib.gt_graphics_draw_horizontal_line.argtypes = [c_void_p, c_double, \ - c_double, Color, \ - c_double, c_double] - gtlib.gt_graphics_draw_box.argtypes = [c_void_p, c_double, c_double, \ - c_double, c_double, Color, c_int, \ - c_double, c_double, Color, c_int] - gtlib.gt_graphics_draw_dashes.argtypes = [c_void_p, c_double, c_double, \ - c_double, c_double, c_int, \ - c_double, c_double, Color] - gtlib.gt_graphics_draw_caret.argtypes = [c_void_p, c_double, c_double, \ - c_double, c_double, c_int, \ - c_double, c_double, Color] - gtlib.gt_graphics_draw_rectangle.argtypes = [c_void_p, c_double, c_double, \ - c_int, Color, c_int, Color, \ - c_double, c_double, c_double] - gtlib.gt_graphics_draw_arrowhead.argtypes = [c_void_p, c_double, c_double, \ - Color, c_int] - gtlib.gt_graphics_draw_curve_data.argtypes = [c_void_p, c_double, c_double,\ - Color, c_void_p, c_ulong, \ - Range, c_ulong] - gtlib.gt_graphics_save_to_file.restype = c_int - gtlib.gt_graphics_save_to_file.argtypes = [c_void_p, c_char_p, Error] - gtlib.gt_graphics_save_to_stream.argtypes = [c_void_p, Str] - register = classmethod(register) + + def __init__(self, p): + self.g = p + self.own = False + self._as_parameter_ = self.g + + def __del__(self): + if self.own: + try: + gtlib.gt_graphics_delete(self.g) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, Graphics): + raise TypeError, "argument must be a Graphics" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def draw_text(self, x, y, text): + gtlib.gt_graphics_draw_text(self.g, x, y, text) + + def draw_text_centered(self, x, y, text): + gtlib.gt_graphics_draw_text_centered(self.g, x, y, text) + + def draw_text_right(self, x, y, text): + gtlib.gt_graphics_draw_text_right(self.g, x, y, text) + + def draw_colored_text(self, x, y, color, text): + gtlib.gt_graphics_draw_colored_text(self.g, x, y, color, text) + + def get_image_height(self): + return gtlib.gt_graphics_get_image_height(self.g) + + def get_image_width(self): + return gtlib.gt_graphics_get_image_width(self.g) + + def get_text_height(self): + return gtlib.gt_graphics_get_text_height(self.g) + + def get_text_width(self, txt): + return gtlib.gt_graphics_get_text_width(self.g, txt) + + def set_margins(self, xmargs, ymargs): + gtlib.gt_graphics_set_margins(self.g, xmargs, ymargs) + + def get_xmargins(self): + return gtlib.gt_graphics_get_xmargins(self.g) + + def get_ymargins(self): + return gtlib.gt_graphics_get_ymargins(self.g) + + def draw_line(self, x, y, xto, yto, color, stroke_width): + gtlib.gt_graphics_draw_line(self.g, x, y, xto, yto, color, + stroke_width) + + def draw_horizontal_line(self, x, y, color, width, stroke_width): + gtlib.gt_graphics_draw_horizontal_line(self.g, x, y, color, + width, stroke_width) + + def draw_vertical_line(self, x, y, color, length, stroke_width): + gtlib.gt_graphics_draw_vertical_line(self.g, x, y, color, length, + stroke_width) + + def draw_box( + self, + x, + y, + width, + height, + fillcolor, + astatus, + awidth, + swidth, + scolor, + dashed, + ): + if dashed: + d_int = 1 + else: + d_int = 0 + gtlib.gt_graphics_draw_box( + self.g, + x, + y, + width, + height, + fillcolor, + astatus, + awidth, + swidth, + scolor, + d_int, + ) + + def draw_dashes( + self, + x, + y, + width, + height, + astatus, + awidth, + swidth, + scolor, + ): + gtlib.gt_graphics_draw_dashes( + self.g, + x, + y, + width, + height, + astatus, + awidth, + swidth, + scolor, + ) + + def draw_caret( + self, + x, + y, + width, + height, + astatus, + awidth, + swidth, + scolor, + ): + gtlib.gt_graphics_draw_caret( + self.g, + x, + y, + width, + height, + astatus, + awidth, + swidth, + scolor, + ) + + def draw_rectangle( + self, + x, + y, + filled, + fcolor, + stroked, + scolor, + swidth, + width, + height, + ): + if filled: + f_int = 1 + else: + f_int = 0 + if stroked: + s_int = 1 + else: + s_int = 0 + gtlib.gt_graphics_draw_rectangle( + self.g, + x, + y, + f_int, + fcolor, + s_int, + scolor, + swidth, + width, + height, + ) + + def draw_arrowhead(self, x, y, color, status): + gtlib.gt_graphics_draw_arrowhead(self.g, x, y, color, status) + + def draw_curve_data(self, x, y, color, data, ndata, valrange, height): + from ctypes import c_double + NDblArr = c_double * ndata + cdata = NDblArr() + for i in range(0, ndata): + cdata[i] = data[i] + gtlib.gt_graphics_draw_curve_data(self.g, x, y, color, cdata, + ndata, valrange, height) + + def to_file(self, filename): + err = Error() + if gtlib.gt_graphics_save_to_file(self.g, filename, err) < 0: + gterror(err) + + def to_stream(self): + from ctypes import string_at + s = Str(None) + gtlib.gt_graphics_save_to_stream(self.g, s) + return string_at(s.get_mem(), s.length()) + + def get_text_height(self): + return gtlib.gt_graphics_get_text_height(self.g) + + def get_text_width(self, text): + return gtlib.gt_graphics_get_text_width(self.g, text) + + def register(cls, gtlib): + from ctypes import c_char_p, c_void_p, c_int, POINTER, c_double, \ + c_ulong + gtlib.gt_graphics_draw_text.argtypes = [c_void_p, c_double, + c_double, c_char_p] + gtlib.gt_graphics_draw_text_right.argtypes = [c_void_p, c_double, + c_double, c_char_p] + gtlib.gt_graphics_draw_text_centered.argtypes = [c_void_p, + c_double, c_double, c_char_p] + gtlib.gt_graphics_draw_colored_text.argtypes = [c_void_p, + c_double, c_double, Color, c_char_p] + gtlib.gt_graphics_get_image_height.restype = c_double + gtlib.gt_graphics_get_image_height.argtypes = [c_void_p] + gtlib.gt_graphics_get_image_width.restype = c_double + gtlib.gt_graphics_get_image_width.argtypes = [c_void_p] + gtlib.gt_graphics_get_text_height.restype = c_double + gtlib.gt_graphics_get_text_height.argtypes = [c_void_p] + gtlib.gt_graphics_get_text_width.restype = c_double + gtlib.gt_graphics_get_text_width.argtypes = [c_void_p, c_char_p] + gtlib.gt_graphics_set_margins.argtypes = [c_void_p, c_double, + c_double] + gtlib.gt_graphics_get_xmargins.restype = c_double + gtlib.gt_graphics_get_xmargins.argtypes = [c_void_p] + gtlib.gt_graphics_get_ymargins.restype = c_double + gtlib.gt_graphics_get_ymargins.argtypes = [c_void_p] + gtlib.gt_graphics_draw_vertical_line.argtypes = [c_void_p, + c_double, c_double, Color, c_double, c_double] + gtlib.gt_graphics_draw_line.argtypes = [c_void_p, c_double, + c_double, c_double, c_double, Color, c_double] + gtlib.gt_graphics_draw_horizontal_line.argtypes = [c_void_p, + c_double, c_double, Color, c_double, c_double] + gtlib.gt_graphics_draw_box.argtypes = [ + c_void_p, + c_double, + c_double, + c_double, + c_double, + Color, + c_int, + c_double, + c_double, + Color, + c_int, + ] + gtlib.gt_graphics_draw_dashes.argtypes = [ + c_void_p, + c_double, + c_double, + c_double, + c_double, + c_int, + c_double, + c_double, + Color, + ] + gtlib.gt_graphics_draw_caret.argtypes = [ + c_void_p, + c_double, + c_double, + c_double, + c_double, + c_int, + c_double, + c_double, + Color, + ] + gtlib.gt_graphics_draw_rectangle.argtypes = [ + c_void_p, + c_double, + c_double, + c_int, + Color, + c_int, + Color, + c_double, + c_double, + c_double, + ] + gtlib.gt_graphics_draw_arrowhead.argtypes = [c_void_p, c_double, + c_double, Color, c_int] + gtlib.gt_graphics_draw_curve_data.argtypes = [c_void_p, c_double, + c_double, Color, c_void_p, c_ulong, Range, c_ulong] + gtlib.gt_graphics_save_to_file.restype = c_int + gtlib.gt_graphics_save_to_file.argtypes = [c_void_p, c_char_p, + Error] + gtlib.gt_graphics_save_to_stream.argtypes = [c_void_p, Str] + + register = classmethod(register) class GraphicsCairo(Graphics): - def __init__(self, p): - self.g = p - self.own = False - self._as_parameter_ = self.g - def from_param(cls, obj): - if not isinstance(obj, GraphicsCairo): - raise TypeError, "argument must be a GraphicsCairo" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, p): + self.g = p + self.own = False + self._as_parameter_ = self.g + + def from_param(cls, obj): + if not isinstance(obj, GraphicsCairo): + raise TypeError, "argument must be a GraphicsCairo" + return obj._as_parameter_ + + from_param = classmethod(from_param) class GraphicsCairoPNG(GraphicsCairo): - def __init__(self, width, height): - self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_PNG, width, height) - self.own = True - self._as_parameter_ = self.g - def from_param(cls, obj): - if not isinstance(obj, GraphicsCairoPNG): - raise TypeError, "argument must be a GraphicsCairoPNG" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, width, height): + self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_PNG, width, height) + self.own = True + self._as_parameter_ = self.g + + def from_param(cls, obj): + if not isinstance(obj, GraphicsCairoPNG): + raise TypeError, "argument must be a GraphicsCairoPNG" + return obj._as_parameter_ + + from_param = classmethod(from_param) + class GraphicsCairoPDF(GraphicsCairo): - def __init__(self, width, height): - self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_PDF, width, height) - self.own = True - self._as_parameter_ = self.g - def from_param(cls, obj): - if not isinstance(obj, GraphicsCairoPDF): - raise TypeError, "argument must be a GraphicsCairoPDF" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, width, height): + self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_PDF, width, height) + self.own = True + self._as_parameter_ = self.g + + def from_param(cls, obj): + if not isinstance(obj, GraphicsCairoPDF): + raise TypeError, "argument must be a GraphicsCairoPDF" + return obj._as_parameter_ + + from_param = classmethod(from_param) + class GraphicsCairoPS(GraphicsCairo): - def __init__(self, width, height): - self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_PS, width, height) - self.own = True - self._as_parameter_ = self.g - def from_param(cls, obj): - if not isinstance(obj, GraphicsCairoPS): - raise TypeError, "argument must be a GraphicsCairoPS" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, width, height): + self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_PS, width, height) + self.own = True + self._as_parameter_ = self.g + + def from_param(cls, obj): + if not isinstance(obj, GraphicsCairoPS): + raise TypeError, "argument must be a GraphicsCairoPS" + return obj._as_parameter_ + + from_param = classmethod(from_param) + class GraphicsCairoSVG(GraphicsCairo): - def __init__(self, width, height): - self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_SVG, width, height) - self.own = True - self._as_parameter_ = self.g - def from_param(cls, obj): - if not isinstance(obj, GraphicsCairoSVG): - raise TypeError, "argument must be a GraphicsCairoSVG" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, width, height): + self.g = gtlib.gt_graphics_cairo_new(GRAPHICS_SVG, width, height) + self.own = True + self._as_parameter_ = self.g + + def from_param(cls, obj): + if not isinstance(obj, GraphicsCairoSVG): + raise TypeError, "argument must be a GraphicsCairoSVG" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + diff --git a/gtpython/gt/annotationsketch/image_info.py b/gtpython/gt/annotationsketch/image_info.py index b44be02..e5134a8 100644 --- a/gtpython/gt/annotationsketch/image_info.py +++ b/gtpython/gt/annotationsketch/image_info.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -19,67 +21,72 @@ from gt.dlload import gtlib from gt.annotationsketch.rec_map import RecMap import math + class ImageInfo: - def __init__(self): - self.ii = gtlib.gt_image_info_new() - self._as_parameter_ = self.ii - self.hotspots = None - - def __del__(self): - try: - gtlib.gt_image_info_delete(self.ii) - except AttributeError: - pass - - def from_param(cls, obj): - if not (isinstance(obj, ImageInfo) or obj == None): - raise TypeError, "argument must be an ImageInfo" - if obj == None: - return None - return obj._as_parameter_ - from_param = classmethod(from_param) - - def get_height(self): - return gtlib.gt_image_info_get_height(self.ii) - - def num_of_rec_maps(self): - return gtlib.gt_image_info_num_of_rec_maps(self.ii) - - def compare_hotspots(cls, hs1, hs2): - if hs1[2]-hs1[0]+1 > hs2[2]-hs2[0]+1: - return 1 - elif hs1[2]-hs1[0]+1 == hs2[2]-hs2[0]+1: - if hs1[3] > hs2[3]: - return 1 - elif hs1[3] == hs2[3]: - return 0 - else: - return -1 - else: - return -1 - compare_hotspots = classmethod(compare_hotspots) - - def each_hotspot(self): - if not self.hotspots: - self.hotspots = [] - for i in range(self.num_of_rec_maps()): - rm = RecMap(gtlib.gt_image_info_get_rec_map(self.ii, i)) - self.hotspots.append([math.floor(rm.get_northwest_x()), \ - math.floor(rm.get_northwest_y()), \ - math.floor(rm.get_southeast_x()), \ - math.floor(rm.get_southeast_y()), \ - rm.get_genome_feature()]) - self.hotspots.sort(ImageInfo.compare_hotspots) - for hs in self.hotspots: - yield hs[0],hs[1],hs[2],hs[3],hs[4] - - def register(cls, gtlib): - from ctypes import c_void_p, c_ulong, c_uint - gtlib.gt_image_info_get_rec_map.restype = c_void_p - gtlib.gt_image_info_get_rec_map.argtypes = [c_void_p, c_ulong] - gtlib.gt_image_info_num_of_rec_maps.restype = c_ulong - gtlib.gt_image_info_num_of_rec_maps.argtypes = [c_void_p] - gtlib.gt_image_info_get_height.restype = c_uint - gtlib.gt_image_info_get_height.argtypes = [c_void_p] - gtlib.gt_image_info_new.restype = c_void_p - register = classmethod(register) + + def __init__(self): + self.ii = gtlib.gt_image_info_new() + self._as_parameter_ = self.ii + self.hotspots = None + + def __del__(self): + try: + gtlib.gt_image_info_delete(self.ii) + except AttributeError: + pass + + def from_param(cls, obj): + if not (isinstance(obj, ImageInfo) or obj == None): + raise TypeError, "argument must be an ImageInfo" + if obj == None: + return None + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def get_height(self): + return gtlib.gt_image_info_get_height(self.ii) + + def num_of_rec_maps(self): + return gtlib.gt_image_info_num_of_rec_maps(self.ii) + + def compare_hotspots(cls, hs1, hs2): + if hs1[2] - hs1[0] + 1 > hs2[2] - hs2[0] + 1: + return 1 + elif hs1[2] - hs1[0] + 1 == hs2[2] - hs2[0] + 1: + if hs1[3] > hs2[3]: + return 1 + elif hs1[3] == hs2[3]: + return 0 + else: + return -1 + else: + return -1 + + compare_hotspots = classmethod(compare_hotspots) + + def each_hotspot(self): + if not self.hotspots: + self.hotspots = [] + for i in range(self.num_of_rec_maps()): + rm = RecMap(gtlib.gt_image_info_get_rec_map(self.ii, i)) + self.hotspots.append([math.floor(rm.get_northwest_x()), + math.floor(rm.get_northwest_y()), math.floor(rm.get_southeast_x()), + math.floor(rm.get_southeast_y()), rm.get_genome_feature()]) + self.hotspots.sort(ImageInfo.compare_hotspots) + for hs in self.hotspots: + yield (hs[0], hs[1], hs[2], hs[3], hs[4]) + + def register(cls, gtlib): + from ctypes import c_void_p, c_ulong, c_uint + gtlib.gt_image_info_get_rec_map.restype = c_void_p + gtlib.gt_image_info_get_rec_map.argtypes = [c_void_p, c_ulong] + gtlib.gt_image_info_num_of_rec_maps.restype = c_ulong + gtlib.gt_image_info_num_of_rec_maps.argtypes = [c_void_p] + gtlib.gt_image_info_get_height.restype = c_uint + gtlib.gt_image_info_get_height.argtypes = [c_void_p] + gtlib.gt_image_info_new.restype = c_void_p + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/layout.py b/gtpython/gt/annotationsketch/layout.py index f6e2c58..a8316e3 100644 --- a/gtpython/gt/annotationsketch/layout.py +++ b/gtpython/gt/annotationsketch/layout.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -21,41 +23,47 @@ from gt.annotationsketch.diagram import Diagram from gt.annotationsketch.style import Style from gt.core.error import Error, gterror + class Layout: - def __init__(self, diagram, width, style): - err = Error() - self.layout = gtlib.gt_layout_new(diagram, width, style, err) - if err.is_set(): - gterror(err) - self._as_parameter_ = self.layout - - def __del__(self): - try: - gtlib.gt_layout_delete(self.layout) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, Layout): - raise TypeError, "argument must be a Layout" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def sketch(self, canvas): - err = Error() - had_err = gtlib.gt_layout_sketch(self.layout, canvas, err) - if had_err < 0: - gterror(err) - - def get_height(self): - return gtlib.gt_layout_get_height(self.layout) - - def register(cls, gtlib): - from ctypes import c_ulong, c_void_p, c_int - gtlib.gt_layout_new.restype = c_void_p - gtlib.gt_layout_new.argtypes = [Diagram, c_ulong, Style] - gtlib.gt_layout_sketch.restype = c_int - gtlib.gt_layout_sketch.argtypes = [c_void_p, Canvas, Error] - gtlib.gt_layout_get_height.restype = c_ulong - gtlib.gt_layout_get_height.argtypes = [c_void_p] - register = classmethod(register) + + def __init__(self, diagram, width, style): + err = Error() + self.layout = gtlib.gt_layout_new(diagram, width, style, err) + if err.is_set(): + gterror(err) + self._as_parameter_ = self.layout + + def __del__(self): + try: + gtlib.gt_layout_delete(self.layout) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, Layout): + raise TypeError, "argument must be a Layout" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def sketch(self, canvas): + err = Error() + had_err = gtlib.gt_layout_sketch(self.layout, canvas, err) + if had_err < 0: + gterror(err) + + def get_height(self): + return gtlib.gt_layout_get_height(self.layout) + + def register(cls, gtlib): + from ctypes import c_ulong, c_void_p, c_int + gtlib.gt_layout_new.restype = c_void_p + gtlib.gt_layout_new.argtypes = [Diagram, c_ulong, Style] + gtlib.gt_layout_sketch.restype = c_int + gtlib.gt_layout_sketch.argtypes = [c_void_p, Canvas, Error] + gtlib.gt_layout_get_height.restype = c_ulong + gtlib.gt_layout_get_height.argtypes = [c_void_p] + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/rec_map.py b/gtpython/gt/annotationsketch/rec_map.py index a38bb88..2e52e47 100644 --- a/gtpython/gt/annotationsketch/rec_map.py +++ b/gtpython/gt/annotationsketch/rec_map.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -18,48 +20,57 @@ from gt.dlload import gtlib from gt.extended.feature_node import FeatureNode + class RecMap: - def __init__(self, rm): - self.rm = rm - self._as_parameter_ = self.rm - def from_param(cls, obj): - if not isinstance(obj, RecMap): - raise TypeError, "argument must be a RecMap" - return obj._as_parameter_ - from_param = classmethod(from_param) + def __init__(self, rm): + self.rm = rm + self._as_parameter_ = self.rm + + def from_param(cls, obj): + if not isinstance(obj, RecMap): + raise TypeError, "argument must be a RecMap" + return obj._as_parameter_ + + from_param = classmethod(from_param) - def get_northwest_x(self): - return gtlib.gt_rec_map_get_northwest_x(self.rm) + def get_northwest_x(self): + return gtlib.gt_rec_map_get_northwest_x(self.rm) - def get_northwest_y(self): - return gtlib.gt_rec_map_get_northwest_y(self.rm) + def get_northwest_y(self): + return gtlib.gt_rec_map_get_northwest_y(self.rm) - def get_southeast_x(self): - return gtlib.gt_rec_map_get_southeast_x(self.rm) + def get_southeast_x(self): + return gtlib.gt_rec_map_get_southeast_x(self.rm) - def get_southeast_y(self): - return gtlib.gt_rec_map_get_southeast_y(self.rm) + def get_southeast_y(self): + return gtlib.gt_rec_map_get_southeast_y(self.rm) + + def get_genome_feature(self): - def get_genome_feature(self): #refcount only this FeatureNode! - return FeatureNode.create_from_ptr(gtlib.gt_rec_map_get_genome_feature(self.rm), True) - - def has_omitted_children(self): - return (gtlib.gt_rec_map_has_omitted_children(self.rm) == 1) - - def register(cls, gtlib): - from ctypes import c_int, c_void_p, c_double - gtlib.gt_rec_map_get_northwest_x.restype = c_double - gtlib.gt_rec_map_get_northwest_x.argtypes = [c_void_p] - gtlib.gt_rec_map_get_northwest_y.restype = c_double - gtlib.gt_rec_map_get_northwest_y.argtypes = [c_void_p] - gtlib.gt_rec_map_get_southeast_x.restype = c_double - gtlib.gt_rec_map_get_southeast_x.argtypes = [c_void_p] - gtlib.gt_rec_map_get_southeast_y.restype = c_double - gtlib.gt_rec_map_get_southeast_y.argtypes = [c_void_p] - gtlib.gt_rec_map_get_genome_feature.restype = c_void_p - gtlib.gt_rec_map_get_genome_feature.argtypes = [c_void_p] - gtlib.gt_rec_map_has_omitted_children.restype = c_int - gtlib.gt_rec_map_has_omitted_children.argtypes = [c_void_p] - register = classmethod(register) + + return FeatureNode.create_from_ptr(gtlib.gt_rec_map_get_genome_feature(self.rm), + True) + + def has_omitted_children(self): + return gtlib.gt_rec_map_has_omitted_children(self.rm) == 1 + + def register(cls, gtlib): + from ctypes import c_int, c_void_p, c_double + gtlib.gt_rec_map_get_northwest_x.restype = c_double + gtlib.gt_rec_map_get_northwest_x.argtypes = [c_void_p] + gtlib.gt_rec_map_get_northwest_y.restype = c_double + gtlib.gt_rec_map_get_northwest_y.argtypes = [c_void_p] + gtlib.gt_rec_map_get_southeast_x.restype = c_double + gtlib.gt_rec_map_get_southeast_x.argtypes = [c_void_p] + gtlib.gt_rec_map_get_southeast_y.restype = c_double + gtlib.gt_rec_map_get_southeast_y.argtypes = [c_void_p] + gtlib.gt_rec_map_get_genome_feature.restype = c_void_p + gtlib.gt_rec_map_get_genome_feature.argtypes = [c_void_p] + gtlib.gt_rec_map_has_omitted_children.restype = c_int + gtlib.gt_rec_map_has_omitted_children.argtypes = [c_void_p] + + register = classmethod(register) + + diff --git a/gtpython/gt/annotationsketch/style.py b/gtpython/gt/annotationsketch/style.py index 540471a..2d91233 100644 --- a/gtpython/gt/annotationsketch/style.py +++ b/gtpython/gt/annotationsketch/style.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -22,131 +24,142 @@ from gt.core.gtstr import Str from gt.core.str_array import StrArray from gt.extended.genome_node import GenomeNode + class Style: - def __init__(self, ptr = None): - if ptr: - self.style = ptr - self.own = False - else: - e = Error() - self.style = gtlib.gt_style_new(False, e) - if self.style == 0 or self.style == None: - gterror(e) - self.own = True - self._as_parameter_ = self.style - - def __del__(self): - if self.own: - try: - gtlib.gt_style_delete(self.style) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, Style): - raise TypeError, "argument must be a Style" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def load_file(self, filename): - err = Error() - rval = gtlib.gt_style_load_file(self.style, filename, err) - if rval != 0: - gterror(err) - - def load_str(self, string): - err = Error() - strg = Str(string) - rval = gtlib.gt_style_load_str(self.style, strg, err) - if rval != 0: - gterror(err) - - def to_str(self): - err = Error() - string = Str() - if gtlib.gt_style_to_str(self.style, string, err) == 0: - return str(string) - else: - gterror(err) - - def clone(self): - sty = Style() - str = self.to_str() - sty.load_str(str) - return sty - - def get_color(self, section, key, gn = None): - from ctypes import byref - color = Color() - if gtlib.gt_style_get_color(self.style, section, key, byref(color), gn) == 1: - return color - else: - return None - - def set_color(self, section, key, color): - from ctypes import byref - gtlib.gt_style_set_color(self.style, section, key, byref(color)) - - def get_cstr(self, section, key, gn = None): - string = Str() - if gtlib.gt_style_get_str(self.style, section, key, string, gn) == 1: - return str(string) - else: - return None - - def set_cstr(self, section, key, value): - string = Str(value) - gtlib.gt_style_set_str(self.style, section, key, string) - - def get_num(self, section, key, gn = None): - from ctypes import c_double, byref - double = c_double() - if gtlib.gt_style_get_num(self.style, section, key, byref(double), gn) == 1: - return double.value - else: - return None - - def set_num(self, section, key, number): - from ctypes import c_double - num = c_double(number) - gtlib.gt_style_set_num(self.style, section, key, num) - - def get_bool(self, section, key, gn = None): - from ctypes import byref, c_int - bool = c_int() - if gtlib.gt_style_get_bool(self.style, section, key, byref(bool), gn) == 1: - if bool.value == 1: - return True - else: - return False - else: - return None - - def set_bool(self, section, key, val): - if val == True: - gtlib.gt_style_set_bool(self.style, section, key, 1) - else: - gtlib.gt_style_set_bool(self.style, section, key, 0) - - def unset(self, section, key): - gtlib.gt_style_unset(self.style, section, key) - - def register(cls, gtlib): - from ctypes import c_char_p, c_double, c_float, c_void_p, POINTER, c_int - gtlib.gt_style_get_bool.restype = c_int - gtlib.gt_style_get_bool.argtypes = [c_void_p, c_char_p, c_char_p, \ - POINTER(c_int), c_void_p] - gtlib.gt_style_get_num.restype = c_int - gtlib.gt_style_get_num.argtypes = [c_void_p, c_char_p, c_char_p, \ - POINTER(c_double), c_void_p] - gtlib.gt_style_get_str.restype = c_int - gtlib.gt_style_get_str.argtypes = [c_void_p, c_char_p, c_char_p, \ - Str, c_void_p] - gtlib.gt_style_get_color.restype = c_int - gtlib.gt_style_get_color.argtypes = [c_void_p, c_char_p, c_char_p, \ - POINTER(Color), c_void_p] - gtlib.gt_style_load_str.argtypes = [c_void_p, Str, Error] - gtlib.gt_style_load_file.argtypes = [c_void_p, c_char_p, Error] - gtlib.gt_style_unset.argtypes = [c_void_p, c_char_p, c_char_p] - gtlib.gt_style_to_str.argtypes = [c_void_p, Str, Error] - register = classmethod(register) + + def __init__(self, ptr=None): + if ptr: + self.style = ptr + self.own = False + else: + e = Error() + self.style = gtlib.gt_style_new(False, e) + if self.style == 0 or self.style == None: + gterror(e) + self.own = True + self._as_parameter_ = self.style + + def __del__(self): + if self.own: + try: + gtlib.gt_style_delete(self.style) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, Style): + raise TypeError, "argument must be a Style" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def load_file(self, filename): + err = Error() + rval = gtlib.gt_style_load_file(self.style, filename, err) + if rval != 0: + gterror(err) + + def load_str(self, string): + err = Error() + strg = Str(string) + rval = gtlib.gt_style_load_str(self.style, strg, err) + if rval != 0: + gterror(err) + + def to_str(self): + err = Error() + string = Str() + if gtlib.gt_style_to_str(self.style, string, err) == 0: + return str(string) + else: + gterror(err) + + def clone(self): + sty = Style() + str = self.to_str() + sty.load_str(str) + return sty + + def get_color(self, section, key, gn=None): + from ctypes import byref + color = Color() + if gtlib.gt_style_get_color(self.style, section, key, byref(color), + gn) == 1: + return color + else: + return None + + def set_color(self, section, key, color): + from ctypes import byref + gtlib.gt_style_set_color(self.style, section, key, byref(color)) + + def get_cstr(self, section, key, gn=None): + string = Str() + if gtlib.gt_style_get_str(self.style, section, key, string, gn) == \ + 1: + return str(string) + else: + return None + + def set_cstr(self, section, key, value): + string = Str(value) + gtlib.gt_style_set_str(self.style, section, key, string) + + def get_num(self, section, key, gn=None): + from ctypes import c_double, byref + double = c_double() + if gtlib.gt_style_get_num(self.style, section, key, byref(double), + gn) == 1: + return double.value + else: + return None + + def set_num(self, section, key, number): + from ctypes import c_double + num = c_double(number) + gtlib.gt_style_set_num(self.style, section, key, num) + + def get_bool(self, section, key, gn=None): + from ctypes import byref, c_int + bool = c_int() + if gtlib.gt_style_get_bool(self.style, section, key, byref(bool), + gn) == 1: + if bool.value == 1: + return True + else: + return False + else: + return None + + def set_bool(self, section, key, val): + if val == True: + gtlib.gt_style_set_bool(self.style, section, key, 1) + else: + gtlib.gt_style_set_bool(self.style, section, key, 0) + + def unset(self, section, key): + gtlib.gt_style_unset(self.style, section, key) + + def register(cls, gtlib): + from ctypes import c_char_p, c_double, c_float, c_void_p, \ + POINTER, c_int + gtlib.gt_style_get_bool.restype = c_int + gtlib.gt_style_get_bool.argtypes = [c_void_p, c_char_p, c_char_p, + POINTER(c_int), c_void_p] + gtlib.gt_style_get_num.restype = c_int + gtlib.gt_style_get_num.argtypes = [c_void_p, c_char_p, c_char_p, + POINTER(c_double), c_void_p] + gtlib.gt_style_get_str.restype = c_int + gtlib.gt_style_get_str.argtypes = [c_void_p, c_char_p, c_char_p, + Str, c_void_p] + gtlib.gt_style_get_color.restype = c_int + gtlib.gt_style_get_color.argtypes = [c_void_p, c_char_p, + c_char_p, POINTER(Color), c_void_p] + gtlib.gt_style_load_str.argtypes = [c_void_p, Str, Error] + gtlib.gt_style_load_file.argtypes = [c_void_p, c_char_p, Error] + gtlib.gt_style_unset.argtypes = [c_void_p, c_char_p, c_char_p] + gtlib.gt_style_to_str.argtypes = [c_void_p, Str, Error] + + register = classmethod(register) + + diff --git a/gtpython/gt/core/__init__.py b/gtpython/gt/core/__init__.py index 880b504..6edaced 100644 --- a/gtpython/gt/core/__init__.py +++ b/gtpython/gt/core/__init__.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg diff --git a/gtpython/gt/core/array.py b/gtpython/gt/core/array.py index a5f806b..396ee1a 100644 --- a/gtpython/gt/core/array.py +++ b/gtpython/gt/core/array.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -17,49 +19,56 @@ from gt.dlload import gtlib + class Array: - def create(size=None, own = True): - if size is None: - from ctypes import c_void_p, sizeof - size = sizeof(c_void_p) - return Array(gtlib.gt_array_new(size), own) - create = staticmethod(create) - - def __init__(self, arr, own = False): - self.array = arr - self._as_parameter_ = self.array - self.own = own - - def __del__(self): - if self.own: - try: - gtlib.gt_array_delete(self.array) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, Array): - raise TypeError, "argument must be an Array" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def get(self, i): - return gtlib.gt_array_get(self.array, i).contents - - def size(self): - return gtlib.gt_array_size(self.array) - - def add(self, val): - gtlib.gt_array_add_ptr(self.array, val._as_parameter_) - - def register(cls, gtlib): - from ctypes import c_void_p, c_uint, c_ulong, POINTER - gtlib.gt_array_new.restype = c_void_p - gtlib.gt_array_new.argtypes = [c_uint] - gtlib.gt_array_ref.restype = c_void_p - gtlib.gt_array_ref.argtypes = [c_void_p] - gtlib.gt_array_get.restype = POINTER(c_void_p) - gtlib.gt_array_get.argtypes = [c_ulong] - gtlib.gt_array_size.restype = c_ulong - gtlib.gt_array_add_ptr.argtypes = [c_void_p, c_void_p] - register = classmethod(register) + + def create(size=None, own=True): + if size is None: + from ctypes import c_void_p, sizeof + size = sizeof(c_void_p) + return Array(gtlib.gt_array_new(size), own) + + create = staticmethod(create) + + def __init__(self, arr, own=False): + self.array = arr + self._as_parameter_ = self.array + self.own = own + + def __del__(self): + if self.own: + try: + gtlib.gt_array_delete(self.array) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, Array): + raise TypeError, "argument must be an Array" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def get(self, i): + return gtlib.gt_array_get(self.array, i).contents + + def size(self): + return gtlib.gt_array_size(self.array) + + def add(self, val): + gtlib.gt_array_add_ptr(self.array, val._as_parameter_) + + def register(cls, gtlib): + from ctypes import c_void_p, c_uint, c_ulong, POINTER + gtlib.gt_array_new.restype = c_void_p + gtlib.gt_array_new.argtypes = [c_uint] + gtlib.gt_array_ref.restype = c_void_p + gtlib.gt_array_ref.argtypes = [c_void_p] + gtlib.gt_array_get.restype = POINTER(c_void_p) + gtlib.gt_array_get.argtypes = [c_ulong] + gtlib.gt_array_size.restype = c_ulong + gtlib.gt_array_add_ptr.argtypes = [c_void_p, c_void_p] + + register = classmethod(register) + + diff --git a/gtpython/gt/core/error.py b/gtpython/gt/core/error.py index ee4047d..5ffe904 100644 --- a/gtpython/gt/core/error.py +++ b/gtpython/gt/core/error.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -17,53 +19,62 @@ from gt.dlload import gtlib + class GTError(Exception): - pass + + pass + class Error: - def __init__(self, ptr = None): - if ptr: - self.error = ptr - self.own = False - else: - self.error = gtlib.gt_error_new() - self.own = True - self._as_parameter_ = self.error - - def __del__(self): - if self.own: - try: - gtlib.gt_error_delete(self.error) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, Error): - raise TypeError, "argument must be an Error" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def get(self): - return gtlib.gt_error_get(self.error) - - def is_set(self): - return (gtlib.gt_error_is_set(self.error) == 1) - - def unset(self): - gtlib.gt_error_unset(self.error) - - def register(cls, gtlib): - from ctypes import c_void_p, c_char_p, c_int - gtlib.gt_error_new.restype = c_void_p - gtlib.gt_error_get.restype = c_char_p - gtlib.gt_error_is_set.restype = c_int - gtlib.gt_error_get.argtypes = [c_void_p] - gtlib.gt_error_is_set.argtypes = [c_void_p] - gtlib.gt_error_unset.argtypes = [c_void_p] - register = classmethod(register) + + def __init__(self, ptr=None): + if ptr: + self.error = ptr + self.own = False + else: + self.error = gtlib.gt_error_new() + self.own = True + self._as_parameter_ = self.error + + def __del__(self): + if self.own: + try: + gtlib.gt_error_delete(self.error) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, Error): + raise TypeError, "argument must be an Error" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def get(self): + return gtlib.gt_error_get(self.error) + + def is_set(self): + return gtlib.gt_error_is_set(self.error) == 1 + + def unset(self): + gtlib.gt_error_unset(self.error) + + def register(cls, gtlib): + from ctypes import c_void_p, c_char_p, c_int + gtlib.gt_error_new.restype = c_void_p + gtlib.gt_error_get.restype = c_char_p + gtlib.gt_error_is_set.restype = c_int + gtlib.gt_error_get.argtypes = [c_void_p] + gtlib.gt_error_is_set.argtypes = [c_void_p] + gtlib.gt_error_unset.argtypes = [c_void_p] + + register = classmethod(register) + def gterror(err): - if isinstance(err, Error): - raise GTError, "GenomeTools error: " + err.get() - else: - raise GTError, "GenomeTools error: " + err + if isinstance(err, Error): + raise GTError, "GenomeTools error: " + err.get() + else: + raise GTError, "GenomeTools error: " + err + + diff --git a/gtpython/gt/core/gtrange.py b/gtpython/gt/core/gtrange.py index 67b650c..73842c9 100644 --- a/gtpython/gt/core/gtrange.py +++ b/gtpython/gt/core/gtrange.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -18,6 +20,9 @@ from gt.dlload import gtlib from ctypes import Structure, c_ulong + class Range(Structure): - _fields_ = [("start", c_ulong), - ("end" , c_ulong)] + + _fields_ = [("start", c_ulong), ("end", c_ulong)] + + diff --git a/gtpython/gt/core/gtstr.py b/gtpython/gt/core/gtstr.py index d1d84d2..36837c9 100644 --- a/gtpython/gt/core/gtstr.py +++ b/gtpython/gt/core/gtstr.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -17,48 +19,54 @@ from gt.dlload import gtlib + class Str: - def __init__(self, s = None): - if s == None or isinstance(s, str): - self.strg = gtlib.gt_str_new_cstr(s) - self.own = True - else: - self.strg = s - self.own = False - self._as_parameter_ = self.strg - def __del__(self): - if self.own: - try: - gtlib.gt_str_delete(self.strg) - except AttributeError: - pass + def __init__(self, s=None): + if s == None or isinstance(s, str): + self.strg = gtlib.gt_str_new_cstr(s) + self.own = True + else: + self.strg = s + self.own = False + self._as_parameter_ = self.strg + + def __del__(self): + if self.own: + try: + gtlib.gt_str_delete(self.strg) + except AttributeError: + pass + + def __str__(self): + return gtlib.gt_str_get(self.strg) + + def from_param(cls, obj): + if not isinstance(obj, Str): + raise TypeError, "argument must be a Str" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def append_cstr(self, string): + gtlib.gt_str_append_cstr(self.strg, string) - def __str__(self): - return gtlib.gt_str_get(self.strg) + def length(self): + return gtlib.gt_str_length(self.strg) - def from_param(cls, obj): - if not isinstance(obj, Str): - raise TypeError, "argument must be a Str" - return obj._as_parameter_ - from_param = classmethod(from_param) + def get_mem(self): + return gtlib.gt_str_get_mem(self.strg) - def append_cstr(self, string): - gtlib.gt_str_append_cstr(self.strg, string) + def register(cls, gtlib): + from ctypes import c_void_p, c_char_p, c_ulong + gtlib.gt_str_new.restype = c_void_p + gtlib.gt_str_new_cstr.restype = c_void_p + gtlib.gt_str_new_cstr.argtypes = [c_char_p] + gtlib.gt_str_append_cstr.argtypes = [c_void_p, c_char_p] + gtlib.gt_str_get.restype = c_char_p + gtlib.gt_str_get_mem.restype = c_void_p + gtlib.gt_str_length.restype = c_ulong - def length(self): - return gtlib.gt_str_length(self.strg) + register = classmethod(register) - def get_mem(self): - return gtlib.gt_str_get_mem(self.strg) - def register(cls, gtlib): - from ctypes import c_void_p, c_char_p, c_ulong - gtlib.gt_str_new.restype = c_void_p - gtlib.gt_str_new_cstr.restype = c_void_p - gtlib.gt_str_new_cstr.argtypes = [c_char_p] - gtlib.gt_str_append_cstr.argtypes = [c_void_p, c_char_p] - gtlib.gt_str_get.restype = c_char_p - gtlib.gt_str_get_mem.restype = c_void_p - gtlib.gt_str_length.restype = c_ulong - register = classmethod(register) diff --git a/gtpython/gt/core/str_array.py b/gtpython/gt/core/str_array.py index f9f5f24..43282ea 100644 --- a/gtpython/gt/core/str_array.py +++ b/gtpython/gt/core/str_array.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -17,24 +19,27 @@ from gt.dlload import gtlib + class StrArray: - def __init__(self, arr = None): - if not arr: - arr = gtlib.gt_str_array_new() - self.strarr = arr - self._as_parameter_ = self.strarr - - def __del__(self): - try: - gtlib.gt_str_array_delete(self.strarr) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, StrArray): - raise TypeError, "argument must be a StrArray" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, arr=None): + if not arr: + arr = gtlib.gt_str_array_new() + self.strarr = arr + self._as_parameter_ = self.strarr + + def __del__(self): + try: + gtlib.gt_str_array_delete(self.strarr) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, StrArray): + raise TypeError, "argument must be a StrArray" + return obj._as_parameter_ + + from_param = classmethod(from_param) #def __str__(self): #r = "" @@ -42,22 +47,25 @@ class StrArray: #r = r + " " + gtlib.gt_str_array_get(self.strarr, i) #return r - def to_list(self): - result = [] - for i in range(gtlib.gt_str_array_size(self.strarr)): - result.append(gtlib.gt_str_array_get(self.strarr, i)) - return result - - def size(self): - return gtlib.gt_str_array_size(self.strarr) - - def get(self, i): - return gtlib.gt_str_array_get(self.strarr, i) - - def register(cls, gtlib): - from ctypes import c_void_p, c_char_p, c_ulong - gtlib.gt_str_array_get.restype = c_char_p - gtlib.gt_str_array_get.argtypes = [c_void_p, c_ulong] - gtlib.gt_str_array_size.restype = c_ulong - gtlib.gt_str_array_add_cstr.argtypes = [c_void_p, c_char_p] - register = classmethod(register) + def to_list(self): + result = [] + for i in range(gtlib.gt_str_array_size(self.strarr)): + result.append(gtlib.gt_str_array_get(self.strarr, i)) + return result + + def size(self): + return gtlib.gt_str_array_size(self.strarr) + + def get(self, i): + return gtlib.gt_str_array_get(self.strarr, i) + + def register(cls, gtlib): + from ctypes import c_void_p, c_char_p, c_ulong + gtlib.gt_str_array_get.restype = c_char_p + gtlib.gt_str_array_get.argtypes = [c_void_p, c_ulong] + gtlib.gt_str_array_size.restype = c_ulong + gtlib.gt_str_array_add_cstr.argtypes = [c_void_p, c_char_p] + + register = classmethod(register) + + diff --git a/gtpython/gt/dlload.py b/gtpython/gt/dlload.py index c87ba87..ad02470 100644 --- a/gtpython/gt/dlload.py +++ b/gtpython/gt/dlload.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -19,10 +21,11 @@ from ctypes import CDLL, CFUNCTYPE, c_char_p, c_void_p import sys # inspired from the ruby bindings, maybe there is a better way + if sys.platform == "darwin": - soext = ".dylib" + soext = ".dylib" else: - soext = ".so" + soext = ".so" gtlib = CDLL("libgenometools" + soext) gtlib.gt_allocators_init() diff --git a/gtpython/gt/extended/__init__.py b/gtpython/gt/extended/__init__.py index 935af03..2f0d537 100644 --- a/gtpython/gt/extended/__init__.py +++ b/gtpython/gt/extended/__init__.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg diff --git a/gtpython/gt/extended/add_introns_stream.py b/gtpython/gt/extended/add_introns_stream.py index 76fd5b8..5d51326 100644 --- a/gtpython/gt/extended/add_introns_stream.py +++ b/gtpython/gt/extended/add_introns_stream.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -18,19 +20,25 @@ from gt.dlload import gtlib from gt.extended.genome_stream import GenomeStream + class AddIntronsStream(GenomeStream): - def __init__(self, genome_stream): - self.gs = gtlib.gt_add_introns_stream_new(genome_stream) - self._as_parameter_ = self.gs - - def from_param(cls, obj): - if not isinstance(obj, AddIntronsStream): - raise TypeError, "argument must be a AddIntronsStream" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def register(cls, gtlib): - from ctypes import c_void_p - gtlib.gt_add_introns_stream_new.restype = c_void_p - gtlib.gt_add_introns_stream_new.argtypes = [GenomeStream] - register = classmethod(register) + + def __init__(self, genome_stream): + self.gs = gtlib.gt_add_introns_stream_new(genome_stream) + self._as_parameter_ = self.gs + + def from_param(cls, obj): + if not isinstance(obj, AddIntronsStream): + raise TypeError, "argument must be a AddIntronsStream" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def register(cls, gtlib): + from ctypes import c_void_p + gtlib.gt_add_introns_stream_new.restype = c_void_p + gtlib.gt_add_introns_stream_new.argtypes = [GenomeStream] + + register = classmethod(register) + + diff --git a/gtpython/gt/extended/feature_node.py b/gtpython/gt/extended/feature_node.py index fb6bc7d..ce09c83 100644 --- a/gtpython/gt/extended/feature_node.py +++ b/gtpython/gt/extended/feature_node.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008-2009 Sascha Steinbiss # Copyright (c) 2008-2009 Center for Bioinformatics, University of Hamburg @@ -22,170 +24,189 @@ from gt.core.str_array import StrArray from gt.extended.genome_node import GenomeNode from gt.props import cachedproperty + class FeatureNode(GenomeNode): - def __init__(self): - pass - - @classmethod - def create_new(cls, seqid, type, start, end, strand): - from gt.extended.strand import strandchars - if not strand in strandchars: - gterror("Invalid strand '%s' -- must be one of %s" \ - % (strand, strandchars)) - s = Str(seqid) - fn = gtlib.gt_feature_node_new(s, type, start, end, \ - strandchars.index(strand)) - n = cls.create_from_ptr(fn, True) - return n - - def update_attrs(self): - attribs = {} - def py_collect_func(tag, val, data): - attribs[tag] = val - collect_func = CollectFunc(py_collect_func) - gtlib.gt_feature_node_foreach_attribute(self.gn, \ - collect_func, \ - None) - return attribs - - def add_child(self, node): - gtlib.gt_feature_node_add_child(self.gn, node) - - def from_param(cls, obj): - if not isinstance(obj, FeatureNode): - raise TypeError, "argument must be a FeatureNode" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def get_source(self): - return gtlib.gt_feature_node_get_source(self.gn) - - def set_source(self, source): - s = Str(source) - gtlib.gt_feature_node_set_source(self.gn, s) - source = cachedproperty(get_source, set_source) - - def get_type(self): - return gtlib.gt_feature_node_get_type(self.gn) - type = cachedproperty(get_type, None) - - def has_type(self, type): - return (gtlib.gt_feature_node_has_type(self.gn, type) == 1) - - def set_strand(self, strand): - from gt.extended.strand import strandchars - if not strand in strandchars: - gterror("Invalid strand '%s' -- must be one of %s" \ - % (strand, strandchars)) - gtlib.gt_feature_node_set_strand(self.gn, strandchars.index(strand)) - - def get_strand(self): - from gt.extended.strand import strandchars - return strandchars[gtlib.gt_feature_node_get_strand(self.gn)] - strand = cachedproperty(get_strand, set_strand) - - def get_phase(self): - return gtlib.gt_feature_node_get_phase(self.gn) - - def set_phase(self, phase): - return gtlib.gt_feature_node_set_phase(self.gn, phase) - phase = cachedproperty(get_phase, set_phase) - - def score_is_defined(self): - return (gtlib.gt_feature_node_score_is_defined(self.gn) == 1) - - def get_score(self): - if gtlib.gt_feature_node_score_is_defined(self.gn) == 1: - return gtlib.gt_feature_node_get_score(self.gn) - else: - return None - - def set_score(self, score): - gtlib.gt_feature_node_set_score(self.gn, score) - - def unset_score(self): - gtlib.gt_feature_node_unset_score(self.gn) - score = cachedproperty(get_score, set_score, unset_score) - - def get_attribute(self, attrib): - return gtlib.gt_feature_node_get_attribute(self.gn, attrib) - - def add_attribute(self, attrib, value): - if attrib == "" or value == "": - gterror("attribute keys or values must not be empty!") - gtlib.gt_feature_node_add_attribute(self.gn, attrib, value) - - def each_attribute(self): - attribs = self.update_attrs() - for tag, val in attribs.iteritems(): - yield (tag, val) - - - def get_attribs(self): - return dict(self.each_attribute()) - attribs = property(get_attribs) - - def register(cls, gtlib): - from ctypes import c_char_p, c_float, c_int, c_int, c_void_p, c_ulong, \ - c_float - gtlib.gt_feature_node_new.restype = c_void_p - gtlib.gt_feature_node_new.argtypes = [Str, c_char_p, c_ulong, c_ulong,\ - c_int] - gtlib.gt_feature_node_add_child.argtypes = [c_void_p, FeatureNode] - gtlib.gt_feature_node_set_source.argtypes = [c_void_p, Str] - gtlib.gt_feature_node_get_source.restype = c_char_p - gtlib.gt_feature_node_get_source.argtypes = [c_void_p] - gtlib.gt_feature_node_get_type.restype = c_char_p - gtlib.gt_feature_node_get_type.argtypes = [c_void_p] - gtlib.gt_feature_node_has_type.restype = c_int - gtlib.gt_feature_node_has_type.argtypes = [c_void_p, c_char_p] - gtlib.gt_feature_node_get_score.restype = c_float - gtlib.gt_feature_node_get_score.argtypes = [c_void_p] - gtlib.gt_feature_node_set_score.argtypes = [c_void_p, c_float] - gtlib.gt_feature_node_get_phase.restype = c_int - gtlib.gt_feature_node_get_phase.argtypes = [c_void_p] - gtlib.gt_feature_node_set_phase.argtypes = [c_void_p, c_int] - gtlib.gt_feature_node_score_is_defined.restype = c_int - gtlib.gt_feature_node_score_is_defined.argtypes = [c_void_p] - gtlib.gt_feature_node_get_strand.restype = c_int - gtlib.gt_feature_node_get_strand.argtypes = [c_void_p] - gtlib.gt_feature_node_set_strand.argtypes = [c_void_p, c_int] - gtlib.gt_feature_node_unset_score.argtypes = [c_void_p] - gtlib.gt_feature_node_add_attribute.argtypes = [c_void_p, c_char_p, \ - c_char_p] - gtlib.gt_feature_node_get_attribute.restype = c_char_p - gtlib.gt_feature_node_get_attribute.argtypes = [c_void_p, c_char_p] - register = classmethod(register) + def __init__(self): + pass + + @classmethod + def create_new(cls, seqid, type, start, end, strand): + from gt.extended.strand import strandchars + if not strand in strandchars: + gterror("Invalid strand '%s' -- must be one of %s" % (strand, + strandchars)) + s = Str(seqid) + fn = gtlib.gt_feature_node_new(s, type, start, end, strandchars.index(strand)) + n = cls.create_from_ptr(fn, True) + return n + + def update_attrs(self): + attribs = {} + + def py_collect_func(tag, val, data): + attribs[tag] = val + + collect_func = CollectFunc(py_collect_func) + gtlib.gt_feature_node_foreach_attribute(self.gn, collect_func, + None) + return attribs + + def add_child(self, node): + gtlib.gt_feature_node_add_child(self.gn, node) + + def from_param(cls, obj): + if not isinstance(obj, FeatureNode): + raise TypeError, "argument must be a FeatureNode" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def get_source(self): + return gtlib.gt_feature_node_get_source(self.gn) + + def set_source(self, source): + s = Str(source) + gtlib.gt_feature_node_set_source(self.gn, s) + + source = cachedproperty(get_source, set_source) + + def get_type(self): + return gtlib.gt_feature_node_get_type(self.gn) + + type = cachedproperty(get_type, None) + + def has_type(self, type): + return gtlib.gt_feature_node_has_type(self.gn, type) == 1 + + def set_strand(self, strand): + from gt.extended.strand import strandchars + if not strand in strandchars: + gterror("Invalid strand '%s' -- must be one of %s" % (strand, + strandchars)) + gtlib.gt_feature_node_set_strand(self.gn, strandchars.index(strand)) + + def get_strand(self): + from gt.extended.strand import strandchars + return strandchars[gtlib.gt_feature_node_get_strand(self.gn)] + + strand = cachedproperty(get_strand, set_strand) + + def get_phase(self): + return gtlib.gt_feature_node_get_phase(self.gn) + + def set_phase(self, phase): + return gtlib.gt_feature_node_set_phase(self.gn, phase) + + phase = cachedproperty(get_phase, set_phase) + + def score_is_defined(self): + return gtlib.gt_feature_node_score_is_defined(self.gn) == 1 + + def get_score(self): + if gtlib.gt_feature_node_score_is_defined(self.gn) == 1: + return gtlib.gt_feature_node_get_score(self.gn) + else: + return None + + def set_score(self, score): + gtlib.gt_feature_node_set_score(self.gn, score) + + def unset_score(self): + gtlib.gt_feature_node_unset_score(self.gn) + + score = cachedproperty(get_score, set_score, unset_score) + + def get_attribute(self, attrib): + return gtlib.gt_feature_node_get_attribute(self.gn, attrib) + + def add_attribute(self, attrib, value): + if attrib == "" or value == "": + gterror("attribute keys or values must not be empty!") + gtlib.gt_feature_node_add_attribute(self.gn, attrib, value) + + def each_attribute(self): + attribs = self.update_attrs() + for (tag, val) in attribs.iteritems(): + yield (tag, val) + + def get_attribs(self): + return dict(self.each_attribute()) + + attribs = property(get_attribs) + + def register(cls, gtlib): + from ctypes import c_char_p, c_float, c_int, c_int, c_void_p, \ + c_ulong, c_float + gtlib.gt_feature_node_new.restype = c_void_p + gtlib.gt_feature_node_new.argtypes = [Str, c_char_p, c_ulong, + c_ulong, c_int] + gtlib.gt_feature_node_add_child.argtypes = [c_void_p, + FeatureNode] + gtlib.gt_feature_node_set_source.argtypes = [c_void_p, Str] + gtlib.gt_feature_node_get_source.restype = c_char_p + gtlib.gt_feature_node_get_source.argtypes = [c_void_p] + gtlib.gt_feature_node_get_type.restype = c_char_p + gtlib.gt_feature_node_get_type.argtypes = [c_void_p] + gtlib.gt_feature_node_has_type.restype = c_int + gtlib.gt_feature_node_has_type.argtypes = [c_void_p, c_char_p] + gtlib.gt_feature_node_get_score.restype = c_float + gtlib.gt_feature_node_get_score.argtypes = [c_void_p] + gtlib.gt_feature_node_set_score.argtypes = [c_void_p, c_float] + gtlib.gt_feature_node_get_phase.restype = c_int + gtlib.gt_feature_node_get_phase.argtypes = [c_void_p] + gtlib.gt_feature_node_set_phase.argtypes = [c_void_p, c_int] + gtlib.gt_feature_node_score_is_defined.restype = c_int + gtlib.gt_feature_node_score_is_defined.argtypes = [c_void_p] + gtlib.gt_feature_node_get_strand.restype = c_int + gtlib.gt_feature_node_get_strand.argtypes = [c_void_p] + gtlib.gt_feature_node_set_strand.argtypes = [c_void_p, c_int] + gtlib.gt_feature_node_unset_score.argtypes = [c_void_p] + gtlib.gt_feature_node_add_attribute.argtypes = [c_void_p, + c_char_p, c_char_p] + gtlib.gt_feature_node_get_attribute.restype = c_char_p + gtlib.gt_feature_node_get_attribute.argtypes = [c_void_p, + c_char_p] + + register = classmethod(register) + class FeatureNodeIterator(object): - def next(self): - ret = gtlib.gt_feature_node_iterator_next(self.i) - if ret != None: - return FeatureNode.create_from_ptr(ret) - return ret - - def __del__(self): - try: - gtlib.gt_feature_node_iterator_delete(self.i) - except AttributeError: - pass - - def register(cls, gtlib): - from ctypes import c_void_p - gtlib.gt_feature_node_iterator_new.restype = c_void_p - gtlib.gt_feature_node_iterator_new.argtypes = [FeatureNode] - gtlib.gt_feature_node_iterator_new_direct.restype = c_void_p - gtlib.gt_feature_node_iterator_new_direct.argtypes = [FeatureNode] - gtlib.gt_feature_node_iterator_next.restype = c_void_p - register = classmethod(register) + + def next(self): + ret = gtlib.gt_feature_node_iterator_next(self.i) + if ret != None: + return FeatureNode.create_from_ptr(ret) + return ret + + def __del__(self): + try: + gtlib.gt_feature_node_iterator_delete(self.i) + except AttributeError: + pass + + def register(cls, gtlib): + from ctypes import c_void_p + gtlib.gt_feature_node_iterator_new.restype = c_void_p + gtlib.gt_feature_node_iterator_new.argtypes = [FeatureNode] + gtlib.gt_feature_node_iterator_new_direct.restype = c_void_p + gtlib.gt_feature_node_iterator_new_direct.argtypes = [FeatureNode] + gtlib.gt_feature_node_iterator_next.restype = c_void_p + + register = classmethod(register) + class FeatureNodeIteratorDepthFirst(FeatureNodeIterator): - def __init__(self, node): - self.i = gtlib.gt_feature_node_iterator_new(node) - self._as_parameter_ = self.i + + def __init__(self, node): + self.i = gtlib.gt_feature_node_iterator_new(node) + self._as_parameter_ = self.i + class FeatureNodeIteratorDirect(FeatureNodeIterator): - def __init__(self, node): - self.i = gtlib.gt_feature_node_iterator_new_direct(node) - self._as_parameter_ = self.i + + def __init__(self, node): + self.i = gtlib.gt_feature_node_iterator_new_direct(node) + self._as_parameter_ = self.i + + diff --git a/gtpython/gt/extended/genome_node.py b/gtpython/gt/extended/genome_node.py index ed3c904..f4cfcf5 100644 --- a/gtpython/gt/extended/genome_node.py +++ b/gtpython/gt/extended/genome_node.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008-2009 Sascha Steinbiss # Copyright (c) 2008-2009 Center for Bioinformatics, University of Hamburg @@ -21,88 +23,100 @@ from gt.extended.gff3_visitor import GFF3Visitor from gt.core.gtstr import Str from gt.props import cachedproperty + class GenomeNode(object): - - def __init__(self): - pass - - @classmethod - def create_from_ptr(cls, node_ptr, newref=False): - if node_ptr == 0 or node_ptr == None: - gterror("GenomeNode pointer cannot be NULL (was: " + str(node_ptr) + ")") - n = cls() - if newref: - n.own = True - n.gn = gtlib.gt_genome_node_ref(node_ptr) - else: - n.own = False - n.gn = node_ptr - n._as_parameter_ = n.gn - return n - - def __repr__(self): - c = self.__class__.__name__ - return "%s(start=%i, end=%i, seqid=\"%s\")" % \ - (c, self.start, self.end, self.seqid) - - def __del__(self): - if self.own: - try: - gtlib.gt_genome_node_delete(self.gn) - except AttributeError: + + def __init__(self): pass - def from_param(cls, obj): - if not isinstance(obj, GenomeNode): - raise TypeError, "argument must be a GenomeNode" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def get_range(self): - return (gtlib.gt_genome_node_get_start(self.gn), \ - gtlib.gt_genome_node_get_end(self.gn)) - range = property(get_range) - - def get_seqid(self): - return str(Str(gtlib.gt_genome_node_get_seqid(self.gn))) - seqid = cachedproperty(get_seqid) - - def get_start(self): - return gtlib.gt_genome_node_get_start(self.gn) - start = cachedproperty(get_start) - - def get_end(self): - return gtlib.gt_genome_node_get_end(self.gn) - end = cachedproperty(get_end) - - def get_filename(self): - return gtlib.gt_genome_node_get_filename(self.gn) - filename = property(get_filename) - - def get_line_number(self): - return gtlib.gt_genome_node_get_line_number(self.gn) - line_number = property(get_line_number) - - def accept(self, visitor): - err = Error() - rval = gtlib.gt_genome_node_accept(self.gn, visitor, err) - if rval != 0: - gterror(err) - - def register(cls, gtlib): - from ctypes import c_char_p, c_ulong, c_int, c_void_p, c_uint - gtlib.gt_genome_node_get_filename.restype = c_char_p - gtlib.gt_genome_node_get_filename.argtypes = [c_void_p] - gtlib.gt_genome_node_get_start.restype = c_ulong - gtlib.gt_genome_node_get_start.argtypes = [c_void_p] - gtlib.gt_genome_node_get_end.restype = c_ulong - gtlib.gt_genome_node_get_end.argtypes = [c_void_p] - gtlib.gt_genome_node_get_seqid.argtypes = [c_void_p] - gtlib.gt_genome_node_get_seqid.restype = Str - gtlib.gt_genome_node_get_filename.argtypes = [c_void_p] - gtlib.gt_genome_node_get_filename.restype = c_char_p - gtlib.gt_genome_node_get_line_number.argtypes = [c_void_p] - gtlib.gt_genome_node_get_line_number.restype = c_uint - gtlib.gt_genome_node_accept.restype = c_int - gtlib.gt_genome_node_accept.argtypes = [c_void_p, GFF3Visitor, Error] - register = classmethod(register) + @classmethod + def create_from_ptr(cls, node_ptr, newref=False): + if node_ptr == 0 or node_ptr == None: + gterror("GenomeNode pointer cannot be NULL (was: " + str(node_ptr) + + ")") + n = cls() + if newref: + n.own = True + n.gn = gtlib.gt_genome_node_ref(node_ptr) + else: + n.own = False + n.gn = node_ptr + n._as_parameter_ = n.gn + return n + + def __repr__(self): + c = self.__class__.__name__ + return "%s(start=%i, end=%i, seqid=\"%s\")" % (c, self.start, + self.end, self.seqid) + + def __del__(self): + if self.own: + try: + gtlib.gt_genome_node_delete(self.gn) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, GenomeNode): + raise TypeError, "argument must be a GenomeNode" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def get_range(self): + return (gtlib.gt_genome_node_get_start(self.gn), gtlib.gt_genome_node_get_end(self.gn)) + + range = property(get_range) + + def get_seqid(self): + return str(Str(gtlib.gt_genome_node_get_seqid(self.gn))) + + seqid = cachedproperty(get_seqid) + + def get_start(self): + return gtlib.gt_genome_node_get_start(self.gn) + + start = cachedproperty(get_start) + + def get_end(self): + return gtlib.gt_genome_node_get_end(self.gn) + + end = cachedproperty(get_end) + + def get_filename(self): + return gtlib.gt_genome_node_get_filename(self.gn) + + filename = property(get_filename) + + def get_line_number(self): + return gtlib.gt_genome_node_get_line_number(self.gn) + + line_number = property(get_line_number) + + def accept(self, visitor): + err = Error() + rval = gtlib.gt_genome_node_accept(self.gn, visitor, err) + if rval != 0: + gterror(err) + + def register(cls, gtlib): + from ctypes import c_char_p, c_ulong, c_int, c_void_p, c_uint + gtlib.gt_genome_node_get_filename.restype = c_char_p + gtlib.gt_genome_node_get_filename.argtypes = [c_void_p] + gtlib.gt_genome_node_get_start.restype = c_ulong + gtlib.gt_genome_node_get_start.argtypes = [c_void_p] + gtlib.gt_genome_node_get_end.restype = c_ulong + gtlib.gt_genome_node_get_end.argtypes = [c_void_p] + gtlib.gt_genome_node_get_seqid.argtypes = [c_void_p] + gtlib.gt_genome_node_get_seqid.restype = Str + gtlib.gt_genome_node_get_filename.argtypes = [c_void_p] + gtlib.gt_genome_node_get_filename.restype = c_char_p + gtlib.gt_genome_node_get_line_number.argtypes = [c_void_p] + gtlib.gt_genome_node_get_line_number.restype = c_uint + gtlib.gt_genome_node_accept.restype = c_int + gtlib.gt_genome_node_accept.argtypes = [c_void_p, GFF3Visitor, + Error] + + register = classmethod(register) + + diff --git a/gtpython/gt/extended/genome_stream.py b/gtpython/gt/extended/genome_stream.py index 0d58c8a..a866ca1 100644 --- a/gtpython/gt/extended/genome_stream.py +++ b/gtpython/gt/extended/genome_stream.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -22,36 +24,40 @@ from ctypes import byref, c_void_p class GenomeStream: - def __init__(self, *args): - raise NotImplementedError, "Please call the constructor of a " \ - "GenomeStream implementation." - - def __del__(self): - try: - gtlib.gt_node_stream_delete(self.gs) - except AttributeError: - pass - - def from_param(cls, obj): - if not isinstance(obj, GenomeStream): - raise TypeError, "argument must be a GenomeStream" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def next_tree(self): - err = Error() - genome_node = c_void_p() - rval = gtlib.gt_node_stream_next(self.gs, byref(genome_node), err) - if rval != 0: - gterror(err) - if genome_node.value == None: - return None - else: - return GenomeNode.create_from_ptr(genome_node.value) - - def pull(self): - err = Error() - rval = gtlib.gt_node_stream_pull(self.gs, err) - if rval != 0: - gterror(err) + + def __init__(self, *args): + raise NotImplementedError, \ + 'Please call the constructor of a GenomeStream implementation.' + + def __del__(self): + try: + gtlib.gt_node_stream_delete(self.gs) + except AttributeError: + pass + + def from_param(cls, obj): + if not isinstance(obj, GenomeStream): + raise TypeError, "argument must be a GenomeStream" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def next_tree(self): + err = Error() + genome_node = c_void_p() + rval = gtlib.gt_node_stream_next(self.gs, byref(genome_node), + err) + if rval != 0: + gterror(err) + if genome_node.value == None: + return None + else: + return GenomeNode.create_from_ptr(genome_node.value) + + def pull(self): + err = Error() + rval = gtlib.gt_node_stream_pull(self.gs, err) + if rval != 0: + gterror(err) + diff --git a/gtpython/gt/extended/gff3_in_stream.py b/gtpython/gt/extended/gff3_in_stream.py index 58cd819..c535147 100644 --- a/gtpython/gt/extended/gff3_in_stream.py +++ b/gtpython/gt/extended/gff3_in_stream.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -20,31 +22,37 @@ from gt.core.error import gterror from gt.core.str_array import StrArray from gt.extended.genome_stream import GenomeStream + class GFF3InStream(GenomeStream): - def __init__(self, filename): - try: - p = open(filename) - tmp = p.readline() - p.close() - except: - gterror("File " + filename + " not readable!") - self.gs = gtlib.gt_gff3_in_stream_new_sorted(filename, 0) - self._as_parameter_ = self.gs - - def from_param(cls, obj): - if not isinstance(obj, GFF3InStream): - raise TypeError, "argument must be a GFF3InStream" - return obj._as_parameter_ - from_param = classmethod(from_param) - - def get_used_types(self): - str_array_ptr = gtlib.gt_gff3_in_stream_get_used_types(self.gs) - used_types = StrArray(str_array_ptr) - return used_types.to_list() - - def register(cls, gtlib): - from ctypes import c_char_p, c_int, c_void_p - gtlib.gt_gff3_in_stream_new_sorted.argtypes = [c_char_p, c_int] - gtlib.gt_gff3_in_stream_get_used_types.restype = c_void_p - gtlib.gt_gff3_in_stream_new_sorted.restype = c_void_p - register = classmethod(register) + + def __init__(self, filename): + try: + p = open(filename) + tmp = p.readline() + p.close() + except: + gterror("File " + filename + " not readable!") + self.gs = gtlib.gt_gff3_in_stream_new_sorted(filename, 0) + self._as_parameter_ = self.gs + + def from_param(cls, obj): + if not isinstance(obj, GFF3InStream): + raise TypeError, "argument must be a GFF3InStream" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + def get_used_types(self): + str_array_ptr = gtlib.gt_gff3_in_stream_get_used_types(self.gs) + used_types = StrArray(str_array_ptr) + return used_types.to_list() + + def register(cls, gtlib): + from ctypes import c_char_p, c_int, c_void_p + gtlib.gt_gff3_in_stream_new_sorted.argtypes = [c_char_p, c_int] + gtlib.gt_gff3_in_stream_get_used_types.restype = c_void_p + gtlib.gt_gff3_in_stream_new_sorted.restype = c_void_p + + register = classmethod(register) + + diff --git a/gtpython/gt/extended/gff3_out_stream.py b/gtpython/gt/extended/gff3_out_stream.py index 9abcca1..153214e 100644 --- a/gtpython/gt/extended/gff3_out_stream.py +++ b/gtpython/gt/extended/gff3_out_stream.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -18,13 +20,18 @@ from gt.dlload import gtlib from gt.extended.genome_stream import GenomeStream + class GFF3OutStream(GenomeStream): - def __init__(self, genome_stream): - self.gs = gtlib.gt_gff3_out_stream_new(genome_stream, None) - self._as_parameter_ = self.gs - - def from_param(cls, obj): - if not isinstance(obj, GFF3OutStream): - raise TypeError, "argument must be a GFF3OutStream" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self, genome_stream): + self.gs = gtlib.gt_gff3_out_stream_new(genome_stream, None) + self._as_parameter_ = self.gs + + def from_param(cls, obj): + if not isinstance(obj, GFF3OutStream): + raise TypeError, "argument must be a GFF3OutStream" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + diff --git a/gtpython/gt/extended/gff3_visitor.py b/gtpython/gt/extended/gff3_visitor.py index b1a5867..ca01427 100644 --- a/gtpython/gt/extended/gff3_visitor.py +++ b/gtpython/gt/extended/gff3_visitor.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -17,16 +19,21 @@ from gt.dlload import gtlib + class GFF3Visitor: - def __init__(self): - self.gv = gtlib.gt_gff3_visitor_new(None) - self._as_parameter_ = self.gv - - def __del__(self): - gtlib.gt_node_visitor_delete(self.gv) - - def from_param(cls, obj): - if not isinstance(obj, GFF3Visitor): - raise TypeError, "argument must be a GFF3Visitor" - return obj._as_parameter_ - from_param = classmethod(from_param) + + def __init__(self): + self.gv = gtlib.gt_gff3_visitor_new(None) + self._as_parameter_ = self.gv + + def __del__(self): + gtlib.gt_node_visitor_delete(self.gv) + + def from_param(cls, obj): + if not isinstance(obj, GFF3Visitor): + raise TypeError, "argument must be a GFF3Visitor" + return obj._as_parameter_ + + from_param = classmethod(from_param) + + diff --git a/gtpython/gt/extended/strand.py b/gtpython/gt/extended/strand.py index 8708ed9..cc02cad 100644 --- a/gtpython/gt/extended/strand.py +++ b/gtpython/gt/extended/strand.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -15,4 +17,4 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # -strandchars = ['+','-','.'] +strandchars = ['+', '-', '.'] diff --git a/gtpython/gt/props.py b/gtpython/gt/props.py index a731b64..4091589 100644 --- a/gtpython/gt/props.py +++ b/gtpython/gt/props.py @@ -1,6 +1,10 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + class cachedproperty(object): - """ + + ''' >>> class C(object): ... def __init__(self, x): ... self._x = x @@ -58,18 +62,20 @@ class cachedproperty(object): >>> del c.x >>> c.x getting x - 'i am deleted' + \'i am deleted\' >>> c.set_x(22) setting x with 22 # but the property cant konw about it... >>> c.x - 'i am deleted' + \'i am deleted\' + + ''' - """ __slots__ = ("fget", "fset", "fdel", "n") + def __init__(self, fget=None, fset=None, fdel=None): self.fget = fget self.fset = fset @@ -77,28 +83,34 @@ class cachedproperty(object): self.n = "__" + fget.__name__ def __get__(self, o, otype=None): - if o is None: return None - if self.n in o.__dict__: return o.__dict__[self.n] - result = o.__dict__[self.n] = self.fget(o) + if o is None: + return None + if self.n in o.__dict__: + return (o.__dict__)[self.n] + result = (o.__dict__)[self.n] = self.fget(o) return result - + def __set__(self, o, value): if self.fset is None: - raise AttributeError, "unsettable %s (with %s)" % (self.n, value) + raise AttributeError, "unsettable %s (with %s)" % (self.n, + value) else: if o == 22: print self.n, o, value - if self.n in o.__dict__: del o.__dict__[self.n] + if self.n in o.__dict__: + del (o.__dict__)[self.n] self.fset(o, value) def __delete__(self, o): if self.fdel is None: - raise AttributeError, "undeletable %s (with %s)" % (self.n, value) + raise AttributeError, "undeletable %s (with %s)" % (self.n, + value) else: - if self.n in o.__dict__: del o.__dict__[self.n] + if self.n in o.__dict__: + del (o.__dict__)[self.n] self.fdel(o) - + if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + doctest.testmod() diff --git a/gtpython/setup.py b/gtpython/setup.py index 9b30551..66b3367 100644 --- a/gtpython/setup.py +++ b/gtpython/setup.py @@ -1,12 +1,10 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- from distutils.core import setup -setup(name='GenomeTools Python bindings', - version='0.1', - description='Python bindings for GenomeTools', - author='Sascha Steinbiss', - author_email='steinbiss@zbh.uni-hamburg.de', - url='http://www.genometools.org ', - packages=['gt', 'gt.core', 'gt.annotationsketch', 'gt.extended'], - ) +setup(name='GenomeTools Python bindings', version='0.1', description= + 'Python bindings for GenomeTools', author='Sascha Steinbiss', + author_email='steinbiss@zbh.uni-hamburg.de', url= + 'http://www.genometools.org ', packages=['gt', 'gt.core', + 'gt.annotationsketch', 'gt.extended']) diff --git a/gtpython/sketch_constructed.py b/gtpython/sketch_constructed.py index 669150e..e011ec3 100644 --- a/gtpython/sketch_constructed.py +++ b/gtpython/sketch_constructed.py @@ -1,3 +1,6 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + from gt.core import * from gt.extended import * from gt.annotationsketch import * @@ -6,38 +9,42 @@ from gt.core.gtrange import Range import sys if __name__ == "__main__": - if len(sys.argv) != 3: - sys.stderr.write("Usage: " + sys.argv[0] + " style_file PNG_file\n") - sys.exit(1) + if len(sys.argv) != 3: + sys.stderr.write("Usage: " + (sys.argv)[0] + + " style_file PNG_file\n") + sys.exit(1) - seqid = "chromosome_21" - nodes = [] + seqid = "chromosome_21" + nodes = [] # construct a gene on the forward strand with two exons - gene = FeatureNode.create_new(seqid, "gene", 100, 900, "+") - exon = FeatureNode.create_new(seqid, "exon", 100, 200, "+") - gene.add_child(exon) - intron = FeatureNode.create_new(seqid, "intron", 201, 799, "+") - gene.add_child(intron) - exon = FeatureNode.create_new(seqid, "exon", 800, 900, "+") - gene.add_child(exon) + + gene = FeatureNode.create_new(seqid, "gene", 100, 900, "+") + exon = FeatureNode.create_new(seqid, "exon", 100, 200, "+") + gene.add_child(exon) + intron = FeatureNode.create_new(seqid, "intron", 201, 799, "+") + gene.add_child(intron) + exon = FeatureNode.create_new(seqid, "exon", 800, 900, "+") + gene.add_child(exon) # construct a single-exon gene on the reverse strand # (within the intron of the forward strand gene) - reverse_gene = FeatureNode.create_new(seqid, "gene", 400, 600, "-") - reverse_exon = FeatureNode.create_new(seqid, "exon", 400, 600, "-") - reverse_gene.add_child(reverse_exon) - pngfile = sys.argv[2] + reverse_gene = FeatureNode.create_new(seqid, "gene", 400, 600, "-") + reverse_exon = FeatureNode.create_new(seqid, "exon", 400, 600, "-") + reverse_gene.add_child(reverse_exon) + + pngfile = (sys.argv)[2] - style = Style() - style.load_file(sys.argv[1]) + style = Style() + style.load_file((sys.argv)[1]) - diagram = Diagram.from_array([gene, reverse_gene], Range(1,1000), style) + diagram = Diagram.from_array([gene, reverse_gene], Range(1, 1000), + style) - layout = Layout(diagram, 600, style) - height = layout.get_height() - canvas = CanvasCairoFile(style, 600, height) - layout.sketch(canvas) + layout = Layout(diagram, 600, style) + height = layout.get_height() + canvas = CanvasCairoFile(style, 600, height) + layout.sketch(canvas) - canvas.to_file(pngfile) + canvas.to_file(pngfile) diff --git a/gtpython/sketch_parsed.py b/gtpython/sketch_parsed.py index 1360a43..d184e68 100644 --- a/gtpython/sketch_parsed.py +++ b/gtpython/sketch_parsed.py @@ -1,39 +1,51 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + from gt.annotationsketch import * from gt.core.gtrange import Range import sys if __name__ == "__main__": - if len(sys.argv) != 4: - sys.stderr.write("Usage: " + sys.argv[0] + " Style_file PNG_file GFF3_file\n") - sys.stderr.write("Create PNG representation of GFF3 annotation file.") - sys.exit(1) + if len(sys.argv) != 4: + sys.stderr.write("Usage: " + (sys.argv)[0] + + " Style_file PNG_file GFF3_file\n") + sys.stderr.write("Create PNG representation of GFF3 annotation file.") + sys.exit(1) - pngfile = sys.argv[2] + pngfile = (sys.argv)[2] # load style file - style = Style() - style.load_file(sys.argv[1]) + + style = Style() + style.load_file((sys.argv)[1]) # create feature index - feature_index = FeatureIndexMemory() + + feature_index = FeatureIndexMemory() # add GFF3 file to index - feature_index.add_gff3file(sys.argv[3]) + + feature_index.add_gff3file((sys.argv)[3]) # create diagram for first sequence ID in feature index - seqid = feature_index.get_first_seqid() - range = feature_index.get_range_for_seqid(seqid) - diagram = Diagram.from_index(feature_index, seqid, range, style) + + seqid = feature_index.get_first_seqid() + range = feature_index.get_range_for_seqid(seqid) + diagram = Diagram.from_index(feature_index, seqid, range, style) # create layout - layout = Layout(diagram, 600, style) - height = layout.get_height() + + layout = Layout(diagram, 600, style) + height = layout.get_height() # create canvas - canvas = CanvasCairoFile(style, 600, height) + + canvas = CanvasCairoFile(style, 600, height) # sketch layout on canvas - layout.sketch(canvas) + + layout.sketch(canvas) # write canvas to file - canvas.to_file(pngfile) + + canvas.to_file(pngfile) diff --git a/gtpython/tests/__init__.py b/gtpython/tests/__init__.py index 816acc0..a3ed30d 100644 --- a/gtpython/tests/__init__.py +++ b/gtpython/tests/__init__.py @@ -1,7 +1,9 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + from test_featurenode import * from test_iterators import * from test_stream import * - import unittest unittest.main() diff --git a/gtpython/tests/test_featurenode.py b/gtpython/tests/test_featurenode.py index 19f23c5..3a6ebac 100644 --- a/gtpython/tests/test_featurenode.py +++ b/gtpython/tests/test_featurenode.py @@ -1,7 +1,10 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- import unittest from gt import FeatureNode, FeatureNodeIteratorDepthFirst, GenomeNode + class FeatureNodeTestCase(unittest.TestCase): def setUp(self): @@ -9,8 +12,7 @@ class FeatureNodeTestCase(unittest.TestCase): def test_repr(self): self.assertEqual(str(self.fn), - 'FeatureNode(start=100, end=500, seqid="test")') - + 'FeatureNode(start=100, end=500, seqid="test")') def test_score(self): fn = self.fn @@ -40,26 +42,28 @@ class FeatureNodeTestCase(unittest.TestCase): fn = self.fn self.assertEqual(fn.start, 100) self.assertEqual(fn.end, 500) - + def test_attributes(self): fn = self.fn - fn.add_attribute("test","testval") - fn.add_attribute("test2","testval2") + fn.add_attribute("test", "testval") + fn.add_attribute("test2", "testval2") self.assert_("test" in fn.attribs) self.assert_("test2" in fn.attribs) nattrs = 0 - for tag, val in fn.each_attribute(): + for (tag, val) in fn.each_attribute(): self.assertEqual(val, fn.get_attribute(tag)) nattrs += 1 - + self.assertEqual(nattrs, 2) + class TestFeatureNodeChildren(unittest.TestCase): + def setUp(self): - self.fn = FeatureNode.create_new("test", "type", 100, 500, "+") - self.fn2 = FeatureNode.create_new("test", "type2", 200,300,"+") + self.fn = FeatureNode.create_new("test", "type", 100, 500, "+") + self.fn2 = FeatureNode.create_new("test", "type2", 200, 300, "+") self.fn.add_child(self.fn2) def test_phase(self): @@ -79,9 +83,7 @@ class TestFeatureNodeChildren(unittest.TestCase): num_features += 1 self.assertEqual(num_features, 2) - - - fn3 = FeatureNode.create_new("test", "type3", 250,300,"+") + fn3 = FeatureNode.create_new("test", "type3", 250, 300, "+") fn.add_child(fn3) fni = FeatureNodeIteratorDepthFirst(fn) @@ -94,8 +96,9 @@ class TestFeatureNodeChildren(unittest.TestCase): class TestFeatureNodeProperties(unittest.TestCase): + def setUp(self): - self.fn = FeatureNode.create_new("test", "type", 100, 500, "+") + self.fn = FeatureNode.create_new("test", "type", 100, 500, "+") def test_strand(self): fn = self.fn @@ -114,6 +117,7 @@ class TestFeatureNodeProperties(unittest.TestCase): fn.set_score(4) self.assertEqual(2, fn.score) + def test_range(self): fn = self.fn self.assertEqual((100, 500), fn.range) @@ -126,7 +130,7 @@ class TestFeatureNodeProperties(unittest.TestCase): f2 = FeatureNode.create_from_ptr(g.gn, True) self.assertEqual((100, 500), f2.range) + if __name__ == "__main__": unittest.main() - diff --git a/gtpython/tests/test_iterators.py b/gtpython/tests/test_iterators.py index f11af64..c341869 100644 --- a/gtpython/tests/test_iterators.py +++ b/gtpython/tests/test_iterators.py @@ -1,10 +1,13 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- import unittest import gt import os op = os.path -datadir = op.abspath(op.join(op.dirname(__file__), "..", "..", "testdata")) +datadir = op.abspath(op.join(op.dirname(__file__), "..", "..", + "testdata")) """ ### @@ -22,6 +25,7 @@ datadir = op.abspath(op.join(op.dirname(__file__), "..", "..", "testdata")) ### """ + class FeatureNodeIteratorTest(unittest.TestCase): def setUp(self): @@ -45,7 +49,6 @@ class FeatureNodeIteratorTest(unittest.TestCase): found = dfi.next() self.assertEqual(found.type, 'exon') - def test_direct(self): di = gt.FeatureNodeIteratorDirect(self.feature) found = di.next() @@ -55,6 +58,7 @@ class FeatureNodeIteratorTest(unittest.TestCase): found = di.next() #self.assert_('gene' in types) + self.assert_('exon' in types) self.assert_('five_prime_splice_site' in types) self.assert_('three_prime_splice_site' in types) @@ -65,4 +69,3 @@ class FeatureNodeIteratorTest(unittest.TestCase): if __name__ == "__main__": unittest.main() - diff --git a/gtpython/tests/test_stream.py b/gtpython/tests/test_stream.py index 728e16d..378a80d 100644 --- a/gtpython/tests/test_stream.py +++ b/gtpython/tests/test_stream.py @@ -1,10 +1,14 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- import unittest import gt import os op = os.path -datadir = op.abspath(op.join(op.dirname(__file__), "..", "..", "testdata")) +datadir = op.abspath(op.join(op.dirname(__file__), "..", "..", + "testdata")) + class StreamTest(unittest.TestCase): diff --git a/src/external/rnv-1.7.8/tools/rvp.py b/src/external/rnv-1.7.8/tools/rvp.py index c1f9b00..54ac0f8 100644 --- a/src/external/rnv-1.7.8/tools/rvp.py +++ b/src/external/rnv-1.7.8/tools/rvp.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # $Id: rvp.py,v 1.5 2004/11/09 11:29:31 dvd Exp $ # embedding sample for RVP, a part of RNV, http://davidashen.net/rnv.html @@ -7,143 +9,193 @@ # details of the protocol are in a long comment near the start of rvp.c # -import sys, os, string, re, xml.parsers.expat +import sys +import os +import string +import re +import xml.parsers.expat -global rvpin,rvpout,pat,errors,parser,text,ismixed,prevline,prevcol +global rvpin, rvpout, pat, errors, parser, text, ismixed, prevline, \ + prevcol # raised by resp if it gets something the module does not understand + + class ProtocolError(Exception): - def __init__(self, value): - self.value = value - def __str__(self): - return repr(self.value) + + def __init__(self, value): + self.value = value + + def __str__(self): + return repr(self.value) + # run RVP with grammar specified on the command line + + def launch(): - global rvpin,rvpout - inp,out=os.popen2('rvp '+string.join(sys.argv[1:],' ')) - rvpin,rvpout=inp.fileno(),out.fileno() # os.read|os.write will be used + global rvpin, rvpout + (inp, out) = os.popen2('rvp ' + string.join((sys.argv)[1:], ' ')) + (rvpin, rvpout) = (inp.fileno(), out.fileno()) # os.read|os.write will be used + # terminate string with zero, encode in utf-8 and then send to RVP + + def send(s): - os.write(rvpin,s.encode('UTF-8')+'\0') + os.write(rvpin, s.encode('UTF-8') + '\0') + # receive a zero-terminated response from RVP, zero byte is dropped + + def recv(): - s='' - while 1: - s=s+os.read(rvpout,16) # 16 is good for ok responses, errors should be rare - if(s[-1]=='\0'): break # last character in a response is always '\0' - return s[:-1] + s = '' + while 1: + s = s + os.read(rvpout, 16) # 16 is good for ok responses, errors should be rare + if s[-1] == '\0': # last character in a response is always '\0' + break + return s[:-1] + # return current pattern value, and print error message on stderr, if any + + def resp(): - global errors,prevline,prevcol - r=string.split(recv(),' ',3) - if(r[0]=='ok'): return r[1] - if(r[0]=='error'): - errors=1 - if(r[3]!=''): # if the error string is empty, then error - # is occured in erroneous state; don't report - line,col=parser.ErrorLineNumber,parser.ErrorColumnNumber - if(line!=prevline or col!=prevcol): # one report per file position - sys.stderr.write(str(line)+","+str(col)+": "+r[3]) - prevline,prevcol=line,col - return r[1] - if(r[0]=='er'): - errors=1 - return r[1] - raise ProtocolError,"unexpected response '"+r[0]+"'" - -def start_tag_open(cur,name): - send('start-tag-open '+cur+' '+name) - return resp() - -def attribute(cur,name,val): - send('attribute '+cur+' '+name+' '+val) - return resp() - -def start_tag_close(cur,name): - send('start-tag-close '+cur+' '+name) - return resp() - -def end_tag(cur,name): - send('end-tag '+cur+' '+name) - return resp() - -def textonly(cur,text): - send('text '+cur+' '+text) - return resp() + global errors, prevline, prevcol + r = string.split(recv(), ' ', 3) + if r[0] == 'ok': + return r[1] + if r[0] == 'error': + errors = 1 + if r[3] != '': # if the error string is empty, then error + + # is occured in erroneous state; don't report + + (line, col) = (parser.ErrorLineNumber, parser.ErrorColumnNumber) + if line != prevline or col != prevcol: # one report per file position + sys.stderr.write(str(line) + "," + str(col) + ": " + r[3]) + (prevline, prevcol) = (line, col) + return r[1] + if r[0] == 'er': + errors = 1 + return r[1] + raise ProtocolError, "unexpected response '" + r[0] + "'" + + +def start_tag_open(cur, name): + send('start-tag-open ' + cur + ' ' + name) + return resp() + + +def attribute(cur, name, val): + send('attribute ' + cur + ' ' + name + ' ' + val) + return resp() + + +def start_tag_close(cur, name): + send('start-tag-close ' + cur + ' ' + name) + return resp() + + +def end_tag(cur, name): + send('end-tag ' + cur + ' ' + name) + return resp() + + +def textonly(cur, text): + send('text ' + cur + ' ' + text) + return resp() + # in mixed content, whitespace is simply discarded, and any # non-whitespace is equal; but this optimization gives only # 5% increase in speed at most in practical cases -def mixed(cur,text): - if(re.search('[^\t\n ]',text)): - send('mixed '+cur+' .') - return resp() - else: return cur + + +def mixed(cur, text): + if re.search('[^\t\n ]', text): + send('mixed ' + cur + ' .') + return resp() + else: + return cur + def start(g): - send('start '+g) - return resp() + send('start ' + g) + return resp() + def quit(): - send('quit') - return resp() + send('quit') + return resp() + # Expat handlers # If I remember correctly, Expat has the custom not to concatenate # text nodes; therefore CharDataHandler just concatenates them into # text, and then flush_text passes the text to the validator + + def flush_text(): - global ismixed,pat,text + global ismixed, pat, text + + if ismixed: + pat = mixed(pat, text) + else: + pat = textonly(pat, text) + text = '' - if(ismixed): - pat=mixed(pat,text) - else: - pat=textonly(pat,text) - text='' -def start_element(name,attrs): - global ismixed,pat +def start_element(name, attrs): + global ismixed, pat + + ismixed = 1 + flush_text() + pat = start_tag_open(pat, name) + ismixed = 0 + for (n, v) in attrs.items(): + pat = attribute(pat, n, v) + pat = start_tag_close(pat, name) - ismixed=1 - flush_text() - pat=start_tag_open(pat,name) - ismixed=0 - for n,v in attrs.items(): pat=attribute(pat,n,v) - pat=start_tag_close(pat,name) def end_element(name): - global ismixed,pat + global ismixed, pat + + flush_text() + pat = end_tag(pat, name) + ismixed = 1 - flush_text() - pat=end_tag(pat,name) - ismixed=1 def characters(data): - global text + global text + + text = text + data - text=text+data # Main -errors=0 +errors = 0 launch() -pat=start('0') # that is, start of the first grammar; - # multiple grammars can be passed to rvp -parser=xml.parsers.expat.ParserCreate('UTF-8',':') # last colon in the name +pat = start('0') # that is, start of the first grammar; + + # multiple grammars can be passed to rvp + +parser = xml.parsers.expat.ParserCreate('UTF-8', ':') # last colon in the name + # separates local name from namespace URI -parser.StartElementHandler=start_element -parser.EndElementHandler=end_element -parser.CharacterDataHandler=characters -text='' -prevline,prevcol=-1,-1 +parser.StartElementHandler = start_element +parser.EndElementHandler = end_element +parser.CharacterDataHandler = characters + +text = '' +(prevline, prevcol) = (-1, -1) parser.ParseFile(sys.stdin) -quit() # this stops RVP; many files can be validated with a single RVP +quit() # this stops RVP; many files can be validated with a single RVP + # running, concurrently or sequentially + sys.exit(errors) diff --git a/testdata/gtpython/block_stuff.py b/testdata/gtpython/block_stuff.py index 9b24ea4..85b1fc7 100644 --- a/testdata/gtpython/block_stuff.py +++ b/testdata/gtpython/block_stuff.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -22,40 +23,45 @@ from gt.annotationsketch import * import sys import re + class TestFailedError(Exception): - pass + + pass + def testfunc(bl): - b = Block(bl) - print "%s %s %s %d" % (b.get_type(), b.get_strand(), b.get_range(), b.get_size()) - if not b.get_top_level_feature(): - raise TestFailedError - return b.get_type() + b = Block(bl) + print "%s %s %s %d" % (b.get_type(), b.get_strand(), b.get_range(), + b.get_size()) + if not b.get_top_level_feature(): + raise TestFailedError + return b.get_type() + if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Test the TrackSelector callback and Block bindings.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write("Test the TrackSelector callback and Block bindings.") + sys.exit(1) - genome_stream = GFF3InStream(sys.argv[1]) + genome_stream = GFF3InStream((sys.argv)[1]) # instantiate index object - feature_index = FeatureIndexMemory() - genome_stream = FeatureStream(genome_stream, feature_index) - feature = genome_stream.next_tree() - while feature: - feature = genome_stream.next_tree() + feature_index = FeatureIndexMemory() + genome_stream = FeatureStream(genome_stream, feature_index) - seqid = feature_index.get_first_seqid() - range = feature_index.get_range_for_seqid(seqid) + feature = genome_stream.next_tree() + while feature: + feature = genome_stream.next_tree() - style = Style() - diagram = Diagram.from_index(feature_index, seqid, range, style) - diagram.set_track_selector_func(testfunc) + seqid = feature_index.get_first_seqid() + range = feature_index.get_range_for_seqid(seqid) - layout = Layout(diagram, 800, style) - height = layout.get_height() + style = Style() + diagram = Diagram.from_index(feature_index, seqid, range, style) + diagram.set_track_selector_func(testfunc) + layout = Layout(diagram, 800, style) + height = layout.get_height() diff --git a/testdata/gtpython/feature_node.py b/testdata/gtpython/feature_node.py index defccc1..d080768 100644 --- a/testdata/gtpython/feature_node.py +++ b/testdata/gtpython/feature_node.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -22,60 +23,63 @@ from gt.annotationsketch import * import sys import re + class TestFailedError(Exception): - pass + + pass + if __name__ == "__main__": - fn = FeatureNode.create_new("test", "type", 100, 500, "+") - fn.add_attribute("test","testval") - fn.add_attribute("test2","testval2") - if fn.score_is_defined(): - raise TestFailedError - fn.set_score(2) - if not fn.score_is_defined(): - raise TestFailedError - if fn.get_score() != 2: - raise TestFailedError - fn.unset_score() - if fn.score_is_defined(): - raise TestFailedError - if fn.has_type("foo"): - raise TestFailedError - if not fn.has_type("type"): - raise TestFailedError - if not fn.get_strand() == "+": - raise TestFailedError - fn2 = FeatureNode.create_new("test", "type2", 200,300,"+") - fn.add_child(fn2) - num_attrs = 0 - for tag, val in fn.each_attribute(): - if not val == fn.get_attribute(tag): - raise TestFailedError - num_attrs += 1 - if not num_attrs == 2: - raise TestFailedError - if fn.get_phase() != 3: #undefined - raise TestFailedError - fn.set_phase(0) - if fn.get_phase() != 0: #zero - raise TestFailedError + fn = FeatureNode.create_new("test", "type", 100, 500, "+") + fn.add_attribute("test", "testval") + fn.add_attribute("test2", "testval2") + if fn.score_is_defined(): + raise TestFailedError + fn.set_score(2) + if not fn.score_is_defined(): + raise TestFailedError + if fn.get_score() != 2: + raise TestFailedError + fn.unset_score() + if fn.score_is_defined(): + raise TestFailedError + if fn.has_type("foo"): + raise TestFailedError + if not fn.has_type("type"): + raise TestFailedError + if not fn.get_strand() == "+": + raise TestFailedError + fn2 = FeatureNode.create_new("test", "type2", 200, 300, "+") + fn.add_child(fn2) + num_attrs = 0 + for (tag, val) in fn.each_attribute(): + if not val == fn.get_attribute(tag): + raise TestFailedError + num_attrs += 1 + if not num_attrs == 2: + raise TestFailedError + if fn.get_phase() != 3: #undefined + raise TestFailedError + fn.set_phase(0) + if fn.get_phase() != 0: #zero + raise TestFailedError - fni = FeatureNodeIteratorDepthFirst(fn) - num_features = 0 - tfn = fni.next() - while tfn: - num_features += 1 + fni = FeatureNodeIteratorDepthFirst(fn) + num_features = 0 tfn = fni.next() - if num_features != 2: - raise TestFailedError + while tfn: + num_features += 1 + tfn = fni.next() + if num_features != 2: + raise TestFailedError - fn3 = FeatureNode.create_new("test", "type3", 250,300,"+") - fn.add_child(fn3) - fni = FeatureNodeIteratorDepthFirst(fn) - num_features = 0 - tfn = fni.next() - while tfn: - num_features += 1 + fn3 = FeatureNode.create_new("test", "type3", 250, 300, "+") + fn.add_child(fn3) + fni = FeatureNodeIteratorDepthFirst(fn) + num_features = 0 tfn = fni.next() - if num_features != 3: - raise TestFailedError + while tfn: + num_features += 1 + tfn = fni.next() + if num_features != 3: + raise TestFailedError diff --git a/testdata/gtpython/feature_stuff.py b/testdata/gtpython/feature_stuff.py index f18c5bb..43d97c6 100644 --- a/testdata/gtpython/feature_stuff.py +++ b/testdata/gtpython/feature_stuff.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -22,48 +23,53 @@ from gt.annotationsketch import * import sys import re + class TestFailedError(Exception): - pass + + pass + if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Test the FeatureIndex and FeatureStream bindings on" \ - "GFF3 file.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write('Test the FeatureIndex and FeatureStream bindings onGFF3 file.') + sys.exit(1) - genome_stream = GFF3InStream(sys.argv[1]) + genome_stream = GFF3InStream((sys.argv)[1]) # try to instantiate an interface -> should fail - try: - feature_index = FeatureIndex() - except NotImplementedError: - pass - else: - raise + + try: + feature_index = FeatureIndex() + except NotImplementedError: + pass + else: + raise # instantiate index object - feature_index = FeatureIndexMemory() - genome_stream = FeatureStream(genome_stream, feature_index) - feature = genome_stream.next_tree() - while feature: + feature_index = FeatureIndexMemory() + genome_stream = FeatureStream(genome_stream, feature_index) + feature = genome_stream.next_tree() + while feature: + feature = genome_stream.next_tree() - seqid = feature_index.get_first_seqid() - features = feature_index.get_features_for_seqid(seqid) - if not features: - raise TestFailedError + seqid = feature_index.get_first_seqid() + features = feature_index.get_features_for_seqid(seqid) + if not features: + raise TestFailedError - rng = feature_index.get_range_for_seqid(seqid) - features_r = feature_index.get_features_for_range(rng.start, rng.end, seqid) + rng = feature_index.get_range_for_seqid(seqid) + features_r = feature_index.get_features_for_range(rng.start, rng.end, + seqid) - if not len(features) == len(features_r): - raise TestFailedError + if not len(features) == len(features_r): + raise TestFailedError - gff3_visitor = GFF3Visitor() + gff3_visitor = GFF3Visitor() - for feature in features: - feature.accept(gff3_visitor) - if not isinstance(feature, FeatureNode): - raise TestFailedError + for feature in features: + feature.accept(gff3_visitor) + if not isinstance(feature, FeatureNode): + raise TestFailedError diff --git a/testdata/gtpython/genome_visitor.py b/testdata/gtpython/genome_visitor.py index d4863cd..f17bdad 100644 --- a/testdata/gtpython/genome_visitor.py +++ b/testdata/gtpython/genome_visitor.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,14 +24,14 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Parse and output the given GFF3 file.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write("Parse and output the given GFF3 file.") + sys.exit(1) - genome_stream = GFF3InStream(sys.argv[1]) - gff3_visitor = GFF3Visitor() - feature = genome_stream.next_tree() - while feature: - feature.accept(gff3_visitor) + genome_stream = GFF3InStream((sys.argv)[1]) + gff3_visitor = GFF3Visitor() feature = genome_stream.next_tree() + while feature: + feature.accept(gff3_visitor) + feature = genome_stream.next_tree() diff --git a/testdata/gtpython/gff3.py b/testdata/gtpython/gff3.py index 8c7a1d3..d5f0872 100644 --- a/testdata/gtpython/gff3.py +++ b/testdata/gtpython/gff3.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,13 +24,13 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Parse and output the given GFF3 file.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write("Parse and output the given GFF3 file.") + sys.exit(1) - in_stream = GFF3InStream(sys.argv[1]) - out_stream = GFF3OutStream(in_stream) - feature = out_stream.next_tree() - while feature: + in_stream = GFF3InStream((sys.argv)[1]) + out_stream = GFF3OutStream(in_stream) feature = out_stream.next_tree() + while feature: + feature = out_stream.next_tree() diff --git a/testdata/gtpython/graphics_stuff.py b/testdata/gtpython/graphics_stuff.py index 0a11272..60abddc 100644 --- a/testdata/gtpython/graphics_stuff.py +++ b/testdata/gtpython/graphics_stuff.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -21,58 +22,114 @@ from gt.extended import * from gt.annotationsketch import * import sys + class TestFailedError(Exception): - pass + + pass + if __name__ == "__main__": - if len(sys.argv) != 3: - sys.stderr.write("Usage: " + sys.argv[0] + " curve_coords PNG_file\n") - sys.stderr.write("Tests drawing functions in the Graphics object.\n") - sys.exit(1) - g = GraphicsCairoSVG(700, 800) + if len(sys.argv) != 3: + sys.stderr.write("Usage: " + (sys.argv)[0] + + " curve_coords PNG_file\n") + sys.stderr.write("Tests drawing functions in the Graphics object.\n") + sys.exit(1) + g = GraphicsCairoSVG(700, 800) - g.draw_text(300, 20, "draw_text") - g.draw_text_right(300, 40, "draw_text_left") - g.draw_text_centered(300, 60, "draw_text_centered") - g.draw_colored_text(300, 80, Color(1,0,0,0.7), "draw_colored_text") - g.set_margins(20, 30) - if g.get_image_height() != 800: - raise TestFailedError - if g.get_image_width() != 700: - raise TestFailedError - if g.get_xmargins() != 20: - raise TestFailedError - if g.get_ymargins() != 30: - raise TestFailedError + g.draw_text(300, 20, "draw_text") + g.draw_text_right(300, 40, "draw_text_left") + g.draw_text_centered(300, 60, "draw_text_centered") + g.draw_colored_text(300, 80, Color(1, 0, 0, 0.7), + "draw_colored_text") + g.set_margins(20, 30) + if g.get_image_height() != 800: + raise TestFailedError + if g.get_image_width() != 700: + raise TestFailedError + if g.get_xmargins() != 20: + raise TestFailedError + if g.get_ymargins() != 30: + raise TestFailedError - g.draw_horizontal_line(150, 100, Color(0,0,0,0.3), 300, 5.5) - g.draw_vertical_line(200, 70, Color(1,0,0,0.3), 100, 3.5) - g.draw_box(150, 120, 300, 30, Color(0,1,0,0.2), ARROW_LEFT, 20, 2, \ - Color(0,0,0,1), False) - g.draw_box(150, 170, 300, 20, Color(0,1,1,0.2), ARROW_RIGHT, 20, 1, \ - Color(0,0,0,1), False) - g.draw_box(150, 210, 300, 20, Color(1,0,0,0.2), ARROW_BOTH, 20, 1, \ - Color(0,0,0,1), False) - g.draw_box(300, 250, 20, 20, Color(1,0,0,0.2), ARROW_BOTH, 20, 1, \ - Color(0,0,0,1), False) - g.draw_box(150, 290, 300, 20, Color(1,0,0,0.2), ARROW_NONE, 20, 1, \ - Color(0,0,0,1), False) - g.draw_dashes(159, 340, 90, 20, ARROW_RIGHT, 20, 1, Color(0,0,0,1)) - g.draw_dashes(359, 340, 60, 20, ARROW_LEFT, 20, 1, Color(0,0,0,1)) - g.draw_dashes(59, 340, 60, 20, ARROW_NONE, 20, 1, Color(0,0,0,1)) - g.draw_dashes(559, 340, 60, 20, ARROW_BOTH, 20, 1, Color(0,0,0,1)) - g.draw_caret(159, 390, 90, 20, ARROW_RIGHT, 20, 1, Color(0,0,0,1)) - g.draw_caret(359, 390, 60, 20, ARROW_LEFT, 20, 1, Color(0,0,0,1)) - g.draw_caret(59, 390, 60, 20, ARROW_NONE, 20, 1, Color(0,0,0,1)) - g.draw_caret(559, 390, 60, 20, ARROW_BOTH, 20, 1, Color(0,0,0,1)) + g.draw_horizontal_line(150, 100, Color(0, 0, 0, 0.3), 300, 5.5) + g.draw_vertical_line(200, 70, Color(1, 0, 0, 0.3), 100, 3.5) + g.draw_box( + 150, + 120, + 300, + 30, + Color(0, 1, 0, 0.2), + ARROW_LEFT, + 20, + 2, + Color(0, 0, 0, 1), + False, + ) + g.draw_box( + 150, + 170, + 300, + 20, + Color(0, 1, 1, 0.2), + ARROW_RIGHT, + 20, + 1, + Color(0, 0, 0, 1), + False, + ) + g.draw_box( + 150, + 210, + 300, + 20, + Color(1, 0, 0, 0.2), + ARROW_BOTH, + 20, + 1, + Color(0, 0, 0, 1), + False, + ) + g.draw_box( + 300, + 250, + 20, + 20, + Color(1, 0, 0, 0.2), + ARROW_BOTH, + 20, + 1, + Color(0, 0, 0, 1), + False, + ) + g.draw_box( + 150, + 290, + 300, + 20, + Color(1, 0, 0, 0.2), + ARROW_NONE, + 20, + 1, + Color(0, 0, 0, 1), + False, + ) + g.draw_dashes(159, 340, 90, 20, ARROW_RIGHT, 20, 1, Color(0, 0, 0, 1)) + g.draw_dashes(359, 340, 60, 20, ARROW_LEFT, 20, 1, Color(0, 0, 0, 1)) + g.draw_dashes(59, 340, 60, 20, ARROW_NONE, 20, 1, Color(0, 0, 0, 1)) + g.draw_dashes(559, 340, 60, 20, ARROW_BOTH, 20, 1, Color(0, 0, 0, 1)) + g.draw_caret(159, 390, 90, 20, ARROW_RIGHT, 20, 1, Color(0, 0, 0, 1)) + g.draw_caret(359, 390, 60, 20, ARROW_LEFT, 20, 1, Color(0, 0, 0, 1)) + g.draw_caret(59, 390, 60, 20, ARROW_NONE, 20, 1, Color(0, 0, 0, 1)) + g.draw_caret(559, 390, 60, 20, ARROW_BOTH, 20, 1, Color(0, 0, 0, 1)) - from random import random - data = [] - ndata = 0 - f = open(sys.argv[1]) - for line in f: - data.append(float(line)) - ndata+=1 - g.draw_curve_data(20, 430, Color(0,0,1,.6), data, ndata, Range(0, 1), 40) + from random import random + data = [] + ndata = 0 + f = open((sys.argv)[1]) + for line in f: + data.append(float(line)) + ndata += 1 + g.draw_curve_data(20, 430, Color(0, 0, 1, .6), data, ndata, Range(0, + 1), 40) - g.to_file(sys.argv[2]) + g.to_file((sys.argv)[2]) diff --git a/testdata/gtpython/show_recmaps.py b/testdata/gtpython/show_recmaps.py index 5c88bef..0feae2c 100644 --- a/testdata/gtpython/show_recmaps.py +++ b/testdata/gtpython/show_recmaps.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,29 +24,32 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Show RecMaps of GFF3 annotation file.") - sys.exit(1) - - in_stream = GFF3InStream(sys.argv[1]) - feature_index = FeatureIndexMemory() - feature_stream = FeatureStream(in_stream, feature_index) - gn = feature_stream.next_tree() - # fill feature index - while gn: + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write("Show RecMaps of GFF3 annotation file.") + sys.exit(1) + + in_stream = GFF3InStream((sys.argv)[1]) + feature_index = FeatureIndexMemory() + feature_stream = FeatureStream(in_stream, feature_index) gn = feature_stream.next_tree() - seqid = feature_index.get_first_seqid() - range = feature_index.get_range_for_seqid(seqid) + # fill feature index + + while gn: + gn = feature_stream.next_tree() + + seqid = feature_index.get_first_seqid() + range = feature_index.get_range_for_seqid(seqid) - style = Style() - diagram = Diagram.from_index(feature_index, seqid, range, style) - layout = Layout(diagram, 700, style) - height = layout.get_height() - image_info = ImageInfo() - canvas = CanvasCairoFile(style, 800, height, image_info) - layout.sketch(canvas) + style = Style() + diagram = Diagram.from_index(feature_index, seqid, range, style) + layout = Layout(diagram, 700, style) + height = layout.get_height() + image_info = ImageInfo() + canvas = CanvasCairoFile(style, 800, height, image_info) + layout.sketch(canvas) - for x1, y1, x2, y2, gn in image_info.each_hotspot(): - print "x1=%d, y1=%d, x2=%d, y2=%d, gn.type=%s" % (x1,y1,x2,y2,gn.get_type()) + for (x1, y1, x2, y2, gn) in image_info.each_hotspot(): + print "x1=%d, y1=%d, x2=%d, y2=%d, gn.type=%s" % (x1, y1, x2, y2, + gn.get_type()) diff --git a/testdata/gtpython/show_seqids.py b/testdata/gtpython/show_seqids.py index 00cfa1c..8d2032f 100644 --- a/testdata/gtpython/show_seqids.py +++ b/testdata/gtpython/show_seqids.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,19 +24,21 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Show sequence ids contained in GFF3 annotation file.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write("Show sequence ids contained in GFF3 annotation file.") + sys.exit(1) - in_stream = GFF3InStream(sys.argv[1]) - feature_index = FeatureIndexMemory() - feature_stream = FeatureStream(in_stream, feature_index) - gn = feature_stream.next_tree() - # fill feature index - while gn: + in_stream = GFF3InStream((sys.argv)[1]) + feature_index = FeatureIndexMemory() + feature_stream = FeatureStream(in_stream, feature_index) gn = feature_stream.next_tree() - seqids = feature_index.get_seqids() - for seqid in seqids: - print seqid + # fill feature index + + while gn: + gn = feature_stream.next_tree() + + seqids = feature_index.get_seqids() + for seqid in seqids: + print seqid diff --git a/testdata/gtpython/sketch-failures.py b/testdata/gtpython/sketch-failures.py index e84b4fb..c7b0b8b 100644 --- a/testdata/gtpython/sketch-failures.py +++ b/testdata/gtpython/sketch-failures.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,72 +24,79 @@ from gt.annotationsketch import * import sys import re + class TestFailedError(Exception): - pass + + pass + if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Test failures in GFF3 annotation file.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write("Test failures in GFF3 annotation file.") + sys.exit(1) - feature_index = FeatureIndexMemory() - feature_index.add_gff3file(sys.argv[1]) + feature_index = FeatureIndexMemory() + feature_index.add_gff3file((sys.argv)[1]) - seqid = feature_index.get_first_seqid() - range = feature_index.get_range_for_seqid(seqid) + seqid = feature_index.get_first_seqid() + range = feature_index.get_range_for_seqid(seqid) - style = Style() - style.set_num("format", "margins", 40) + style = Style() + style.set_num("format", "margins", 40) # check error reporting in the Diagram class - try: - diagram = Diagram.from_index(feature_index, "nonexist", range, style) - except GTError, strerr: - if -1 == str(strerr).find("FeatureIndex does not contain seqid"): - raise TestFailedError - else: - raise TestFailedError - try: - diagram = Diagram.from_index(feature_index, seqid, range, 42) - except ArgumentError, strerr: - if -1 == str(strerr).find("must be a Style"): - raise TestFailedError - else: - raise TestFailedError - try: - diagram = Diagram.from_index(feature_index, seqid, 42, style) - except AttributeError, strerr: - if -1 == str(strerr).find("object has no attribute 'start'"): - raise TestFailedError - else: - raise TestFailedError - try: - diagram = Diagram.from_index(42, seqid, range, style) - except ArgumentError, strerr: - if -1 == str(strerr).find("must be a FeatureIndex"): - raise TestFailedError - else: - raise TestFailedError - diagram = Diagram.from_index(feature_index, seqid, range, style) + + try: + diagram = Diagram.from_index(feature_index, "nonexist", range, + style) + except GTError, strerr: + if -1 == str(strerr).find("FeatureIndex does not contain seqid"): + raise TestFailedError + else: + raise TestFailedError + try: + diagram = Diagram.from_index(feature_index, seqid, range, 42) + except ArgumentError, strerr: + if -1 == str(strerr).find("must be a Style"): + raise TestFailedError + else: + raise TestFailedError + try: + diagram = Diagram.from_index(feature_index, seqid, 42, style) + except AttributeError, strerr: + if -1 == str(strerr).find("object has no attribute 'start'"): + raise TestFailedError + else: + raise TestFailedError + try: + diagram = Diagram.from_index(42, seqid, range, style) + except ArgumentError, strerr: + if -1 == str(strerr).find("must be a FeatureIndex"): + raise TestFailedError + else: + raise TestFailedError + diagram = Diagram.from_index(feature_index, seqid, range, style) # check error reporting in the Layout class - try: - layout = Layout(diagram, 70, style) - except GTError, strerr: - if -1 == str(strerr).find("layout width must at least be twice"): - raise TestFailedError - else: - raise TestFailedError - layout = Layout(diagram, 700, style) - height = layout.get_height() + + try: + layout = Layout(diagram, 70, style) + except GTError, strerr: + if -1 == str(strerr).find("layout width must at least be twice"): + raise TestFailedError + else: + raise TestFailedError + layout = Layout(diagram, 700, style) + height = layout.get_height() # check error reporting in the CanvasCairoFile class - try: - canvas = CanvasCairoFile(12, 700, height, None) - except ArgumentError, strerr: - if -1 == str(strerr).find("must be a Style"): - raise TestFailedError - else: - raise TestFailedError - canvas = CanvasCairoFile(style, 700, height, None) + + try: + canvas = CanvasCairoFile(12, 700, height, None) + except ArgumentError, strerr: + if -1 == str(strerr).find("must be a Style"): + raise TestFailedError + else: + raise TestFailedError + canvas = CanvasCairoFile(style, 700, height, None) diff --git a/testdata/gtpython/sketch.py b/testdata/gtpython/sketch.py index 749da8b..52d766e 100644 --- a/testdata/gtpython/sketch.py +++ b/testdata/gtpython/sketch.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,28 +24,31 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 3: - sys.stderr.write("Usage: " + sys.argv[0] + " PNG_file GFF3_file\n") - sys.stderr.write("Create PNG representation of GFF3 annotation file.") - sys.exit(1) + if len(sys.argv) != 3: + sys.stderr.write("Usage: " + (sys.argv)[0] + + " PNG_file GFF3_file\n") + sys.stderr.write("Create PNG representation of GFF3 annotation file.") + sys.exit(1) - pngfile = sys.argv[1] - in_stream = GFF3InStream(sys.argv[2]) - feature_index = FeatureIndexMemory() - feature_stream = FeatureStream(in_stream, feature_index) - gn = feature_stream.next_tree() - # fill feature index - while gn: + pngfile = (sys.argv)[1] + in_stream = GFF3InStream((sys.argv)[2]) + feature_index = FeatureIndexMemory() + feature_stream = FeatureStream(in_stream, feature_index) gn = feature_stream.next_tree() - seqid = feature_index.get_first_seqid() - range = feature_index.get_range_for_seqid(seqid) + # fill feature index + + while gn: + gn = feature_stream.next_tree() + + seqid = feature_index.get_first_seqid() + range = feature_index.get_range_for_seqid(seqid) - style = Style() - diagram = Diagram.from_index(feature_index, seqid, range, style) - layout = Layout(diagram, 700, style) - height = layout.get_height() - ii = ImageInfo() - canvas = CanvasCairoFile(style, 700, height, ii) - layout.sketch(canvas) - canvas.to_file(pngfile) + style = Style() + diagram = Diagram.from_index(feature_index, seqid, range, style) + layout = Layout(diagram, 700, style) + height = layout.get_height() + ii = ImageInfo() + canvas = CanvasCairoFile(style, 700, height, ii) + layout.sketch(canvas) + canvas.to_file(pngfile) diff --git a/testdata/gtpython/sketch_simple.py b/testdata/gtpython/sketch_simple.py index 880b2b6..4acc73c 100644 --- a/testdata/gtpython/sketch_simple.py +++ b/testdata/gtpython/sketch_simple.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,23 +24,24 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 3: - sys.stderr.write("Usage: " + sys.argv[0] + " PNG_file GFF3_file\n") - sys.stderr.write("Create PNG representation of GFF3 annotation file.") - sys.exit(1) + if len(sys.argv) != 3: + sys.stderr.write("Usage: " + (sys.argv)[0] + + " PNG_file GFF3_file\n") + sys.stderr.write("Create PNG representation of GFF3 annotation file.") + sys.exit(1) - pngfile = sys.argv[1] - gff3file = sys.argv[2] - feature_index = FeatureIndexMemory() - feature_index.add_gff3file(gff3file) + pngfile = (sys.argv)[1] + gff3file = (sys.argv)[2] + feature_index = FeatureIndexMemory() + feature_index.add_gff3file(gff3file) - seqid = feature_index.get_first_seqid() - range = feature_index.get_range_for_seqid(seqid) + seqid = feature_index.get_first_seqid() + range = feature_index.get_range_for_seqid(seqid) - style = Style() - diagram = Diagram.from_index(feature_index, seqid, range, style) - layout = Layout(diagram, 700, style) - height = layout.get_height() - canvas = CanvasCairoFile(style, 700, height) - layout.sketch(canvas) - canvas.to_file(pngfile) + style = Style() + diagram = Diagram.from_index(feature_index, seqid, range, style) + layout = Layout(diagram, 700, style) + height = layout.get_height() + canvas = CanvasCairoFile(style, 700, height) + layout.sketch(canvas) + canvas.to_file(pngfile) diff --git a/testdata/gtpython/sketch_stream.py b/testdata/gtpython/sketch_stream.py index 8b91c84..e2cb50e 100644 --- a/testdata/gtpython/sketch_stream.py +++ b/testdata/gtpython/sketch_stream.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,33 +24,36 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 3: - sys.stderr.write("Usage: " + sys.argv[0] + " PNG_file GFF3_file\n") - sys.stderr.write("Create PNG representation of GFF3 annotation file (adds introns).") - sys.exit(1) - - pngfile = sys.argv[1] - in_stream = GFF3InStream(sys.argv[2]) - add_introns_stream = AddIntronsStream(in_stream) - feature_index = FeatureIndexMemory() - feature_stream = FeatureStream(add_introns_stream, feature_index) - gn = feature_stream.next_tree() - # fill feature index - while gn: + if len(sys.argv) != 3: + sys.stderr.write("Usage: " + (sys.argv)[0] + + " PNG_file GFF3_file\n") + sys.stderr.write("Create PNG representation of GFF3 annotation file (adds introns).") + sys.exit(1) + + pngfile = (sys.argv)[1] + in_stream = GFF3InStream((sys.argv)[2]) + add_introns_stream = AddIntronsStream(in_stream) + feature_index = FeatureIndexMemory() + feature_stream = FeatureStream(add_introns_stream, feature_index) gn = feature_stream.next_tree() - seqid = feature_index.get_first_seqid() - range = feature_index.get_range_for_seqid(seqid) - - style = Style() - diagram = Diagram.from_index(feature_index, seqid, range, style) - layout = Layout(diagram, 700, style) - height = layout.get_height() - ii = ImageInfo() - canvas = CanvasCairoFile(style, 700, height, ii) - layout.sketch(canvas) - - pngstream = canvas.to_stream() - outfile = open(pngfile, "w") - outfile.write(pngstream) - outfile.close() + # fill feature index + + while gn: + gn = feature_stream.next_tree() + + seqid = feature_index.get_first_seqid() + range = feature_index.get_range_for_seqid(seqid) + + style = Style() + diagram = Diagram.from_index(feature_index, seqid, range, style) + layout = Layout(diagram, 700, style) + height = layout.get_height() + ii = ImageInfo() + canvas = CanvasCairoFile(style, 700, height, ii) + layout.sketch(canvas) + + pngstream = canvas.to_stream() + outfile = open(pngfile, "w") + outfile.write(pngstream) + outfile.close() diff --git a/testdata/gtpython/style.py b/testdata/gtpython/style.py index ceb207e..3123982 100644 --- a/testdata/gtpython/style.py +++ b/testdata/gtpython/style.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -21,158 +22,181 @@ from gt.extended import * from gt.annotationsketch import * import sys + class TestFailedException(Exception): - pass + + pass + if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " style_file") - sys.stderr.write("Load style_file and test style bindings.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " style_file") + sys.stderr.write("Load style_file and test style bindings.") + sys.exit(1) - stylefile = sys.argv[1] + stylefile = (sys.argv)[1] # create new style object - style = Style() + + style = Style() # load style file - style.load_file(stylefile) + + style.load_file(stylefile) # clone style file - clone = style.clone() - if not clone: - raise TestFailedException + + clone = style.clone() + if not clone: + raise TestFailedException # get color - color = style.get_color("exon", "fill") - if not color: - raise TestFailedException + + color = style.get_color("exon", "fill") + if not color: + raise TestFailedException # set color - color = Color(0.3, 0.4, 0.3) - style.set_color("exon", "fill", color) - color2 = style.get_color("exon", "fill") - if color2.red != color.red \ - and color2.green != color.green \ - and color2.blue != color.blue: - raise TestFailedException + + color = Color(0.3, 0.4, 0.3) + style.set_color("exon", "fill", color) + color2 = style.get_color("exon", "fill") + if color2.red != color.red and color2.green != color.green and \ + color2.blue != color.blue: + raise TestFailedException # unset color - style.unset("exon", "fill") - color2 = style.get_color("exon", "fill") - if color2: - raise TestFailedException + + style.unset("exon", "fill") + color2 = style.get_color("exon", "fill") + if color2: + raise TestFailedException # get undefined color - color = style.get_color("undefined", "undefined") - if color: - raise TestFailedException + + color = style.get_color("undefined", "undefined") + if color: + raise TestFailedException # get string - string = style.get_cstr("exon", "style") - if string != "box": - raise TestFailedException + + string = style.get_cstr("exon", "style") + if string != "box": + raise TestFailedException # set string - style.set_cstr("exon", "style", "line") - string = style.get_cstr("exon", "style") - if string != "line": - raise TestFailedException + + style.set_cstr("exon", "style", "line") + string = style.get_cstr("exon", "style") + if string != "line": + raise TestFailedException # unset string - style.unset("exon", "style") - string = style.get_cstr("exon", "style") - if string: - raise TestFailedException + + style.unset("exon", "style") + string = style.get_cstr("exon", "style") + if string: + raise TestFailedException # get undefined string - string = style.get_cstr("undefined", "undefined") - if string: - raise TestFailedException + + string = style.get_cstr("undefined", "undefined") + if string: + raise TestFailedException # get number - num = style.get_num("format", "margins") - if num != 30: - raise TestFailedException + + num = style.get_num("format", "margins") + if num != 30: + raise TestFailedException # set number - style.set_num("format", "margins", 20) - num = style.get_num("format", "margins") - if num != 20: - raise TestFailedException + + style.set_num("format", "margins", 20) + num = style.get_num("format", "margins") + if num != 20: + raise TestFailedException # unset number - style.unset("format", "margins"); - num = style.get_num("format", "margins") - if num: - raise TestFailedException + + style.unset("format", "margins") + None + num = style.get_num("format", "margins") + if num: + raise TestFailedException #get undefined number - num = style.get_num("undefined", "undefined") - if num: - raise TestFailedException + + num = style.get_num("undefined", "undefined") + if num: + raise TestFailedException # get boolean - bool = style.get_bool("format", "show_grid") - if bool == False: - raise TestFailedException + + bool = style.get_bool("format", "show_grid") + if bool == False: + raise TestFailedException # get undefined boolean - bool = style.get_bool("undefined", "undefined") - if not bool == None: - raise TestFailedException + + bool = style.get_bool("undefined", "undefined") + if not bool == None: + raise TestFailedException # set boolean - style.set_bool("format", "show_grid", False) - bool = style.get_bool("format", "show_grid") - if bool: - raise TestFailedException + + style.set_bool("format", "show_grid", False) + bool = style.get_bool("format", "show_grid") + if bool: + raise TestFailedException # unset boolean - style.unset("format", "show_grid") - bool = style.get_bool("format", "show_grid") - if not bool == None: - raise TestFailedException + + style.unset("format", "show_grid") + bool = style.get_bool("format", "show_grid") + if not bool == None: + raise TestFailedException # serialise style to Lua code - style.set_num("format", "margins", 20) - style.set_bool("format", "show_grid", True) - color = Color(0.3, 0.4, 0.3) - style.set_color("exon", "fill", color) - luacode = style.to_str() - if luacode == None or len(luacode) == 0: - raise TestFailedException + + style.set_num("format", "margins", 20) + style.set_bool("format", "show_grid", True) + color = Color(0.3, 0.4, 0.3) + style.set_color("exon", "fill", color) + luacode = style.to_str() + if luacode == None or len(luacode) == 0: + raise TestFailedException # load style from Lua code - style.load_str(luacode) - num = style.get_num("format", "margins") - if num != 20: - raise TestFailedException - bool = style.get_bool("format", "show_grid") - if not bool: - raise TestFailedException - color2 = style.get_color("exon", "fill") - if color2.red != color.red \ - and color2.green != color.green \ - and color2.blue != color.blue: - raise TestFailedException + + style.load_str(luacode) + num = style.get_num("format", "margins") + if num != 20: + raise TestFailedException + bool = style.get_bool("format", "show_grid") + if not bool: + raise TestFailedException + color2 = style.get_color("exon", "fill") + if color2.red != color.red and color2.green != color.green and \ + color2.blue != color.blue: + raise TestFailedException # clone style from existing copy - style2 = style.clone() - num = style2.get_num("format", "margins") - if num != 20: - raise TestFailedException - bool = style2.get_bool("format", "show_grid") - if not bool: - raise TestFailedException - color2 = style2.get_color("exon", "fill") - if color2.red != color.red \ - and color2.green != color.green \ - and color2.blue != color.blue: - raise TestFailedException - style2.set_num("format", "margins", 30) - if style2.get_num("format", "margins") != 30: - raise TestFailedException - if style.get_num("format", "margins") \ - == style2.get_num("format", "margins"): - raise TestFailedException + + style2 = style.clone() + num = style2.get_num("format", "margins") + if num != 20: + raise TestFailedException + bool = style2.get_bool("format", "show_grid") + if not bool: + raise TestFailedException + color2 = style2.get_color("exon", "fill") + if color2.red != color.red and color2.green != color.green and \ + color2.blue != color.blue: + raise TestFailedException + style2.set_num("format", "margins", 30) + if style2.get_num("format", "margins") != 30: + raise TestFailedException + if style.get_num("format", "margins") == style2.get_num("format", + "margins"): + raise TestFailedException diff --git a/testdata/gtpython/used_types.py b/testdata/gtpython/used_types.py index 8a0f7ce..9468079 100644 --- a/testdata/gtpython/used_types.py +++ b/testdata/gtpython/used_types.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (c) 2008 Sascha Steinbiss # Copyright (c) 2008 Center for Bioinformatics, University of Hamburg @@ -23,16 +24,18 @@ import sys import re if __name__ == "__main__": - if len(sys.argv) != 2: - sys.stderr.write("Usage: " + sys.argv[0] + " GFF3_file\n") - sys.stderr.write("Parse and output the types used in GFF3_file.") - sys.exit(1) + if len(sys.argv) != 2: + sys.stderr.write("Usage: " + (sys.argv)[0] + " GFF3_file\n") + sys.stderr.write("Parse and output the types used in GFF3_file.") + sys.exit(1) - in_stream = GFF3InStream(sys.argv[1]) - gn = in_stream.next_tree() - # fill feature index - while gn: + in_stream = GFF3InStream((sys.argv)[1]) gn = in_stream.next_tree() - for type in in_stream.get_used_types(): - print type + # fill feature index + + while gn: + gn = in_stream.next_tree() + + for type in in_stream.get_used_types(): + print type