{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://prepaire.example/contracts/metagenomics-evidence-package/1.0.0",
  "title": "PREPAIRE-METAGENOMICS-EVIDENCE-PACKAGE@1.0.0",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contract",
    "packageId",
    "subjectRef",
    "sampleRef",
    "runId",
    "specimen",
    "batch",
    "inputEvidence",
    "controls",
    "quality",
    "databaseSnapshots",
    "profiles",
    "releasedFeatures",
    "longitudinalContext",
    "provenance"
  ],
  "properties": {
    "contract": { "const": "PREPAIRE-METAGENOMICS-EVIDENCE-PACKAGE@1.0.0" },
    "packageId": { "type": "string", "format": "uuid" },
    "subjectRef": {
      "type": "string",
      "minLength": 1,
      "description": "Pseudonymous subject identifier. Direct identifiers are prohibited."
    },
    "sampleRef": { "type": "string", "minLength": 1 },
    "runId": { "type": "string", "pattern": "^RUN-" },
    "specimen": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "collectedAt", "preservationMethod", "receivedAt", "extractionMethod"],
      "properties": {
        "type": { "$ref": "#/$defs/codedConcept" },
        "collectedAt": { "type": "string", "format": "date-time" },
        "preservationMethod": { "type": "string", "minLength": 1 },
        "receivedAt": { "type": "string", "format": "date-time" },
        "extractionMethod": { "type": "string", "minLength": 1 },
        "spikeIn": {
          "type": "object",
          "additionalProperties": false,
          "required": ["material", "lot", "amount", "unit"],
          "properties": {
            "material": { "type": "string", "minLength": 1 },
            "lot": { "type": "string", "minLength": 1 },
            "amount": { "type": "number", "exclusiveMinimum": 0 },
            "unit": { "type": "string", "minLength": 1 }
          }
        }
      }
    },
    "batch": {
      "type": "object",
      "additionalProperties": false,
      "required": ["extractionBatchId", "libraryBatchId", "sequencingRunId", "instrumentAssetId", "instrumentSerial", "readTechnology"],
      "properties": {
        "extractionBatchId": { "type": "string", "minLength": 1 },
        "libraryBatchId": { "type": "string", "minLength": 1 },
        "sequencingRunId": { "type": "string", "minLength": 1 },
        "instrumentAssetId": { "type": "string", "minLength": 1 },
        "instrumentSerial": { "type": "string", "minLength": 1 },
        "readTechnology": { "enum": ["SHORT_READ", "ONT_LONG_READ", "OTHER"] }
      }
    },
    "inputEvidence": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["drsId", "artifactRole", "format", "sha256", "restrictedHumanData"],
        "properties": {
          "drsId": { "type": "string", "minLength": 1 },
          "artifactRole": { "enum": ["RAW_SIGNAL", "FASTQ", "BASECALLED_BAM", "HOST_DEPLETED_FASTQ", "RUN_METADATA", "CONTROL_READS"] },
          "format": { "type": "string", "minLength": 1 },
          "sha256": { "$ref": "#/$defs/sha256" },
          "restrictedHumanData": { "type": "boolean" }
        }
      }
    },
    "controls": {
      "type": "array",
      "minItems": 3,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["controlId", "controlType", "sampleRef", "state", "acceptancePolicyRef", "metricsDrsId"],
        "properties": {
          "controlId": { "type": "string", "format": "uuid" },
          "controlType": { "enum": ["EXTRACTION_BLANK", "NO_TEMPLATE", "POSITIVE_MOCK_COMMUNITY", "SPIKE_IN", "TECHNICAL_REPLICATE"] },
          "sampleRef": { "type": "string", "minLength": 1 },
          "state": { "enum": ["passed", "failed", "qualified"] },
          "acceptancePolicyRef": { "type": "string", "minLength": 1 },
          "metricsDrsId": { "type": "string", "minLength": 1 },
          "flags": { "type": "array", "items": { "type": "string" } }
        }
      }
    },
    "quality": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "rawReadCount", "retainedReadCount", "hostFraction", "controlState", "gateDecisionId"],
      "properties": {
        "state": { "enum": ["accepted", "qualified", "rejected"] },
        "rawReadCount": { "type": "integer", "minimum": 0 },
        "retainedReadCount": { "type": "integer", "minimum": 0 },
        "hostFraction": { "type": "number", "minimum": 0, "maximum": 1 },
        "libraryComplexity": { "type": "number", "minimum": 0, "maximum": 1 },
        "controlState": { "enum": ["passed", "failed", "qualified"] },
        "gateDecisionId": { "type": "string", "format": "uuid" },
        "flags": { "type": "array", "items": { "type": "string" } }
      }
    },
    "databaseSnapshots": {
      "type": "array",
      "minItems": 2,
      "items": { "$ref": "#/$defs/referenceAsset" }
    },
    "profiles": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["profileId", "profileType", "drsId", "format", "producer", "databaseAssetIds", "releaseScope"],
        "properties": {
          "profileId": { "type": "string", "format": "uuid" },
          "profileType": { "enum": ["TAXONOMY_CANONICAL", "TAXONOMY_COMPARATOR", "GENE_FAMILY", "PATHWAY_ABUNDANCE", "PATHWAY_COVERAGE", "AMR", "MAG_RESEARCH"] },
          "drsId": { "type": "string", "minLength": 1 },
          "format": { "type": "string", "minLength": 1 },
          "producer": { "$ref": "#/$defs/softwareIdentity" },
          "databaseAssetIds": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "type": "string" } },
          "releaseScope": { "enum": ["clinical_support", "informational", "research_only", "not_releaseable"] }
        }
      }
    },
    "releasedFeatures": {
      "type": "array",
      "items": { "$ref": "#/$defs/releasedFeature" }
    },
    "longitudinalContext": {
      "type": "object",
      "additionalProperties": false,
      "required": ["comparablePriorSamples", "comparabilityState", "baselineEligible", "basis"],
      "properties": {
        "comparablePriorSamples": { "type": "integer", "minimum": 0 },
        "comparabilityState": { "enum": ["comparable", "partially_comparable", "not_comparable", "no_prior"] },
        "baselineEligible": { "type": "boolean" },
        "basis": { "type": "string", "minLength": 1 }
      }
    },
    "provenance": { "$ref": "#/$defs/provenance" }
  },
  "$defs": {
    "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "codedConcept": {
      "type": "object",
      "additionalProperties": false,
      "required": ["system", "code", "display", "version"],
      "properties": {
        "system": { "type": "string", "format": "uri" },
        "code": { "type": "string", "minLength": 1 },
        "display": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 }
      }
    },
    "softwareIdentity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "version", "ociDigest"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "ociDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" }
      }
    },
    "referenceAsset": {
      "type": "object",
      "additionalProperties": false,
      "required": ["referenceAssetId", "name", "version", "source", "sha256"],
      "properties": {
        "referenceAssetId": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "source": { "type": "string", "format": "uri" },
        "sha256": { "$ref": "#/$defs/sha256" }
      }
    },
    "releasedFeature": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "featureId",
        "featureType",
        "label",
        "sourceProfileIds",
        "measurementBasis",
        "value",
        "unit",
        "confidence",
        "intendedUse",
        "summary",
        "limitations",
        "causalStatus",
        "review"
      ],
      "properties": {
        "featureId": { "type": "string", "format": "uuid" },
        "featureType": { "enum": ["TAXON", "GENE_FAMILY", "PATHWAY", "AMR_GENE", "DIVERSITY", "OTHER"] },
        "label": { "type": "string", "minLength": 1 },
        "taxonomy": {
          "type": "object",
          "additionalProperties": false,
          "required": ["taxId", "rank", "scientificName", "taxonomyVersion"],
          "properties": {
            "taxId": { "type": "string", "minLength": 1 },
            "rank": { "type": "string", "minLength": 1 },
            "scientificName": { "type": "string", "minLength": 1 },
            "taxonomyVersion": { "type": "string", "minLength": 1 }
          }
        },
        "sourceProfileIds": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "type": "string", "format": "uuid" } },
        "measurementBasis": { "enum": ["RELATIVE_ABUNDANCE", "ABSOLUTE_ABUNDANCE", "PRESENCE_ABSENCE", "CLR_TRANSFORM", "COUNT", "OTHER"] },
        "value": { "type": "number" },
        "unit": { "type": "string", "minLength": 1 },
        "detectionLimit": { "type": "number", "minimum": 0 },
        "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
        "absoluteQuantitationEvidence": {
          "type": "object",
          "additionalProperties": false,
          "required": ["method", "calibratorOrSpikeIn", "validationDecisionId"],
          "properties": {
            "method": { "enum": ["SPIKE_IN", "QPCR", "FLOW_CYTOMETRY", "OTHER_VALIDATED_METHOD"] },
            "calibratorOrSpikeIn": { "type": "string", "minLength": 1 },
            "validationDecisionId": { "type": "string", "format": "uuid" }
          }
        },
        "intendedUse": { "enum": ["clinical_support", "informational", "research"] },
        "summary": { "type": "string", "minLength": 1 },
        "limitations": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 1 } },
        "causalStatus": { "enum": ["association_only", "mechanistically_supported", "clinically_validated"] },
        "actionPolicyRefs": { "type": "array", "items": { "type": "string", "minLength": 1 } },
        "review": {
          "type": "object",
          "additionalProperties": false,
          "required": ["state", "reviewerRole", "reviewedAt", "releaseDecisionId"],
          "properties": {
            "state": { "enum": ["EXPERT_REVIEWED", "RELEASED", "REVOKED"] },
            "reviewerRole": { "type": "string", "minLength": 1 },
            "reviewedAt": { "type": "string", "format": "date-time" },
            "releaseDecisionId": { "type": "string", "format": "uuid" }
          }
        }
      },
      "allOf": [
        {
          "if": { "properties": { "measurementBasis": { "const": "ABSOLUTE_ABUNDANCE" } }, "required": ["measurementBasis"] },
          "then": { "required": ["absoluteQuantitationEvidence"] }
        }
      ]
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["pipelineDefinitionSha256", "softwareBuilds", "generatedAt", "generatedByWorkload", "auditDrsId"],
      "properties": {
        "pipelineDefinitionSha256": { "$ref": "#/$defs/sha256" },
        "softwareBuilds": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/softwareIdentity" } },
        "generatedAt": { "type": "string", "format": "date-time" },
        "generatedByWorkload": { "type": "string", "minLength": 1 },
        "auditDrsId": { "type": "string", "minLength": 1 }
      }
    }
  }
}
