%function [fitresult, gof] = CurvefittingRarefactionCurves(A_spec1, A_spec2,A_met1,A_met2, A_tax1, A_tax2) %CREATEFITS(A_SPEC1,A_MET1,A_TAX1,A_SPEC2,A_MET2,A_TAX2) % Create fits. % % Data for 'Met1' fit: % X Input : A_spec1 % Y Output: A_met1 % Data for 'Met2' fit: % X Input : A_spec2 % Y Output: A_met2 % Data for 'Tax1' fit: % X Input : A_spec1 % Y Output: A_tax1 % Data for 'Tax2' fit: % X Input : A_spec2 % Y Output: A_tax2 % Output: % fitresult : a cell-array of fit objects representing the fits. % gof : structure array with goodness-of fit info. % % See also FIT, CFIT, SFIT. % Auto-generated by MATLAB on 04-Jan-2018 15:45:28 % Modified by: Lisa Wenzel %% Clear close all; clc; %% Import data from the combined data table A = xlsread('Combined-table-Matlab'); % 1D-approach A_spec1=A(:,1); A_met1=A(:,4); A_tax1=A(:,5); % 2D-approach A_spec2=A(:,6); A_met2=A(:,9); A_tax2=A(:,10); %% Initialization. % Initialize arrays to store fits and goodness-of-fit. fitresult = cell( 4, 1 ); gof = struct( 'sse', cell( 4, 1 ), ... 'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] ); %% Fit: 'Met1'. [xData_1, yData_1] = prepareCurveData( A_spec1, A_met1 ); % Set up fittype and options. ft = fittype( '(a*x)/(b+x)', 'independent', 'x', 'dependent', 'y' ); opts = fitoptions( 'Method', 'NonlinearLeastSquares' ); opts.Display = 'Off'; opts.StartPoint = [0.0749443966209411 0.483050724600071]; % Fit model to data. [fitresult{1}, gof(1)] = fit( xData_1, yData_1, ft, opts ); %% Fit: 'Met2'. [xData_2, yData_2] = prepareCurveData( A_spec2, A_met2 ); % Set up fittype and options. ft = fittype( '(a*x)/(b+x)', 'independent', 'x', 'dependent', 'y' ); opts = fitoptions( 'Method', 'NonlinearLeastSquares' ); opts.Display = 'Off'; opts.StartPoint = [0.632251826416449 0.0493137758052661]; % Fit model to data. [fitresult{2}, gof(2)] = fit( xData_2, yData_2, ft, opts ); %% Fit: 'Tax1'. [xData_3, yData_3] = prepareCurveData( A_spec1, A_tax1 ); % Set up fittype and options. ft = fittype( '(a*x)/(b+x)', 'independent', 'x', 'dependent', 'y' ); opts = fitoptions( 'Method', 'NonlinearLeastSquares' ); opts.Display = 'Off'; opts.StartPoint = [0.464533863489609 0.207894030111393]; % Fit model to data. [fitresult{3}, gof(3)] = fit( xData_3, yData_3, ft, opts ); %% Fit: 'Tax2'. [xData_4, yData_4] = prepareCurveData( A_spec2, A_tax2 ); % Set up fittype and options. ft = fittype( '(a*x)/(b+x)', 'independent', 'x', 'dependent', 'y' ); opts = fitoptions( 'Method', 'NonlinearLeastSquares' ); opts.Display = 'Off'; opts.StartPoint = [0.752407459129325 0.595738073442943]; % Fit model to data. [fitresult{4}, gof(4)] = fit( xData_4, yData_4, ft, opts ); %% Get parameters a, b and R_square from curvefitting for all functions and create one matrix and write a .xlsx-file % Obtain the parameters a and b from the curvefitting for all 18 functions for i=1:4 results_parameter(:,i+1)=coeffvalues(fitresult{i}); end % Write the results in an array with text in the first column Text={'Name';'1D-Metaprot';'2D-Metaprot';'1D-Tax';'2D-Tax'}; results_parameter_cell = num2cell(results_parameter'); Results_parameter=[Text,results_parameter_cell]; % Obtain the parameter R_square from the curvefitting for all functions R_square=[gof.rsquare]'; % Insert a heading, create a cell-array and the final matrix Text_rsquare={'R-square'}; R_square_cell=num2cell(R_square); Results_Rsquare=[Text_rsquare;R_square_cell]; Results=[Results_parameter,Results_Rsquare]; % Final matrix % Export the Resultsmatrix as .xlsx-file filename='ResultsCurvefitting.xlsx'; xlswrite(filename,Results) %% Plot data points for sample 1 (1D and 2D) % Plot for metaproteins figure('Name','Curve fitting') subplot(2,2,1) plot(fitresult{1},'b-',xData_1, yData_1,'r.') hold on; plot(fitresult{2},'k-',xData_2, yData_2,'m.') xlabel('Number of spectra') ylabel('Cumulative sum of identified metaproteins') grid on; % Subplot title ta=title('(a) Metaproteins: Data points and fitted curves '); set(ta,'FontWeight','bold') % Create a legend legend('1D-Metaproteins','1D-Metaproteins-Fitting','2D-Metaproteins','2D-Metaproteins-Fitting','Location','SouthEast') % Plot for species subplot(2,2,2) plot(fitresult{3},'b-',xData_1, yData_3,'r.') hold on; plot(fitresult{4},'k-',xData_2, yData_4,'m.') xlabel('Number of spectra') ylabel('Cumulative sum of identified species') grid on; % Subplot title tb=title('(b) Species: Data points and fitted curves '); set(tb,'FontWeight','bold') % Create a legend legend('1D-Species','1D-Species-Fitting','2D-Species','2D-Species-Fitting','Location','SouthEast') %% Calculate predicted values with the parameters a and b from curvefitting % Import the results matrix B = xlsread('ResultsCurvefitting'); % Create a vector with the number of spectra for the prediction X=[1:100000]'; % Calculate the Cumulative sum of identified metaproteins and % species for the given vector X for i=1:4 y(:,i)=(X.*B(i+1,1))./(B(i+1,2)+X); end % Plot the predicted values for metaproteins and species subplot(2,2,3) plot(X,y(:,1),'b-',X,y(:,2),'b--',X,y(:,3),'k-',X,y(:,4),'k--') xlabel('Number of spectra') ylabel('Cumulative sum of identified metaproteins/species') grid on; % Subplot title tc=title('(c) Predicted curves for 10^5 spectra '); set(tc,'FontWeight','bold') % Create a legend legend('1D-Metaproteins','2D-Metaproteins','1D-Species','2D-Species','Location','SouthEast') %end