function [Temp]=DNAMeltingArbitrary(sequence, distance)

% Formula: Tm°C=(Entalpy[kcal/mol]/(Entropy[cal/mol/°K]+(GasConstant[cal/ml/°K]*log(Concentration))))-273.15;
% 0<distance<(sequence length/2) (arbitrary)

% distance is the opened window to calculate the adjustments of melting
% temperature for any given pair in the sequence, such pair is at the
% mid-point of the window.

% Minimum and maximum distance parameters need testing and verification,
% both by (physics-chemical) literature, and slope (fft) informativeness.

% Singles gives the result of each single pair Tm, at present is only
% informative, however may become useful for high-throughput analysis.

% A specific Tm has been calculated for closed end pairs and could be 
% implemented here, however since they are for CLOSED ends (like priming or
% RNA secondary structure) and we are not considering such phenomena in
% the current analysis, we believe it is better to keep them out.


% NOTE: Code needs VERIFICATION for errors


% Constants
GasConstant=1.987;
Concentration=.5;

% Convert to UPPERCASE
sequence=upper(sequence);

% Varibale initialize
seqlen=length(sequence);
AllH=zeros(1,seqlen);
AllS=zeros(1,seqlen);
dH=zeros(1,seqlen);
dS=zeros(1,seqlen);
Temp=zeros(1,seqlen);

% Retrieving entalpy and entropy parameters
% for i=1:seqlen-1
%     [AllH(i),AllS(i)]=EnergyPairParam(sequence([i,i+1]));
%     Singles(i)=(AllH(i)*1000/(AllS(i)+(GasConstant*log(Concentration))))-273.15;
% end

% Retrieving TM values (arbitrary)
for i=1:seqlen-1
    [AllH(i)]=EnergyPairParamArbitrary(sequence([i,i+1]));
end


% Calculating cumulative energy for any given pair, such pair considered as
% the mid-point for the window

for j=distance+1:seqlen-distance
%     dH(j)=sum(AllH(j-distance:j+distance-1));
%     dS(j)=sum(AllS(j-distance:j+distance-1));
%     Temp(j)=((dH(j)*1000/(distance*2))/((dS(j)/(distance*2))+(GasConstant*log(Concentration))))-273.15;
%     Temp(j)= ((dH(j)*1000)/(dS(j)+distance*2*((GasConstant*log(Concentration)))))-273.15;


    % Function change for different implementation due to the feasibility
    % to recover the sequence out of a signal
    
    % Previous formula was:
    % Tm°C=({sum of all pairs} Entalpy[kcal/mol]/({sum of all pairs} Entropy[cal/mol/°K]+(GasConstant[cal/ml/°K]*log(Concentration))))-273.15;
    % referred from other sources
    
    % Current formula is:
    % {sum of all pairs}Tm°C=(Entalpy[kcal/mol]/(Entropy[cal/mol/°K]+(GasConstant[cal/ml/°K]*log(Concentration))))-273.15;
    % referred from Santalucia

    
    
    % NOTE!!!  Verify function EnergyPairParam. With current formula we can
    % add a new column to EntalpyPairs with the solution for all pairs and
    % take it directly into this function instead of making all
    % calculations for every input sequence, therfore quickening the
    % algorithm to a ludicrous time of operation
    
%     dH=AllH(j-distance:j+distance-1);
%     dS=AllS(j-distance:j+distance-1);
%     Temp(j)=(sum(((dH*1000)./(dS+(GasConstant*log(Concentration))))-273.15))/(distance*2);
    dH=AllH(j-distance:j+distance-1);
    Temp(j)=(sum(dH))/(distance*2);
end

          
