Skip to content

Commit

Permalink
adjust investigation.xlsx file io for FABLE compatabiity
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Oct 9, 2021
1 parent 9c52b6c commit ea12093
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 273 deletions.
2 changes: 1 addition & 1 deletion src/ISADotNet.XLSX/InvestigationFile/Assays.fs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module Assays =
)
{matrix with CommentKeys = commentKeys |> List.distinct |> List.rev}

let fromRows (prefix : string option) lineNumber (rows) =
let fromRows (prefix : string option) lineNumber (rows : IEnumerator<SparseRow>) =
match prefix with
| Some p -> SparseTable.FromRows(rows,labels,lineNumber,p)
| None -> SparseTable.FromRows(rows,labels,lineNumber)
Expand Down
31 changes: 6 additions & 25 deletions src/ISADotNet.XLSX/InvestigationFile/Contacts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,13 @@ module Contacts =
{matrix with CommentKeys = commentKeys |> List.distinct |> List.rev}


let readPersons (prefix : string option) lineNumber (en:IEnumerator<Row>) =
let prefix = match prefix with | Some p -> p + " " | None -> ""
let rec loop (matrix : SparseTable) remarks lineNumber =
let fromRows (prefix : string option) lineNumber (rows : IEnumerator<SparseRow>) =
match prefix with
| Some p -> SparseTable.FromRows(rows,labels,lineNumber,p)
| None -> SparseTable.FromRows(rows,labels,lineNumber)
|> fun (s,ln,rs,sm) -> (s,ln,rs, fromSparseTable sm)

if en.MoveNext() then
let row = en.Current |> Row.getIndexedValues None |> Seq.map (fun (i,v) -> int i - 1,v)
match Seq.tryItem 0 row |> Option.map snd, Seq.trySkip 1 row with

| Comment k, Some v ->
loop (SparseTable.AddComment k v matrix) remarks (lineNumber + 1)

| Remark k, _ ->
loop matrix (Remark.make lineNumber k :: remarks) (lineNumber + 1)

| Some k, Some v when List.exists (fun label -> k = prefix + label) labels ->
let label = List.find (fun label -> k = prefix + label) labels
loop (SparseTable.AddRow label v matrix) remarks (lineNumber + 1)

| Some k, _ -> Some k,lineNumber,remarks,fromSparseTable matrix
| _ -> None, lineNumber,remarks,fromSparseTable matrix
else
None,lineNumber,remarks,fromSparseTable matrix
loop (SparseTable.Create()) [] lineNumber


let writePersons (prefix : string option) (persons : Person list) =
let toRows (prefix : string option) (persons : Person list) =
persons
|> toSparseTable
|> fun m ->
Expand Down
32 changes: 6 additions & 26 deletions src/ISADotNet.XLSX/InvestigationFile/DesignDescriptors.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,13 @@ module DesignDescriptors =
{matrix with CommentKeys = commentKeys |> List.distinct |> List.rev}


let readDesigns (prefix : string option) lineNumber (en:IEnumerator<Row>) =
let prefix = match prefix with | Some p -> p + " " | None -> ""
let rec loop (matrix : SparseTable) remarks lineNumber =

if en.MoveNext() then
let row = en.Current |> Row.getIndexedValues None |> Seq.map (fun (i,v) -> int i - 1,v)
match Seq.tryItem 0 row |> Option.map snd, Seq.trySkip 1 row with

| Comment k, Some v ->
loop (SparseTable.AddComment k v matrix) remarks (lineNumber + 1)

| Remark k, _ ->
loop matrix (Remark.make lineNumber k :: remarks) (lineNumber + 1)

| Some k, Some v when List.exists (fun label -> k = prefix + label) labels ->
let label = List.find (fun label -> k = prefix + label) labels
loop (SparseTable.AddRow label v matrix) remarks (lineNumber + 1)

| Some k, _ -> Some k,lineNumber,remarks,fromSparseTable matrix
| _ -> None, lineNumber,remarks,fromSparseTable matrix
else
None,lineNumber,remarks,fromSparseTable matrix
loop (SparseTable.Create()) [] lineNumber


let fromRows (prefix : string option) lineNumber (rows : IEnumerator<SparseRow>) =
match prefix with
| Some p -> SparseTable.FromRows(rows,labels,lineNumber,p)
| None -> SparseTable.FromRows(rows,labels,lineNumber)
|> fun (s,ln,rs,sm) -> (s,ln,rs, fromSparseTable sm)

let writeDesigns (prefix : string option) (designs : OntologyAnnotation list) =
let toRows (prefix : string option) (designs : OntologyAnnotation list) =
designs
|> toSparseTable
|> fun m ->
Expand Down
30 changes: 6 additions & 24 deletions src/ISADotNet.XLSX/InvestigationFile/Factors.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,14 @@ module Factors =
{matrix with CommentKeys = commentKeys |> List.distinct |> List.rev}


let readFactors (prefix : string option) lineNumber (en:IEnumerator<Row>) =
let prefix = match prefix with | Some p -> p + " " | None -> ""
let rec loop (matrix : SparseTable) remarks lineNumber =

if en.MoveNext() then
let row = en.Current |> Row.getIndexedValues None |> Seq.map (fun (i,v) -> int i - 1,v)
match Seq.tryItem 0 row |> Option.map snd, Seq.trySkip 1 row with

| Comment k, Some v ->
loop (SparseTable.AddComment k v matrix) remarks (lineNumber + 1)

| Remark k, _ ->
loop matrix (Remark.make lineNumber k :: remarks) (lineNumber + 1)

| Some k, Some v when List.exists (fun label -> k = prefix + label) labels ->
let label = List.find (fun label -> k = prefix + label) labels
loop (SparseTable.AddRow label v matrix) remarks (lineNumber + 1)

| Some k, _ -> Some k,lineNumber,remarks,fromSparseTable matrix
| _ -> None, lineNumber,remarks,fromSparseTable matrix
else
None,lineNumber,remarks,fromSparseTable matrix
loop (SparseTable.Create()) [] lineNumber
let fromRows (prefix : string option) lineNumber (rows : IEnumerator<SparseRow>) =
match prefix with
| Some p -> SparseTable.FromRows(rows,labels,lineNumber,p)
| None -> SparseTable.FromRows(rows,labels,lineNumber)
|> fun (s,ln,rs,sm) -> (s,ln,rs, fromSparseTable sm)


let writeFactors prefix (factors : Factor list) =
let toRows prefix (factors : Factor list) =
factors
|> toSparseTable
|> fun m ->
Expand Down
124 changes: 58 additions & 66 deletions src/ISADotNet.XLSX/InvestigationFile/Investigation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,11 @@ module Investigation =
{matrix with CommentKeys = commentKeys |> List.distinct |> List.rev}


static member ReadInvestigationInfo lineNumber (en:IEnumerator<Row>) =
let rec loop (matrix : SparseTable) remarks lineNumber =

if en.MoveNext() then
let row = en.Current |> Row.getIndexedValues None |> Seq.map (fun (i,v) -> int i - 1,v)
match Seq.tryItem 0 row |> Option.map snd, Seq.trySkip 1 row with

| Comment k, Some v ->
loop (SparseTable.AddComment k v matrix) remarks (lineNumber + 1)

| Remark k, _ ->
loop matrix (Remark.make lineNumber k :: remarks) (lineNumber + 1)

| Some k, Some v when List.contains k InvestigationInfo.Labels ->
loop (SparseTable.AddRow k v matrix) remarks (lineNumber + 1)

| Some k, _ -> Some k,lineNumber,remarks,InvestigationInfo.FromSparseTable matrix
| _ -> None, lineNumber,remarks,InvestigationInfo.FromSparseTable matrix
else
None,lineNumber,remarks,InvestigationInfo.FromSparseTable matrix
loop (SparseTable.Create()) [] lineNumber

static member fromRows lineNumber (rows : IEnumerator<SparseRow>) =
SparseTable.FromRows(rows,InvestigationInfo.Labels,lineNumber)
|> fun (s,ln,rs,sm) -> (s,ln,rs, InvestigationInfo.FromSparseTable sm)

static member WriteInvestigationInfo (investigation : Investigation) =
static member toRows (investigation : Investigation) =
investigation
|> InvestigationInfo.ToSparseTable
|> SparseTable.ToRows
Expand All @@ -134,7 +115,7 @@ module Investigation =
remarks


let fromRows (rows:seq<Row>) =
let fromRows (rows:seq<SparseRow>) =
let en = rows.GetEnumerator()

let emptyInvestigationInfo = InvestigationInfo.create "" "" "" "" "" []
Expand All @@ -143,63 +124,43 @@ module Investigation =
match lastLine with

| Some k when k = ontologySourceReferenceLabel ->
let currentLine,lineNumber,newRemarks,ontologySourceReferences = OntologySourceReference.readTermSources (lineNumber + 1) en
let currentLine,lineNumber,newRemarks,ontologySourceReferences = OntologySourceReference.fromRows (lineNumber + 1) en
loop currentLine ontologySourceReferences investigationInfo publications contacts studies (List.append remarks newRemarks) lineNumber

| Some k when k = investigationLabel ->
let currentLine,lineNumber,newRemarks,investigationInfo = InvestigationInfo.ReadInvestigationInfo (lineNumber + 1) en
let currentLine,lineNumber,newRemarks,investigationInfo = InvestigationInfo.fromRows (lineNumber + 1) en
loop currentLine ontologySourceReferences investigationInfo publications contacts studies (List.append remarks newRemarks) lineNumber

| Some k when k = publicationsLabel ->
let currentLine,lineNumber,newRemarks,publications = Publications.readPublications (Some publicationsLabelPrefix) (lineNumber + 1) en
let currentLine,lineNumber,newRemarks,publications = Publications.fromRows (Some publicationsLabelPrefix) (lineNumber + 1) en
loop currentLine ontologySourceReferences investigationInfo publications contacts studies (List.append remarks newRemarks) lineNumber

| Some k when k = contactsLabel ->
let currentLine,lineNumber,newRemarks,contacts = Contacts.readPersons (Some contactsLabelPrefix) (lineNumber + 1) en
let currentLine,lineNumber,newRemarks,contacts = Contacts.fromRows (Some contactsLabelPrefix) (lineNumber + 1) en
loop currentLine ontologySourceReferences investigationInfo publications contacts studies (List.append remarks newRemarks) lineNumber

| Some k when k = studyLabel ->
let currentLine,lineNumber,newRemarks,study = Study.readStudy (lineNumber + 1) en
let currentLine,lineNumber,newRemarks,study = Study.fromRows (lineNumber + 1) en
loop currentLine ontologySourceReferences investigationInfo publications contacts (study::studies) (List.append remarks newRemarks) lineNumber

| k ->
fromParts investigationInfo ontologySourceReferences publications contacts (List.rev studies) remarks

if en.MoveNext () then
let currentLine = en.Current |> Row.tryGetValueAt None 1u
let currentLine = en.Current |> SparseRow.tryGetValueAt 0
loop currentLine [] emptyInvestigationInfo [] [] [] [] 1

else
failwith "emptyInvestigationFile"


let fromSpreadsheet (doc:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument) =
doc
|> Spreadsheet.getRowsBySheetIndex 0u
|> fromRows


let fromFile (path : string) =
let doc = Spreadsheet.fromFile path false
try
fromSpreadsheet doc
finally
doc.Close()

let fromStream (stream : System.IO.Stream) =
let doc = Spreadsheet.fromStream stream false
try
fromSpreadsheet doc
finally
doc.Close()


let toRows (investigation:Investigation) : seq<Row> =
let insertRemarks (remarks:Remark list) (rows:seq<Row>) =
let toRows (investigation:Investigation) : seq<SparseRow> =
let insertRemarks (remarks:Remark list) (rows:seq<SparseRow>) =
let rm = remarks |> List.map Remark.toTuple |> Map.ofList
let rec loop i l nl =
match Map.tryFind i rm with
| Some remark ->
Row.ofValues None 1u [wrapRemark remark] :: nl
SparseRow.fromValues [wrapRemark remark] :: nl
|> loop (i+1) l
| None ->
match l with
Expand All @@ -209,24 +170,51 @@ module Investigation =
loop 1 (rows |> List.ofSeq) []
|> List.rev
seq {
yield Row.ofValues None 0u [ontologySourceReferenceLabel]
yield! OntologySourceReference.writeTermSources (Option.defaultValue [] investigation.OntologySourceReferences)
yield SparseRow.fromValues[ontologySourceReferenceLabel]
yield! OntologySourceReference.toRows (Option.defaultValue [] investigation.OntologySourceReferences)

yield Row.ofValues None 0u [investigationLabel]
yield! InvestigationInfo.WriteInvestigationInfo investigation
yield SparseRow.fromValues[investigationLabel]
yield! InvestigationInfo.toRows investigation

yield Row.ofValues None 0u [publicationsLabel]
yield! Publications.writePublications (Some publicationsLabelPrefix) (Option.defaultValue [] investigation.Publications)
yield SparseRow.fromValues[publicationsLabel]
yield! Publications.toRows (Some publicationsLabelPrefix) (Option.defaultValue [] investigation.Publications)

yield Row.ofValues None 0u [contactsLabel]
yield! Contacts.writePersons (Some contactsLabelPrefix) (Option.defaultValue [] investigation.Contacts)
yield SparseRow.fromValues[contactsLabel]
yield! Contacts.toRows (Some contactsLabelPrefix) (Option.defaultValue [] investigation.Contacts)

for study in (Option.defaultValue [] investigation.Studies) do
yield Row.ofValues None 0u [studyLabel]
yield! Study.writeStudy study
yield SparseRow.fromValues[studyLabel]
yield! Study.toRows study
}
|> insertRemarks investigation.Remarks
|> Seq.mapi (fun i row -> Row.updateRowIndex (i+1 |> uint) row)
|> insertRemarks investigation.Remarks
|> seq

/// Diesen Block durch JS ersetzen ---->
let rowOfSparseRow (vs : SparseRow) =
vs
|> Seq.fold (fun r (i,v) -> Row.insertValueAt None (uint32 (i+1)) v r) (Row.empty())

let fromSpreadsheet (doc:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument) =
doc
|> Spreadsheet.getRowsBySheetIndex 0u
|> Seq.map (Row.getIndexedValues None >> Seq.map (fun (i,v) -> (int i) - 1, v))
|> fromRows


let fromFile (path : string) =
let doc = Spreadsheet.fromFile path false
try
fromSpreadsheet doc
finally
doc.Close()

let fromStream (stream : System.IO.Stream) =
let doc = Spreadsheet.fromStream stream false
try
fromSpreadsheet doc
finally
doc.Close()


let toSpreadsheet (doc:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument) (investigation:Investigation) =
Expand All @@ -235,6 +223,9 @@ module Investigation =

investigation
|> toRows
|> Seq.mapi (fun i row ->
rowOfSparseRow row
|> Row.updateRowIndex (i+1 |> uint))
|> Seq.fold (fun s r ->
SheetData.appendRow r s
) sheet
Expand All @@ -254,3 +245,4 @@ module Investigation =
finally
doc.Close()

/// ----> Bis hier
27 changes: 4 additions & 23 deletions src/ISADotNet.XLSX/InvestigationFile/OntologySourceReference.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,11 @@ module OntologySourceReference =
)
{matrix with CommentKeys = commentKeys |> List.distinct |> List.rev}

let readTermSources lineNumber (en:IEnumerator<Row>) =
let rec loop (matrix : SparseTable) remarks lineNumber =

if en.MoveNext() then
let row = en.Current |> Row.getIndexedValues None |> Seq.map (fun (i,v) -> int i - 1,v)
match Seq.tryItem 0 row |> Option.map snd, Seq.trySkip 1 row with

| Comment k, Some v ->
loop (SparseTable.AddComment k v matrix) remarks (lineNumber + 1)

| Remark k, _ ->
loop matrix (Remark.make lineNumber k :: remarks) (lineNumber + 1)

| Some k, Some v when List.contains k labels ->
loop (SparseTable.AddRow k v matrix) remarks (lineNumber + 1)

| Some k, _ -> Some k,lineNumber,remarks,fromSparseTable matrix
| _ -> None, lineNumber,remarks,fromSparseTable matrix
else
None,lineNumber,remarks,fromSparseTable matrix
loop (SparseTable.Create()) [] lineNumber

let fromRows lineNumber (rows : IEnumerator<SparseRow>) =
SparseTable.FromRows(rows,labels,lineNumber)
|> fun (s,ln,rs,sm) -> (s,ln,rs, fromSparseTable sm)

let writeTermSources (termSources : OntologySourceReference list) =
let toRows (termSources : OntologySourceReference list) =
termSources
|> toSparseTable
|> fun m -> SparseTable.ToRows(m)
Loading

0 comments on commit ea12093

Please sign in to comment.