|
| 1 | +"""Validate input metadata TSV file against metadata convention. |
| 2 | +
|
| 3 | +DESCRIPTION |
| 4 | +This script takes a TSV metadata file and validates against a metadata convention |
| 5 | +using the python jsonschema library. The metadata convention JSON schema |
| 6 | +represents the rules that should be enforced on metadata files for studies |
| 7 | +participating under the convention. |
| 8 | +
|
| 9 | +EXAMPLE |
| 10 | +# Using JSON file for latest Alexandria metadata convention in repo, validate input TSV |
| 11 | +$ python3 metadata_validation.py ../../tests/data/annotation/metadata/convention/valid_array_v2.1.2.txt |
| 12 | +
|
| 13 | +# generate an issues.json file to compare with reference test files |
| 14 | +$ python3 metadata_validation.py --issues-json ../../tests/data/annotation/metadata/convention/valid_no_array_v2.0.0.tsv |
| 15 | +
|
| 16 | +# generate a BigQuery upload file to compare with reference test files |
| 17 | +$ python3 metadata_validation.py --bq-json ../../tests/data/annotation/metadata/convention/valid_no_array_v2.0.0.tsv |
| 18 | +
|
| 19 | +# use a different metadata convention for validation |
| 20 | +$ python3 metadata_validation.py --convention <path to convention json> ../../tests/data/annotation/metadata/convention/valid_no_array_v2.0.0.tsv |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +import argparse |
| 25 | +import json |
| 26 | +import logging |
| 27 | +from collections import defaultdict |
| 28 | +import sys |
| 29 | +import requests |
| 30 | +import urllib.parse as encoder |
| 31 | +import re |
| 32 | +import os |
| 33 | +import numbers |
| 34 | +import time |
| 35 | +import backoff |
| 36 | +import csv |
| 37 | +import copy |
| 38 | +import itertools |
| 39 | +import math |
| 40 | +import pandas as pd |
| 41 | + |
| 42 | +import colorama |
| 43 | +from colorama import Fore |
| 44 | +import jsonschema |
| 45 | +from google.cloud import bigquery |
| 46 | + |
| 47 | +sys.path.append("..") |
| 48 | +try: |
| 49 | + # Used when importing internally and in tests |
| 50 | + from cell_metadata import CellMetadata |
| 51 | + from validation.validate_metadata import ( |
| 52 | + report_issues, |
| 53 | + validate_input_metadata, |
| 54 | + write_metadata_to_bq, |
| 55 | + serialize_issues, |
| 56 | + exit_if_errors, |
| 57 | + ) |
| 58 | +except ImportError: |
| 59 | + # Used when importing as external package, e.g. imports in single_cell_portal code |
| 60 | + from ..cell_metadata import CellMetadata |
| 61 | + |
| 62 | + |
| 63 | +def create_parser(): |
| 64 | + """Parse command line values for validate_metadata |
| 65 | + """ |
| 66 | + parser = argparse.ArgumentParser( |
| 67 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 68 | + ) |
| 69 | + # parser.add_argument( |
| 70 | + # '--output', |
| 71 | + # '-o', |
| 72 | + # type=str, |
| 73 | + # help='Output file name [optional]', |
| 74 | + # default=None |
| 75 | + # ) |
| 76 | + # parser.add_argument( |
| 77 | + # '--key_id', |
| 78 | + # '-k' |
| 79 | + # type=str, |
| 80 | + # help='Key metadata name for parsing; CellID for metadata, BiosampleID for sample sheets [optional]', |
| 81 | + # default='CellID' |
| 82 | + # ) |
| 83 | + |
| 84 | + # helper param to create JSON representation of metadata.issues |
| 85 | + # to generate reference output for tests |
| 86 | + parser.add_argument("--issues-json", action="store_true") |
| 87 | + # helper param to create JSON representation of convention metadata |
| 88 | + # to generate json for bigquery testing |
| 89 | + parser.add_argument("--bq-json", action="store_true") |
| 90 | + # overwrite existing output |
| 91 | + parser.add_argument("--force", action="store_true") |
| 92 | + # test BigQuery upload functions |
| 93 | + parser.add_argument("--upload", action="store_true") |
| 94 | + # validate_metadata.py CLI only for dev, bogus defaults below shouldn't propagate |
| 95 | + # make bogus defaults obviously artificial for ease of detection |
| 96 | + parser.add_argument( |
| 97 | + "--study-id", |
| 98 | + help="MongoDB study identifier", |
| 99 | + default="dec0dedfeed1111111111111", |
| 100 | + ) |
| 101 | + parser.add_argument( |
| 102 | + "--study-file-id", |
| 103 | + help="MongoDB file identifier", |
| 104 | + default="addedfeed000000000000000", |
| 105 | + ) |
| 106 | + parser.add_argument( |
| 107 | + "--study-accession", help="SCP study accession", default="SCPtest" |
| 108 | + ) |
| 109 | + parser.add_argument( |
| 110 | + "--bq-dataset", help="BigQuery dataset identifier", default="cell_metadata" |
| 111 | + ) |
| 112 | + parser.add_argument( |
| 113 | + "--bq-table", help="BigQuery table identifier", default="alexandria_convention" |
| 114 | + ) |
| 115 | + parser.add_argument( |
| 116 | + "--convention", |
| 117 | + help="Metadata convention JSON file", |
| 118 | + default="../../schema/alexandria_convention/alexandria_convention_schema.json", |
| 119 | + ) |
| 120 | + parser.add_argument("input_metadata", help="Metadata TSV file") |
| 121 | + return parser |
| 122 | + |
| 123 | + |
| 124 | +def check_if_old_output(): |
| 125 | + """Exit if old output files found |
| 126 | + """ |
| 127 | + output_files = ["bq.json"] |
| 128 | + |
| 129 | + old_output = False |
| 130 | + for file in output_files: |
| 131 | + if os.path.exists(file): |
| 132 | + print(f"{file} already exists, please delete file and try again") |
| 133 | + old_output = True |
| 134 | + if old_output: |
| 135 | + exit(1) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + args = create_parser().parse_args() |
| 140 | + arguments = vars(args) |
| 141 | + if not args.force: |
| 142 | + check_if_old_output() |
| 143 | + |
| 144 | + with open(args.convention, "r") as f: |
| 145 | + convention = json.load(f) |
| 146 | + metadata = CellMetadata( |
| 147 | + file_path=args.input_metadata, |
| 148 | + study_id=args.study_id, |
| 149 | + study_file_id=args.study_file_id, |
| 150 | + study_accession=args.study_accession, |
| 151 | + ) |
| 152 | + metadata.preprocess(True) |
| 153 | + print("Validating", args.input_metadata) |
| 154 | + |
| 155 | + validate_input_metadata(metadata, convention, args.bq_json) |
| 156 | + if args.issues_json: |
| 157 | + serialize_issues(metadata) |
| 158 | + report_issues(metadata) |
| 159 | + if args.upload: |
| 160 | + write_metadata_to_bq(metadata, args.bq_dataset, args.bq_table) |
| 161 | + exit_if_errors(metadata) |
0 commit comments