File: alignment.c

package info (click to toggle)
glam2 1064-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 956 kB
  • sloc: ansic: 6,925; xml: 757; asm: 74; makefile: 54; sh: 11
file content (69 lines) | stat: -rw-r--r-- 1,589 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>  /* free */
#include <string.h>  /* strlen */
#include "util.h"
#include "alignment.h"

int aln_same_lengths(const alignment *a) {
  const size_t len = strlen(a->key_positions);
  size_t i;
  for (i = 0; i != a->seq_num; ++i)
    if (strlen(a->seqs[i].seq) != len)
      return 0;
  return 1;
}

static char *next_word(const char *cs) {
  return skipws(skipnw(cs));
}

void aln_read(alignment *a, FILE *stream) {
  char *line = NULL;
  size_t line_size = 0;
  int state = 0;
  a->seq_num = 0;
  a->seqs = NULL;
  a->key_positions = NULL;

  while (xgetline(&line, &line_size, stream)) {
    if (state == 0) {
      char *beg = skipws(line);
      char *end = skipnw(beg);
      if (*beg == '*' && *(end-1) == '*') {
        *end = 0;
        a->key_positions = xstrdup(beg);
        state = 1;
      }
    } else {
      char *name = line;
      char *start = next_word(name);
      char *seq = next_word(start);
      if (*seq == 0)
        break;
      *skipnw(name) = 0;
      *skipnw(start) = 0;
      *skipnw(seq) = 0;
      ++a->seq_num;
      /* slow but simple linear reallocation for now: */
      a->seqs = XREALLOC(a->seqs, a->seq_num);
      a->seqs[a->seq_num-1].name = xstrdup(name);
      a->seqs[a->seq_num-1].seq = xstrdup(seq);
      a->seqs[a->seq_num-1].start = xstrdup(start);
    }
  }

  free(line);
}

static void seq_free(aligned_seq *s) {
  free(s->name);
  free(s->seq);
  free(s->start);
}

void aln_free(alignment *a) {
  size_t i;
  for (i = 0; i != a->seq_num; ++i)
    seq_free(&a->seqs[i]);
  free(a->seqs);
  free(a->key_positions);
}