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

« back to all changes in this revision

Viewing changes to src/gth/chain_collection.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) 2003-2009 Gordon Gremme <gremme@zbh.uni-hamburg.de>
 
3
  Copyright (c) 2003-2008 Center for Bioinformatics, University of Hamburg
 
4
 
 
5
  Permission to use, copy, modify, and distribute this software for any
 
6
  purpose with or without fee is hereby granted, provided that the above
 
7
  copyright notice and this permission notice appear in all copies.
 
8
 
 
9
  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
10
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
11
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
12
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
13
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
14
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
15
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
*/
 
17
 
 
18
#include "core/ma_api.h"
 
19
#include "gth/chain_collection.h"
 
20
 
 
21
struct GthChainCollection {
 
22
  GtArray *chains;
 
23
};
 
24
 
 
25
GthChainCollection* gth_chain_collection_new(void)
 
26
{
 
27
  GthChainCollection *chain_collection = gt_malloc(sizeof *chain_collection);
 
28
  chain_collection->chains = gt_array_new(sizeof (GthChain*));
 
29
  return chain_collection;
 
30
}
 
31
 
 
32
void gth_chain_collection_delete(GthChainCollection *chain_collection)
 
33
{
 
34
  unsigned long i;
 
35
  if (!chain_collection) return;
 
36
  for (i = 0; i < gt_array_size(chain_collection->chains); i++)
 
37
    gth_chain_delete(*(GthChain**) gt_array_get(chain_collection->chains, i));
 
38
  gt_array_delete(chain_collection->chains);
 
39
  gt_free(chain_collection);
 
40
}
 
41
 
 
42
void gth_chain_collection_add(GthChainCollection *chain_collection,
 
43
                              GthChain *chain)
 
44
{
 
45
  gt_assert(chain_collection && chain);
 
46
  gt_array_add(chain_collection->chains, chain);
 
47
}
 
48
 
 
49
static int compare_chains(const void *dataA, const void *dataB)
 
50
{
 
51
  GthChain *chainA = *(GthChain**) dataA;
 
52
  GthChain *chainB = *(GthChain**) dataB;
 
53
 
 
54
  gt_assert(chainA->ref_file_num == chainB->ref_file_num);
 
55
 
 
56
  /* sort after genomic file number */
 
57
  if (chainA->gen_file_num < chainB->gen_file_num)
 
58
    return -1;
 
59
  if (chainA->gen_file_num > chainB->gen_file_num)
 
60
    return 1;
 
61
 
 
62
  /* genomic file number is the same,
 
63
     sort after genomic sequence number */
 
64
  if (chainA->gen_seq_num < chainB->gen_seq_num)
 
65
    return -1;
 
66
  if (chainA->gen_seq_num > chainB->gen_seq_num)
 
67
    return 1;
 
68
 
 
69
  /* genomic sequence number is also the same,
 
70
     sort after reference sequence number */
 
71
  if (chainA->ref_seq_num < chainB->ref_seq_num)
 
72
    return -1;
 
73
  if (chainA->ref_seq_num > chainB->ref_seq_num)
 
74
    return 1;
 
75
 
 
76
  /* reference sequence number is also the same,
 
77
     sort after reference sequence coverage */
 
78
  if (chainA->refseqcoverage < chainB->refseqcoverage)
 
79
    return -1;
 
80
  if (chainA->refseqcoverage > chainB->refseqcoverage)
 
81
    return 1;
 
82
 
 
83
  return 0;
 
84
}
 
85
 
 
86
void gth_chain_collection_sort(GthChainCollection *chain_collection)
 
87
{
 
88
  gt_assert(chain_collection);
 
89
  if (gt_array_size(chain_collection->chains)) {
 
90
    qsort(gt_array_get_space(chain_collection->chains),
 
91
          gt_array_size(chain_collection->chains), sizeof (GthChain*),
 
92
          compare_chains);
 
93
  }
 
94
}
 
95
 
 
96
unsigned long gth_chain_collection_size(const GthChainCollection
 
97
                                        *chain_collection)
 
98
{
 
99
  gt_assert(chain_collection);
 
100
  return gt_array_size(chain_collection->chains);
 
101
}
 
102
 
 
103
GthChain* gth_chain_collection_get(const GthChainCollection *chain_collection,
 
104
                                   unsigned long idx)
 
105
{
 
106
  gt_assert(chain_collection);
 
107
  return *(GthChain**) gt_array_get(chain_collection->chains, idx);
 
108
}