File: align.c

package info (click to toggle)
squizz 0.99b%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,648 kB
  • ctags: 8,291
  • sloc: sh: 12,012; ansic: 2,607; lex: 1,944; yacc: 1,659; makefile: 119
file content (222 lines) | stat: -rw-r--r-- 4,752 bytes parent folder | download
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* align.c - Generic alignment functions */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <err.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#endif

#ifndef HAVE_FSEEKO
# define fseeko fseek
# define ftello ftell
#endif

#include "align.h"
#include "align/clustal.h"
#include "align/fasta2.h"
#include "align/mega.h"
#include "align/msf.h"
#include "align/nexusi.h"
#include "align/phylipi.h"
#include "align/phylips.h"
#include "align/stock.h"


static int namcmp(const void *a, const void *b) {
  const char * const *p = a, * const *q = b;
  return strcmp(*p, *q); }


/* Create a new alignment object */
align_t *align_new(void) {
  size_t len;
  align_t *ali;

  /* Allocate */
  len = sizeof(*ali);
  if ((ali = malloc(len)) == NULL) { return NULL; }

  /* Initialise */
  ali->seq = NULL;
  ali->cur = ali->seq;
  ali->_tmp = NULL;

  return ali; }


/* Delete an alignment object */
void align_free(align_t *ali) {
  sequence_t **p;

  if (ali == NULL) { return; }

  p = ali->seq;
  while (p && *p) {
    sequence_free(*p);
    p++; }
  free(ali->seq);
  free(ali->_tmp);
  free(ali);

  return; }


/* Parse a formatted alignment from file */
align_t *align_parse(FILE *f, alifmt_t fmt) {
  align_t *ali;

  ali = NULL;

  switch (fmt) {
  case ALIFMT_CLUSTAL:
    ali = clustal_parse(f); break;
  case ALIFMT_FASTA:
    ali = fasta2_parse(f); break;
  case ALIFMT_MEGA:
    ali = mega_parse(f); break;
  case ALIFMT_MSF:
    ali = msf_parse(f); break;
  case ALIFMT_NEXUSI:
    ali = nexusi_parse(f); break;
  case ALIFMT_PHYLIPI:
    ali = phylipi_parse(f); break;
  case ALIFMT_PHYLIPS:
    ali = phylips_parse(f); break;
  case ALIFMT_STOCK:
    ali = stock_parse(f); break;
  default: break; }

  return ali; }


/* Check alignment format */
int align_check(FILE *f, alifmt_t fmt) {
  int i;

  switch (fmt) {
  case ALIFMT_CLUSTAL:
    i = clustal_check(f); break;
  case ALIFMT_FASTA:
    i = fasta2_check(f); break;
  case ALIFMT_MEGA:
    i = mega_check(f); break;
  case ALIFMT_MSF:
    i = msf_check(f); break;
  case ALIFMT_NEXUSI:
    i = nexusi_check(f); break;
  case ALIFMT_PHYLIPI:
    i = phylipi_check(f); break;
  case ALIFMT_PHYLIPS:
    i = phylips_check(f); break;
  case ALIFMT_STOCK:
    i = stock_check(f); break;
  default:
    i = 1; break; }

  return i; }


/* Check aligment file format */
alifmt_t align_format(FILE *f) {
  int i;
  off_t off;
  alifmt_t fmt;

  if ((off = ftello(f)) == -1) {
    err(EXIT_FAILURE, "file position"); }

  for (fmt = ALIFMT_UNKWN + 1; fmt != ALIFMT_NONE; fmt++) {

    i = align_check(f, fmt);

    if (fseeko(f, off, SEEK_SET) != 0) {
      err(EXIT_FAILURE, "file position"); }

    if (i == 0) { break; }

  }

  return fmt; }


/* Check for alignment strict conditions */
int align_strict(align_t *ali) {
  int i, d, n, num;
  size_t len;
  char **x, **buf;
  sequence_t **p;

  if (ali == NULL || ali->seq == NULL) { return 1; }

  d = (getenv("SQUIZZ_DEBUG") != NULL) ? 1 : 0;

  /* Need more than 1 sequence */
  p = ali->seq; n = 0;
  while (p && *p) { n++; p++; }
  if (d) { fprintf(stderr, "Found %d sequence(s)\n", n); }
  if (n < 2) { return 1; }
  num = n;

  /* All sequences must have the same length */
  p = ali->seq; n = 0; len = (*p)->strlen;
  while (p && *p) {
    if ((*p)->strlen != len) { n++; }
    p++; }
  if (d) { fprintf(stderr, "Found %d sequence(s) with bad lengths\n", n); }
  if (n > 0) { return 1; }

  /* Sequence names must exists */
  p = ali->seq; n = 0;
  while (p && *p) {
    if ((*p)->nam == NULL) { n++; }
    p++; }
  if (d) { fprintf(stderr, "Found %d unnamed sequence(s)\n", n); }
  if (n > 0) { return 1; }

  /* Sequence names must be unique */
  len = num * sizeof(*buf);
  if ((buf = malloc(len)) == NULL) { return 1; }
  p = ali->seq; x = buf; n = 0;
  while (p && *p) {
    *x = (*p)->nam; x++; p++; }
  qsort(buf, num, sizeof(*buf), namcmp);
  for (i = 0, x = buf; i < num - 1; i++, x++) {
    if (strcmp(*x, *(x+1)) != 0) { continue; }
    n++; }
  free(buf);
  if (d) { fprintf(stderr, "Found %d identical sequence name pair(s)\n", n); }
  if (n > 0) { return 1; }

  return 0; }


/* Print alignment */
void align_print(FILE *f, align_t *ali, alifmt_t fmt) {

  switch (fmt) {
  case ALIFMT_CLUSTAL:
    clustal_print(f, ali); break;
  case ALIFMT_FASTA:
    fasta2_print(f, ali); break;
  case ALIFMT_MEGA:
    mega_print(f, ali); break;
  case ALIFMT_MSF:
    msf_print(f, ali); break;
  case ALIFMT_NEXUSI:
    nexusi_print(f, ali); break;
  case ALIFMT_PHYLIPI:
    phylipi_print(f, ali); break;
  case ALIFMT_PHYLIPS:
    phylips_print(f, ali); break;
  case ALIFMT_STOCK:
    stock_print(f, ali); break;
  default:
    break; }

  return; }