Skip to content

Commit e296ea1

Browse files
committed
Return sequences from set-backed readers
1 parent f79c60d commit e296ea1

14 files changed

Lines changed: 607 additions & 60 deletions

File tree

src/BioFSharp.IO.INSDC/Analysis.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ open BioFSharp.IO.INSDC.Internal
66
/// Read and write INSDC Analysis records.
77
module Analysis =
88

9-
/// Read an INSDC Analysis XML record from the file at `filePath`.
10-
let read (filePath: string) : Analysis =
11-
XmlSerializer.read<Analysis> filePath
9+
/// Read INSDC Analysis XML records from the file at `filePath`.
10+
let read (filePath: string) : seq<Analysis> =
11+
XmlSerializer.readOrSet<Analysis, AnalysisSet> "ANALYSIS_SET" (fun set -> set.Analysis) filePath
1212

13-
/// Parse an INSDC Analysis XML record from an in-memory string.
14-
let readString (xml: string) : Analysis =
15-
XmlSerializer.readString<Analysis> xml
13+
/// Parse INSDC Analysis XML records from an in-memory string.
14+
let readString (xml: string) : seq<Analysis> =
15+
XmlSerializer.readStringOrSet<Analysis, AnalysisSet> "ANALYSIS_SET" (fun set -> set.Analysis) xml
1616

1717
/// Write an INSDC Analysis `analysis` to the file at `filePath` as XML.
1818
let write (filePath: string) (analysis: Analysis) : unit =

src/BioFSharp.IO.INSDC/BioProject.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ open BioFSharp.IO.INSDC.Internal
88
/// this library).
99
module BioProject =
1010

11-
/// Read an INSDC BioProject XML record from the file at `filePath`.
12-
let read (filePath: string) : BioProject =
13-
XmlSerializer.read<BioProject> filePath
11+
/// Read INSDC BioProject XML records from the file at `filePath`.
12+
let read (filePath: string) : seq<BioProject> =
13+
XmlSerializer.readOrSet<BioProject, BioProjectSet> "PROJECT_SET" (fun set -> set.Project) filePath
1414

15-
/// Parse an INSDC BioProject XML record from an in-memory string.
16-
let readString (xml: string) : BioProject =
17-
XmlSerializer.readString<BioProject> xml
15+
/// Parse INSDC BioProject XML records from an in-memory string.
16+
let readString (xml: string) : seq<BioProject> =
17+
XmlSerializer.readStringOrSet<BioProject, BioProjectSet> "PROJECT_SET" (fun set -> set.Project) xml
1818

1919
/// Write an INSDC BioProject `project` to the file at `filePath` as XML.
2020
let write (filePath: string) (project: BioProject) : unit =

src/BioFSharp.IO.INSDC/BioSample.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ open BioFSharp.IO.INSDC.Internal
88
/// this library).
99
module BioSample =
1010

11-
/// Read an INSDC BioSample XML record from the file at `filePath`.
12-
let read (filePath: string) : BioSample =
13-
XmlSerializer.read<BioSample> filePath
11+
/// Read INSDC BioSample XML records from the file at `filePath`.
12+
let read (filePath: string) : seq<BioSample> =
13+
XmlSerializer.readOrSet<BioSample, BioSampleSet> "SAMPLE_SET" (fun set -> set.Sample) filePath
1414

15-
/// Parse an INSDC BioSample XML record from an in-memory string.
16-
let readString (xml: string) : BioSample =
17-
XmlSerializer.readString<BioSample> xml
15+
/// Parse INSDC BioSample XML records from an in-memory string.
16+
let readString (xml: string) : seq<BioSample> =
17+
XmlSerializer.readStringOrSet<BioSample, BioSampleSet> "SAMPLE_SET" (fun set -> set.Sample) xml
1818

1919
/// Write an INSDC BioSample `sample` to the file at `filePath` as XML.
2020
let write (filePath: string) (sample: BioSample) : unit =

src/BioFSharp.IO.INSDC/Experiment.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ open BioFSharp.IO.INSDC.Internal
66
/// Read and write INSDC Experiment records.
77
module Experiment =
88

9-
/// Read an INSDC Experiment XML record from the file at `filePath`.
10-
let read (filePath: string) : Experiment =
11-
XmlSerializer.read<Experiment> filePath
9+
/// Read INSDC Experiment XML records from the file at `filePath`.
10+
let read (filePath: string) : seq<Experiment> =
11+
XmlSerializer.readOrSet<Experiment, ExperimentSet> "EXPERIMENT_SET" (fun set -> set.Experiment) filePath
1212

13-
/// Parse an INSDC Experiment XML record from an in-memory string.
14-
let readString (xml: string) : Experiment =
15-
XmlSerializer.readString<Experiment> xml
13+
/// Parse INSDC Experiment XML records from an in-memory string.
14+
let readString (xml: string) : seq<Experiment> =
15+
XmlSerializer.readStringOrSet<Experiment, ExperimentSet> "EXPERIMENT_SET" (fun set -> set.Experiment) xml
1616

1717
/// Write an INSDC Experiment `experiment` to the file at `filePath` as XML.
1818
let write (filePath: string) (experiment: Experiment) : unit =

src/BioFSharp.IO.INSDC/Internal/XmlSerializer.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace BioFSharp.IO.INSDC.Internal
33
open System
44
open System.Collections.Concurrent
55
open System.IO
6+
open System.Xml.Linq
67
open System.Xml.Serialization
78

89
/// Internal helpers wrapping `System.Xml.Serialization.XmlSerializer` for the
@@ -27,6 +28,24 @@ module XmlSerializer =
2728
use reader = new StringReader(xml)
2829
(getSerializer typeof<'T>).Deserialize(reader) :?> 'T
2930

31+
/// Deserialize INSDC records from XML that may either use the single
32+
/// record root (`RUN`, `STUDY`, ...) or the ENA API's set root
33+
/// (`RUN_SET`, `STUDY_SET`, ...).
34+
let readStringOrSet<'T, 'TSet> (setRootName: string) (select: 'TSet -> seq<'T>) (xml: string) : seq<'T> =
35+
let rootName = XDocument.Parse(xml).Root.Name.LocalName
36+
37+
if rootName = setRootName then
38+
xml |> readString<'TSet> |> select
39+
else
40+
xml |> readString<'T> |> Seq.singleton
41+
42+
/// Deserialize INSDC records from a file that may either use the single
43+
/// record root (`RUN`, `STUDY`, ...) or the ENA API's set root
44+
/// (`RUN_SET`, `STUDY_SET`, ...).
45+
let readOrSet<'T, 'TSet> (setRootName: string) (select: 'TSet -> seq<'T>) (filePath: string) : seq<'T> =
46+
File.ReadAllText(filePath)
47+
|> readStringOrSet<'T, 'TSet> setRootName select
48+
3049
/// Serialize an INSDC record `value` to the file at `filePath`.
3150
let write<'T> (filePath: string) (value: 'T) : unit =
3251
use stream = File.Create(filePath)

src/BioFSharp.IO.INSDC/Run.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ open BioFSharp.IO.INSDC.Internal
66
/// Read and write INSDC Run records.
77
module Run =
88

9-
/// Read an INSDC Run XML record from the file at `filePath`.
10-
let read (filePath: string) : Run =
11-
XmlSerializer.read<Run> filePath
9+
/// Read INSDC Run XML records from the file at `filePath`.
10+
let read (filePath: string) : seq<Run> =
11+
XmlSerializer.readOrSet<Run, RunSet> "RUN_SET" (fun set -> set.Run) filePath
1212

13-
/// Parse an INSDC Run XML record from an in-memory string.
14-
let readString (xml: string) : Run =
15-
XmlSerializer.readString<Run> xml
13+
/// Parse INSDC Run XML records from an in-memory string.
14+
let readString (xml: string) : seq<Run> =
15+
XmlSerializer.readStringOrSet<Run, RunSet> "RUN_SET" (fun set -> set.Run) xml
1616

1717
/// Write an INSDC Run `run` to the file at `filePath` as XML.
1818
let write (filePath: string) (run: Run) : unit =

src/BioFSharp.IO.INSDC/Study.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ open BioFSharp.IO.INSDC.Internal
66
/// Read and write INSDC Study records.
77
module Study =
88

9-
/// Read an INSDC Study XML record from the file at `filePath`.
10-
let read (filePath: string) : Study =
11-
XmlSerializer.read<Study> filePath
9+
/// Read INSDC Study XML records from the file at `filePath`.
10+
let read (filePath: string) : seq<Study> =
11+
XmlSerializer.readOrSet<Study, StudySet> "STUDY_SET" (fun set -> set.Study) filePath
1212

13-
/// Parse an INSDC Study XML record from an in-memory string.
14-
let readString (xml: string) : Study =
15-
XmlSerializer.readString<Study> xml
13+
/// Parse INSDC Study XML records from an in-memory string.
14+
let readString (xml: string) : seq<Study> =
15+
XmlSerializer.readStringOrSet<Study, StudySet> "STUDY_SET" (fun set -> set.Study) xml
1616

1717
/// Write an INSDC Study `study` to the file at `filePath` as XML.
1818
let write (filePath: string) (study: Study) : unit =

src/BioFSharp.IO.INSDC/Submission.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ open BioFSharp.IO.INSDC.Internal
66
/// Read and write INSDC Submission records.
77
module Submission =
88

9-
/// Read an INSDC Submission XML record from the file at `filePath`.
10-
let read (filePath: string) : Submission =
11-
XmlSerializer.read<Submission> filePath
9+
/// Read INSDC Submission XML records from the file at `filePath`.
10+
let read (filePath: string) : seq<Submission> =
11+
XmlSerializer.readOrSet<Submission, SubmissionSet> "SUBMISSION_SET" (fun set -> set.Submission) filePath
1212

13-
/// Parse an INSDC Submission XML record from an in-memory string.
14-
let readString (xml: string) : Submission =
15-
XmlSerializer.readString<Submission> xml
13+
/// Parse INSDC Submission XML records from an in-memory string.
14+
let readString (xml: string) : seq<Submission> =
15+
XmlSerializer.readStringOrSet<Submission, SubmissionSet> "SUBMISSION_SET" (fun set -> set.Submission) xml
1616

1717
/// Write an INSDC Submission `submission` to the file at `filePath` as XML.
1818
let write (filePath: string) (submission: Submission) : unit =

0 commit comments

Comments
 (0)