package org.opencb.bionetdb.core.utils.converters;

import org.neo4j.driver.v1.Record;
import org.opencb.biodata.models.variant.Variant;
import org.opencb.biodata.models.variant.VariantBuilder;
import org.opencb.biodata.models.variant.avro.VariantType;
import org.opencb.biodata.tools.Converter;
import org.opencb.bionetdb.core.utils.NodeBuilder;

import java.util.HashMap;
import java.util.Map;

public class RecordToVariantConverter implements Converter<Record, Variant> {

    @Override
    public Variant convert(Record record) {
        VariantBuilder variantBuilder = Variant.newBuilder();

        if (!record.get(NodeBuilder.CHROMOSOME).isNull()) {
            variantBuilder.setChromosome(record.get(NodeBuilder.CHROMOSOME).asString());
        }

        if (!record.get(NodeBuilder.START).isNull()) {
            variantBuilder.setStart(Integer.parseInt(record.get(NodeBuilder.START).asString()));
        }

        if (!record.get(NodeBuilder.END).isNull()) {
            variantBuilder.setStart(Integer.parseInt(record.get(NodeBuilder.END).asString()));
        }

        if (!record.get(NodeBuilder.REFERENCE).isNull()) {
            variantBuilder.setReference(record.get(NodeBuilder.REFERENCE).asString());
        }

        if (!record.get(NodeBuilder.ALTERNATE).isNull()) {
            variantBuilder.setAlternate(record.get(NodeBuilder.ALTERNATE).asString());
        }

        if (!record.get(NodeBuilder.TYPE).isNull()) {
            variantBuilder.setType(VariantType.valueOf(record.get(NodeBuilder.TYPE).asString()));
        }

        variantBuilder.setStudyId("S");
        variantBuilder.setFormat("GT");

        Map<String, String> systemMap = new HashMap<>();
        if (!record.get(NodeBuilder.TARGET_PROTEIN).isNull()) {
            systemMap.put(NodeBuilder.TARGET_PROTEIN, record.get(NodeBuilder.TARGET_PROTEIN).asString());
        }
        if (!record.get(NodeBuilder.COMPLEX).isNull()) {
            systemMap.put(NodeBuilder.COMPLEX, record.get(NodeBuilder.COMPLEX).asString());
        }
        if (!record.get(NodeBuilder.REACTION).isNull()) {
            systemMap.put(NodeBuilder.REACTION, record.get(NodeBuilder.REACTION).asString());
        }
        if (!record.get(NodeBuilder.PANEL_PROTEIN).isNull()) {
            systemMap.put(NodeBuilder.PANEL_PROTEIN, record.get(NodeBuilder.PANEL_PROTEIN).asString());
        }
        if (!record.get(NodeBuilder.PANEL_GENE).isNull()) {
            systemMap.put(NodeBuilder.PANEL_GENE, record.get(NodeBuilder.PANEL_GENE).asString());
        }
        variantBuilder.setAttributes(systemMap);

        return variantBuilder.build();
    }
}

@@ -183,24 +183,156 @@
 antBuilder;%0A
+import org.opencb.biodata.models.variant.avro.AdditionalAttribute;%0Aimport org.opencb.biodata.models.variant.avro.VariantAnnotation;%0A
 import org.o
@@ -1758,24 +1758,146 @@
 mat(%22GT%22);%0A%0A
+        Variant variant = variantBuilder.build();%0A%0A        // Add protein system info in annotation additional attributes%0A
         Map%3C
@@ -2787,24 +2787,25 @@
       %7D%0A
+%0A
         
 variantB
@@ -2792,38 +2792,120 @@
  %7D%0A%0A        
-variantBuilder
+AdditionalAttribute additionalAttribute = new AdditionalAttribute();%0A        additionalAttribute
 .setAttribut
@@ -2905,17 +2905,16 @@
 ttribute
-s
 (systemM
@@ -2931,37 +2931,372 @@
     
-return variantBuilder.build()
+Map%3CString, AdditionalAttribute%3E additionalAttributes = new HashMap%3C%3E();%0A        additionalAttributes.put(%22proteinSystem%22, additionalAttribute);%0A%0A        VariantAnnotation variantAnnotation = new VariantAnnotation();%0A        variantAnnotation.setAdditionalAttributes(additionalAttributes);%0A%0A        variant.setAnnotation(variantAnnotation);%0A%0A        return variant
 ;%0A  

store protein system info in annotation additional attributes