-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeadSorting.m
More file actions
107 lines (100 loc) · 3.74 KB
/
Copy pathBeadSorting.m
File metadata and controls
107 lines (100 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function successmessage=BeadSorting(filepath,file_range)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% file:BeadSorting.m
% ***Description***:
% This function serves automatically search for Bead Peaks and sort them
% from the Cell peaks in Blood samples where Beads were also spiked.
% Currently this function is run from within SamplePeakDetection.m function
% when a flag indicated Beads were added to blood sample.
% Written By: Nilay Vora (nvora01@tufts.edu)
% Date Written: 01/13/2022
% Modifying Author:Nilay Vora
% Date Modified: 01/19/2022
% Latest Revision: Manually modified indexing due to error in lines 49-52,
% not sure if this is the right correction.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function details
% Inputs:
% filepath = Location of all main folder where all subfolders are
% file_range = Which files do you want to analyze in a specific day
% Outputs:
% successmessage = a string that indicates the completion of the code
%
% Usage SamplePeakDetection(filepath,file_range)
% Example:
% filepath = 'U:\Nilay\IVFC\Acquired Data\Blood Data\NV_092821_Blood_LNPs';
% file_range= [1:3];
%
% output=SamplePeakDetection(filepath,file_range)
%% Initialization
clc
%% Find Folders
mainFolder=filepath;
cd(mainFolder)
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
dirinfo(ismember( {dirinfo.name}, {'.', '..'})) = []; %remove . and ..
[~,c]=natsortfiles({dirinfo.name});
subdirinfo = cell(length(dirinfo));
for K = 1 : length(dirinfo)
thisdir = dirinfo(c(K)).name;
subdirinfo{K} = dir(fullfile(thisdir, '*RFLR.mat'));
end
subdirinfo = subdirinfo(~cellfun('isempty',subdirinfo));
if file_range(1)~=1
file_range=file_range-(file_range(1)-1);
end
%% Main Loop: Split the Cells from Beads
for i=file_range
disp(['Evaluating File # ',num2str(i),' of ',num2str(size(subdirinfo,1))]);
del=[]; % Matrix of bead peaks
truepeaks=[]; % Matrix of cell peaks
if~isempty(subdirinfo{i})
cd(subdirinfo{i}(1).folder)
load(subdirinfo{i}(1).name)
pv1=peak_values;
chunks=unique(peak_values(:,6));
for j=min(chunks):max(chunks)
disp(['Chunk # ',num2str(j),' of ',num2str(max(chunks))]);
a=pv1(:,6)==j;
loc=pv1(a,7);
load([subdirinfo{i}(1).name(1:end-8),'NoScatAll.mat'],'peak_values')
pv2=peak_values;
a2=find(pv2(:,6)==j);
loc2=pv2(a2,7);
wid2=pv2(a2,9);
for k=1:length(loc2)
l2=loc2(k);
match=find(loc>=l2-wid2(k).*1.5 & loc<=l2+wid2(k).*1.5,1);
if ~isempty(match)
del=[del;pv2(a2(k),:)]; %#ok<AGROW>
else
truepeaks=[truepeaks;pv2(a2(k),:)]; %#ok<AGROW>
end
end
end
peak_values=truepeaks;
save([subdirinfo{i}(1).name(1:end-8),'NoScatCell.mat'],'peak_values')
peak_values=del;
save([subdirinfo{i}(1).name(1:end-8),'NoScatBeads.mat'],'peak_values')
end
end
%% Save all sorted files
pv1=[];
pv2=[];
for i=file_range
disp(['Evaluating File # ',num2str(i),' of ',num2str(size(subdirinfo,1))]);
if~isempty(subdirinfo{i}(1))
cd(subdirinfo{i}(1).folder)
load([subdirinfo{i}(1).name(1:end-8),'NoScatBeads.mat'],'peak_values')
pv1=[pv1;peak_values]; %#ok<AGROW>
load([subdirinfo{i}(1).name(1:end-8),'NoScatCell.mat'],'peak_values')
pv2=[pv2;peak_values]; %#ok<AGROW>
end
end
cd(mainFolder)
peak_values=pv1;
save([subdirinfo{i}(1).name(4:end-8),'NoScatBeads.mat'],'peak_values')
peak_values=pv2;
save([subdirinfo{i}(1).name(4:end-8),'NoScatCell.mat'],'peak_values')
successmessage='Completed Seperating Bead Peaks';