Skip to content

Commit

Permalink
extensions of v2
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullahbas committed Mar 20, 2022
1 parent 4dc48fa commit acafc6e
Show file tree
Hide file tree
Showing 24 changed files with 1,559 additions and 0 deletions.
140 changes: 140 additions & 0 deletions Dependencies/FID-A-MASTER/io_loadlcmdetail.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
%io_loadlcmdetail.m
%Jamie Near, McGill University 2014.
%
% USAGE:
% [metabs,corrMatrix]=io_loadlcmdetail(filename);
%
% DESCRIPTION:
% This function loads in the "detailed output" of LCModel and returns the
% matrix of metabolite correlation coefficients.
%
% INPUTS:
% filename = Filename of the lcmodel detailed output file.

function [metabs,corrMatrix]=io_loadlcmdetail(filename);

%First open the detailed output file and search for the beginning of the
%Correlation Coefficients table
fid=fopen(filename);
line=fgets(fid);
search_index=findstr(line,'Correlation coefficients');

while isempty(search_index)
line=fgets(fid);
search_index=findstr(line,'Correlation coefficients');
end

%Now that we've found the top of the correlation coefficients table, we
%need to record the names of all of the metabolites that are listed in the
%table. Start by going down two lines to where the metabolite names are
%listed:
line=fgets(fid);
line=fgets(fid);

%Read the first line of metabolite names and then go down one line. The
%table of correlation coefficients can only fit 17 metabolites per row.
%Therefore if there are more than 17 metabolites to read, then we will have
%to go down another row and read again. A simple trick to know whether to
%go down another row is to check and see if the first token in the next row
%is equal to the second token from the first row. If so, then we're done
%reading metabolite names. If not, then we need to keep reading.
metabstr=line;
[temp,remain]=strtok(line);
secondMetabName=strtok(remain);
line=fgets(fid);

while ~strcmp(strtok(line),secondMetabName)
metabstr=[metabstr line];
line=fgets(fid);
end

%We are now finished reading all of the metabolite names. Now make a cell
%array containing the names of all of the meatbolites. Note that at this
%point in the code, the final element in metabs will be a blank space,
%since the last metabolite name is missing from the top row of the correlation
%coefficient table in the LCModel detailed output.
remain=metabstr;
n=1;
while ~isempty(remain)
[metabs{n},remain]=strtok(remain);
n=n+1;
end

%Now calculate the total number of metabolites, and initialize the
%correlation coefficient matrix:
numMetabs=numel(metabs);
corrMatrix=zeros(numMetabs);
fillLine=1;

%Now start populating the correlation coefficient matrix. The first token
%of each line is a metabolite name, so we have to remove it. The rest of
%the line is a row in the correlation matrix. The first 17 lines are
%simple to read becuase they all have 17 elements or less, so they don't
%wrap around the page (remember there are only 17 columns in the page). NOTE:
%The correlation matrix is symmetric about the diagonal, but we start by only
%constructing the lower half of the diagonal (The upper half will be all
%zeros). The diagonal elements themselves will be set to 0.5. Then later,
%the full matrix, will be completed by adding the matrix to it's transpose,
%thus completing the symmetry, and making all of the diagonals ones. We
%will also rename "metabs" as we go, becuase the full names of each
%metabolite are given with greater precision on the left column of the
%table in the lcmodel detailed output (compared to the top row of the table).
corrMatrix(1,1)=0.5;
if numMetabs>17
upto=18;
else
upto=numMetabs;
end
while fillLine<upto
[name,corrs]=strtok(line);
metabs{fillLine+1}=name;
corrs=[corrs(1:end-1) ' 0.5'];
corrMatrix(fillLine+1,1:length(str2num(corrs)))=str2num(corrs);
line=fgets(fid);
fillLine=fillLine+1;
end



line=fgets(fid);

%For the 18th to 34th lines, there is one line of 'wrap around'.
if numMetabs>34
upto=35;
else
upto=numMetabs;
end
while fillLine<upto
[name,corrs]=strtok(line);
metabs{fillLine+1}=name;
line=fgets(fid);
corrs=[corrs(1:end-1) ' ' line(1:end-1) ' 0.5'];
corrMatrix(fillLine+1,1:length(str2num(corrs)))=str2num(corrs);
line=fgets(fid);
fillLine=fillLine+1;
end


%For the 35th to 52nd lines, there are two lines of 'wrap around'. Assume
%that there will be no more than 52 elemens in the correlation matrix.
while fillLine<numMetabs
[name,corrs]=strtok(line);
metabs{fillLine+1}=name;
line1=fgets(fid);
line2=fgets(fid);
corrs=[corrs(1:end-1) ' ' line1(1:end-1) ' ' line2(1:end-1) ' 0.5'];
size(str2num(corrs));
corrMatrix(fillLine+1,1:length(str2num(corrs)))=str2num(corrs);
line=fgets(fid);
fillLine=fillLine+1;
end
metabs{end}=name;

%Now add the correlation matrix (the lower have of which is currently only
%populated) to its transpose to complete the symmetry.
corrMatrix=corrMatrix+corrMatrix';

fclose(fid);

% imagesc(corrMatrix);
% impixelinfo;
74 changes: 74 additions & 0 deletions Dependencies/FID-A-MASTER/io_readlcmcoord.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
%io_readlcmcoord.m
%Jamie Near, McGill University 2014.
%
% USAGE:
% out = io_readlcmcoord(filename,metab)
%
% DESCRIPTION:
% Reads a LCModel .coord file and extracts the desired part.
%
% INPUTS:
% filename = filename of the LCModel .coord file.
% part = Which metabolite fit to extract from the .coord file -
% The abbreviated metabolite name should be given (ie.
% 'Cr','PCr','Glu','GABA',etc.)
%
%Esin Ozturk Isik, Bogazici University, 01/06/2017. Corrected the handling
%of non-existing metabolites.

function [out]=io_readlcmcoord(filename,metab)

fid=fopen(filename);
linenum=1;
line=fgets(fid);

ppmstr='points on ppm-axis =';
ppm_index=findstr(line,ppmstr);
while isempty(ppm_index)
line=fgets(fid);
ppm_index=findstr(line,ppmstr);
end

Npts=str2num(line(2:5));

line=fgets(fid);
while linenum<=(Npts/10)
[A,count, errmsg, nextindex] = sscanf(line, '%f', inf);
A;
ppm((linenum-1)*10+1:linenum*10,1)=A;
linenum=linenum+1;
line=fgets(fid);
end


metab_str='';
metab_str(2:1+length(metab))=metab;
metab_str(11:17)='Conc. =';
metab_str;
metab_index=findstr(line,metab);
while isempty(metab_index) & ~feof(fid)
line=fgets(fid);

metab_index=findstr(line,metab);
end
linenum=1;
line=fgets(fid);

if ~feof(fid)
while linenum<=(Npts/10)
[A,count, errmsg, nextindex] = sscanf(line, '%f', inf);
A;
specs((linenum-1)*10+1:linenum*10,1)=A;
linenum=linenum+1;
line=fgets(fid);
end

out.ppm=ppm;
out.specs=specs;
else
out.ppm=[];
out.specs=[];
fclose('all');
end


74 changes: 74 additions & 0 deletions Dependencies/FID-A-MASTER/io_readlcmcoord_getBackground.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
%io_readlcmcoord_getBackground.m
%Jamie Near, McGill University 2014.
%
% USAGE:
% out = io_readlcmcoord_getBackground(filename,part)
%
% DESCRIPTION:
% Reads a LCModel .coord file and extracts the desired part.
%
% INPUTS:
% filename = filename of the LCModel .coord file.
% part = Which part of the .coord file to extract - 'bg' extracts the
% LCModel baseline signal, 'sp' extracts the spectrum, and
% 'fit' extracts the fit.

function [out]=io_readlcmcoord_getBackground(filename,part)

fid=fopen(filename);
linenum=1;
line=fgets(fid)


%READ PPM AXIS
ppmstr='points on ppm-axis ='
ppm_index=findstr(line,ppmstr);
while isempty(ppm_index)
line=fgets(fid);
ppm_index=findstr(line,ppmstr);
end

Npts=str2num(line(2:5))

line=fgets(fid);
while linenum<=(Npts/10)
[A,count, errmsg, nextindex] = sscanf(line, '%f', inf);
A;
ppm((linenum-1)*10+1:linenum*10,1)=A;
linenum=linenum+1;
line=fgets(fid);
end



%GET BACKGROUND

linenum=1;
switch part
case 'bg'
bgstr='NY background values follow'
case 'sp'
bgstr='NY phased data points follow'
case 'fit'
bgstr='NY points of the fit to the data follow'
otherwise
error('ERROR: part not found');
end
bg_index=findstr(line,bgstr);
while isempty(bg_index)
line=fgets(fid);
bg_index=findstr(line,bgstr);
end

line=fgets(fid);
while linenum<=(Npts/10)
[A,count, errmsg, nextindex] = sscanf(line, '%f', inf);
A;
bg((linenum-1)*10+1:linenum*10,1)=A;
linenum=linenum+1;
line=fgets(fid);
end


out.ppm=ppm;
out.specs=bg;
Loading

0 comments on commit acafc6e

Please sign in to comment.