Skip to content

Commit

Permalink
Merge pull request #3 from PharmCat/dev
Browse files Browse the repository at this point in the history
update 0.2.5
  • Loading branch information
PharmCat committed Jan 13, 2023
2 parents d4c71b2 + 1ed7f8d commit 290376f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ODMXMLTools"
uuid = "2456a17b-6ca2-4f51-9342-f0287e829718"
authors = ["PharmCat <[email protected]>"]
version = "0.2.4"
version = "0.2.5"

[deps]

Expand Down
85 changes: 73 additions & 12 deletions src/odmxml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ function appendelements!(inds::AbstractVector, n::AbstractODMNode, nname::Symbol
end
inds
end

function appendelements!(inds::AbstractVector, n::AbstractODMNode, nname::Union{Set{Symbol}, AbstractVector{Symbol}})
for i in n.el
if name(i) in nname
push!(inds, i)
end
end
inds
end


function content(n::ODMTextNode)
getfield(n, :content)
end
Expand All @@ -191,6 +202,7 @@ function content(n::AbstractODMNode)
nothing
end


t_collect(a::Tuple) = [i for i in a];
t_collect(a::Vector) = a;
t_collect(a) = collect(a);
Expand Down Expand Up @@ -728,27 +740,58 @@ end


"""
clinicaldatatable(cd::AbstractODMNode; itemgroup = nothing)
clinicaldatatable(cd::AbstractODMNode;
itemgroup = nothing,
form = nothing,
item::Union{Nothing, AbstractString, <: AbstractVector{<:AbstractString}} = nothing,
categ = false,
addstudyid = false,
addstudyidcol = false,
idlnames = nothing)
Return clinical data table in long formal. `cd` should be ClinicalData.
* `itemgroup` - only this ItemGroupOID;
* `form` - only this FormOID;
* `item` - only this ItemOID;
* `categ` - make collumns categorical;
* `addstudyid` - add StudyOID as prefix to SubjectKey: "StudyOID_SubjectKey";
* `addstudyidcol` - add StudyOID as collumn to dataframe;
* `idlnames` - only this types of data will be collected, for example: ItemData, ItemDataInteger, ets (if `nothing`` - all will be collected).
"""
function clinicaldatatable(cd::AbstractODMNode;
itemgroup = nothing,
form = nothing,
item::Union{Nothing, AbstractString, <: AbstractVector{<:AbstractString}} = nothing,
categ = false, )
categ = false,
addstudyid = false,
addstudyidcol = false,
idlnames = nothing)


if name(cd) != :ClinicalData error("This is not ClinicalData") end
# For TypedData
datatype = String
#df = DataFrame(SubjectKey = String[], StudyEventOID = CategoricalArray(String[]), FormOID = CategoricalArray(String[]), ItemGroupOID = CategoricalArray(String[]), ItemGroupRepeatKey = CategoricalArray(String[]), ItemOID = CategoricalArray(String[]), Value = String[])
df = DataFrame(SubjectKey = String[], StudyEventOID = String[], FormOID = String[], ItemGroupOID = String[], ItemGroupRepeatKey = String[], ItemOID = String[], Value = String[])
df = DataFrame(SubjectKey = String[], StudyEventOID = String[], FormOID = String[], ItemGroupOID = String[], ItemGroupRepeatKey = String[], ItemOID = String[], Value = datatype[])
sdl = findelements(cd, :SubjectData)
edl = AbstractODMNode[]
fdl = AbstractODMNode[]
gdl = AbstractODMNode[]
idl = AbstractODMNode[]

if isnothing(idlnames)
idlnames = pushfirst!(collect(ITEMDATATYPE), :ItemData)
end

if isa(item, AbstractString) item = [item] end
#sdl = Iterators.filter(isSubjectData, cd.el)
for s in sdl
if addstudyid
subjid = attribute(cd, :StudyOID)*"_"*attribute(s, :SubjectKey)
else
subjid = attribute(s, :SubjectKey)
end
resize!(edl, 0)
appendelements!(edl, s, :StudyEventData)
#edl = Iterators.filter(isStudyEventData, s.el)
Expand All @@ -768,19 +811,34 @@ function clinicaldatatable(cd::AbstractODMNode;
if attribute(f, :FormOID) != form continue end
end
resize!(idl, 0)
appendelements!(idl, g, :ItemData)
#idl = Iterators.filter(isItemData, g.el)
appendelements!(idl, g, idlnames)
#idl = Iterators.filter(isItemData, g.el)
for i in idl
if !isnothing(item)
if !(attribute(i, :ItemOID) in item) continue end
end
push!(df, (attribute(s, :SubjectKey),
attribute(e, :StudyEventOID),
attribute(f, :FormOID),
attribute(g, :ItemGroupOID),
attribute(g, :ItemGroupRepeatKey),
attribute(i, :ItemOID),
attribute(i, :Value)))
if isItemData(i)
push!(df, (subjid,
attribute(e, :StudyEventOID),
attribute(f, :FormOID),
attribute(g, :ItemGroupOID),
attribute(g, :ItemGroupRepeatKey),
attribute(i, :ItemOID),
attribute(i, :Value)))
else
val = content(i)
if isnothing(val)
@warn "ItemData[TYPE] content is empty"
val = ""
end
push!(df, (subjid,
attribute(e, :StudyEventOID),
attribute(f, :FormOID),
attribute(g, :ItemGroupOID),
attribute(g, :ItemGroupRepeatKey),
attribute(i, :ItemOID),
val))
end
end
end
end
Expand All @@ -793,6 +851,9 @@ function clinicaldatatable(cd::AbstractODMNode;
transform!(df, :ItemGroupRepeatKey => categorical, renamecols=false)
transform!(df, :ItemOID => categorical, renamecols=false)
end
if addstudyidcol
insertcols!(df, 1, :StudyOID=>fill(attribute(cd, :StudyOID), size(df, 1)); copycols=false)
end
df
end
"""
Expand Down
12 changes: 6 additions & 6 deletions src/spss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ end


"""
spss_form_variable_labels(mdb, form; variable, labels)
spss_form_variable_labels(mdb, form; variable = :OID, labels = :Comment)
SPSS command to set variable labels.
`variable` - varable names attribute, `SASFieldName` by default.
`variable` - varable names attribute, `OID` by default.
`labels` - labels names attribute, `Comment` by default.
"""
function spss_form_variable_labels(mdb, form; variable = :SASFieldName, labels = :Comment)
function spss_form_variable_labels(mdb, form; variable = :OID, labels = :Comment)
df = itemformcontent_(mdb, form; optional = true)
v = Vector{Pair}(undef, length(df))
for i = 1:size(df, 1)
Expand All @@ -70,13 +70,13 @@ function spss_form_variable_labels(mdb, form, pairs; kwargs...)
end

"""
spss_form_value_labels(mdb, form; variable = :SASFieldName)
spss_form_value_labels(mdb, form; variable = :OID)
SPSS command to set value labels.
`variable` - varable names attribute, `SASFieldName` by default.
`variable` - varable names attribute, `OID` by default.
"""
function spss_form_value_labels(mdb, form; variable = :SASFieldName)
function spss_form_value_labels(mdb, form; variable = :OID)
items = itemformcontent_(mdb, form)
v = Vector{Pair}(undef, 0)
for i = 1:length(items)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ using Test

@test_nowarn ODMXMLTools.validateodm(odm)

@test_nowarn ODMXMLTools.clinicaldatatable(odm, addstudyid= true, addstudyidcol = true)

cdat = ODMXMLTools.findclinicaldata(odm, "ST1", "v2")

@test_nowarn ODMXMLTools.clinicaldatatable(cdat)
Expand Down
9 changes: 8 additions & 1 deletion test/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@
<Include StudyOID="DEFS" MetaDataVersionOID="v1"/>
<ItemGroupDef OID="IG_1" Name="Item group 1" Repeating="No" SASDatasetName="UNGRP">
<ItemRef ItemOID="I_1" OrderNumber="1" Mandatory="Yes" />
<ItemRef ItemOID="I_2" OrderNumber="1" Mandatory="Yes" />
<ItemRef ItemOID="I_2" OrderNumber="2" Mandatory="Yes" />
<ItemRef ItemOID="I_3" OrderNumber="3" Mandatory="Yes" />
</ItemGroupDef>
<ItemDef OID="I_2" Name="Item 2" DataType="integer" Length="3" SASFieldName="IT2" Comment="Comments 2">
<CodeListRef CodeListOID="CL_1"/>
</ItemDef>
<ItemDef OID="I_3" Name="Item 3" DataType="integer" Length="3" SASFieldName="IT3" Comment="Comments 3">
</ItemDef>
<CodeList OID="CL_1" Name="NP" DataType="integer" SASFormatName="NP">
<CodeListItem CodedValue="0">
<Decode>
Expand All @@ -90,6 +93,8 @@
<FormData FormOID="FORM_1">
<ItemGroupData ItemGroupOID="IG_1" TransactionType="Insert">
<ItemData ItemOID="I_1" Value="FieldValue"/>
<ItemData ItemOID="I_2" Value="2"/>
<ItemDataInteger ItemOID="I_3">4</ItemDataInteger>
</ItemGroupData>
</FormData>
</StudyEventData>
Expand All @@ -102,6 +107,7 @@
<ItemGroupData ItemGroupOID="IG_1" TransactionType="Insert">
<ItemData ItemOID="I_1" Value="FieldValue"/>
<ItemData ItemOID="I_2" Value="1"/>
<ItemDataInteger ItemOID="I_3">1</ItemDataInteger>
</ItemGroupData>
</FormData>
</StudyEventData>
Expand All @@ -112,6 +118,7 @@
<ItemGroupData ItemGroupOID="IG_1" TransactionType="Insert">
<ItemData ItemOID="I_1" Value="FieldValue"/>
<ItemData ItemOID="I_2" Value="3"/>
<ItemDataInteger ItemOID="I_3">2</ItemDataInteger>
</ItemGroupData>
</FormData>
</StudyEventData>
Expand Down

2 comments on commit 290376f

@PharmCat
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/75649

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.5 -m "<description of version>" 290376ffe6e88ad871d427df322c295953e4a306
git push origin v0.2.5

Please sign in to comment.