<reponame>AlexsLemonade/refinebio<filename>common/data_refinery_common/migrations/0061_transcriptome_database_name.py
# Generated by Django 2.2.13 on 2020-08-28 19:40

import json

from django.db import migrations

import requests


def find_division_by_species(species: str, division_jsons: dict) -> str:
    species_name = str.lower(species)

    divisions = [
        "EnsemblPlants",
        "EnsemblProtists",
        "EnsemblFungi",
        "EnsemblMetazoa",
        "EnsemblBacteria",
    ]

    # Go through each division json to check if the organism we are looking for is in there
    for division in divisions:
        for organism in division_jsons[division]:
            if organism["name"] == species_name:
                return division
    return "EnsemblMain"


def add_transcriptome_database_names(apps, schema_editor):
    """ Updates `database_name` values for transcriptome indices """
    OrganismIndex = apps.get_model("data_refinery_common", "OrganismIndex")
    transcriptome_indices = OrganismIndex.objects.all()

    if not transcriptome_indices.exists():
        # Nothing to do!
        return True

    # First make requests to the divisions for ensembl so we don't have to multiple times
    # Note: EnsemblBacteria takes a while to load and may give an error
    division_jsons = {}
    divisions = [
        "EnsemblPlants",
        "EnsemblProtists",
        "EnsemblFungi",
        "EnsemblMetazoa",
        "EnsemblBacteria",
    ]

    root_url = "https://rest.ensembl.org/info/genomes/division/"

    for division in divisions:
        r = requests.get(root_url + division, headers={"Content-Type": "application/json"})
        division_jsons[division] = json.loads(r.text)

    # Make a dict of which species correspond to which division because of double entries
    discovered_species = {}

    for transcriptome_index in transcriptome_indices:
        organism_name = transcriptome_index.organism.name

        if organism_name in discovered_species.keys():
            division = discovered_species[organism_name]
        else:
            division = find_division_by_species(organism_name, division_jsons)
            discovered_species[organism_name] = division

        transcriptome_index.database_name = division
        transcriptome_index.save()


class Migration(migrations.Migration):

    dependencies = [
        ("data_refinery_common", "0060_auto_20200828_1650"),
    ]

    operations = [
        migrations.RunPython(add_transcriptome_database_names),
    ]

# Generated by Django 2.2.13 on 2020-08-28 19:40

import json

from django.db import migrations

import requests


def find_division_by_species(species: str, division_jsons: dict) -> str:
    species_name = str.lower(species)

    divisions = [
        "EnsemblPlants",
        "EnsemblProtists",
        "EnsemblFungi",
        "EnsemblMetazoa",
        "EnsemblBacteria",
    ]

    # Go through each division json to check if the organism we are looking for is in there
    for division in divisions:
        for organism in division_jsons[division]:
            if organism["name"] == species_name:
                return division
    return "EnsemblMain"


def add_transcriptome_database_names(apps, schema_editor):
    """ Updates `database_name` values for transcriptome indices """
    OrganismIndex = apps.get_model("data_refinery_common", "OrganismIndex")
    transcriptome_indices = OrganismIndex.objects.all()

    if not transcriptome_indices.exists():
        # Nothing to do!
        return True

    # First make requests to the divisions for ensembl so we don't have to multiple times
    # Note: EnsemblBacteria takes a while to load and may give an error
    division_jsons = {}
    divisions = [
        "EnsemblPlants",
        "EnsemblProtists",
        "EnsemblFungi",
        "EnsemblMetazoa",
        "EnsemblBacteria",
    ]

    root_url = "https://rest.ensembl.org/info/genomes/division/"

    for division in divisions:
        r = requests.get(root_url + division, headers={"Content-Type": "application/json"})
        division_jsons[division] = json.loads(r.text)

    # Make a dict of which species correspond to which division because of double entries
    discovered_species = {}

    for transcriptome_index in transcriptome_indices:
        organism_name = transcriptome_index.organism.name

        if organism_name in discovered_species.keys():
            division = discovered_species[organism_name]
        else:
            division = find_division_by_species(organism_name, division_jsons)
            discovered_species[organism_name] = division

        transcriptome_index.database_name = division
        transcriptome_index.save()


class Migration(migrations.Migration):

    dependencies = [
        ("data_refinery_common", "0060_auto_20200828_1650"),
    ]

    operations = [
        migrations.RunPython(add_transcriptome_database_names),
    ]
# Generated by Django 2.2.13 on 2020-08-28 19:40 # Go through each division json to check if the organism we are looking for is in there Updates `database_name` values for transcriptome indices # Nothing to do! # First make requests to the divisions for ensembl so we don't have to multiple times # Note: EnsemblBacteria takes a while to load and may give an error # Make a dict of which species correspond to which division because of double entries