# -*- coding: utf-8 -*-

"""
Copyright [2009-2017] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import logging
import typing as ty

from rnacentral_pipeline.databases import data

from rnacentral_pipeline.databases.pdb.data import ChainInfo
from rnacentral_pipeline.databases.pdb.data import ReferenceMapping
from rnacentral_pipeline.databases.pdb import helpers

LOGGER = logging.getLogger(__name__)


def as_entry(info: ChainInfo, reference_mapping: ReferenceMapping):
    return data.Entry(
        primary_id=info.pdb_id.upper(),
        accession=info.accession(),
        ncbi_tax_id=helpers.taxid(info),
        database="PDBE",
        sequence=helpers.sequence(info),
        regions=[],
        rna_type=helpers.rna_type(info),
        url=helpers.url(info),
        seq_version="1",
        note_data=helpers.note_data(info),
        product=helpers.product(info),
        optional_id=info.chain_id,
        description=helpers.description(info),
        species=helpers.species(info),
        lineage=helpers.lineage(info),
        parent_accession=info.pdb_id.upper(),
        references=helpers.references_for(info, reference_mapping),
    )


def parse(
    rna_chains: ty.List[ChainInfo],
    reference_mapping: ReferenceMapping,
    override_list: ty.Dict[str, str]
) -> ty.Iterator[data.Entry]:
    disqualified = {"mRNA": 0, "other": 0}
    for chain in rna_chains:
        if chain.pdb_id in override_list.keys() and chain.chain_id in override_list.values():
            LOGGER.debug("Overriding %s", chain)
            pass
        else:
            if helpers.is_mrna(chain):
                LOGGER.debug("Disqualifing %s", chain)
                disqualified["mRNA"] += 1
                continue

            if not helpers.is_ncrna(chain):
                LOGGER.debug("Skipping %s", chain)
                disqualified["other"] += 1
                continue

        try:
            yield as_entry(chain, reference_mapping)
        except helpers.InvalidSequence:
            LOGGER.warn(f"Invalid sequence for {chain}")
    LOGGER.info("Disqualified %i mRNA chains", disqualified["mRNA"])
    LOGGER.info("Disqualified %i non ncRNA chains", disqualified["other"])

@@ -689,17 +689,16 @@
 rt data%0A
-%0A
 from rna
@@ -727,29 +727,24 @@
 ases.pdb
-.data
  import 
 ChainInf
@@ -735,25 +735,23 @@
  import 
-ChainInfo
+helpers
 %0Afrom rn
@@ -793,16 +793,27 @@
 a import
+ ChainInfo,
  Referen
@@ -825,62 +825,8 @@
 ping
-%0Afrom rnacentral_pipeline.databases.pdb import helpers
 %0A%0ALO
@@ -1741,16 +1741,17 @@
 tr, str%5D
+,
 %0A) -%3E ty
@@ -1856,16 +1856,30 @@
      if 
+(%0A            
 chain.pd
@@ -1906,16 +1906,28 @@
 t.keys()
+%0A           
  and cha
@@ -1963,16 +1963,26 @@
 values()
+%0A        )
 :%0A      
@@ -2544,16 +2544,114 @@
 hain%7D%22)%0A
+        except helpers.MissingTypeInfo:%0A            LOGGER.warn(f%22Missing type info for %7Bchain%7D%22)%0A
     LOGG

Handle new exception properly for pdbe