~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to src/annotationsketch/line.c

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2007      Christin Schaerfer <schaerfer@zbh.uni-hamburg.de>
 
3
  Copyright (c) 2008-2010 Sascha Steinbiss <steinbiss@zbh.uni-hamburg.de>
 
4
  Copyright (c) 2007-2010 Center for Bioinformatics, University of Hamburg
 
5
 
 
6
  Permission to use, copy, modify, and distribute this software for any
 
7
  purpose with or without fee is hereby granted, provided that the above
 
8
  copyright notice and this permission notice appear in all copies.
 
9
 
 
10
  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
11
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
12
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
13
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
14
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
15
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
16
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
17
*/
 
18
 
 
19
#include "annotationsketch/block.h"
 
20
#include "annotationsketch/default_formats.h"
 
21
#include "annotationsketch/line.h"
 
22
#include "core/ensure.h"
 
23
#include "core/interval_tree.h"
 
24
#include "core/ma.h"
 
25
#include "core/mathsupport.h"
 
26
#include "core/range.h"
 
27
#include "core/undef_api.h"
 
28
#include "extended/gff3_defines.h"
 
29
 
 
30
struct GtLine {
 
31
  bool has_captions;
 
32
  GtArray *blocks;
 
33
};
 
34
 
 
35
GtLine* gt_line_new(void)
 
36
{
 
37
  GtLine *line;
 
38
  line = gt_calloc(1, sizeof (GtLine));
 
39
  line->blocks = gt_array_new(sizeof (GtBlock*));
 
40
  line->has_captions = false;
 
41
  return line;
 
42
}
 
43
 
 
44
void gt_line_insert_block(GtLine *line, GtBlock *block)
 
45
{
 
46
  gt_assert(line && block);
 
47
  if (!line->has_captions && gt_block_get_caption(block) != NULL)
 
48
    line->has_captions = true;
 
49
  gt_array_add(line->blocks, block);
 
50
}
 
51
 
 
52
bool gt_line_has_captions(const GtLine *line)
 
53
{
 
54
  gt_assert(line);
 
55
  return line->has_captions;
 
56
}
 
57
 
 
58
GtArray* gt_line_get_blocks(GtLine* line)
 
59
{
 
60
  gt_assert(line);
 
61
  return line->blocks;
 
62
}
 
63
 
 
64
int gt_line_sketch(GtLine *line, GtCanvas *canvas, GtError *err)
 
65
{
 
66
  int i = 0, had_err = 0;
 
67
  gt_assert(line && canvas);
 
68
  had_err = gt_canvas_visit_line_pre(canvas, line, err);
 
69
  if (!had_err)
 
70
  {
 
71
    for (i = 0; !had_err && i < gt_array_size(line->blocks); i++) {
 
72
      GtBlock *block;
 
73
      block = *(GtBlock**) gt_array_get(line->blocks, i);
 
74
      had_err = gt_block_sketch(block, canvas, err);
 
75
    }
 
76
  }
 
77
  if (!had_err)
 
78
    had_err = gt_canvas_visit_line_post(canvas, line, err);
 
79
  return had_err;
 
80
}
 
81
 
 
82
int gt_line_get_height(GtLine *line, double *height, const GtStyle *sty,
 
83
                       GtError *err)
 
84
{
 
85
  double line_height = 0;
 
86
  unsigned long i;
 
87
  gt_assert(line && sty);
 
88
  for (i = 0; i < gt_array_size(line->blocks); i++) {
 
89
    GtBlock *block;
 
90
    double myheight = BAR_HEIGHT_DEFAULT;
 
91
    block = *(GtBlock**) gt_array_get(line->blocks, i);
 
92
    /* check again for caption presence, may have changed in the meantime */
 
93
    if (!line->has_captions && gt_block_get_caption(block) != NULL) {
 
94
      line->has_captions = true;
 
95
    }
 
96
    if (gt_block_get_max_height(block, &myheight, sty, err) < 0) {
 
97
      return -1;
 
98
    }
 
99
    if (gt_double_smaller_double(line_height, myheight)) {
 
100
      line_height = myheight;
 
101
    }
 
102
  }
 
103
  *height = line_height;
 
104
  return 0;
 
105
}
 
106
 
 
107
int gt_line_unit_test(GtError *err)
 
108
{
 
109
  GtRange r1, r2, r3;
 
110
  GtArray* blocks;
 
111
  GtStr *seqid1;
 
112
  int had_err = 0;
 
113
  GtGenomeNode *parent, *gn1, *gn2, *gn3;
 
114
  double height;
 
115
  GtLine *l1;
 
116
  GtBlock *b1, *b2;
 
117
  GtStyle *sty = NULL;
 
118
  const char* foo = "foo";
 
119
  const char* bar = "bar";
 
120
  const char* blub = "blub";
 
121
  gt_error_check(err);
 
122
 
 
123
  r1.start = 10;
 
124
  r1.end = 40;
 
125
  r2.start = 51;
 
126
  r2.end = 80;
 
127
  r3.start = 0;
 
128
  r3.end = 7;
 
129
 
 
130
  seqid1 = gt_str_new_cstr("test1");
 
131
 
 
132
  parent = gt_feature_node_new(seqid1, gt_ft_gene, r1.start, r2.end,
 
133
                               GT_STRAND_FORWARD);
 
134
  gn1 = gt_feature_node_new(seqid1, gt_ft_exon, r1.start, r1.end,
 
135
                            GT_STRAND_FORWARD);
 
136
  gn2 = gt_feature_node_new(seqid1, gt_ft_exon, r2.start, r2.end,
 
137
                            GT_STRAND_FORWARD);
 
138
  gn3 = gt_feature_node_new(seqid1, gt_ft_TF_binding_site, r3.start, r3.end,
 
139
                            GT_STRAND_FORWARD);
 
140
 
 
141
  gt_feature_node_add_child((GtFeatureNode*) parent, (GtFeatureNode*) gn1);
 
142
  gt_feature_node_add_child((GtFeatureNode*) parent, (GtFeatureNode*) gn2);
 
143
 
 
144
  l1 = gt_line_new();
 
145
 
 
146
  gt_feature_node_add_attribute((GtFeatureNode*) parent, GT_GFF_NAME, foo);
 
147
  gt_feature_node_add_attribute((GtFeatureNode*) gn1, GT_GFF_NAME, bar);
 
148
  gt_feature_node_add_attribute((GtFeatureNode*) gn2, GT_GFF_NAME, bar);
 
149
  gt_feature_node_add_attribute((GtFeatureNode*) gn3, GT_GFF_NAME, blub);
 
150
 
 
151
  b1 = gt_block_new();
 
152
  b2 = gt_block_new();
 
153
 
 
154
  gt_block_insert_element(b1, (GtFeatureNode*) parent);
 
155
  gt_block_insert_element(b1, (GtFeatureNode*) gn1);
 
156
  gt_block_insert_element(b1, (GtFeatureNode*) gn2);
 
157
  gt_block_insert_element(b2, (GtFeatureNode*) gn3);
 
158
  gt_block_set_range(b1, r1);
 
159
  gt_block_set_range(b2, r2);
 
160
 
 
161
  /* test gt_line_insert_block */
 
162
  gt_ensure(had_err,  (0 == gt_array_size(gt_line_get_blocks(l1))));
 
163
  gt_line_insert_block(l1, b1);
 
164
  gt_ensure(had_err,  (1 == gt_array_size(gt_line_get_blocks(l1))));
 
165
  gt_line_insert_block(l1, b2);
 
166
  gt_ensure(had_err,  (2 == gt_array_size(gt_line_get_blocks(l1))));
 
167
 
 
168
  /* test gt_line_get_blocks */
 
169
  blocks = gt_line_get_blocks(l1);
 
170
  gt_ensure(had_err, (2 == gt_array_size(blocks)));
 
171
 
 
172
  /* test gt_line_get_height() */
 
173
  if (!had_err)
 
174
  {
 
175
    sty = gt_style_new(err);
 
176
    gt_ensure(had_err, sty && !gt_error_is_set(err));
 
177
    gt_ensure(had_err, gt_line_get_height(l1, &height, sty, err) == 0);
 
178
    gt_ensure(had_err, height == BAR_HEIGHT_DEFAULT);
 
179
    gt_ensure(had_err, !gt_error_is_set(err));
 
180
    gt_style_set_num(sty, "exon", "bar_height", 42);
 
181
    gt_ensure(had_err, gt_line_get_height(l1, &height, sty, err) == 0);
 
182
    gt_ensure(had_err, height == 42);
 
183
    gt_ensure(had_err, !gt_error_is_set(err));
 
184
    gt_style_set_num(sty, "gene", "bar_height", 23);
 
185
    gt_ensure(had_err, gt_line_get_height(l1, &height, sty, err) == 0);
 
186
    gt_ensure(had_err, height == 42);
 
187
    gt_ensure(had_err, !gt_error_is_set(err));
 
188
    gt_style_unset(sty, "exon", "bar_height");
 
189
    gt_ensure(had_err, gt_line_get_height(l1, &height, sty, err) == 0);
 
190
    gt_ensure(had_err, height == 23);
 
191
    gt_ensure(had_err, !gt_error_is_set(err));
 
192
    gt_style_unset(sty, "gene", "bar_height");
 
193
    gt_style_set_num(sty, "format", "bar_height", 99);
 
194
    gt_ensure(had_err, gt_line_get_height(l1, &height, sty, err) == 0);
 
195
    gt_ensure(had_err, height == 99);
 
196
    gt_ensure(had_err, !gt_error_is_set(err));
 
197
  }
 
198
 
 
199
  gt_str_delete(seqid1);
 
200
  gt_line_delete(l1);
 
201
  gt_style_delete(sty);
 
202
  gt_genome_node_delete(parent);
 
203
  gt_genome_node_delete(gn3);
 
204
  return had_err;
 
205
}
 
206
 
 
207
void gt_line_delete(GtLine *line)
 
208
{
 
209
  unsigned long i;
 
210
  if (!line) return;
 
211
  for (i = 0; i < gt_array_size(line->blocks); i++)
 
212
    gt_block_delete(*(GtBlock**) gt_array_get(line->blocks, i));
 
213
  gt_array_delete(line->blocks);
 
214
  gt_free(line);
 
215
}