from lungmap_client import lungmap_utils
import django
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lap.settings")
django.setup()

from analytics import models


image_sets = lungmap_utils.get_image_set_candidates()

for key, value in image_sets.items():
    print('loading image set ', key)

    image_set = models.ImageSet.objects.get_or_create(
        image_set_name=key,
        magnification=value['magnification'],
        species=value['species'],
        development_stage=value['development_stage']
    )

    for image in value['images']:
        experiment, experiment_create = models.Experiment.objects.get_or_create(
            experiment_id=image['image_id'].split('_')[0]
        )

        image_object = models.Image.objects.get_or_create(
            s3key=image['s3key'],
            image_name=image['image_name'],
            image_id=image['image_id'],
            x_scaling=image['x_scaling'],
            y_scaling=image['y_scaling'],
            image_set=image_set[0],
            experiment_id=experiment.experiment_id
        )

    for p in value['probes']:
        probe_object, probe_object_create = models.Probe.objects.get_or_create(
            label=p['probe_label'].strip()
        )

        image_set_probe_map = models.ImageSetProbeMap.objects.get_or_create(
            color=p['color'],
            probe=probe_object,
            image_set=image_set[0]
        )

        for exp in value['experiments']:
            experiment_object = models.Experiment.objects.get(
                experiment_id=exp
            )

            experiment_probe_map = models.ExperimentProbeMap.objects.create(
                color=p['color'],
                experiment_id=experiment_object,
                probe=probe_object,
            )


# need to fill in with actual data from ontology
def add_probe_anatomy(probe, class_name):
    """
    Add a probe/classification combo, this can be either cell or stucture as identified
    by the class_type
    :param probe: str: a probe name
    :param class_name: str: a classficiation name
    :return:
    """
    probe_instance = models.Probe.objects.get(label=probe)
    anatomy = models.Anatomy.objects.create(name=class_name)

    models.AnatomyProbeMap.objects.create(
        probe=probe_instance,
        anatomy=anatomy
    )

add_probe_anatomy('Sox2', 'bronchiolar_epithelial_cell')
add_probe_anatomy('TTF-1', 'epithelial_cell_of_the_lung')
add_probe_anatomy('α-Smooth Muscle Actin', 'bronchiolar-associated_smooth_muscle_cell')
add_probe_anatomy('α-Smooth Muscle Actin', 'bronchiole')
add_probe_anatomy('Sox9', 'distal_acinar_tubule')
add_probe_anatomy('α-Smooth Muscle Actin', 'proximal_acinar_tubule')
add_probe_anatomy('α-Smooth Muscle Actin', 'blood_vessel')

@@ -676,31 +676,82 @@
 ge%5B'
-image_id'%5D.split('_')%5B0
+experiment_id'%5D,%0A            experiment_type_id=image%5B'experiment_type_id'
 %5D%0A  
@@ -1604,16 +1604,33 @@
 t_id=exp
+%5B'experiment_id'%5D
 %0A       
@@ -2015,16 +2015,17 @@
 ll or st
+r
 ucture a
@@ -2134,12 +2134,12 @@
 lass
+i
 fic
-i
 atio
@@ -2162,24 +2162,37 @@
 rn:%0A    %22%22%22%0A
+    try:%0A    
     probe_in
@@ -2238,16 +2238,67 @@
 =probe)%0A
+    except models.Probe.DoesNotExist:%0A        pass%0A
     anat
@@ -2474,16 +2474,21 @@
 natomy('
+Anti-
 Sox2', '
@@ -2540,11 +2540,17 @@
 my('
-TTF
+Anti-Nkx2
 -1',
@@ -2600,37 +2600,26 @@
 natomy('
-%CE%B1-Smooth Muscle Actin
+Anti-Acta2
 ', 'bron
@@ -2677,37 +2677,26 @@
 natomy('
-%CE%B1-Smooth Muscle Actin
+Anti-Acta2
 ', 'bron
@@ -2723,16 +2723,21 @@
 natomy('
+Anti-
 Sox9', '
@@ -2778,37 +2778,26 @@
 natomy('
-%CE%B1-Smooth Muscle Actin
+Anti-Acta2
 ', 'prox
@@ -2840,29 +2840,18 @@
 my('
-%CE%B1-Smooth Muscle Actin
+Anti-Acta2
 ', '

update preloading script