@@ -3,6 +3,7 @@ namespace BioFSharp.IO.INSDC.Internal
33open System
44open System.Collections .Concurrent
55open System.IO
6+ open System.Xml .Linq
67open 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)
0 commit comments