DMPexcel2mat.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. %% CALLED BY EASYconsole.m %%
  2. global mpdmFile
  3. global masterPlateFile
  4. global drugMediaFile
  5. global matDir
  6. % If we already have mpdmFile, don't recreate
  7. if (exist(mpdmFile, 'file') && ~isempty(mpdmFile))
  8. fprintf('The Drug Media/MasterPlate Annotation File: %s exists, skipping DMPexcel2mat.m\n', mpdmFile);
  9. return
  10. end
  11. % Do our best to find and load a relevant MasterPlate file
  12. if ~exist(masterPlateFile, 'file') || isempty(masterPlateFile)
  13. if exist(fullfile(matDir), 'dir')
  14. % Try to find the masterPlateFile automatically (newest created first)
  15. try
  16. files=dir(matDir);
  17. mpFiles={files(strncmp(files.name, 'MasterPlate_', 12)).name};
  18. if isempty(mpFiles)
  19. throw(MException('MATLAB:dir', 'No MasterPlate_ files in the default MasterPlate directory'));
  20. end
  21. % this sorts by date (newest first)
  22. [~, sortedIndices]=sort(datenum({files(strncmp(mpFiles.name, 'MasterPlate_', 12)).date}), 'descend');
  23. sortedFiles=mpFiles(sortedIndices);
  24. masterPlateFile=sortedFiles{1};
  25. disp('Using newest MasterPlate file: ', masterPlateFile, ', skipping directory selection');
  26. catch ME
  27. % This can be silent, not really an error
  28. end
  29. try
  30. % For standalone mode
  31. % GUI input for selecting a MasterPlate Excel file
  32. questdlg('Select MasterPlate Directory (containing DrugMedia_ & MasterPlate_ files)','Directory Selection','OK',...
  33. struct('Default','OK','Interpreter','tex'))
  34. dirToScan=uigetdir();
  35. files=dir(dirToScan);
  36. mpFiles={files(strncmp(files.name, 'MasterPlate_', 12)).name};
  37. if isempty(mpFiles)
  38. throw(MException('MATLAB:dir', 'No MasterPlate_ files in directory'))
  39. end
  40. % this sorts by date (newest first)
  41. [~, sortedIndices]=sort(datenum({files(strncmp(mpFiles.name, 'MasterPlate_', 12)).date}), 'descend');
  42. sortedFiles=mpFiles{sortedIndices};
  43. masterPlateFile=sortedFiles{1};
  44. disp('Using newest MasterPlate file: ', masterPlateFile, ', skipping directory selection');
  45. break
  46. catch ME
  47. h=msgbox(ME.message, 'Error', 'error');
  48. disp('Rerunning directory selection');
  49. % I don't know what else we'll need to do here
  50. end
  51. else
  52. try
  53. % For standalone mode
  54. % GUI input for selecting a MasterPlate Excel file
  55. questdlg('Select MasterPlate Directory (containing DrugMedia_ & MasterPlate_ files)','Directory Selection','OK',...
  56. struct('Default','OK','Interpreter','tex'))
  57. dirToScan=uigetdir();
  58. files=dir(dirToScan);
  59. mpFiles={files(strncmp(files.name, 'MasterPlate_', 12)).name};
  60. if isempty(mpFiles)
  61. throw (MException('MATLAB:dir', 'No MasterPlate_ files in directory'));
  62. end
  63. % this sorts by date (newest first)
  64. [~, sortedIndices]=sort(datenum({files(strncmp(mpFiles.name, 'MasterPlate_', 12)).date}), 'descend');
  65. sortedFiles=mpFiles{sortedIndices};
  66. masterPlateFile=sortedFiles{1};
  67. disp('Using newest MasterPlate file: ', masterPlateFile, ', skipping directory selection');
  68. break
  69. catch ME
  70. h=msgbox(ME.message, 'Error', 'error');
  71. uiwait(h);
  72. disp('Rerunning directory selection');
  73. % I don't know what else we'll need to do here
  74. end
  75. end
  76. else
  77. fprintf('Using MasterPlate file: %s skipping directory selection\n', masterPlateFile);
  78. end
  79. % fid=fopen(masterPlateFile)%('exp23PrintTimes.xls'); % textread puts date and time sequentially into vector
  80. % TODO needs explanation, it isn't clear what it is for
  81. % The input files should be csv, not xlsx
  82. % This whole file could be much better and more portable
  83. if ispc
  84. [num, txt, raw]=xlsread(masterPlateFile); %,'Yor1HitsMPsetFinal');
  85. fields={txt(2,1:15)}; %or 1:17 for later but dont wish to exceed and cause error ? if used
  86. else
  87. clear MPtbl
  88. opts=detectImportOptions(masterPlateFile);
  89. MPtbl=readtable(masterPlateFile,opts);
  90. MPtbl=readtable(masterPlateFile);
  91. fields={opts.VariableNames}; %? if used anywhere although 'saved' to MPDMmat
  92. MPcell=readcell(masterPlateFile);
  93. end
  94. numb=0;
  95. clear MP;
  96. try
  97. if ispc
  98. excLnNum=3;
  99. while (isequal(txt{excLnNum,1},'###'))
  100. numb=numb+1;
  101. MP(numb).head={raw(excLnNum,2:6)};
  102. MP(numb).recNum={raw((excLnNum+1):(excLnNum+384),1)};
  103. MP(numb).orf={raw((excLnNum+1):(excLnNum+384),2)};
  104. MP(numb).strain={raw((excLnNum+1):(excLnNum+384),3)};
  105. MP(numb).genename={raw((excLnNum+1):(excLnNum+384),12)};
  106. MP(numb).drug={raw((excLnNum+1):(excLnNum+384),8)};
  107. MP(numb).media={raw((excLnNum+1):(excLnNum+384),7)};
  108. if size(raw,2)>15
  109. MP(numb).orfRep={raw((excLnNum+1):(excLnNum+384),16)}; % added 12_1005 to specify replicates Orfs in MP
  110. MP(numb).specifics={raw((excLnNum+1):(excLnNum+384),17)}; % added 12_1008 to specify replicates Specific details in MP
  111. else
  112. MP(numb).orfRep=' ';
  113. MP(numb).specifics= ' ';
  114. end
  115. % Future MP field
  116. % if size(raw,2)>17
  117. % MP(numb).specifics2= {raw((excLnNum+1):(excLnNum+384),18)}; % added 12_1008 to specify strain Bkground in MP
  118. % else
  119. % MP(numb).specifics2=' ';
  120. % end
  121. excLnNum=excLnNum+385;
  122. msg=strcat('NumberOfMP=',num2str(numb), ' lastLineNo.=',num2str(excLnNum));
  123. end
  124. else
  125. excLnNum=1;
  126. while (isequal(MPcell{(excLnNum+2),1},'###'))
  127. numb=numb+1;
  128. MP(numb).head={MPtbl(excLnNum,2:6)};
  129. MP(numb).head{1}=table2cell(MP(numb).head{1});
  130. MP(numb).recNum={MPtbl((excLnNum+1):(excLnNum+384),1)};
  131. MP(numb).recNum{1}=table2cell(MP(numb).recNum{1});
  132. MP(numb).orf={MPtbl((excLnNum+1):(excLnNum+384),2)};
  133. MP(numb).orf{1}=table2cell(MP(numb).orf{1});
  134. MP(numb).strain={MPtbl((excLnNum+1):(excLnNum+384),3)};
  135. MP(numb).strain{1}=table2cell(MP(numb).strain{1});
  136. MP(numb).genename={MPtbl((excLnNum+1):(excLnNum+384),12)};
  137. MP(numb).genename{1}= table2cell(MP(numb).genename{1});
  138. MP(numb).media= {MPtbl((excLnNum+1):(excLnNum+384),7)};
  139. MP(numb).media{1}= table2cell(MP(numb).media{1});
  140. if size(MPtbl,2)>15
  141. MP(numb).orfRep= {MPtbl((excLnNum+1):(excLnNum+384),16)}; % added 12_1005 to specify replicates Orfs in MP
  142. MP(numb).orfRep{1}=table2cell(MP(numb).orfRep{1});
  143. MP(numb).specifics={MPtbl((excLnNum+1):(excLnNum+384),17)}; % added 12_1008 to specify replicates Specific details in MP
  144. MP(numb).specifics{1}=table2cell(MP(numb).specifics{1});
  145. else
  146. MP(numb).orfRep= ' ';
  147. MP(numb).specifics= ' ';
  148. end
  149. excLnNum=excLnNum+385;
  150. msg=strcat('NumberOfMP=',num2str(numb), 'lastLineNo.=',num2str(excLnNum));
  151. end
  152. end
  153. catch ME
  154. h=msgbox(ME.message, 'Error', 'error');
  155. uiwait(h);
  156. end
  157. %DMupload
  158. %Drug and Media Plate setup Upload from Excel
  159. excLnNum=1;
  160. numOfDrugs=0;
  161. numOfMedias=0;
  162. % Grabbing the bare filename from the MasterPlate file to see if we can automatically
  163. % find a matching DrugMedia file
  164. [mpFile, mpPath]=fullfile(masterPlateFile);
  165. mpFileParts=strsplit(mpFile, '_');
  166. mpBareFileName=strjoin(mpFileParts(2:end-1), '_');
  167. if ~exist(drugMediaFile, 'file') || isempty(drugMediaFile)
  168. if exist(fullfile(matDir), 'dir')
  169. try
  170. dmFileToTest=fullfile(mpPath, 'DrugMedia_', mpBareFileName, '.xlsx');
  171. if exist(dmFileToTest, 'file') % Try to find a matching drug media file
  172. drugMediaFile=dmFileToTest;
  173. fprintf('Using matching DrugMedia file: %s, skipping directory selection\n', drugMediaFile);
  174. else
  175. % Try to find the DrugMedia file automatically (newest created first)
  176. files=dir(matDir);
  177. dmFiles={files(strncmp(files.name, 'DrugMedia_', 10)).name};
  178. if isempty(dmFiles)
  179. throw (MException('MATLAB:dir', 'No DrugMedia_ files in directory'));
  180. end
  181. % this sorts by date (newest first)
  182. [~, sortedIndices]=sort(datenum({files(strncmp(dmFiles.name, 'DrugMedia_', 10)).date}), 'descend');
  183. sortedFiles=dmFiles{sortedIndices};
  184. drugMediaFile=sortedFiles{1};
  185. fprintf('Using newest DrugMedia file: %s, skipping directory selection\n', drugMediaFile);
  186. end
  187. catch Me
  188. % This can be silent, not really an error
  189. end
  190. else
  191. for i=1:5 % give users 5 chances to get it right
  192. try
  193. % GUI input for selecting a DrugMedia file
  194. % sort by newest matching DrugMedia file and return that if we find one
  195. % For standalone mode
  196. % GUI input for selecting a MasterPlate Excel file
  197. questdlg('Select DrugMedia directory','Directory Selection','OK',...
  198. struct('Default','OK','Interpreter','tex'))
  199. dirToScan=uigetdir();
  200. files=dir(dirToScan);
  201. dmFiles={files(strncmp(files.name, 'DrugMedia_', 10)).name};
  202. if isempty(dmFiles)
  203. throw (MException('MATLAB:dir', 'No DrugMedia_ files in directory'));
  204. end
  205. % this sorts by date (newest first)
  206. [~, sortedIndices]=sort(datenum({files(strncmp(dmFiles.name, 'DrugMedia_', 10)).date}), 'descend');
  207. sortedFiles=dmFiles{sortedIndices};
  208. drugMediaFile=sortedFiles{1};
  209. catch ME
  210. h=msgbox(ME.message, 'Error', 'error');
  211. uiwait(h);
  212. disp('Rerunning directory selection');
  213. % I don't know what else we'll need to do here
  214. end
  215. end
  216. end
  217. else
  218. fprintf('Using drugMediaFile: %s, skipping directory selection\n', drugMediaFile);
  219. end
  220. % Drug and Media Plate setup
  221. % TODO needs better explanation
  222. if ispc
  223. [num, txt, raw]=xlsread(drugMediaFile); %'Yor1HitsMPsetFinal');
  224. fields={txt(2,1:5)};
  225. Linked=num(1,1);
  226. else
  227. opts=detectImportOptions(drugMediaFile);
  228. DMtbl=readtable(drugMediaFile,opts);
  229. fields=opts.VariableOptions;
  230. Linked=DMtbl{1,1};
  231. DMcell=readcell(drugMediaFile);
  232. end
  233. % TODO needs better explanation
  234. numb=0;
  235. if isequal(Linked,1) % Drugs and Media are linked 1 to 1; else they are combinatorial
  236. clear DM;
  237. excLnNum=2;
  238. if ispc
  239. while (~isequal(txt{excLnNum,2},'###'))
  240. numb=numb+1;
  241. DM.drug(numb)={raw(excLnNum,2)};
  242. DM.conc(numb)={raw(excLnNum,3)};
  243. DM.media(numb)={raw(excLnNum,4)};
  244. DM.mod1(numb)={raw(excLnNum,5)};
  245. DM.conc1(numb)={raw(excLnNum,6)};
  246. DM.mod2(numb)={raw(excLnNum,7)};
  247. DM.conc2(numb)={raw(excLnNum,8)};
  248. excLnNum=excLnNum+1;
  249. msg=strcat('NumberOf1:1DrugMediaPlates=',num2str(numb), ' lastLineNo.=',num2str(excLnNum));
  250. end
  251. else
  252. clear DM
  253. excLnNum=1;
  254. while (~isequal(DMcell{excLnNum+1,2},'###'))
  255. numb=numb+1;
  256. DM.drug(numb)={DMtbl(excLnNum,2)};
  257. DM.drug(numb)=table2cell(DM.drug{numb});
  258. DM.conc(numb)={DMtbl(excLnNum,3)};
  259. DM.conc(numb)=table2cell(DM.conc{numb});
  260. DM.media(numb)={DMtbl(excLnNum,4)};
  261. DM.media(numb)=table2cell(DM.media{numb});
  262. DM.mod1(numb)={DMtbl(excLnNum,5)};
  263. DM.mod1(numb)=table2cell(DM.mod1{numb});
  264. DM.conc1(numb)={DMtbl(excLnNum,6)};
  265. DM.conc1(numb)=table2cell(DM.conc1{numb});
  266. DM.mod2(numb)={DMtbl(excLnNum,7)};
  267. DM.mod2(numb)=table2cell(DM.mod2{numb});
  268. DM.conc2(numb)={DMtbl(excLnNum,8)};
  269. DM.conc2(numb)=table2cell(DM.conc2{numb});
  270. excLnNum=excLnNum+1;
  271. msg=strcat('NumberOf1:1DrugMediaPlates=',num2str(numb), ' lastLineNo.=',num2str(excLnNum));
  272. end
  273. end
  274. end
  275. % Legacy contengency: not ever used
  276. if isequal(Linked,0) % 0 indicates Drugs and Media are combinatorial
  277. clear DM;
  278. excLnNum=2;
  279. drgCnt=0;
  280. medCnt=0;
  281. if ispc
  282. while (~isequal(txt{excLnNum,2},'###'))
  283. drgCnt=drgCnt+1;
  284. DM.drug(drgCnt)={raw(excLnNum,2)};
  285. DM.conc(drgCnt)={raw(excLnNum,3)};
  286. excLnNum=excLnNum+1;
  287. end
  288. while (~isequal(txt{excLnNum,4},'###'))
  289. medCnt=medCnt+1;
  290. DM.media(medCnt)={raw(excLnNum,4)};
  291. excLnNum=excLnNum+1;
  292. end
  293. else
  294. excLnNum=1;
  295. while (~isequal(DMcell{excLnNum+1,2},'###'))
  296. drgCnt=drgCnt+1;
  297. DM.drug(drgCnt)={DMtbl(excLnNum,2)};
  298. DM.conc(drgCnt)={DMtbl(excLnNum,3)};
  299. excLnNum=excLnNum+1;
  300. end
  301. while (~isequal(DMcel{excLnNum+1,4},'###'))
  302. medCnt=medCnt+1;
  303. DM.media(medCnt)={DMtbl(excLnNum,4)};
  304. excLnNum=excLnNum+1;
  305. end
  306. end
  307. msg=strcat('NumberOfDrugs=',num2str(drgCnt), ' NumberOfMedias=',num2str(medCnt) );
  308. end
  309. save(mpdmFile, 'fields','MP','DM','Linked');
  310. msgbox(sprintf('Drug-Media-MasterPlate Annotation File %s Generation Complete', mpdmFile))