Home > bml > annot > bml_annot_table.m

bml_annot_table

PURPOSE ^

BML_ANNOT_TABLE transforms a table into an annotations table [internal]

SYNOPSIS ^

function annot=bml_annot_table(x, description, x_var_name)

DESCRIPTION ^

 BML_ANNOT_TABLE transforms a table into an annotations table [internal]

 Use as
   annot=bml_annot_table(x)
   annot=bml_annot_table(x, description)
   annot=bml_annot_table(x, [], x_var_name)

 x - table: should contains 'starts' and 'ends' variables
 description - string: description of the annotation table
 x_var_name - string: name of the variable used for the annot table, as
     returned by inputname(). Usefull for anidated calls.

 returns a table with variables 'id', 'starts', 'ends' and any additonal
 variable present in x. Issues a error on inconsistent input.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function annot=bml_annot_table(x, description, x_var_name)
0002 
0003 % BML_ANNOT_TABLE transforms a table into an annotations table [internal]
0004 %
0005 % Use as
0006 %   annot=bml_annot_table(x)
0007 %   annot=bml_annot_table(x, description)
0008 %   annot=bml_annot_table(x, [], x_var_name)
0009 %
0010 % x - table: should contains 'starts' and 'ends' variables
0011 % description - string: description of the annotation table
0012 % x_var_name - string: name of the variable used for the annot table, as
0013 %     returned by inputname(). Usefull for anidated calls.
0014 %
0015 % returns a table with variables 'id', 'starts', 'ends' and any additonal
0016 % variable present in x. Issues a error on inconsistent input.
0017 
0018 if isempty(x) 
0019   x=table();
0020 end
0021 
0022 if isnumeric(x)
0023   if size(x,1) < size(x,2)
0024     x = x';
0025   end
0026   x = table(x);
0027 end
0028 if iscell(x); x=cell2table(x); end
0029 if isstruct(x); x=struct2table(x); end
0030 
0031 if ~exist('description','var') || isempty(description)
0032   if isempty(x.Properties.Description)
0033     if exist('x_var_name','var')
0034       description=x_var_name;
0035     else
0036       description=inputname(1);
0037     end
0038   else
0039     description=x.Properties.Description;
0040   end
0041 end
0042 
0043 if iscellstr(description) 
0044   if numel(description)==1
0045     description = description{1};
0046   else
0047     error('The Description property must be a character vector.');
0048   end
0049 end
0050 
0051 if isempty(x)
0052   annot=table();
0053   annot.Properties.Description = description;
0054   return
0055 end
0056 
0057 if ~ismember('starts',x.Properties.VariableNames)
0058   if width(x)<=2 
0059     x.Properties.VariableNames(1)={'starts'};
0060   else
0061     error('x should have variable ''starts''');
0062   end
0063 end
0064 if(any(ismissing(x.starts)))
0065   warning("starts contains nans");
0066 end
0067 
0068 if ~ismember('ends',x.Properties.VariableNames)
0069   if width(x)==1 
0070     x.ends = x.starts;
0071   elseif width(x)==2 
0072     x.Properties.VariableNames(2)={'ends'};
0073   else
0074     error('x should have variable ''ends''');
0075   end  
0076 end
0077 if(any(ismissing(x.ends)))
0078   warning("ends contains nans");
0079 end
0080 
0081 if ~strcmp('id',x.Properties.VariableNames)
0082   x = sortrows(x,'starts');
0083   x = [table(transpose(1:height(x)),'VariableNames',{'id'}), x];
0084 elseif length(unique(x.id)) < height(x)
0085   error('inconsistent id variable')
0086 else
0087    x = sortrows(x,'id');
0088 end
0089 
0090 if any(strcmp('duration',x.Properties.VariableNames))
0091   x.duration = [];
0092 end
0093 x = [x, table(x.ends - x.starts,'VariableNames',{'duration'})];
0094 
0095 annot = bml_annot_reorder_vars(x, {'id','starts','ends','duration'});
0096 annot.Properties.Description = description;
0097 
0098

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005