-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrem_dup.c
207 lines (181 loc) · 5.23 KB
/
rem_dup.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
/**
>HEADER
Copyright (c) 2004 Haixu Tang [email protected]
This file is part of the RepGraph package.
RepGraph is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RepGraph is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RepGraph. If not, see <http://www.gnu.org/licenses/>.
<HEADER
**/
#include <stdinc.h>
#include <param.h>
#include <extfunc.h>
#define MAX_NUM_ALN 100000
char inpfile[100], outfile[100], qualfile[100], overlapfile[100], seqfile[100], lenfile[100], segfile[100];
int qualinp;
int min_leg;
double min_id;
void initenv(int argc, char **argv);
char chk_aln(int *posindex, int index1, int index2);
char chk_ovp(int *pos1, int *pos2);
main(int argc, char **argv)
{
int i, j, k, l, m, n;
char c;
int len_chro[100];
int num_chro;
char temp[100];
char **chrname;
char *label;
char **seq, **seqname;
int *len_seq;
int num_seq;
int *tmpclass, **alnclass, *size_class, size_tmpclass, num_class;
int *tmpaln, *num_aln, **alnindex, size_tmp;
int *posindex;
FILE *fp, *fp1;
readpar();
random1(&idum);
initenv(argc, argv);
/* read in chromosome length */
chrname = alloc_name(100, 100);
fp = ckopen(lenfile, "r");
num_chro = readlen(fp, len_chro, chrname);
fclose(fp);
printf("# chromosome %d\n", num_chro);
/* read in pairwise alignments */
len_seq = (int *) ckalloc(MAX_NUM_ALN * sizeof(int));
seq = (char **) ckalloc(2 * MAX_NUM_ALN * sizeof(char *));
seqname = (char **) ckalloc(2 * MAX_NUM_ALN * sizeof(char *));
for(i = 0; i < 2 * MAX_NUM_ALN; i ++) {
seqname[i] = (char *) ckalloc(100 * sizeof(char));
}
fp = ckopen(inpfile, "r");
num_seq = readintv(seq, fp, len_seq, seqname);
fclose(fp);
printf("# alignments input: %d.\n", num_seq);
/* Remove duplicated alignment */
label = (char *) ckalloc(num_seq * sizeof(char));
posindex = (int *) ckalloc(num_seq * 6 * sizeof(int));
for(m = 0; m < num_seq; m ++) {
transform_len(seqname[2 * m], chrname, num_chro, len_chro, &posindex[6 * m]);
transform_len(seqname[2 * m + 1], chrname, num_chro, len_chro, &posindex[6 * m + 3]);
}
for(m = 0; m < num_seq; m ++) {
if(label[m] == 1) {
continue;
}
for(i = m + 1; i < num_seq; i ++) {
if(label[i] == 1) {
continue;
}
c = chk_aln(posindex, m, i);
if(c) {
label[i] = 1;
break;
}
}
}
free((void *) posindex);
printf("Matrix computed\n");
/* Write alignments */
fp = ckopen(outfile, "w");
for(i = 0; i < num_seq; i ++) {
if(label[i] == 0) {
outputseq(fp, seq[2 * i], len_seq[i], seqname[2 * i]);
outputseq(fp, seq[2 * i + 1], len_seq[i], seqname[2 * i + 1]);
}
}
fclose(fp);
printf("Done...\n");
free((void *) label);
for(i = 0; i < 2 * num_seq; i ++) {
free((void **) seq[i]);
}
for(i = 0; i < 2 * MAX_NUM_ALN; i ++) {
free((void *) seqname[i]);
}
free((void **) seq);
free((void **) seqname);
free((void *) len_seq);
chrname = free_name(chrname, 100);
}
char chk_aln(int *posindex, int index1, int index2)
{
char c1, c2;
c1 = chk_ovp(&posindex[6 * index1], &posindex[6 * index2 + 3]);
c2 = chk_ovp(&posindex[6 * index1 + 3], &posindex[6 * index2]);
if(c1 == 1 && c2 == 1) {
return(1);
}
c1 = chk_ovp(&posindex[6 * index1], &posindex[6 * index2]);
c2 = chk_ovp(&posindex[6 * index1 + 3], &posindex[6 * index2 + 3]);
if(c1 == 1 && c2 == 1) {
return(1);
}
return(0);
}
char chk_ovp(int *pos1, int *pos2)
{
int i;
for(i = 0; i < 3; i ++) {
if(pos1[i] != pos2[i]) break;
}
if(i == 3) return(1);
else return(0);
}
void initenv(int argc, char **argv)
{
int copt;
int inpseq, outseq;
extern char *optarg;
min_leg = 500;
min_id = 0.99;
inpseq = qualinp = outseq = 0;
while ((copt=getopt(argc,argv,"i:o:l:d:c:")) != EOF) {
switch(copt) {
case 'i':
inpseq = 1;
sscanf(optarg,"%s", inpfile);
continue;
case 'o':
outseq = 1;
sscanf(optarg,"%s", outfile);
continue;
case 'c':
sscanf(optarg,"%s", lenfile);
continue;
case 'l':
sscanf(optarg,"%d", &min_leg);
continue;
case 'd':
sscanf(optarg,"%lf", &min_id);
continue;
default:
printf("rem_dup -i InpFile -c LenFile -o outfile [-l min_leg -d min_id]\n");
printf("-i InpFile: The input file name of alignments\n");
printf("-c lenfile: inpput file for chromosome length.\n");
printf("-o OutFile: output alignment file\n");
printf("-l min_leg: minimum length of repeats.\n");
printf("-d min_id: minimum identity of repeats.\n");
exit(-1);
}
optind--;
}
if(inpseq == 0 || outseq == 0) {
printf("rem_dup -i InpFile -c LenFile -o outfile [-l min_leg -d min_id]\n");
printf("-i InpFile: The input file name of alignments\n");
printf("-c lenfile: inpput file for chromosome length.\n");
printf("-o OutFile: output alignment file\n");
printf("-l min_leg: minimum length of repeats.\n");
printf("-d min_id: minimum identity of repeats.\n");
exit(-1);
}
}