import re


def check_if_border(feature, operon_borders):
    """Return start/end coordinate of the SeqFeature if its accession is the first/last in the given tuple.

    Parameters
    ----------
    feature : SeqFeature
        Directory with input genbank files.
    operon_borders : str
        Directory to store the output.
        
    Returns
    -------
    tuple(str, int)
        The first value is either 'end' or 'start'.
        The second value is the corresponding coordinate.
        
        If SeqFeature's ID is not in the given tuple, returns None. 
    
    """
    prot_id_regexp = re.compile('[A-Z]{2}_[0-9]+\.[0-9]')
        
    start_id = operon_borders[0]
    end_id = operon_borders[-1]
    
    if 'protein_id' in feature.qualifiers:
                    
        if feature.qualifiers['protein_id'][0]  == start_id:
            return ('start', feature.location.start + 1) # genbank is 1-based, python is 0-based
                           
        if feature.qualifiers['protein_id'][0]  == end_id:
            return ('end', int(feature.location.end))
                
    elif 'pseudo' in feature.qualifiers:        
        if 'inference' in feature.qualifiers:
                        
            inference_prot_id_search = prot_id_regexp.search(feature.qualifiers['inference'][0])
                        
            if inference_prot_id_search is not None:
                            
                inference_prot_id = inference_prot_id_search.group(0)
                            
                if inference_prot_id  == start_id:
                    return('start', feature.location.start + 1)
                        
                if inference_prot_id  == end_id:
                    return ('end', int(feature.location.end))
                    
                else:
                    if feature.qualifiers['locus_tag'][0]  == start_id:
                        return ('start', feature.location.start + 1)

                    if feature.qualifiers['locus_tag'][0] == end_id:
                        return ('end', int(feature.location.end))
                    
    return None