-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_seq.c
275 lines (265 loc) · 6.31 KB
/
sort_seq.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*************************************************************************/
// File: sort_seq.c
// Author: Xavier Sol Acha
// Description: Here are the functions to write the output of the program
// in a file.
/*************************************************************************/
#include <stdio.h>
#include "sort_seq.h"
#include "align.h"
#include "auxiliary.h"
#include "ent_seq.h"
#include "const.h"
#include "multiple.h"
#ifdef HEX_ONLY_MODE
int hex_mode = 1;
#else
int hex_mode = 0;
#endif
void write_align_output_file(int output_format)
{
int k = 0, i, j, l;
unsigned char asteriscs[MAXLENSEQ];
unsigned char seq_out[MAXLENSEQ];
// output_format = 1; // cut sequences
output_format = 0; // do not cut sequences
if (output_format == 0) /* do not cut sequences */
{
printf("Number of sequences=%d Alignment length=%ld Alignment score=%ld\n", clusters[0]->num_seqs,
clusters[0]->len_seqs, clusters[0]->score);
i = 0;
while (i < clusters[0]->len_seqs)
{
asteriscs[i] = '*';
i++;
}
asteriscs[i] = EOS;
while (k < clusters[0]->num_seqs)
{
write_sequence_cluster(seqs[0][k], asteriscs);
k++;
}
i = 0;
while (i <= 10)
{
seq_out[i] = ' ';
i++;
}
k = 0;
while (k < clusters[0]->len_seqs)
{
//if ((asteriscs[k] == 'A') || (asteriscs[k] == 'C') || (asteriscs[k] == 'G') || (asteriscs[k] == 'T'))
//{
// seq_out[i] = '*';
//}
//else
//{
seq_out[i] = ' ';
//}
i++;
k++;
}
seq_out[i] = EOS;
printf("%s\n", seq_out);
}
else /* cut the sequences */
{
printf("\nNumber of sequences=%d Alignment length=%ld Alignment score=%ld\n", clusters[0]->num_seqs,
clusters[0]->len_seqs, clusters[0]->score);
l = 0;
while (l < clusters[0]->len_seqs)
{
i = 0;
while (i < 58)
{
asteriscs[i] = '*';
i++;
}
asteriscs[i] = EOS;
k = 0;
while (k < clusters[0]->num_seqs)
{
write_cut_cluster_sequence(seqs[0][k], asteriscs, l);
k++;
}
i = 0;
while (i <= 10)
{
seq_out[i] = ' ';
i++;
}
k = 0;
while (k < 58)
{
//if ((asteriscs[k] == 'A') || (asteriscs[k] == 'C') || (asteriscs[k] == 'G') || (asteriscs[k] == 'T'))
//{
// seq_out[i] = '*';
//}
//else
//{
seq_out[i] = ' ';
//}
i++;
k++;
}
seq_out[i] = EOS;
printf("%s\n\n", seq_out);
l = l + 58;
}
}
}
void write_cut_cluster_sequence(int num_seq, unsigned char *asteriscs, int pos)
{
unsigned char seq[MAXLENSEQ + 1], name[15], seq_out[MAXLENSEQ + 1];
int i = 0, j = 0, k = 0, trobat_fi = 0;
// First we copy the name
fseek(clusters_file, pos_seq[num_seq], SEEK_SET);
load_sequence_name(name, num_seq);
i = 0;
trobat_fi = 0;
while (i <= 10)
{
if ((name[i] == EOS) || (name[i] == '\n') || (trobat_fi == 1))
{
seq_out[i] = ' ';
trobat_fi = 1;
}
else
{
seq_out[i] = name[i];
}
i++;
}
j = pos;
k = 0;
fgets(seq, MAXLENSEQ, clusters_file);
while ((seq[j] != EOS) && (seq[j] != '\n') && (i <= MAXLENSEQ) && ((j - pos) < 58))
{
seq_out[i] = seq[j];
if (asteriscs[k] == '*')
{
asteriscs[k] = seq[j];
}
else
{
if (asteriscs[k] != '-')
{
if (asteriscs[k] != seq[j])
{
asteriscs[k] = '-';
}
}
}
i++;
j++;
k++;
}
seq_out[i] = EOS;
asteriscs[k] = EOS;
printf("%s\n", seq_out);
}
void write_sequence_cluster(int num_seq, unsigned char *asteriscs)
{
unsigned char seq[MAXLENSEQ + 1], name[15], seq_out[MAXLENSEQ + 1];
int i = 0, j = 0, trobat_fi = 0;
// int hex_mode = 1;
int hex_mode = 0;
hex_mode = hex_output_mode;
// first, copy the name
fseek(clusters_file, pos_seq[num_seq], SEEK_SET);
load_sequence_name(name, num_seq);
i = 0;
trobat_fi = 0;
while (i <= 10)
{
if ((name[i] == EOS) || (name[i] == '\n') || (trobat_fi == 1))
{
seq_out[i] = ' ';
trobat_fi = 1;
}
else
{
seq_out[i] = name[i];
}
i++;
}
j = 0;
fgets(seq, MAXLENSEQ, clusters_file);
while ((seq[j] != EOS) && (seq[j] != '\n') && (i <= MAXLENSEQ))
{
seq_out[i] = seq[j];
if (asteriscs[j] == '*')
{
asteriscs[j] = seq[j];
if (hex_mode) {
unsigned char c, c0, c1;
c = seq_out[i];
c0 = c % 16;
c1 = c / 16;
if (c0 > 9)
{
c0 = c0 - 10 + 'a';
} else {
c0 = c0 + '0';
}
if (c1 > 9)
{
c1 = c1 - 10 + 'a';
} else {
c1 = c1 + '0';
}
if (c == 0x2d)
{
c0 = '-';
c1 = '-';
}
seq_out[i] = c1;
seq_out[i+1] = c0;
seq_out[i+2] = ' ';
i++; i++;
}
}
else
{
if (asteriscs[j] != '-')
{
if (asteriscs[j] != seq[j])
{
asteriscs[j] = '-';
}
}
if (hex_mode) {
unsigned char c, c0, c1;
c = seq_out[i];
c0 = c % 16;
c1 = c / 16;
if (c0 > 9)
{
c0 = c0 - 10 + 'a';
} else {
c0 = c0 + '0';
}
if (c1 > 9)
{
c1 = c1 - 10 + 'a';
} else {
c1 = c1 + '0';
}
if (c == 0x2d)
{
c0 = '-';
c1 = '-';
}
seq_out[i] = c1;
seq_out[i+1] = c0;
seq_out[i+2] = ' ';
i++; i++;
}
}
i++;
j++;
}
seq_out[i] = EOS;
asteriscs[i] = EOS;
printf("%s\n", seq_out);
}