Skip to content

Commit

Permalink
adjust xlsx io to FSharpSpreadsheetML 0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Oct 9, 2021
1 parent 2147cb9 commit f60edbd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 58 deletions.
34 changes: 17 additions & 17 deletions src/ISADotNet.XLSX/AssayFile/AnnotationNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ module AnnotationNode =
Seq.tryPick tryParseUnitHeader headers
|> Option.map (fun h ->
let unitNameGetter matrix i =
Dictionary.tryGetValue (h.HeaderString,i) matrix
Dictionary.tryGetValue (i,h.HeaderString) matrix
let termAccessionGetter =
match Seq.tryPick (tryParseTermAccessionNumberHeader h) headers with
| Some h ->
fun matrix i ->
match Dictionary.tryGetValue (h.HeaderString,i) matrix with
match Dictionary.tryGetValue (i,h.HeaderString) matrix with
| Some "user-specific" -> None
| Some v -> Some v
| _ -> None
Expand All @@ -149,12 +149,12 @@ module AnnotationNode =
match Seq.tryPick (tryParseTermSourceReferenceHeader h) headers with
| Some h ->
fun matrix i ->
match Dictionary.tryGetValue (h.HeaderString,i) matrix with
match Dictionary.tryGetValue (i,h.HeaderString) matrix with
| Some "user-specific" -> None
| Some v -> Some v
| _ -> None
| None -> fun _ _ -> None
fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->
OntologyAnnotation.make
None
(unitNameGetter matrix i |> Option.map AnnotationValue.fromString)
Expand All @@ -169,8 +169,8 @@ module AnnotationNode =
match Seq.tryPick (tryParseTermAccessionNumberHeader valueHeader) headers with
| Some h ->
h.Term,
fun matrix i ->
match Dictionary.tryGetValue (h.HeaderString,i) matrix with
fun (matrix:System.Collections.Generic.Dictionary<int*string,string>) (i:int) ->
match Dictionary.tryGetValue (i,h.HeaderString) matrix with
| Some "user-specific" -> None
| Some v -> Some v
| _ -> None
Expand All @@ -180,7 +180,7 @@ module AnnotationNode =
| Some h ->
h.Term,
fun matrix i ->
match Dictionary.tryGetValue (h.HeaderString,i) matrix with
match Dictionary.tryGetValue (i,h.HeaderString) matrix with
| Some "user-specific" -> None
| Some v -> Some v
| _ -> None
Expand All @@ -199,7 +199,7 @@ module AnnotationNode =
let valueGetter =
fun matrix i ->
let value =
match Dictionary.tryGetValue (valueHeader.HeaderString,i) matrix with
match Dictionary.tryGetValue (i,valueHeader.HeaderString) matrix with
| Some "user-specific" -> None
// Trim() should remove any accidental whitespaces at the beginning or end of a term
| Some v -> Some v
Expand Down Expand Up @@ -235,7 +235,7 @@ module AnnotationNode =
let parameter = category |> Option.map (Some >> ProtocolParameter.make None)

parameter,
fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->
ProcessParameterValue.make
parameter
(valueGetter matrix i)
Expand All @@ -257,7 +257,7 @@ module AnnotationNode =
)

factor,
fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->
FactorValue.make
None
factor
Expand All @@ -276,7 +276,7 @@ module AnnotationNode =
let characteristic = category |> Option.map (Some >> MaterialAttribute.make None)

characteristic,
fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->
MaterialAttributeValue.make
None
characteristic
Expand All @@ -296,11 +296,11 @@ module AnnotationNode =
else None
let numberComment = h.Number |> Option.map (string >> (Comment.fromString "Number") >> List.singleton)

fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->

Data.make
None
(Dictionary.tryGetValue (h.HeaderString,i) matrix)
(Dictionary.tryGetValue (i,h.HeaderString) matrix)
dataType
numberComment
)
Expand All @@ -309,16 +309,16 @@ module AnnotationNode =
let tryGetSampleNameGetter (headers:string seq) =
Seq.tryPick tryParseSampleName headers
|> Option.map (fun h ->
fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
Dictionary.tryGetValue (h.HeaderString,i) matrix
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->
Dictionary.tryGetValue (i,h.HeaderString) matrix
)

/// If the headers of a node depict a source name, returns a function for parsing the values of the matrix to the source names
let tryGetSourceNameGetter (headers:string seq) =
Seq.tryPick tryParseSourceName headers
|> Option.map (fun h ->
fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
Dictionary.tryGetValue (h.HeaderString,i) matrix
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->
Dictionary.tryGetValue (i,h.HeaderString) matrix
)

/// Returns true, if the headers contain a value node: characteristic, parameter or factor
Expand Down
2 changes: 1 addition & 1 deletion src/ISADotNet.XLSX/AssayFile/AnnotationTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ module AnnotationTable =
characteristics,
factors,
protocol,
fun (matrix : System.Collections.Generic.Dictionary<(string * int),string>) i ->
fun (matrix : System.Collections.Generic.Dictionary<(int * string),string>) i ->
Process.make
None
None
Expand Down
26 changes: 16 additions & 10 deletions src/ISADotNet.XLSX/AssayFile/Assay.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ module Process =
/// matrixHeaders are the column headers of the table
///
/// sparseMatrix is a sparse representation of the sheet table, with the first part of the key being the column header and the second part being a zero based row index
let fromSparseMatrix (processNameRoot:string) matrixHeaders (sparseMatrix : Dictionary<string*int,string>) =
let fromSparseMatrix (processNameRoot:string) matrixHeaders (sparseMatrix : Dictionary<int*string,string>) =
let len =
let mutable i = 0
for kv in sparseMatrix do
let j = kv.Key |> snd
let j = kv.Key |> fst
if j > i then i <- j
i + 1
let characteristic,factors,protocol,processGetter =
Expand Down Expand Up @@ -46,14 +46,14 @@ module Assay =
/// matrixHeaders are the column headers of the table
///
/// sparseMatrix is a sparse representation of the sheet table, with the first part of the key being the column header and the second part being a zero based row index
let fromSparseMatrix (processNameRoot:string) matrixHeaders (sparseMatrix : Dictionary<string*int,string>) =
let fromSparseMatrix (processNameRoot:string) matrixHeaders (sparseMatrix : Dictionary<int*string,string>) =
let characteristics,factors,protocols,processes = Process.fromSparseMatrix processNameRoot matrixHeaders sparseMatrix
factors,protocols,Assay.create(CharacteristicCategories = characteristics,ProcessSequence = Seq.toList processes)

/// Returns an assay from a sequence of sparseMatrix representations of assay.xlsx sheets
///
/// See "fromSparseMatrix" function for parameter documentation
let fromSparseMatrices (sheets : (string*(string seq)*Dictionary<string*int,string>) seq) =
let fromSparseMatrices (sheets : (string*(string seq)*Dictionary<int*string,string>) seq) =
let characteristics,factors,protocols,processes =
sheets
|> Seq.map (fun (name,matrixHeaders,matrix) -> Process.fromSparseMatrix name matrixHeaders matrix)
Expand All @@ -72,16 +72,20 @@ module Assay =
/// Create a new ISADotNet.XLSX assay file constisting of two sheets. The first has the name of the assayIdentifier and is meant to store parameters used in the assay. The second stores additional assay metadata
let init metadataSheetName assayIdentifier path =
Spreadsheet.initWithSST assayIdentifier path
Spreadsheet.initWithSst assayIdentifier path
|> MetaData.init metadataSheetName
|> Spreadsheet.close

/// Parses the assay file
/// Reads an assay from an xlsx spreadsheetdocument
///
/// As factors and protocols are used for the investigation file, they are returned individually
///
/// The persons from the metadata sheet are returned independently as they are not a part of the assay object
let fromSpreadsheet (doc:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument) =

let sst = Spreadsheet.tryGetSharedStringTable doc

// Get the metadata from the metadata sheet
// Reading the "Investigation" metadata sheet. Here metadata
let assayMetaData,contacts =
Spreadsheet.tryGetSheetBySheetName "Investigation" doc
|> Option.map (fun sheet ->
Expand All @@ -93,7 +97,8 @@ module Assay =
|> fun (a,p) -> Option.defaultValue Assay.empty a, p
)
|> Option.defaultValue (Assay.empty,[])


// All sheetnames in the spreadsheetDocument
let sheetNames =
Spreadsheet.getWorkbookPart doc
|> Workbook.get
Expand All @@ -108,19 +113,20 @@ module Assay =
| Some wsp ->
match Table.tryGetByNameBy (fun s -> s.StartsWith "annotationTable") wsp with
| Some table ->
// Extract the sheetdata as a sparse matrix
let sheet = Worksheet.getSheetData wsp.Worksheet
let headers = Table.getColumnHeaders table
let m = Table.toSparseValueMatrix sst sheet table
Seq.singleton (sheetName,headers,m)
| None -> Seq.empty
| None -> Seq.empty
)
|> fromSparseMatrices
|> fromSparseMatrices // Feed the sheets (represented as sparse matrices) into the assay parser function

factors,
protocols |> Seq.toList,
contacts,
API.Update.UpdateByExisting.updateRecordType assayMetaData assay
API.Update.UpdateByExisting.updateRecordType assayMetaData assay // Merges the assay containing the assay meta data and the assay containing the processes retrieved from the sheets

/// Parses the assay file
let fromFile (path:string) =
Expand Down
2 changes: 1 addition & 1 deletion src/ISADotNet.XLSX/ISADotNet.XLSX.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharpSpreadsheetML" Version="0.0.6" />
<PackageReference Include="FSharpSpreadsheetML" Version="0.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/ISADotNet.XLSX/InvestigationFile/Investigation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ module Investigation =
|> ignore

let toFile (path : string) (investigation:Investigation) =
let doc = Spreadsheet.initWithSST "isa_investigation" path
let doc = Spreadsheet.initWithSst "isa_investigation" path
try
toSpreadsheet doc investigation
finally
Expand Down
Loading

0 comments on commit f60edbd

Please sign in to comment.