-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalignseqfile2.m
203 lines (168 loc) · 3.61 KB
/
alignseqfile2.m
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
function [aln] = alignseqfile2(seqtype,filename,geneticcode)
%ALIGNSEQFILE - Align sequence file.
%
% Syntax: [aln] = alignseqfile2(seqtype,filename,geneticcode)
%
% Inputs:
% seqtype - 'DNA'|'CDS'|'PROTEIN'
% filename - Input sequence file (FASTA format accepted).
% geneticcode - Genetic code
%
% Outputs:
% aln - Alignment structure
%
% See also:
% Molecular Biology and Evolution Toolbox (MBEToolbox)
% Author: James Cai
% Email: jcai@tamu.edu
% Website: http://bioinformatics.org/mbetoolbox/
%
% $LastChangedDate: 2013-01-05 12:04:29 -0600 (Sat, 05 Jan 2013) $
% $LastChangedRevision: 327 $
% $LastChangedBy: jcai $
aln=[];
if(nargin<1)
error('Please input seqtype.')
return;
end
if ~(ischar(seqtype))
error('Wrong sequence type.')
return;
end
if (nargin<2),
[filename, pathname, filterindex] = uigetfile( ...
{'*.*', 'Sequence Files (*.*)'}, ...
'Select a sequence file (FASTA)');
if ~(filename), return; end
filename=[pathname,filename];
end
switch (upper(seqtype))
case {'DNA'}
seqtype = 1;
if (nargin<3), geneticcode=0; end
aln = runclustalw(filename,seqtype,geneticcode);
case ('CDS')
seqtype = 2;
if nargin < 3
[stype, geneticcode]=selectSeqTypeAndGeneticCode;
if (isempty(stype)|isempty(geneticcode)), aln=[]; return; end
end
if ~(seqtype == 2)
error('Must be coding-DNA sequence file.');
return;
end
AlnNT = readfasta(filename,2,geneticcode);
AlnAA = translatealn(AlnNT);
if (i_includeStopCodon(AlnAA))
error('ERROR: Stop codon within sequences.')
return;
end
% if (any(any(AlnAA.seq==i_encode_a('*')))>0)
% error('ERROR: Stop codon within sequences.')
% return;
% end
%cmd = 'alignseqfile.m';
%dirstr=chdir2where(cmd);
oldpath=pwd;
%cdmbe;
cd(fileparts(which(mfilename)));
cd 'addins';
cd 'clustalw';
try
i_writeFASTA(AlnAA,'infile');
[AlnAA] = runclustalw('infile',3,geneticcode);
aln=i_alignCDS2Protein(AlnNT,AlnAA);
catch
cd(oldpath);
end
cd(oldpath);
case ('PROTEIN')
seqtype = 3;
if (nargin<3), geneticcode=1; end
aln = runclustalw(filename,seqtype,geneticcode);
otherwise
error('Wrong sequence seqtype.')
return;
end
%%%%%%%%%%%%%
%%% SUBS %%%
%%%%%%%%%%%%%
function [y] = i_includeStopCodon(AlnAA)
y=0;
[n,m]=size(AlnAA.seq);
[NT,AA] = seqcode;
% AA = 'ARNDCQEGHILKMFPSTWYV*-';
removeable =find(AA=='-'|AA=='*');
Seq = AlnAA.seq;
for i=1:n,
s=Seq(i,:);
for (j=length(s):-1:1),
if (ismember(s(j),removeable)),
s(j)=[];
else
break;
end
end
x=find(AA=='*');
if (any(s==x)),
y=1;
return;
end
end
%%%%%%%%%%%%%
%%% SUBS %%%
%%%%%%%%%%%%%
function i_writeFASTA(aln,filename)
if nargin < 2
error('No filename')
end
[n,m]=size(aln.seq);
[NT,AA] = seqcode;
if ~(aln.seqtype==3)
error('Must be protein sequences')
else
Seq = AA(aln.seq);
end
fid = fopen(filename,'wt');
if fid == -1,
error('Unable to open file.')
end
for i=1:n,
name=char(aln.seqnames(i));
s=Seq(i,:);
for (j=length(s):-1:1),
if (strcmp(s(1,j),'-'));
s(j)=[];
else
break;
end
end
fprintf(fid, ['>%s\n'],name);
fprintf(fid, ['%s\n'],char(s));
end
fclose(fid);
%%%%%%%%%%%%%
%%% SUBS %%%
%%%%%%%%%%%%%
function [aln] = i_alignCDS2Protein(AlnNT,AlnAA)
[n,m]=size(AlnAA.seq);
aln=copyalnheader(AlnNT);
S=ones(n,m*3)*5;
SA = AlnAA.seq;
SN = AlnNT.seq;
for (i=1:n),
x=1;
y=1;
for (j=1:m),
if ~(SA(i,j)==i_getcode4gap('PROTEIN'))
S(i,x)=SN(i,y);
S(i,x+1)=SN(i,y+1);
S(i,x+2)=SN(i,y+2);
x=x+3;
y=y+3;
else
x=x+3;
end
end
end
aln.seq = S;