Skip to content

Commit

Permalink
Use different rt_tol for uncorrected rt
Browse files Browse the repository at this point in the history
  • Loading branch information
yufongpeng committed Jan 4, 2024
1 parent 988e4e1 commit c214876
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/SphingolipidsID.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,19 +412,20 @@ propertynames(quant::Quantification) = (:batch, :config, propertynames(getfield(
"""
RTCorrection
Type holding rt data and regression line for correcting rt from old batch (data for identification) to new batch (data for quantification).
Type holding rt data and regression line for correcting rt from new batch (data for quantification) to old batch (data for identification).
* `formula`: formula of regression line.
* `data`: raw data from new batch.
* `table`: old transition table generated from `project.analyte` and function `analytetable_mrm`.
* `fn`: `Dictionary` containg regression coefficents for each class.
* `model`: `Dictionary` containg regression coefficents for each class.
* `config`: `Dictionary` containgin configuration.
This is a callable object taking a class and rt or vector of class and a vector of rt as input, and returning new rt or a vector of new rt. If the class is not in `fn`, it will return the original rt.
"""
struct RTCorrection
data::AbstractRawData
table::Table
model::Dictionary
config::Dictionary
end
(fn::RTCorrection)(cls::ClassSP, dt) = model_predict(get(fn.model, cls, nothing), dt)
(fn::RTCorrection)(cls::Vector{<: ClassSP}, dt) = mapreduce(vcat, cls, dt) do c, d
Expand Down
18 changes: 12 additions & 6 deletions src/quantification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ end
function set_qcdata_mrm!(project::Project, featuretable::Table;
rt_correction = nothing,
rt_tol = last(project.data).config[:rt_tol],
raw_rt_tol = isnothing(rt_correction) ? 0.3 : rt_correction.config[:rt_tol],
mz_tol = last(project.data).config[:mz_tol],
signal = project.appendix[:signal],
est_fn = mean,
err_fn = rsd,
err_tol = 0.5,
other_fn = Dictionary{Symbol, Any}())
push!(project.data, qcdata_mrm!(featuretable, project; rt_correction, rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn))
push!(project.data, qcdata_mrm!(featuretable, project; rt_correction, rt_tol, raw_rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn))
set!(project.quantification.config, :qcdata, last(project.data))
last(project.data)
end
Expand All @@ -138,6 +139,7 @@ qcdata_mrm!(project::Project, featuretable::Table; kwargs...) = qcdata_mrm!(feat
function qcdata_mrm!(featuretable::Table, project = nothing;
rt_correction = nothing,
rt_tol = isnothing(project) ? 0.1 : last(project.data).config[:rt_tol],
raw_rt_tol = isnothing(rt_correction) ? 0.3 : rt_correction.config[:rt_tol],
mz_tol = isnothing(project) ? 0.35 : last(project.data).config[:mz_tol],
signal = isnothing(project) ? :area : project.appendix[:signal],
est_fn = mean,
Expand All @@ -152,7 +154,7 @@ function qcdata_mrm!(featuretable::Table, project = nothing;
end
other_fn = default_other_fn
n = length(unique(featuretable.datafile))
mrm = MRM!(Table(featuretable; match_id = zeros(Int, length(featuretable)), match_score = -getproperty(featuretable, signal)); combine = false, rt_tol, mz_tol, n = 1, signal = nothing, est_fn, err_fn, err_tol, other_fn)
mrm = MRM!(Table(featuretable; match_id = zeros(Int, length(featuretable)), match_score = -getproperty(featuretable, signal)); combine = false, rt_tol = raw_rt_tol, mz_tol, n = 1, signal = nothing, est_fn, err_fn, err_tol, other_fn)
transition_matching!(mrm, project; rt_correction, rt_tol, mz_tol)
at = analysistable(mrm.table; method = mrm.config[:method], data = signal)
if !isnothing(project)
Expand All @@ -174,13 +176,14 @@ function set_serialdilution_mrm!(project::Project, featuretable::Table, concentr
r2_threshold = 0.8,
nlevel = 5,
rt_tol = last(project.data).config[:rt_tol],
raw_rt_tol = isnothing(rt_correction) ? 0.3 : rt_correction.config[:rt_tol],
mz_tol = last(project.data).config[:mz_tol],
signal = project.appendix[:signal],
est_fn = mean,
err_fn = rsd,
err_tol = 0.5,
other_fn = Dictionary{Symbol, Any}())
push!(project.data, serialdilution_mrm!(featuretable, concentration, project; pointlevel, rt_correction, r2_threshold, nlevel, rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn))
push!(project.data, serialdilution_mrm!(featuretable, concentration, project; pointlevel, rt_correction, r2_threshold, nlevel, rt_tol, raw_rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn))
set!(project.quantification.config, :serialdilution, last(project.data))
last(project.data)
end
Expand All @@ -195,6 +198,7 @@ function serialdilution_mrm!(featuretable::Table, concentration::Vector, project
r2_threshold = 0.8,
nlevel = 5,
rt_tol = isnothing(project) ? 0.1 : last(project.data).config[:rt_tol],
raw_rt_tol = isnothing(rt_correction) ? 0.3 : rt_correction.config[:rt_tol],
mz_tol = isnothing(project) ? 0.35 : last(project.data).config[:mz_tol],
signal = isnothing(project) ? :area : project.appendix[:signal],
est_fn = mean,
Expand All @@ -208,7 +212,7 @@ function serialdilution_mrm!(featuretable::Table, concentration::Vector, project
end
end
other_fn = default_other_fn
mrm = MRM!(Table(featuretable; match_id = zeros(Int, length(featuretable)), match_score = -getproperty(featuretable, signal)); combine = false, rt_tol, mz_tol, n = 1, signal = nothing, est_fn, err_fn, err_tol, other_fn)
mrm = MRM!(Table(featuretable; match_id = zeros(Int, length(featuretable)), match_score = -getproperty(featuretable, signal)); combine = false, rt_tol = raw_rt_tol, mz_tol, n = 1, signal = nothing, est_fn, err_fn, err_tol, other_fn)
transition_matching!(mrm, project; rt_correction, rt_tol, mz_tol)
config = dictionary(pairs((; concentration, rt_correction, r2_threshold, nlevel, rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn)))
at = analysistable(mrm.table; method = mrm.config[:method], data = signal)
Expand Down Expand Up @@ -286,13 +290,14 @@ function set_quantdata_mrm!(project::Project, featuretable::Table;
rt_correction = nothing,
name = r"S\d*.*",
rt_tol = last(project.data).config[:rt_tol],
raw_rt_tol = isnothing(rt_correction) ? 0.3 : rt_correction.config[:rt_tol],
mz_tol = last(project.data).config[:mz_tol],
signal = project.appendix[:signal],
est_fn = mean,
err_fn = rsd,
err_tol = 0.5,
other_fn = Dictionary{Symbol, Any}())
push!(project.data, quantdata_mrm!(featuretable, project; rt_correction, name, rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn))
push!(project.data, quantdata_mrm!(featuretable, project; rt_correction, name, rt_tol, raw_rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn))
set!(project.quantification.config, :quantdata, last(project.data))
last(project.data)
end
Expand All @@ -303,6 +308,7 @@ quantdata_mrm!(project::Project, featuretable::Table; kwargs...) = quantdata_mrm
function quantdata_mrm!(featuretable::Table, project = nothing;
rt_correction = nothing,
rt_tol = isnothing(project) ? 0.1 : last(project.data).config[:rt_tol],
raw_rt_tol = isnothing(rt_correction) ? 0.3 : rt_correction.config[:rt_tol],
mz_tol = isnothing(project) ? 0.35 : last(project.data).config[:mz_tol],
signal = isnothing(project) ? :area : project.appendix[:signal],
est_fn = mean,
Expand All @@ -317,7 +323,7 @@ function quantdata_mrm!(featuretable::Table, project = nothing;
end
other_fn = default_other_fn
n = length(unique(featuretable.datafile))
mrm = MRM!(Table(featuretable; match_id = zeros(Int, length(featuretable)), match_score = -getproperty(featuretable, signal)); combine = false, rt_tol, mz_tol, n = 1, signal = false, est_fn, err_fn, err_tol, other_fn)
mrm = MRM!(Table(featuretable; match_id = zeros(Int, length(featuretable)), match_score = -getproperty(featuretable, signal)); combine = false, rt_tol = raw_rt_tol, mz_tol, n = 1, signal = false, est_fn, err_fn, err_tol, other_fn)
transition_matching!(mrm, project; rt_correction, rt_tol, mz_tol)
at = analysistable(mrm.table; method = mrm.config[:method], data = signal)
if !isnothing(project)
Expand Down
4 changes: 2 additions & 2 deletions src/rt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ rt_correction(modelcall::RetentionModelCall, data::AbstractRawData, analytetable
function rt_correction(modelcall::RetentionModelCall, analytetable::Table, data::AbstractRawData; mz_tol = 0.35, rt_tol = 0.3)
RTCorrection(data, analytetable, map(groupview(x -> class(x.analyte), analytetable)) do tbl
rt_correction_modeling(modelcall, tbl, data; rt_tol, mz_tol)
end)
end, Dictionary([:rt_tol, :mz_tol], [rt_tol, mz_tol]))
end

function rt_correction_modeling(modelcall, tbl, data; rt_tol, mz_tol)
Expand All @@ -439,7 +439,7 @@ function rt_correction_modeling(modelcall, tbl, data; rt_tol, mz_tol)
end

function update_rt_correction!(rc::RTCorrection, modelcall::RetentionModelCall, cls::ClassSP)
set!(rc.model, cls, @views rt_correction_modeling(modelcall, rc.table[class.(rc.table.analyte) .== cls], rc.data))
set!(rc.model, cls, @views rt_correction_modeling(modelcall, rc.table[class.(rc.table.analyte) .== cls], rc.data; rt_tol = rc.config[:rt_tol], mz_tol = rc.config[:mz_tol]))
rc
end

Expand Down

0 comments on commit c214876

Please sign in to comment.