-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMZQC-module.cs
More file actions
289 lines (240 loc) · 11.3 KB
/
Copy pathMZQC-module.cs
File metadata and controls
289 lines (240 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// This is a **experimental** mzQC library implementation for C#
// It is tested only to the extent of the TDAuditor usecase. Beyond, there be dragons.
// To parse mzQC JSON data, add the MZQC-module ('Newtonsoft.Json' dependency to your
// assembly) to your project .cs group, then for mzQC input do for example:
// string contents = File.ReadAllText(@"individual-runs.mzQC");
// var iomzqc = Mzqc.FromJson(contents);
// File.WriteAllText(@"regurgitated.mzqc", iomzqc.ToJson());
// for ground-up mzQC design, do for example:
// var mzqc = new MzqcContent { Description = "Sample" }; // YOU need to add more mzQC content!
// var file = new Mzqc {MzqcContent = mzqc};
// File.WriteAllText(@"out.mzqc", file.ToJson());
namespace MzqcCsLib
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
/// JSON schema specifying the mzQC format v1.0.0 developed by the HUPO-PSI Quality Control
/// working group (http://psidev.info/groups/quality-control).
public partial class Mzqc
{
/// <summary>
/// Root element of an mzQC file.
/// </summary>
[JsonProperty("mzQC")]
public MzqcContent MzqcContent { get; set; }
}
public partial class MzqcContent
{
/// Contact Address (mail/tel.) for getting in touch with given contact for a particular mzQC file
[JsonProperty("contactAddress", NullValueHandling = NullValueHandling.Ignore)]
public string ContactAddress { get; set; }
/// Name of file creator or person chosen as dedicated contact a particular mzQC file.
[JsonProperty("contactName", NullValueHandling = NullValueHandling.Ignore)]
public string ContactName { get; set; }
/// Collection of controlled vocabulary elements used to refer to the source of the used CV
/// terms in the qualityMetric objects (and others).
[JsonProperty("controlledVocabularies")]
public List<ControlledVocabulary> ControlledVocabularies { get; set; }
/// Creation date of the mzQC file.
[JsonProperty("creationDate")]
public DateTimeOffset CreationDate { get; set; }
/// Description and comments about the mzQC file contents.
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }
/// List of runQuality elements.
[JsonProperty("runQualities", NullValueHandling = NullValueHandling.Ignore)]
public List<BaseQuality> RunQualities { get; set; }
/// List of setQuality elements.
[JsonProperty("setQualities", NullValueHandling = NullValueHandling.Ignore)]
public List<BaseQuality> SetQualities { get; set; }
/// Version of the mzQC format.
[JsonProperty("version")]
public string Version { get; set; }
}
/// Element describing a controlled vocabulary used to refer to the source of the used CV
/// terms in qualityMetric objects (and others).
public partial class ControlledVocabulary
{
/// Full name of the controlled vocabulary.
[JsonProperty("name")]
public string Name { get; set; }
/// Publicly accessible URI of the controlled vocabulary.
[JsonProperty("uri")]
public Uri Uri { get; set; }
/// Version of the controlled vocabulary.
[JsonProperty("version", NullValueHandling = NullValueHandling.Ignore)]
public string Version { get; set; }
}
/// Element containing metadata and qualityMetrics for a single run.
///
/// Base element from which both runQuality and setQuality elements are derived.
///
/// Element containing metadata and qualityMetrics for a collection of related runs (set).
public partial class BaseQuality
{
[JsonProperty("metadata")]
public Metadata Metadata { get; set; }
/// The collection of qualityMetrics for a particular runQuality or setQuality.
[JsonProperty("qualityMetrics")]
public List<QualityMetric> QualityMetrics { get; set; }
}
/// Metadata describing the QC analysis.
public partial class Metadata
{
/// Software tool(s) used to generate the QC metrics.
[JsonProperty("analysisSoftware")]
public List<AnalysisSoftwareElement> AnalysisSoftware { get; set; }
/// OPTIONAL list of cvParameter elements containing additional metadata about its parent
/// runQuality/setQuality.
[JsonProperty("cvParameters", NullValueHandling = NullValueHandling.Ignore)]
public List<CvParameter> CvParameters { get; set; }
/// List of input files from which the QC metrics have been generated.
[JsonProperty("inputFiles")]
public List<InputFile> InputFiles { get; set; }
/// OPTIONAL label name. For setQuality, this a group name, lending itself for example as a
/// axis labels for a plot. OPTIONAL.
[JsonProperty("label", NullValueHandling = NullValueHandling.Ignore)]
public string Label { get; set; }
}
/// Base element for a term that is defined in a controlled vocabulary, with OPTIONAL value.
public partial class AnalysisSoftwareElement
{
/// Accession number identifying the term within its controlled vocabulary.
[JsonProperty("accession")]
public string Accession { get; set; }
/// Definition of the controlled vocabulary term.
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }
/// Name of the controlled vocabulary term describing the parameter.
[JsonProperty("name")]
public string Name { get; set; }
/// Value of the parameter.
[JsonProperty("value")]
public object Value { get; set; }
/// Publicly accessible URI of the software tool or documentation.
[JsonProperty("uri")]
public Uri Uri { get; set; }
/// Version number of the software tool.
[JsonProperty("version")]
public string Version { get; set; }
}
/// Base element for a term that is defined in a controlled vocabulary, with OPTIONAL value.
public partial class CvParameter
{
/// Accession number identifying the term within its controlled vocabulary.
[JsonProperty("accession")]
public string Accession { get; set; }
/// Definition of the controlled vocabulary term.
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }
/// Name of the controlled vocabulary term describing the parameter.
[JsonProperty("name")]
public string Name { get; set; }
/// Value of the parameter.
[JsonProperty("value", NullValueHandling = NullValueHandling.Ignore)]
public object Value { get; set; }
}
/// Input file used to generate the QC metrics.
public partial class InputFile
{
/// Type of input file.
[JsonProperty("fileFormat")]
public CvParameter FileFormat { get; set; }
/// Detailed properties of the input file.
[JsonProperty("fileProperties", NullValueHandling = NullValueHandling.Ignore)]
public List<CvParameter> FileProperties { get; set; }
/// Unique file location. The file URI is RECOMMENDED to be publicly accessible.
[JsonProperty("location")]
public Uri Location { get; set; }
/// Base file name. This MUST be unique across all inputFiles specified in the mzQC file.
[JsonProperty("name")]
public string Name { get; set; }
}
/// Element containing the value and description of a QC metric defined in a controlled
/// vocabulary.
///
/// Base element for a term that is defined in a controlled vocabulary, with OPTIONAL value.
public partial class QualityMetric
{
/// Accession number identifying the term within its controlled vocabulary.
[JsonProperty("accession")]
public string Accession { get; set; }
/// Definition of the controlled vocabulary term.
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }
/// Name of the controlled vocabulary term describing the parameter.
[JsonProperty("name")]
public string Name { get; set; }
/// Value of the parameter.
[JsonProperty("value")]
public object Value { get; set; }
/// One or more controlled vocabulary elements describing the unit of the metric.
[JsonProperty("unit", NullValueHandling = NullValueHandling.Ignore)]
public Unit? Unit { get; set; }
}
/// One or more controlled vocabulary elements describing the unit of the metric.
public partial struct Unit
{
public CvParameter CvParameter;
public List<CvParameter> CvParameterArray;
public static implicit operator Unit(CvParameter CvParameter) => new Unit { CvParameter = CvParameter };
public static implicit operator Unit(List<CvParameter> CvParameterArray) => new Unit { CvParameterArray = CvParameterArray };
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
UnitConverter.Singleton,
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
public partial class Mzqc
{
public static Mzqc FromJson(string json) => JsonConvert.DeserializeObject<Mzqc>(json);
}
public static class Serialize
{
public static string ToJson(this Mzqc self) => JsonConvert.SerializeObject(self, Formatting.Indented);
}
internal class UnitConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(Unit) || t == typeof(Unit?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.StartObject:
var objectValue = serializer.Deserialize<CvParameter>(reader);
return new Unit { CvParameter = objectValue };
case JsonToken.StartArray:
var arrayValue = serializer.Deserialize<List<CvParameter>>(reader);
return new Unit { CvParameterArray = arrayValue };
}
throw new Exception("Cannot unmarshal type Unit");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (Unit)untypedValue;
if (value.CvParameterArray != null)
{
serializer.Serialize(writer, value.CvParameterArray);
return;
}
if (value.CvParameter != null)
{
serializer.Serialize(writer, value.CvParameter);
return;
}
throw new Exception("Cannot marshal type Unit");
}
public static readonly UnitConverter Singleton = new UnitConverter();
}
}