Commit dc60a0ac authored by Antonio Politi's avatar Antonio Politi
Browse files

Function to distribute files in local folders

parent 702de52a
......@@ -25,8 +25,80 @@ else
end
for ifile = 2:length(resfiles)
for ifile = 38:length(resfiles)
resfile = resfiles{ifile};
display(sprintf('master_workflow_fit_proteins processing res file %d/%d %s', ifile, length(resfiles), resfiles{ifile}))
workflow_fit_protein(resfile, '2c');
fnames = workflow_fit_protein(resfile, '2c');
%fnames = {fullfile(fileparts(resfile), '1c_opt.res'), fullfile(fileparts(resfile),'2c_opt.res')};
for iout = 1:length(fnames)
distributefiles(fnames{iout})
end
end
end
function status = appendFiles( readFile, writtenFile )
% APPENDFILES append readFile to writtenFile
fr = fopen( readFile, 'rt' );
fw = fopen( writtenFile, 'at' );
while feof( fr ) == 0
tline = fgetl( fr );
fwrite( fw, sprintf('%s\n',tline ) );
end
fclose(fr);
fclose(fw);
end
function distributefiles(resfile, patterns)
if nargin < 1
[resfile, pathname] = uigetfile({'*.res'; '*.*'},'Select res file', 'C:\Users\toni\Dropbox\NPCMaturation\matlabcode\FCS\example_data\Protein');
if ~resfile
return
end
resfile = fullfile(pathname, resfile);
end
% DISTRIBUTEFILES read data in resfile and create resfiles for the
% different cellular classes
if nargin < 2
patterns = {'\\\w+GFP\w+\\','\\\w+Cherry\w+\\' '\\POI\\', '\\POI_meta\\', '\\LSM\\', '\\WT\\'};
end
[path, basename] = fileparts(resfile);
res = readtable(resfile, 'Delimiter', '\t', 'Header', 1, 'FileType', 'text', 'TreatAsEmpty',{'NA'});
% get the header
filepre = fopen(resfile);
header = '';
for i = 1:2
tline = fgetl(filepre);
header = sprintf('%s%s\n',header,tline);
end
fclose(filepre);
%%
idxs = cell(1,length(patterns));
mdir = cell(1,length(patterns));
for i = 1:length(res.FullPath)
for ip = 1:length(patterns)
[idxst, idxend] = regexpi(res.FullPath{i},patterns{ip}, 'start', 'end', 'matchcase');
if ~isempty(idxst)
if isempty(mdir{ip})
mdir{ip}= res.FullPath{i}(1:idxend(1));
end
idxs{ip} = [idxs{ip};i];
end
end
end
%%
for ip = 1:length(patterns)
if ~isempty(idxs{ip})
tmpfile = fullfile(mdir{ip}, [ 'tmp.res']);
outfile = fullfile(mdir{ip}, [ basename '.res']);
writetable(res(idxs{ip},:), tmpfile , 'WriteVariableNames',false, 'Delimiter', '\t','FileType', 'text');
outfid= fopen(outfile, 'w');
fprintf(outfid, header);
fclose(outfid);
appendFiles(tmpfile, outfile);
delete(tmpfile);
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment