<reponame>SACGF/variantgrid<gh_stars>1-10
# Generated by Django 3.1.3 on 2021-04-06 05:32
import itertools

from django.db import migrations

from library.vcf_utils import get_variant_caller_and_version_from_vcf


def _get_variant_caller_from_vcf_file(VariantCaller, vcf_path):
    variant_caller, version = get_variant_caller_and_version_from_vcf(vcf_path)
    if variant_caller is None:
        variant_caller = "Unknown Variant Caller"
        version = -1

    return VariantCaller.objects.get_or_create(name=variant_caller, version=version)[0]


def _one_off_seqauto_variant_caller(apps, schema_editor):
    SampleSheetCombinedVCFFile = apps.get_model("seqauto", "SampleSheetCombinedVCFFile")
    VCFFile = apps.get_model("seqauto", "VCFFile")
    VCFFromSequencingRun = apps.get_model("seqauto", "VCFFromSequencingRun")
    VariantCaller = apps.get_model("seqauto", "VariantCaller")

    legacy_combo = SampleSheetCombinedVCFFile.objects.filter(sequencing_run__legacy=True)
    legacy_vcf = VCFFile.objects.filter(sequencing_run__legacy=True)
    legacy_vcf_from_runs = VCFFromSequencingRun.objects.filter(sequencing_run__legacy=True)
    if legacy_combo.exists() or legacy_vcf.exists() or legacy_vcf_from_runs.exists():
        legacy_gatk = VariantCaller.objects.get_or_create(name="GATK", version="2014.4-3.3.0-0-ga3711aa")[0]
        legacy_combo.update(variant_caller=legacy_gatk)
        legacy_vcf.update(variant_caller=legacy_gatk)
        legacy_vcf_from_runs.update(variant_caller=legacy_gatk)

    recent_combo = SampleSheetCombinedVCFFile.objects.filter(sequencing_run__legacy=False)
    recent_vcf = VCFFile.objects.filter(sequencing_run__legacy=False)
    recent_vcf_from_runs = VCFFromSequencingRun.objects.filter(sequencing_run__legacy=False)
    if recent_combo.exists() or recent_vcf.exists() or recent_vcf_from_runs.exists():
        # TODO: Reload from actual VCF files
        for seqauto_vcf in itertools.chain(recent_combo, recent_vcf):
            try:
                seqauto_vcf.variant_caller = _get_variant_caller_from_vcf_file(VariantCaller, seqauto_vcf.path)
                seqauto_vcf.save()
            except Exception as e:
                print(e)

            for run_vcf in recent_vcf_from_runs:
                try:
                    vcf_path = run_vcf.vcf.uploadedvcf.uploaded_file.path
                    run_vcf.variant_caller = _get_variant_caller_from_vcf_file(VariantCaller, vcf_path)
                    run_vcf.save()
                except Exception as e:
                    print(e)


class Migration(migrations.Migration):

    dependencies = [
        ('seqauto', '0017_auto_20210406_1151'),
    ]

    operations = [
        migrations.RunPython(_one_off_seqauto_variant_caller),
    ]

# Generated by Django 3.1.3 on 2021-04-06 05:32
import itertools

from django.db import migrations

from library.vcf_utils import get_variant_caller_and_version_from_vcf


def _get_variant_caller_from_vcf_file(VariantCaller, vcf_path):
    variant_caller, version = get_variant_caller_and_version_from_vcf(vcf_path)
    if variant_caller is None:
        variant_caller = "Unknown Variant Caller"
        version = -1

    return VariantCaller.objects.get_or_create(name=variant_caller, version=version)[0]


def _one_off_seqauto_variant_caller(apps, schema_editor):
    SampleSheetCombinedVCFFile = apps.get_model("seqauto", "SampleSheetCombinedVCFFile")
    VCFFile = apps.get_model("seqauto", "VCFFile")
    VCFFromSequencingRun = apps.get_model("seqauto", "VCFFromSequencingRun")
    VariantCaller = apps.get_model("seqauto", "VariantCaller")

    legacy_combo = SampleSheetCombinedVCFFile.objects.filter(sequencing_run__legacy=True)
    legacy_vcf = VCFFile.objects.filter(sequencing_run__legacy=True)
    legacy_vcf_from_runs = VCFFromSequencingRun.objects.filter(sequencing_run__legacy=True)
    if legacy_combo.exists() or legacy_vcf.exists() or legacy_vcf_from_runs.exists():
        legacy_gatk = VariantCaller.objects.get_or_create(name="GATK", version="2014.4-3.3.0-0-ga3711aa")[0]
        legacy_combo.update(variant_caller=legacy_gatk)
        legacy_vcf.update(variant_caller=legacy_gatk)
        legacy_vcf_from_runs.update(variant_caller=legacy_gatk)

    recent_combo = SampleSheetCombinedVCFFile.objects.filter(sequencing_run__legacy=False)
    recent_vcf = VCFFile.objects.filter(sequencing_run__legacy=False)
    recent_vcf_from_runs = VCFFromSequencingRun.objects.filter(sequencing_run__legacy=False)
    if recent_combo.exists() or recent_vcf.exists() or recent_vcf_from_runs.exists():
        # TODO: Reload from actual VCF files
        for seqauto_vcf in itertools.chain(recent_combo, recent_vcf):
            try:
                seqauto_vcf.variant_caller = _get_variant_caller_from_vcf_file(VariantCaller, seqauto_vcf.path)
                seqauto_vcf.save()
            except Exception as e:
                print(e)

            for run_vcf in recent_vcf_from_runs:
                try:
                    vcf_path = run_vcf.vcf.uploadedvcf.uploaded_file.path
                    run_vcf.variant_caller = _get_variant_caller_from_vcf_file(VariantCaller, vcf_path)
                    run_vcf.save()
                except Exception as e:
                    print(e)


class Migration(migrations.Migration):

    dependencies = [
        ('seqauto', '0017_auto_20210406_1151'),
    ]

    operations = [
        migrations.RunPython(_one_off_seqauto_variant_caller),
    ]
# Generated by Django 3.1.3 on 2021-04-06 05:32 # TODO: Reload from actual VCF files