You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iMAT MATLAB插件报错:向量ub含NaN/Inf值问题求助

解决iMAT运行时"Vector ub contiene NaN o Inf valores"报错的方案

问题根源分析

你的代码存在几个关键逻辑错误,导致生成的expressionRxns或传递给iMAT的参数出现异常值,最终触发报错:

  • 未定义变量使用:调用mapExpressionToReactions时用了filteredExpressionLevels,但该变量从未定义,属于笔误。
  • 基因名与表达量未同步筛选:处理NaN时仅过滤了表达量数据,未同步过滤对应位置的基因名,导致后续基因名和表达量长度不匹配。
  • NaN处理逻辑冗余且错误:在已移除NaN的表达量数据上再次判断NaN,导致无效的筛选操作。

修复步骤与修正代码

1. 同步筛选基因名与表达量

移除表达量中的NaN行时,必须同步移除对应位置的基因名,保证两者一一对应:

% Check if there are any cells with NaN values
nanIndices = isnan(expressionLevels);
if any(nanIndices)
    warning('There are %d NaN values in the expression levels.', sum(nanIndices));
end

% 同步移除NaN对应的表达量和基因名
expressionLevels = expressionLevels(~nanIndices);
geneNames = geneNames(~nanIndices);

2. 修正未定义变量的笔误

将未定义的filteredExpressionLevels替换为实际处理后的表达量数据,同时同步筛选对应基因的表达量:

% Filter genes and expression levels that are in model.genes
isMember = ismember(geneNames, model.genes);
if any(~isMember)
    warning('Some genes in expression data are not in model.genes');
end

filteredGenes = geneNames(isMember);
filteredExpressionLevels = expressionLevels(isMember); % 同步筛选对应表达量

% 映射基因到反应
expressionRxns = mapExpressionToReactions(model, filteredGenes, filteredExpressionLevels);

3. 检查模型本身的边界值

报错提示的ub可能来自模型本身的反应上下界,而非你的表达数据。在调用iMAT前添加检查:

% 检查模型原始ub/lb是否存在NaN或Inf
if any(isnan(model.ub)) || any(isinf(model.ub))
    error('Model ub contains NaN/Inf values');
end
if any(isnan(model.lb)) || any(isinf(model.lb))
    error('Model lb contains NaN/Inf values');
end

4. 完整修正后的代码

initCobraToolbox(false)
changeCobraSolver('gurobi');

% Load the expression data from an Excel file
expressionDataFile = 'C:\Users\usuario\cobratoolbox\160824_BP-Fe.xlsx';  % Replace with the correct path
[num, txt, raw] = xlsread(expressionDataFile);

% Load the model .mat file
modelFile = 'C:\Users\usuario\cobratoolbox\iBP1870.mat'; 
load(modelFile);

% Select the columns I want to analyze from the Excel file
geneNames = txt(2:end, 3);
expressionLevels = num(:, 17);

% Check if there are any cells with NaN values
nanIndices = isnan(expressionLevels);
if any(nanIndices)
    warning('There are %d NaN values in the expression levels.', sum(nanIndices));
end

% 同步移除NaN对应的表达量和基因名
expressionLevels = expressionLevels(~nanIndices);
geneNames = geneNames(~nanIndices);

% Filter genes and expression levels that are in model.genes
isMember = ismember(geneNames, model.genes);
if any(~isMember)
    warning('Some genes in expression data are not in model.genes');
end

filteredGenes = geneNames(isMember);
filteredExpressionLevels = expressionLevels(isMember);

% Run this:
expressionRxns = mapExpressionToReactions(model, filteredGenes, filteredExpressionLevels);

% 检查模型边界值
if any(isnan(model.ub)) || any(isinf(model.ub))
    error('Model ub contains NaN/Inf values');
end
if any(isnan(model.lb)) || any(isinf(model.lb))
    error('Model lb contains NaN/Inf values');
end

% Set the expression level thresholds
threshold_ub = 50.0; % Upper threshold for expression levels
threshold_lb = 5.0;  % Lower threshold for expression levels

% Run iMAT
tissueModel = iMAT(model, expressionRxns, threshold_lb, threshold_ub);

额外排查建议

  • 如果仍报错,检查expressionRxns的内容:用isnan(expressionRxns)和isinf(expressionRxns)确认是否存在异常值。
  • 验证mapExpressionToReactions的输出格式是否符合iMAT的要求,可参考COBRA Toolbox官方文档中该函数的返回值说明。

内容的提问来源于stack exchange,提问作者user1892205

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:40:08