# Generated by Django 3.1 on 2020-09-29 06:11

from django.db import migrations


def create_sequencers(apps, schema_editor):
    HISEQ = 'H'
    MISEQ = 'M'

    Manufacturer = apps.get_model("snpdb", "Manufacturer")
    illumina = Manufacturer.objects.create(name="Illumina")
    Manufacturer.objects.create(name="Ion Torrent")
    Manufacturer.objects.create(name="PacBio")

    SequencerModel = apps.get_model("seqauto", "SequencerModel")
    SequencerModel.objects.create(model='HiSeq 2500',
                                  manufacturer=illumina,
                                  data_naming_convention=HISEQ)

    SequencerModel.objects.create(model='NextSeq',
                                  manufacturer=illumina,
                                  data_naming_convention=MISEQ)

    SequencerModel.objects.create(model='MiSeq',
                                  manufacturer=illumina,
                                  data_naming_convention=MISEQ)


def qc_columns(apps, schema_editor):
    QCType = apps.get_model("seqauto", "QCType")
    QCColumn = apps.get_model("seqauto", "QCColumn")

    ILLUMINA_QC = [('mean_cluster_density', 'Mean Cluster Density'),
                   ('mean_pf_cluster_density', 'Mean PF Cluster Density'),
                   ('total_clusters', 'Total Clusters'),
                   ('total_pf_clusters', 'Total PF Clusters'),
                   ('percentage_of_clusters_pf', 'Percentage of Clusters PF'),
                   ('aligned_to_phix', 'Aligned To PhiX')]

    illumina_qc = QCType.objects.create(name='IlluminaFlowcellQC',
                                        qc_object_path='bam_file__unaligned_reads__sequencing_sample__sample_sheet__illuminaflowcellqc')

    for field, name in ILLUMINA_QC:
        QCColumn.objects.create(qc_type=illumina_qc,
                                name=name,
                                field=field)

    FASTQC = [('total_sequences', 'Total Sequences'),
              ('filtered_sequences', 'Filtered Sequences'),
              ('gc', 'GC %')]

    fastqc1 = QCType.objects.create(name='Read_1_FastQC',
                                    qc_object_path='bam_file__unaligned_reads__fastq_r1__fastqc')

    fastqc2 = QCType.objects.create(name='Read_2_FastQC',
                                    qc_object_path='bam_file__unaligned_reads__fastq_r2__fastqc')

    for fastqc in [fastqc1, fastqc2]:
        for field, name in FASTQC:
            QCColumn.objects.create(qc_type=fastqc,
                                    name=name,
                                    field=field)

    FLAGSTATS = [('total', 'Total'),
                 ('read1', 'Read1'),
                 ('read2', 'Read2'),
                 ('mapped', 'Mapped'),
                 ('properly_paired', 'Properly Paired')]

    flagstats = QCType.objects.create(name='Flagstats',
                                      qc_object_path='bam_file__flagstats',
                                      total_field='total')

    for field, name in FLAGSTATS:
        QCColumn.objects.create(qc_type=flagstats,
                                name=name,
                                field=field)

    EXEC_SUMMARY_QC = [('percent_20x', 'Percent of bases >20x'),
                       ('percent_10x', 'Percent of bases >10x'),
                       ('mean_coverage_across_genes', 'Mean coverage across genes'),
                       ('mean_coverage_across_kit', 'Mean coverage across kit'),
                       ('uniformity_of_coverage', 'Uniformity of coverage'),
                       ('percent_read_enrichment', 'Read enrichment (%)'),
                       ('duplicated_alignable_reads', 'Duplicated alignable reads'),
                       ('median_insert', 'Median insert'),
                       ('ts_to_tv_ratio', 'Ts/TV ratio'),
                       ('number_snps', 'Number SNPs'),
                       ('snp_dbsnp_percent', 'SNPs in dbSNP (%)'),
                       ('number_indels', 'Number of indels'),
                       ('indels_dbsnp_percent', 'Indels in dbSNP (%)')]

    exec_summary_qc = QCType.objects.create(name='ExecSummaryQC',
                                            qc_object_path='qcexecsummary')

    for field, name in EXEC_SUMMARY_QC:
        QCColumn.objects.create(qc_type=exec_summary_qc, name=name, field=field)


class Migration(migrations.Migration):
    dependencies = [
        ('seqauto', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_sequencers),
        migrations.RunPython(qc_columns),
    ]
# Generated by Django 3.1 on 2020-09-29 06:11

from django.db import migrations


def create_sequencers(apps, schema_editor):
    HISEQ = 'H'
    MISEQ = 'M'

    Manufacturer = apps.get_model("snpdb", "Manufacturer")
    illumina = Manufacturer.objects.create(name="Illumina")
    Manufacturer.objects.create(name="Ion Torrent")
    Manufacturer.objects.create(name="PacBio")

    SequencerModel = apps.get_model("seqauto", "SequencerModel")
    SequencerModel.objects.create(model='HiSeq 2500',
                                  manufacturer=illumina,
                                  data_naming_convention=HISEQ)

    SequencerModel.objects.create(model='NextSeq',
                                  manufacturer=illumina,
                                  data_naming_convention=MISEQ)

    SequencerModel.objects.create(model='MiSeq',
                                  manufacturer=illumina,
                                  data_naming_convention=MISEQ)


def qc_columns(apps, schema_editor):
    QCType = apps.get_model("seqauto", "QCType")
    QCColumn = apps.get_model("seqauto", "QCColumn")

    ILLUMINA_QC = [('mean_cluster_density', 'Mean Cluster Density'),
                   ('mean_pf_cluster_density', 'Mean PF Cluster Density'),
                   ('total_clusters', 'Total Clusters'),
                   ('total_pf_clusters', 'Total PF Clusters'),
                   ('percentage_of_clusters_pf', 'Percentage of Clusters PF'),
                   ('aligned_to_phix', 'Aligned To PhiX')]

    illumina_qc = QCType.objects.create(name='IlluminaFlowcellQC',
                                        qc_object_path='bam_file__unaligned_reads__sequencing_sample__sample_sheet__illuminaflowcellqc')

    for field, name in ILLUMINA_QC:
        QCColumn.objects.create(qc_type=illumina_qc,
                                name=name,
                                field=field)

    FASTQC = [('total_sequences', 'Total Sequences'),
              ('filtered_sequences', 'Filtered Sequences'),
              ('gc', 'GC %')]

    fastqc1 = QCType.objects.create(name='Read_1_FastQC',
                                    qc_object_path='bam_file__unaligned_reads__fastq_r1__fastqc')

    fastqc2 = QCType.objects.create(name='Read_2_FastQC',
                                    qc_object_path='bam_file__unaligned_reads__fastq_r2__fastqc')

    for fastqc in [fastqc1, fastqc2]:
        for field, name in FASTQC:
            QCColumn.objects.create(qc_type=fastqc,
                                    name=name,
                                    field=field)

    FLAGSTATS = [('total', 'Total'),
                 ('read1', 'Read1'),
                 ('read2', 'Read2'),
                 ('mapped', 'Mapped'),
                 ('properly_paired', 'Properly Paired')]

    flagstats = QCType.objects.create(name='Flagstats',
                                      qc_object_path='bam_file__flagstats',
                                      total_field='total')

    for field, name in FLAGSTATS:
        QCColumn.objects.create(qc_type=flagstats,
                                name=name,
                                field=field)

    EXEC_SUMMARY_QC = [('percent_20x', 'Percent of bases >20x'),
                       ('percent_10x', 'Percent of bases >10x'),
                       ('mean_coverage_across_genes', 'Mean coverage across genes'),
                       ('mean_coverage_across_kit', 'Mean coverage across kit'),
                       ('uniformity_of_coverage', 'Uniformity of coverage'),
                       ('percent_read_enrichment', 'Read enrichment (%)'),
                       ('duplicated_alignable_reads', 'Duplicated alignable reads'),
                       ('median_insert', 'Median insert'),
                       ('ts_to_tv_ratio', 'Ts/TV ratio'),
                       ('number_snps', 'Number SNPs'),
                       ('snp_dbsnp_percent', 'SNPs in dbSNP (%)'),
                       ('number_indels', 'Number of indels'),
                       ('indels_dbsnp_percent', 'Indels in dbSNP (%)')]

    exec_summary_qc = QCType.objects.create(name='ExecSummaryQC',
                                            qc_object_path='qcexecsummary')

    for field, name in EXEC_SUMMARY_QC:
        QCColumn.objects.create(qc_type=exec_summary_qc, name=name, field=field)


class Migration(migrations.Migration):
    dependencies = [
        ('seqauto', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_sequencers),
        migrations.RunPython(qc_columns),
    ]
# Generated by Django 3.1 on 2020-09-29 06:11