forked from calkan/mam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.c
272 lines (229 loc) · 6.57 KB
/
slider.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
/*
MaM : Multiple alignment Manipulator
Implemented by: Can ALKAN & Eray TUZUN
Last Update: Oct 18, 2005
Summary: parsimony score
Summary: config file, run_prog (Oct 18, 2005)
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include "main.h"
#define pairwise 0
#define complete 1
int *flag;
float slide(int, char **, int);
float divergence(char *, char *, int);
void SkipNlines(int n,FILE *in);
int exonloc(int);
FILE *gfopen(char *, char *);
void plotout(char *,int,char *,char *,char *);
position *slidepos;
int posno=0;
int SLIDE_WIDTH;
int WINDOW_SIZE;
void slider(char *arg1, char *arg2, char *arg3, char *arg4, char *arg5){
//argv[1]; alignment file
//argv[2]; exonlocation file
//argv[3]; P/C pairwise or complete deletion or parsimony score
//argv[4]; Slide width
//argv[5]; Window size
int iterator; //To get the begin&end locations of exonfile
int i=0;
int k=0;
FILE *unique;
FILE *repeats;
FILE *exon;
float stddif; //error rate
flag = (int *) malloc(maxLen * sizeof (int));
repeats = gfopen("repeats","w"); //exonfile
unique = gfopen("unique","w"); //non-exon file
SLIDE_WIDTH=atoi(arg4);
WINDOW_SIZE=atoi(arg5);
exon=gfopen(arg2,"r"); //to be changed
slidepos = (position *) malloc(sizeof(position) * arraysize);
while(fscanf(exon, "%d", &iterator) >0){
slidepos[posno].begin=iterator;
fscanf(exon,"%d",&iterator);
slidepos[posno++].end=iterator;
}
for (i=0;i<maxLen;i++) //Initialize
flag[i]=1;
for (i=0;i<seqTot;i++){
for (k=0; k<maxLen; k++){
if ((seqs[i][k]=='-') && !(strcmp(arg3,"C"))) //If complete deletion requested
flag[k] = 0;
else if (!strcmp(arg3, "S")) // if parsimony score
flag[k] = 2;
}
}
for(i=0;i<strlen(seqs[0]);i+=SLIDE_WIDTH){
printf("\rSliding Windows %f%%",(((float)(i+1)/(float)(strlen(seqs[0])))*100));
stddif = slide(i, seqs, seqTot);
if (exonloc(i)){
fprintf(repeats, "%d %f\n", i,stddif);
fprintf(unique, "%d %f\n", i,0.0);
}
else{
fprintf(repeats, "%d %f\n", i,0.0);
fprintf(unique, "%d %f\n", i,stddif);
}
}
fclose(repeats);
fclose(unique);
fclose(exon);
plotout("plotfile",0,arg1,arg2,arg3);
run_prog(PATH_GNUPLOT, "-persist plotfile");
/*
system("gnuplot -persist plotfile");
*/
}//slider
int exonloc(int position)
{
int j;
for (j=0;j<posno;j++)
if ((position<slidepos[j].end) && (position>slidepos[j].begin))
return 1;
return 0;
} //exonloc
float slide(int startpos, char **seqs, int seqtot){
int i, j;
float r;
float totalscore=0.0;
float avgscore=0.0;
int a,t,g,c,n;
int max;
int whichmax; //n:0, a:1 c:2 g:3 t:4
int parscore=0;
if (flag[0] == 2){ // parsimony score
for (j=startpos;j<startpos+WINDOW_SIZE;j++){
a = t = g = c = n = 0;
for (i=0;i<seqtot;i++){
if (tolower(seqs[i][j]) == '-') n++;
else if (tolower(seqs[i][j]) == 'a') a++;
else if (tolower(seqs[i][j]) == 'c') c++;
else if (tolower(seqs[i][j]) == 'g') g++;
else if (tolower(seqs[i][j]) == 't') t++;
}
max = n; whichmax=0;
if (a>max) {max=a; whichmax=1;}
if (c>max) {max=c; whichmax=2;}
if (g>max) {max=g; whichmax=3;}
if (t>max) {max=t; whichmax=4;}
switch(whichmax){
case 0:
parscore=a+c+g+t;
break;
case 1:
parscore=n+c+g+t;
break;
case 2:
parscore=a+n+g+t;
break;
case 3:
parscore=a+c+n+t;
break;
case 4:
parscore=a+c+g+n;
break;
}
totalscore+=(float)parscore/(float)WINDOW_SIZE;
}
avgscore=(float)totalscore/(float)seqtot;
}
else{
for (i=0;i<seqtot;i++){
for (j=0;j<i;j++){
r=divergence(seqs[i],seqs[j],startpos);
totalscore+=r;
} //inner for
} //outer for
avgscore=((float)totalscore/(float)((seqtot-1)*seqtot*2));
}
return avgscore;
} // slide
float divergence(char *S, char *T, int startpos){
//Flag=0 pairwise deletion
//Flag=1 complete deletion if there is a '-' in one of the S[i] we discard the whole i column.
//Flag=2 parsimony score
int i;
int div=0;
int length=0;
float percentage;
int endpos=strlen(seqs[0]);
if ((startpos+WINDOW_SIZE)<endpos)
endpos=startpos+WINDOW_SIZE;
for (i=startpos;i<endpos;i++){
if (flag[i] == 1) //Else it is a complete deletion dont count this position in calculations
{
// If S[i]=='-' or T[i]=='-' we discard the position.
if (S[i] != T[i] && S[i]!='-' && T[i]!='-')
{
div++;
length++;
}
if (S[i]==T[i] && S[i]!='-' && T[i]!='-')
length++;
}
}
if (div==0)
percentage=0.0;
else
percentage = ((float)div/(float)length);
return percentage;
} // divergence
void SkipNlines(int n,FILE *in){
char ch;
int linecount=0;
//Skips first n lines
while(fscanf(in,"%c",&ch) > 0){
if (ch=='\n')
linecount++;
if (linecount==n)
break;
}
} // skipNlines
FILE *gfopen(char *fname, char *mode){
//Gracefully file open, gives an error message if it can't open else returns file pointer
FILE *fp;
if ((fp=fopen(fname,mode))==0){
printf("Cannot open %s \n",fname);
die();
}
return fp;
} // gfopen
void plotout(char *plotfile,int printflag,char *alnname,char *tablename, char *porc){
FILE *plot;
char psfile[200];
plot=gfopen(plotfile,"w"); //gnuplot file
//Plot information comes here
sprintf(psfile,"%s.%s",alnname,tablename);
if (porc[0]=='S')
fprintf(plot,"set title \"DivergenceRate Graph with \'%s\' cDNA file with parsimony score SlideWidth: \'%d\' WindowWidth: \'%d\' \" \n",tablename,SLIDE_WIDTH, WINDOW_SIZE);
else
fprintf(plot,"set title \"DivergenceRate Graph with \'%s\' cDNA file with \'%s\' deletion SlideWidth: \'%d\' WindowWidth: \'%d\' \" \n",tablename,porc,SLIDE_WIDTH, WINDOW_SIZE);
fprintf(plot,"set xlabel \"LengthofAlignment\" \n");
fprintf(plot,"set ylabel \"DivergenceRate\" \n");
fprintf(plot,"plot \"unique\" with impulses, \"repeats\" with boxes\n");
fprintf(plot,"set terminal postscript color\n");
fprintf(plot,"set output \"%s.ps\"\n",psfile);
fprintf(plot,"replot");
fclose(plot);
} // plotout
/*
set palette
set palette {
{ gray | color }
{ gamma <gamma> }
{ rgbformulae <r>,<g>,<b>
| defined { ( <gray1> <color1> {, <grayN> <colorN>}... ) }
| file '<filename>' {datafile-modifiers}
| functions <R>,<G>,<B>
}
{ model { RGB | HSV | CMY | YIQ | XYZ } }
set palette model CMY
*/