function y = filterAlignedseq(Saligned, prop_v, wsize, smooth_n)
% Process and find the 2D-filter respons on the basis of an amino acid alignment 
% and a chemical property. We use a rectangular 2D filter window.
% The vertical filter size is the mean of all the amino acid properties in the 
% aligned sequence position, and the horisontal window size is given by wsize (input)
% It can be used to plot smoothed line of amino acid properties, such as the 
% hydrophobicity in the alignment. This smoothing technique is a way of
% recovering underlying structure in data sets.
% Program by Steinar Thorvaldsen, steinart@math.uit.no, Nov. 2004.

% Input:
% Saligned: character or integer array containing the Sequence alignment
% prop_v: chemical values to filter. From a property table for aa
% wsize: the window length to compute horisontal mean values over, 
% (in addition to the vertical mean that is taken over all sequences).
% smooth_n: smoothing method 1(linear) or 2(exponential)

% Output:
% y: filter response vector

% References:
% Oppenheim, A. V. and R.W. Schafer. Discrete-Time Signal Processing,
% Englewood Cliffs, NJ: Prentice-Hall, 1989, pp. 311-312. 
% Matlab Help on 'filter'.

edgeweight=0.8; % edge weight in the window wsize, may be selected in the interval [0,1].
if wsize < 3
    edgeweight=1.0; %to avoide the unnessesary...
end

% The function is made flexible conserning input parameters:
if nargin < 4
    smooth_n=1; % use linear smoothing
end

% If nessesary, convert amino acid from a character to an integer representation:
if ischar (Saligned)
    Saligned = aa2int(Saligned); %Uint8 format
end
% No amino acids found in sequences:
if isempty(Saligned)
    return
end

[M,N]=size(Saligned);
nterm=1; cterm=N; % may be changed for plot of sequence segments
yindex = nterm:cterm;

% process data, by finding the vertical average for each position in the sequences:
for m = 1 : M
	yy(m,:) = prop_v(Saligned(m,:)); %mapping the property value into the array
end
y=nanmean(yy,1); %vertical average ignoring NaN's
%y=nanmedian(yy,1); %vertical median ignoring NaN's
%y=sum(yy,1)/M; %vertical average

% process data further, using the horisontal window size:
if length(y) < wsize, % controling length of data y
    uiwait(warndlg(['The aa sequence must be longer than the filter window length: ' num2str(wsize) '.']))
    return
end
mid = (wsize + 1)/2;
if smooth_n==1 % use linear
    fb = ones(1,wsize);
    fs = linspace(edgeweight,1,mid); %generate linearly spaced vectors
    fb(1:mid) = fs;
    fb(mid:wsize) = fliplr(fs);
    fb = fb / sum(fb);
% To filter the values in y with the filter described by numerator 
% coefficient vector fb and denominator coefficient 1:
    y = filter(fb,1,y);
    %y = y(mid:(end - mid + 1)); %restrict to full window in use
    %yindex = (nterm + (wsize-1)/2):(cterm - (wsize-1)/2);
elseif smooth_n==2 % use exponential
    fb = ones(1,wsize);
    fb(1) = edgeweight; 
    fb(wsize) = edgeweight;
    if ~edgeweight
        edgeweight = 1e-6;
    end
    k = log(edgeweight) /  (mid - 1);
    fb(2:mid-1) = exp(k) .^((mid-2):-1:1);
    fb((mid+1): (wsize-1)) = fliplr(fb(2:mid-1));
    fb = fb / sum(fb);
% To filter the values in y with the filter described by numerator 
% coefficient vector fb and denominator coefficient 1:
    y = filter(fb,1,y);
    %y = y(mid:(end - mid + 1));
    %yindex = (nterm + (wsize-1)/2):(cterm - (wsize-1)/2);
%elseif handles.uselowess % lowess
%    for n = 1:size(y,2),
%        y(:,n) = masmooth(y(:,n),wsize,'lowess'); %external function
%    end
%    yindex = nterm:cterm;
end


