Skip to content

Commit

Permalink
write and read disk data
Browse files Browse the repository at this point in the history
  • Loading branch information
yufongpeng committed Jan 10, 2024
1 parent 9a53310 commit 12b0b3f
Show file tree
Hide file tree
Showing 425 changed files with 23,757 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/Manifest.toml
/notebook
/SPID
/test/manualtest.jl
/test/manualtest_id.jl
/test/manualtest_qt.jl
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
DataPipes = "02685ad9-2d12-40c3-9f73-c6aeda6a7ff5"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
Expand All @@ -29,10 +30,11 @@ UnitfulMoles = "999f2bd7-36bf-5ba7-9bc1-c9473aa75374"
[compat]
AnovaGLM = "0.2"
CSV = "0.10"
Clustering = "0.15"
ChemistryQuantitativeAnalysis = "0.3"
Clustering = "0.15"
DataPipes = "0.3"
Dictionaries = "0.3"
JLD2 = "0.4"
MLStyle = "0.4"
PlotlyJS = "0.18"
Plots = "1"
Expand Down
12 changes: 6 additions & 6 deletions src/SphingolipidsID.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SphingolipidsID

using CSV, PrettyTables,
MLStyle, DataPipes, SplitApplyCombine, TypedTables, Dictionaries,
Statistics, StatsBase, Clustering, Plots, GLM, ThreadsX, LinearAlgebra, ChemistryQuantitativeAnalysis
Statistics, StatsBase, Clustering, Plots, GLM, ThreadsX, LinearAlgebra, ChemistryQuantitativeAnalysis, JLD2
using AnovaBase: getterms, anova, @formula
using UnitfulMoles: parse_compound, ustrip, @u_str
export SPDB, LIBRARY_POS, FRAGMENT_POS, ADDUCTCODE, CLASSDB,
Expand Down Expand Up @@ -386,18 +386,18 @@ propertynames(sd::SerialDilution) = Tuple(unique((:raw, :batch, :config, propert
#SerialDilution(raw::T, table::Table, level::Vector, config::Dictionary) where {T <: Data} = SerialDilution{T}(raw, table, level, config)

"""
Quantification
Quantification{A}
Type containg information of analyte settings, calibration data, qc data, serial dilution data, and sample data.
* `batch`: `Batch` containg analyte settings, calibration data, and sample data.
* `config`: `Dictionary` containing config information. `config[:qcdata]` stores a `QCData`, `config[:serialdilution]` stores a `SerialDilution`, and `config[:quantdata]` stores a `QuantData` (sample data).
"""
struct Quantification
batch::Batch
struct Quantification{A}
batch::Batch{A}
config::Dictionary
Quantification() = new()
Quantification(batch::Batch, config::Dictionary) = new(batch, config)
Quantification() = new{Any}()
Quantification(batch::Batch{A}, config::Dictionary) where A = new{A}(batch, config)
end # Wrap MethodTable

function getproperty(quant::Quantification, p::Symbol)
Expand Down
2 changes: 2 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ copy_wo_project(aquery::Query) = Query(aquery.project, copy_wo_project.(aquery.r
reuse_copy(aquery::Query) = Query(aquery.project, reuse_copy(aquery.result), deepcopy(aquery.query), true)
reuse_copy(v::Vector) = copy(v)
reuse_copy(v::T) where {T <: SubArray} = T(parent(v), v.indices, v.offset1, v.stride1)
copy(analyte::AnalyteSP) = AnalyteSP(analyte.compound, analyte.rt, analyte.state, analyte.cpdsc, analyte.score)
copy(cpd::CompoundSP) = CompoundSP(cpd.class, cpd.chain, cpd.fragment, cpd.signal, cpd.state, cpd.result, cpd.project)

# equivalent: ion1 and ion2 are the same
equivalent_in(ion, collection) = any(equivalent(ion, x) for x in collection)
Expand Down
335 changes: 305 additions & 30 deletions src/io.jl

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions src/quantification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ function quantification_mrm(analyte::AbstractVector{<: AbstractAnalyteID}, quant
Quantification(Batch(MethodTable{Table}(vcat(isdtable, analytetable), signal, Int[], conctable, nothing)), dictionary(pairs((; signal, anion, rt_tol, mz_tol, quantifier, qualifier, default_mz2, isd_fn, coelution_fn))))
end

function transition_matching!(data::MRM, project = nothing; rt_tol = 0.1, mz_tol = 0.35, rt_correction = nothing)
function transition_matching!(data::MRM, method = nothing; rt_tol = 0.1, mz_tol = 0.35, rt_correction = nothing)
featuretable = data.table
sort!(featuretable, [:id, :injection_order])
set!(data.config, :method, nothing)
isnothing(project) && (featuretable.match_id .= featuretable.id; return sort!(featuretable, [:match_id, :id, :injection_order, :match_score]))
method = project.quantification.batch.method
data.config[:method] = method
isnothing(method) && (featuretable.match_id .= featuretable.id; return sort!(featuretable, [:match_id, :id, :injection_order, :match_score]))
i = 0
for ft in groupview(getproperty(:id), featuretable)
mz1 = mean(ft.mz1)
Expand Down Expand Up @@ -137,6 +134,7 @@ qcdata_mrm(project::Project, featuretable::Table; kwargs...) = qcdata_mrm!(proje
qcdata_mrm(featuretable::Table, project = nothing; kwargs...) = qcdata_mrm!(deepcopy(featuretable), project; kwargs...)
qcdata_mrm!(project::Project, featuretable::Table; kwargs...) = qcdata_mrm!(featuretable, project; kwargs...)
function qcdata_mrm!(featuretable::Table, project = nothing;
method = isnothing(project) ? nothing : isdefined(project.quantification, :batch) ? project.quantification.batch.method : 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],
Expand All @@ -155,13 +153,13 @@ function qcdata_mrm!(featuretable::Table, project = nothing;
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 = 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)
transition_matching!(mrm, method; rt_correction, rt_tol, mz_tol)
at = analysistable(mrm.table; method, data = signal)
if !isnothing(project)
signal = :estimated_concentration
set_quantification!(at, project.quantification.batch; estimated_concentration = signal)
end
config = dictionary(pairs((; rt_correction, rt_tol, mz_tol, n, signal, est_fn, err_fn, err_tol, other_fn)))
config = dictionary(pairs((; method, rt_correction, rt_tol, mz_tol, n, signal, est_fn, err_fn, err_tol, other_fn)))
QCData(mrm, at, config)
end

Expand Down Expand Up @@ -192,6 +190,7 @@ serialdilution_mrm(project::Project, featuretable::Table, concentration::Vector;
serialdilution_mrm(featuretable::Table, concentration::Vector, project = nothing; kwargs...) = serialdilution_mrm!(deepcopy(featuretable), concentration, project; kwargs...)
serialdilution_mrm!(project::Project, featuretable::Table, concentration::Vector; kwargs...) = serialdilution_mrm!(featuretable, concentration, project; kwargs...)
function serialdilution_mrm!(featuretable::Table, concentration::Vector, project = nothing;
method = isnothing(project) ? nothing : isdefined(project.quantification, :batch) ? project.quantification.batch.method : nothing,
pointlevel = nothing,
name = r"cal.*_(\d*)-r\d*.*",
rt_correction = nothing,
Expand All @@ -213,9 +212,9 @@ function serialdilution_mrm!(featuretable::Table, concentration::Vector, project
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 = 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)
transition_matching!(mrm, method; rt_correction, rt_tol, mz_tol)
config = dictionary(pairs((; method, concentration, rt_correction, r2_threshold, nlevel, rt_tol, mz_tol, signal, est_fn, err_fn, err_tol, other_fn)))
at = analysistable(mrm.table; method, data = signal)
signaltable = getproperty(at, signal)
pointlevel = isnothing(pointlevel) ? map(x -> parse(Int, first(match(name, string(x)))), signaltable.sample) : pointlevel
if signaltable isa ColumnDataTable
Expand Down Expand Up @@ -305,6 +304,7 @@ quantdata_mrm(project::Project, featuretable::Table; kwargs...) = quantdata_mrm!
quantdata_mrm(featuretable::Table, project = nothing; kwargs...) = quantdata_mrm!(deepcopy(featuretable), project; kwargs...)
quantdata_mrm!(project::Project, featuretable::Table; kwargs...) = quantdata_mrm!(featuretable, project; kwargs...)
function quantdata_mrm!(featuretable::Table, project = nothing;
method = isnothing(project) ? nothing : isdefined(project.quantification, :batch) ? project.quantification.batch.method : 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],
Expand All @@ -323,12 +323,12 @@ function quantdata_mrm!(featuretable::Table, project = nothing;
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 = 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)
transition_matching!(mrm, method; rt_correction, rt_tol, mz_tol)
at = analysistable(mrm.table; method, data = signal)
if !isnothing(project)
signal = :estimated_concentration
update_quantification!(project.quantification.batch, at; estimated_concentration = signal)
end
config = dictionary(pairs((; rt_correction, rt_tol, mz_tol, n, signal, est_fn, err_fn, err_tol, other_fn)))
config = dictionary(pairs((; method, rt_correction, rt_tol, mz_tol, n, signal, est_fn, err_fn, err_tol, other_fn)))
QuantData(mrm, at, config)
end
13 changes: 11 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ false
```
"""
macro ri_str(expr)
lc, lv, rv, rc = match(r" *([\(\[]) *([+-]*[\d∞]*\.*\d*) *, *([+-]*[\d∞]*\.*\d*) *([\)\]]) *", expr)
return real_interval(expr)
end
function real_interval(expr)
lc, lv, rv, rc = match(r" *([\(\[]) *([+-]*[\d∞Inf]*\.*\d*) *, *([+-]*[\d∞Inf]*\.*\d*) *([\)\]]) *", expr)
lop = @match lc begin
"[" => <=
"(" => <
Expand All @@ -84,19 +87,25 @@ macro ri_str(expr)
end
lv = @match lv begin
"+∞" => Inf
"+Inf" => Inf
"" => Inf
"Inf" => Inf
"-∞" => -Inf
"-Inf" => -Inf
if occursin(".", lv) end => parse(Float64, lv)
_ => parse(Int, lv)
end
rv = @match rv begin
"+∞" => Inf
"+Inf" => Inf
"" => Inf
"Inf" => Inf
"-∞" => -Inf
"-Inf" => -Inf
if occursin(".", rv) end => parse(Float64, rv)
_ => parse(Int, rv)
end
return real_interval(lv, rv, lop, rop)
real_interval(lv, rv, lop, rop)
end
function real_interval(lb, ub, lop, rop)
lop(lb, ub) || return EmptyInterval()
Expand Down
Binary file added test/data/complete.project/analyte.jld2
Binary file not shown.
Binary file added test/data/complete.project/appendix.jld2
Binary file not shown.
Binary file not shown.
Binary file added test/data/complete.project/data/1.preis/mz2.jld2
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 12b0b3f

Please sign in to comment.