DIMaSP
Direct Infusion Mass Spectrometry Processing

Step 4: Shoulder ion removal (main function)
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 data processed by previous codes and check for any shoulder ions based on a user defined intensitycutoff, shouldermassrange, and percentparentiontoshoulderion. The code has been modified to be run through a paired notebook (the “runner” code) so do NOT evaluate this notebook as it will fail.

Simply adjust the settings as desired and then go to the “runner” code to process the data.

Options:
	intensitycutoff is the intensity cut-off value for considering peaks which may have shoulder ions (peaks must be higher than the given intensity)
	shouldermassrange is the mass range (plus minus) in m/z units to check for shoulder ions around a high intensity peak
	percentparentiontoshoulderion is the percentage cut-off for relative intensity based on a local high intensity peak. Peaks below the cut-off value (and within the shouldermassrange) are considered shoulders and removed.

Important notes:
	1. The main body of the 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.

Inputs

intensitycutoff=1000000;
shouldermassrange=0.01;
percentparentiontoshoulderion=1;
mzmin=100;
mzmax=620;
absintensitymin=200;
absintensitymax=5000000;

Main code

This code has been modified to work with another notebook by ATZ. It has commented out the "nf" variable.

SetDirectory[directory];

reffile=Select[FileNames["*csv*"],StringTake[#,{-3,-1}]=="csv"&]

(*reffile=FileNames["*common-ions*"]*)

(*nf=2;*)

data=Import[reffile[[nf]],"CSV"];

datav2=data[[2;;]];

data[[1;;4]]//TableForm

data[[-3;;-1]]//TableForm

datav2[[1;;3]]//TableForm

datav2[[-3;;-1]]//TableForm

header=data[[1]];

Dimensions[datav2];

datav3=Table[Join[datav2[[i]],{i}],{i,Length[datav2]}];

datav3[[1;;3]]//TableForm

datav3[[-3;;-1]]//TableForm

datav4=Sort[datav3,#1[[1]]<#2[[1]]&][[1;;-1]];

datav4[[1;;3]]//TableForm

datav4[[-3;;-1]]//TableForm

Dimensions[datav2];
Dimensions[datav4];

peakions=Select[datav4,#[[2]]>intensitycutoff&];

peakiontab=Table[0,{i,Length[peakions]}];

nonpeakiontab=Table[0,{i,Length[peakions]}];

subpeakiontab=Table[0,{i,Length[peakions]}];

Do[

(

www=Select[datav4,peakions[[jjj,1]]-shouldermassrange≤ #[[1]]≤ peakions[[jjj,1]]+shouldermassrange&];
refint=peakions[[jjj,2]];
refintper=SetAccuracy[(www[[All,2]]/refint)*100,4];
wwwv2=Table[Join[www[[ii]],{refintper[[ii]]}],{ii,Length[www]}];
wwwv3=Select[wwwv2,#[[-1]]≥ percentparentiontoshoulderion&];
wwwv4=Select[wwwv2,#[[-1]]< percentparentiontoshoulderion&];
peakiontab[[jjj]]=wwwv3[[All,1;;-2]];
nonpeakiontab[[jjj]]=wwwv4[[All,1;;-2]];

uuu=Select[datav4, #[[1]]< peakions[[jjj,1]]-shouldermassrange||#[[1]]> peakions[[jjj,1]]+shouldermassrange&||#[[2]]≤ intensitycutoff&];
subpeakiontab[[jjj]]=uuu;

Clear[www,refint,refintper,wwwv2,wwwv3,wwwv4,uuu]),

{jjj,Length[peakions]}]

nonpeakiontabv2a=Select[nonpeakiontab,Length[#]=!=0&];

Dimensions[nonpeakiontabv2a];
Dimensions[nonpeakiontab];

peakiontabv2=DeleteDuplicates[Partition[peakiontab//Flatten,Dimensions[peakiontab[[1]]][[2]]]];

nonpeakiontabv2=DeleteDuplicates[Partition[nonpeakiontabv2a//Flatten,Dimensions[nonpeakiontabv2a[[1]]][[2]]]];

subpeakiontabv2=DeleteDuplicates[Partition[subpeakiontab//Flatten,Dimensions[subpeakiontab[[1]]][[2]]]];

subpeakiontabv3=Select[datav4,#[[2]]≤ intensitycutoff&];

Dimensions[peakions];
Dimensions[peakiontabv2];
Dimensions[nonpeakiontabv2];
Dimensions[subpeakiontabv2];
Dimensions[subpeakiontabv3];
Dimensions[datav4];

alldatanonpeakion=Join[datav4,nonpeakiontabv2];

Dimensions[alldatanonpeakion]

alldatanonpeakion[[1;;3]]//TableForm

alldatanonpeakion[[-3;;-1]]//TableForm

datav4[[1;;3]]//TableForm

datav4[[-3;;-1]]//TableForm

test2=Tally[alldatanonpeakion[[All,7]]];

gggg2=Select[test2,#[[2]]>1&][[All,1]];

trimmedcommonionsrealtab=Table[0,{i,Length[gggg2]}];

qqq=alldatanonpeakion;

Dimensions[qqq];

(*hh=1;*)

Do[
trimmedcommonionsrealtab[[hh]]=Select[qqq,#[[7]]=!=gggg2[[hh]]&];
qqq=trimmedcommonionsrealtab[[hh]];,{hh,Length[trimmedcommonionsrealtab]}];

Dimensions[alldatanonpeakion];
Dimensions[trimmedcommonionsrealtab[[-1]]];

trimmedcommonionsreal=trimmedcommonionsrealtab[[-1]];

Dimensions[trimmedcommonionsreal];
Dimensions[datav4];
Dimensions[nonpeakiontabv2];

Dimensions[nonpeakiontabv2][[1]];

Dimensions[trimmedcommonionsreal][[1]];

Dimensions[datav4][[1]];

Dimensions[trimmedcommonionsreal][[1]]+Dimensions[nonpeakiontabv2][[1]];

trimmedcommonionsreal[[1;;3]]//TableForm

trimmedcommonionsreal[[-3;;-1]]//TableForm

(*ionplotv2=ListPlot[{nonpeakiontabv2[[All,{1,2}]],trimmedcommonionsreal[[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},{"Shoulder ions = "<>ToString[Dimensions[nonpeakiontabv2][[1]]],"Real ions = "<>ToString[Dimensions[trimmedcommonionsreal][[1]]]}],Frame->True,FrameLabel->{"M/Z","Intensity"},PlotLabel->StringTake[reffile[[nf]],{1,-16}]<>"-shoulder-ions"]*)

nonpeakionplot=ListPlot[{nonpeakiontabv2[[All,{1,2}]]},PlotStyle->{Red},Filling->Axis,FillingStyle->{Directive[Red,Thick]},ImageSize-> 600,PlotRange->{{mzmin,mzmax},{absintensitymin,absintensitymax}},Frame->True,FrameLabel->{"M/Z","Intensity"},PlotLabel->StringTake[reffile[[nf]],{1,-16}]<>"-shoulder-ions",Epilog->Inset[Style["Shoulder ions = "<>ToString[Dimensions[nonpeakiontabv2][[1]]],Red,12],{mzmax*.9,absintensitymax*.9}]];

trimmedcommonionsrealplot=ListPlot[{trimmedcommonionsreal[[All,{1,2}]]},PlotStyle->{Black},Filling->Axis,FillingStyle->{Directive[Gray,Thick]},ImageSize-> 600,PlotRange->{{mzmin,mzmax},{absintensitymin,absintensitymax}},Frame->True,FrameLabel->{"M/Z","Intensity"},PlotLabel->StringTake[reffile[[nf]],{1,-16}]<>"-shoulder-ions",Epilog->Inset[Style["Real ions = "<>ToString[Dimensions[trimmedcommonionsreal][[1]]],Black,12],{mzmax*.9,absintensitymax*.8}]];

ionplotv2=Overlay[{trimmedcommonionsrealplot,nonpeakionplot}]

Dimensions[trimmedcommonionsreal[[All,1;;-2]]]
Dimensions[header]

trimmedionfileexport=Join[{header},trimmedcommonionsreal[[All,1;;-2]]];

nonpeakiontabv2export=Join[{header},nonpeakiontabv2[[All,1;;-2]]];

trimmedionfileexport[[1;;4]]//TableForm

nonpeakiontabv2export[[1;;4]]//TableForm

exportname=StringTake[reffile[[nf]],{1,-16}]<>"-shoulder-ions-removed.csv"

exportnameremoved=StringTake[reffile[[nf]],{1,-16}]<>"-shoulder.csv"

exportdirectory=StringTake[directory,{1,-1}]<>"/Shoulder_Ion_Folder"

(*If[!DirectoryQ[exportdirectory],CreateDirectory[exportdirectory]]*)

If[DirectoryQ[exportdirectory]==False,CreateDirectory[exportdirectory]]

SetDirectory[exportdirectory]

Export[exportname,trimmedionfileexport]
Export[exportnameremoved,nonpeakiontabv2export]

(*ResetDirectory[];*)

exportdirectoryplot=StringTake[directory,{1,-1}]<>"/Plots_Shoulder_Ion_Folder"

If[DirectoryQ[exportdirectoryplot]==False,CreateDirectory[exportdirectoryplot]]

SetDirectory[exportdirectoryplot]

plotnamev2=StringTake[reffile[[nf]],{1,-28}]<>"_Shoulder-ions.tiff"

Export[plotnamev2,ionplotv2]