%% Put out the Fire! Exploring the Effects of Fire-Fighting Foams on Microorganisms using Statistics and Plots
% Developed by T. Weathers
% Chabot College, Fall 2020
% tweathers@chabotcollege.edu
%
% Table of Contents
% Objective
% Background
% Data
% Problem
% Instructions
%     1. Read data
%     2. Begin loop
%     3. Calculate the means for each microorganism
%     4. Calculate the variance for each microorganism
%     5. Calculate F-values
%     6. Check if variances are considered equal or unequal
%     7. Calculate t-test statistic, P
%     8. Think and compare, is there a significant difference?
%     9. Print names of significantly different microorganisms
%     10. Create plots of the average relative abundances for each microorganism, with and without PFAAs
% Follow-Up Questions
%     Type your answers in the spaces below each question.
%     Save and submit your changes to this Live Script file to receive credit for the assignment.
%
%% Objective
% Analyze a subset of data from a published research paper using MATLAB. This will focus on data manipulation in MATLAB, including reading in text files, for, if, and while statements, statistics, and plot generation.
%% Background
% Consider the paper published in Environmental Science and Technologytitled “Perfluoroalkyl acids inhibit reductive dechlorination of trichloroethene by repressing Dehalococcoides” found here: https://pubs.acs.org/doi/10.1021/acs.est.5b04854
% In this paper, the authors found that perfluoroalkyl acids (PFAAs, the chemicals in things like Teflon and GoreTex) stop the growth of certain microorganisms. This allows other microorganisms to flourish.
%% Data
% The data you are working with is directly from this paper. You are given the relative abundances (fraction of the total number of microorganisms observed) of different types of microorganisms. One set was exposed to PFAAs, another set was not. Each set consists of three different replicates, or repeat trials.
%% Problem
% Check the statistical significance of each test, i.e., test whether there is a significant difference in relative abundance of each microorganism when it was exposed to PFAAs. We will use the Student’s T-Test, however, we must determine which type of T-Test to use. We will consider a final p-value of less than 0.05 to be significant by following the steps on the next pages. Be sure to read the instructions thoroughly before you begin coding!
%
%% Instructions
% Follow the suggested steps in order to analyze and visualize the data. You can type your own commands as described in the code boxes below. Note: you will need to add code to every box in order to complete this activity.
%% 1. Read data
% The data is located in a separate text file. Input the data into MATLAB using the method of your choosing.

clear;clc;close all; % Clears variables, command window, and closes all other windows
%
% In the space below, read in the given data file


% Depending on the method you use to read the data, you may have to remove
% the headers so that only the data is saved




%% 2. Begin loop
% For the following calculations, it is helpful to complete them in a “for” or “while” loop, e.g. “for i = 1:1:15” where i will represent each row/microorganism

% Begin a for or a while loop here where i represents each
% row / microorganism


%
%% 3. Calculate the means for each microorganism
% For the case with PFAAs, find the average of PFAA_1, PFAA_2, and PFAA_3 for each microorganism. Make this into an array called “meanPFAA”.
% For the case without PFAAs, find the average of No_1, No_2, and No_3 for each microorganism. Make this into an array called “meanNo”.

% Calculate the average of PFAA_1, PFAA_2, and PFAA_3 for each
% microorganism. Name this array "meanPFAA"


   
% Calculate the average of No_1, No_2, and No_3 and name the array "meanNo"



%
%% 4. Calculate the variance for each microorganism
% Use MATLAB’s built-in variance function. Store the variance values in two arrays as described below:
% Find the variance between PFAA_1, PFAA_2, and PFAA_3 for each microorganism. Save this as an array titled “PFAAVariance”
% Find the variance between No_1, No_2, and No_3 for each microorganism. Save this as an array titled “NoVariance”

% Calculate the variance of PFAA_1, PFAA_2, and PFAA_3 for each
% microorganism. Name this array "PFAAVariance"



   
% Calculate the variance of No_1, No_2, and No_3 and name the array
% "NoVariance"


%
%% 5. Calculate F-values
% Create a column vector titled “FValue” for the ratio of \frac{{{\mathrm{variance}}_{\mathrm{large}} }^2 }{{{\mathrm{variance}}_{\mathrm{small}} }^2 }, as in, the larger variance always has to be on the top. Hint: use an if-statement in MATLAB to determine which variance is larger and place it in the numerator.

% Create an if-statement to check if PFAAVariance or NoVariance is greater.
%   if PFAAVariance is greater, put PFAAVariance on top of the fraction
%   elseif NoVariance is greater, put NoVariance on top of the fraction
%   else you may have an error (there should be nothing satisfied by else), feel free to 
%       display an error message 
% Calculate FValue with the greater variance on top of the fraction

    if 

    elseif 
       
    else
        disp('error in Fvalue')
    end

%% 6. Check if variances are considered equal or unequal
% Using an F-test (details provided in the referenced paper) we know that our F-statistic is 19. If our calculated Fvalue from step 5 is smaller than 19, our variance is considered equal. Again, use an if-statement to establish this condition.
% Create a column vector that displays 1 for equal variance and 0 for unequal

% Create an if-statement to check if Fvalue is less than 19.
%   if the Fvalue < 19, assign a value of 1 for equal variance.
%   elseif the Fvalue is greater than or equal to 19, assign a value of 0 for unequal variance.
%   else you may have an error (there should be nothing satisfied by else), feel free to 
%       display an error message
% Store the 1's and 0's in a column vector.
    if 
       
    elseif 
       
    else
        disp('error in eqVar')
    end

%% 7. Calculate t-test statistic, P
% We will be using the Statistics and Machine Learning Toolbox built-in function ttest2 to calculate P
% Use if-statements to calculate the P value for each microorganism based on the following rules. Note that it will be helpful to store each P value in an array with rows corresponding to microorganism.
% If the variance is equal (Fvalue < 19) use the command:
% [H,P] = ttest2(x,y)
% where H indicates whether or not the hypothesis is null
% P is the P value
% x is the array of PFAA_1, PFAA_2, and PFAA_3 for a microorganism
% y is the array of No_1, No_2, and No_3 for a microorganism
% If the variance is unequal (Fvalue >= 19) uses the command:
% [H,P] = ttest2(x,y,’Vartype’,’unequal’)

% Set up an if-statement checking whether the variance is equal or unequal
%   if the Fvalue < 19 or the value in the column of step 6 is 1, the variance is equal - 
%       modify the command [H,P]=ttest2(x,y)
%   elseif the Fvalue >= 19 or the value in the column of step 6 is 0, the variance is unequal
%       - modify the command [H,P]=ttest2(x,y,'Vartype','unequal')
%   else you may have an error (there should be nothing satisfied by else), feel free to 
%       display an error message
    if 
       
        
    elseif 

        
    else
        disp('error in pval')    
    end 

%% 8. Think and compare, is there a significant difference?
% A P value of 0.05 or less is considered a significant difference. For example, if Treponema has a P value of 0.0385, there is a statistically significant difference between Treponema exposed to PFAAs and Treponema without PFAAs. Here, you can end your while or for loop started in step 2.

% End the for or while loop started in line 12-15


%% 9. Print names of significantly different microorganisms
% Have MATLAB print to the command window the names of the microorganisms that were significantly different between No PFAAs and With PFAAs. Hint: You can use the find command to find each row that is significant.

% Use the find command to locate every row where the p-value is less than or equal to 0.05


% Print the names of each microorganism that was found to be significant.
% You can save the names in a vector called "sigMicrobes"
    

%% 10. Create plots of the average relative abundances for each microorganism, with and without PFAAs
% Create a stacked bar chart with PFAA Presence or Absence on the x-axis and average relative abundance on the y-axis. This means that there will be two columns, and each microorganism will correspond to a specific color.
% Appropriately format the plot. Hint: You will need to put both mean values into a 15x2 array. To stack them appropriately, consider playing with transposes. See plot below the code block for an example of what the final product should look like.
% Create a different type of plot of your choice with the same data. Appropriately label the plot.

% Create a 15x2 array with meanPFAA values in one column and meanNo values
% in the other column


% Create a stacked bar plot


% Appropriately format your chart with a legend, x-lable, y-label, and title


% Create a new plot (not a stacked bar) that might show the data in a
% different way. Include legends, axes labels, and a title as needed.





%
%
% Figure 1: Example figure using a stacked bar plot
%
%% Follow-Up Questions
%% Type your answers in the spaces below each question.
%
% 1. Consider the following sentence from the abstract of the paper referenced in this exercise:
% Notably, there was significant repression of Dehalococcoides (8-fold decrease in abundance) coupled with a corresponding enhancement of methane-generating Archaea (a 9-fold increase) [upon exposure to PFAAs].
% Note that “Archaea” refers to Methanobacterium in our data. Does your analysis support this conclusion?
%
%
%
% 2. We chose a p-value of 0.05 to establish significance. What happens if this threshold is higher? Lower? Do you think a p-value of 0.05 truly represents significance?
%
%
%
% 3. Why do you think some microorganisms would be impacted by PFAA presence while others did not change?
%
%
%
% 4. Think about the two plots you generated. Which one does a better job of showing how the averages change upon exposure to PFAAs? Can you think of a better way to show this interplay?
%
%
%
%% Save and submit your changes to this Live Script file to receive credit for the assignment.
%%