|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright [2009-2021] EMBL-European Bioinformatics Institute |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +""" |
| 15 | + |
| 16 | +import logging |
| 17 | +import re |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import click |
| 21 | +import polars as pl |
| 22 | + |
| 23 | +from rnacentral_pipeline.databases.expressionatlas import configuration, parser, sdrf |
| 24 | +from rnacentral_pipeline.databases.expressionatlas.helpers import find_all_taxids |
| 25 | +from rnacentral_pipeline.writers import entry_writer |
| 26 | + |
| 27 | +LOGGER = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +@click.group("expressionatlas") |
| 31 | +def cli(): |
| 32 | + """ |
| 33 | + Commands for parsing expression atlas data |
| 34 | + """ |
| 35 | + |
| 36 | + |
| 37 | +@cli.command("parse") |
| 38 | +@click.argument("genes_mapped", type=click.File("r")) |
| 39 | +@click.argument("lookup", type=click.Path()) |
| 40 | +@click.argument( |
| 41 | + "output", |
| 42 | + default=".", |
| 43 | + type=click.Path(writable=True, dir_okay=True, file_okay=False), |
| 44 | +) |
| 45 | +def process_csv(genes_mapped, lookup, output): |
| 46 | + """ |
| 47 | + Process the csv generated by linking EA data to rnc data |
| 48 | +
|
| 49 | + Args: |
| 50 | + gened_mapped: A concatenated ndjson of all hits for all experiments |
| 51 | + lookup: The retrieved lookup CSV file |
| 52 | + output: The directory in which the load data will be written |
| 53 | + """ |
| 54 | + entries = parser.parse(genes_mapped, lookup) |
| 55 | + |
| 56 | + with entry_writer(Path(output)) as writer: |
| 57 | + try: |
| 58 | + writer.write(entries) |
| 59 | + except ValueError: |
| 60 | + print("No entries from this chunk") |
| 61 | + |
| 62 | + |
| 63 | +@cli.command("get-taxids") |
| 64 | +@click.argument( |
| 65 | + "directory", type=click.Path(exists=True, file_okay=False, dir_okay=True) |
| 66 | +) |
| 67 | +@click.argument("output") |
| 68 | +def get_taxids(directory, output): |
| 69 | + """ |
| 70 | + Find all condensed SDRF files in the subdirectory, and parse out all possible taxids. |
| 71 | + Write these out to a file so we can query with them later. |
| 72 | +
|
| 73 | + Args: |
| 74 | + directory: The path to the top-level directory within which are all Expression atlas |
| 75 | + experiments. |
| 76 | + output: A text file that will have one taxid per line, ready to be used in a future |
| 77 | + sql query |
| 78 | + """ |
| 79 | + taxids = find_all_taxids(directory) |
| 80 | + with open(output, "w") as out: |
| 81 | + for t in taxids: |
| 82 | + out.write(f"{t}\n") |
| 83 | + |
| 84 | + LOGGER.info(f"Found {len(taxids)} unique taxids to search for") |
| 85 | + |
| 86 | + |
| 87 | +@cli.command("parse-dir") |
| 88 | +@click.argument( |
| 89 | + "directory", type=click.Path(exists=True, file_okay=False, dir_okay=True) |
| 90 | +) |
| 91 | +@click.argument("lookup", type=click.Path()) |
| 92 | +@click.argument("output", type=click.Path()) |
| 93 | +def parse_dir(directory, lookup, output): |
| 94 | + """ |
| 95 | + Look at the files within a directory and parse them appropriately |
| 96 | +
|
| 97 | + This will parse down to a urs_taxid <> experiment mapping, saved as ndjson |
| 98 | + The next parsing stage will create entries using the lookup |
| 99 | +
|
| 100 | + Args: |
| 101 | + directory: The path to a specific experiment that we will try to parse |
| 102 | + lookup: Path to the retrieved lookup CSV file |
| 103 | + output: Output filename for writing the ndjson hits |
| 104 | + """ |
| 105 | + directory = Path(directory) |
| 106 | + configurations = list(directory.glob("*configuration.xml")) |
| 107 | + assert len(configurations) == 1 |
| 108 | + config_file = configurations[0] |
| 109 | + config = configuration.parse_config(config_file) |
| 110 | + input("wait...") |
| 111 | + if config.exp_type == "rnaseq_mrna_differential": |
| 112 | + analytics = list(directory.glob("*analytics.tsv")) |
| 113 | + assert len(analytics) == 1 |
| 114 | + analytics_file = analytics[0] |
| 115 | + |
| 116 | + sdrfs = list(directory.glob("*condensed-sdrf.tsv")) |
| 117 | + assert len(sdrfs) == 1 |
| 118 | + sdrf = sdrfs[0] |
| 119 | + print(analytics_file) |
| 120 | + try: |
| 121 | + hits = parser.parse_differential(analytics_file, sdrf, lookup) |
| 122 | + except ValueError: |
| 123 | + LOGGER.error("Failed to parse differential experiment %s", analytics_file) |
| 124 | + hits = pl.DataFrame() |
| 125 | + elif config.exp_type == "rnaseq_mrna_baseline": |
| 126 | + ## There is a transcripts tpms file which we don't want, so grab both with |
| 127 | + ## pathlib glob, then filter to only get the one we want |
| 128 | + tpms = list(directory.glob(r"*-tpms.tsv")) |
| 129 | + tpms = list(filter(lambda x: re.match(".*\d+-tpms\.tsv$", str(x)), tpms))[0] |
| 130 | + |
| 131 | + sdrfs = list(directory.glob("*condensed-sdrf.tsv")) |
| 132 | + assert len(sdrfs) == 1 |
| 133 | + sdrf = sdrfs[0] |
| 134 | + try: |
| 135 | + hits = parser.parse_baseline(tpms, sdrf, lookup) |
| 136 | + except ValueError: |
| 137 | + hits = pl.DataFrame() |
| 138 | + LOGGER.error("failed to parse baseline experiment %s", tpms) |
| 139 | + |
| 140 | + hits.write_ndjson(output) |
0 commit comments