DIMaSP
Direct Infusion Mass Spectrometry Processing

Step 3: Common ion selection
Release Version 1.0

Copyright © 2017 Arthur T Zielinski, Olalekan Popoola, Ivan Kourtchev, Markus Kalberer

This file is part of “Direct Infusion Mass Spectrometry Processing (DIMaSP)”.
Direct Infusion Mass Spectrometry Processing (DIMaSP) is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 
Direct Infusion Mass Spectrometry Processing (DIMaSP) is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Direct Infusion Mass Spectrometry Processing (DIMaSP).  If not, see <http://www.gnu.org/licenses/>.

This notebook will take the files from the directory (input as directory below) that are group in the input table tablename. It will check whether an assigned chemical formula is present in a user-defined number of replicates (set in third column of tablename). If so, it will keep the assignment. Otherwise the assignment is removed. The number of replicates (numberofreplicates) must be set to the number of replicates per sample (and should match the input table, tablename).

To run the code go to Evaluation > Evaluate Notebook. Code will be completed once “Running...” no longer shows at top of window and a dialogue window will appear.

Important notes:
	[1]	Ensure files selected for analysis are placed in the designated folder.
	[2]	The files listed in tablename must all have the same numberofreplicates defined below in the inputs. Process separately if a sample has a different number of replicates.
	[3]	Main body of code is hidden to prevent any accidental changes but can be accessed by selecting (left click) the hidden cell (short line on right of notebook) and going to Cell > Cell Properties > Open and Cell > Cell Properties > Editable.
	[4]	The output data is saved in a new folder directory\Common_Ion_Folder and an output plot is saved in a new folder directory\Plots_Common_Ion_Folder

Example Input:
	directory = “C:\\Users\\Name\\Processed”;
	tablename = “CommonIonsInput.csv”;
	numberofreplicates = 3;

Other Options:
	mzmin sets the lower end of the x-axis in the output plots (m/z).
	mzmax sets the lower end of the x-axis in the output plots (m/z).
	absintensitymin sets the lower end of the y-axis in the output plots (intensity).
	absintensitymax sets the upper end of the y-axis in the output plots (intensity).

Inputs

directory="C:\\Users\\Name\\Processed"; (* Folder containing files to be processed and input table *)
tablename="Input Table Common.csv"; (* Table filename INCLUDING file extension (i.e. ".csv"); The table must contain the sample and replicate number, the file name, and the number of common ions required *)
numberofreplicates=3; (* The number of replicates per sample *)
mzmin=50; (* Minimimum m/z range for plot *)
mzmax=650; (* Maximum m/z range for plot *)
absintensitymin=0; (* Minimum intensity range for plot *)
absintensitymax=5000000; (* Maximum intensity range for plot *)

Main code

SetDirectory[directory];
reffile=FileNames[tablename];
refdata=Import[reffile[[1]],"CSV"][[2;;]];
reptab=Table[0,{i,Length[refdata]/numberofreplicates}];
posref=Table[i,{i,1,Length[refdata],numberofreplicates}];

filetab=Table[0,{i,numberofreplicates}];
filetabv2=Table[0,{i,numberofreplicates}];
filetabv3=Table[0,{i,numberofreplicates}];

If[Max[refdata[[All,3]]]>numberofreplicates||Min[refdata[[All,3]]]==1,Print[Style["Caution!!!,either: 
1. Common ion greater than number of repeats or 
2. Common ion equals to 1. 
Check input csv table. Quitting Mathematica!!!",Red,Italic,Bold,40]];Quit[],Print[Style["Input condition for numberofreplicates OK, continue evaluation !!!",Blue,Italic,Bold,40]]];

Do[
(

SetDirectory[directory];

(*ff=1;*)


Do[If[StringLength[refdata[[posref[[ff]],1]]]==6,reptab[[ff]]=StringTake[refdata[[posref[[ff]],1]],{1,4}],reptab[[ff]]=StringTake[refdata[[posref[[ff]],1]],{1,5}]],{ff,Length[reptab]}];
repeatname=reptab[[ppp]];

Print["Evaluating file "<>ToString[ppp]<>" of "<> ToString[Length[reptab]]];

If[StringLength[repeatname]==4,refdata2=Select[refdata,StringTake[#[[1]],{1,4}]==repeatname&][[All,2]],refdata2=Select[refdata,StringTake[#[[1]],{1,5}]==repeatname&][[All,2]]];

If[StringLength[repeatname]==4,repeatref=Select[refdata,StringTake[#[[1]],{1,4}]==repeatname&][[All,3]][[1]],repeatref=Select[refdata,StringTake[#[[1]],{1,5}]==repeatname&][[All,3]][[1]]];


(*refdata2=Select[refdata,StringTake[#[[1]],{1,-3}]==repeatname&][[All,2]];
repeatref=Select[refdata,StringTake[#[[1]],{1,-3}]==repeatname&][[All,3]][[1]];*)


Do[filetab[[ii]]=Import[FileNames["*"<>refdata2[[ii]]<>"*"][[1]],"CSV"];,{ii,numberofreplicates}];
Do[filetabv2[[ii]]=filetab[[ii]][[2;;]];,{ii,numberofreplicates}];
Do[filetabv3[[k]]=Table[Join[filetabv2[[k]][[i]],{repeatname<>"-"<>ToString[k]}],{i,Length[filetabv2[[k]]]}];,{k,numberofreplicates}];

mergedfile=Partition[Join[filetabv3]//Flatten,Dimensions[filetabv3[[1]]][[2]]];

(*file1=Import[FileNames["*"<>refdata2[[1]]<>"*"][[1]],"CSV"];
file2=Import[FileNames["*"<>refdata2[[2]]<>"*"][[1]],"CSV"];
file3=Import[FileNames["*"<>refdata2[[3]]<>"*"][[1]],"CSV"];
file1v2=file1[[2;;]];
file2v2=file2[[2;;]];
file3v2=file3[[2;;]];

file1v3=Table[Join[file1v2[[i]],{repeatname<>"-1"}],{i,Length[file1v2]}];
file2v3=Table[Join[file2v2[[i]],{repeatname<>"-2"}],{i,Length[file2v2]}];
file3v3=Table[Join[file3v2[[i]],{repeatname<>"-3"}],{i,Length[file3v2]}];

mergedfile=Join[file1v3,file2v3,file3v3];*)


mergedfilev2=Table[Join[mergedfile[[i]],{i}],{i,Length[mergedfile]}];

header={"m/z", "Intensity", "Relative", "Theo. Mass", "Delta (ppm)", "RDB equiv.", "Composition", "C", "H", "N", "S", "O", "13C", "34S","Na", "Theo N.Mass", "DBE", "N.Comp", "C", "H", "N", "S", "O", "13C", "34S","Abs. Corrected Delta","O/C","H/C","OSC","KM","KMD"};
(* ATZ edit v8: Added additional header terms to match output for MassSpecProc code *)
(* Previous version (v7):
header={"m/z", "Intensity", "Relative", "Theo. Mass", "Delta (ppm)", "RDB equiv.", "Composition", "C", "H", "N", "S", "O", "13C", "34S", "Theo N.Mass", "DBE", "N.Comp", "C", "H", "N", "S", "O", "13C", "34S"}; 
*)
commonesttab=Select[Tally[mergedfilev2[[All,7]]],#[[2]]≥  repeatref&];
uncommontab=Select[Tally[mergedfilev2[[All,7]]],#[[2]]<repeatref&];

(*kk=10;
commonesttab[[kk,1]];*)

commonestfiletab=Table[0,{i,Length[commonesttab]}];
uncommonfiletab=Table[0,{i,Length[uncommontab]}];

Do[
(
sppselect=Select[mergedfilev2,#[[7]]==commonesttab[[kk,1]]&];
meanintensity=SetAccuracy[Mean[sppselect[[All,2]]],2];
sppselectonefile=sppselect[[2]];
sppselectonefile[[2]]=meanintensity;
commonestfiletab[[kk]]=sppselectonefile;

Clear[sppselect,meanintensity,sppselectonefile];
)
,{kk,Length[commonestfiletab]}];


Do[
(
uncommonsppselect=Select[mergedfilev2,#[[7]]==uncommontab[[kk,1]]&];
uncommonfiletab[[kk]]=uncommonsppselect;

Clear[uncommonsppselect];
)
,{kk,Length[uncommonfiletab]}];

uncommonfiletabv2=Partition[uncommonfiletab//Flatten,Dimensions[uncommonfiletab[[1]]][[2]]];


commonesttab;
commonestfiletab;

ionplot=ListPlot[{uncommonfiletabv2[[All,{1,2}]],commonestfiletab[[All,{1,2}]]},PlotStyle->{Red,Black},Filling->Axis,FillingStyle->{Directive[Red,Thick],Directive[Gray,Thick]},ImageSize-> 600,PlotRange->{{mzmin,mzmax},{absintensitymin,absintensitymax}},PlotLegends->PointLegend[{Red,Black},{"Ion removed","Common ion"}],Frame->True,FrameLabel->{"M/Z","Intensity"},PlotLabel->StringTake[refdata2[[1]],{1,-1}]<>"-common-ions"];

commonestfiletabv2=commonestfiletab[[All,1;;-3]];

commonestfiletabv2a=commonestfiletabv2;
maxintensity=Max[commonestfiletabv2a[[All,2]]];
commonestfiletabv2a[[All,3]];
newrelativeint=SetAccuracy[((commonestfiletabv2a[[All,2]]/maxintensity)*100),4];
commonestfiletabv2a[[All,3]]=newrelativeint;



commonestfiletabv3=Join[{header},commonestfiletabv2a];

commonestfiletabv3;
exportname=StringTake[refdata2[[1]],{1,-1}]<>"common-ions.csv";
exportdirectory=directory<>"/Common_Ion_Folder";
If[DirectoryQ[exportdirectory]==False,CreateDirectory[exportdirectory]];
SetDirectory[exportdirectory];
Export[exportname,commonestfiletabv3];

exportdirectoryplot=directory<>"/Plots_Common_Ion_Folder";
If[DirectoryQ[exportdirectoryplot]==False,CreateDirectory[exportdirectoryplot]];
SetDirectory[exportdirectoryplot];
plotname=StringTake[refdata2[[1]],{1,-1}]<>"common-ions.tiff";
Export[plotname,ionplot];

Print["Exporting file "<>ToString[ppp]<>": "<>exportname];

Clear[commonestfiletabv3,commonestfiletab,exportname,plotname,ionplot];

ResetDirectory[];

)
,{ppp,1,Length[reptab]}]