#!/usr/bin/python3
'''
Extract inparalogs from given species.
Pipeline usage: SETINPAR
'''
from __future__ import print_function

def read_data(filename,species):
    with open(filename) as fh:
        for line in fh.readlines():
            line = line.strip().rstrip('\n')
            if len(line)>0 and line[0] not in '#':
                elems = line.split()
                if not len(elems)==2: continue
                spc0=elems[0].split(':')
                spc1=elems[1].split(':')
                selelem=None
                inpars=""
                if spc0[0] == species:
                    selelem=elems[0]
                    inpars=spc0[2]
                elif spc1[0] == species:
                    selelem=elems[1]
                    inpars=spc1[2]
                if selelem is None: continue
                if inpars == "{}": continue
                print( selelem )


import argparse

parser = argparse.ArgumentParser(description='Extract inparalogs for a single species')
parser.add_argument('-f', '--file', dest='filename',
                  help='inparalog file', metavar='FILE', required=True)
parser.add_argument('-s', '--species', dest='species',
                  help='selected species', metavar='string', required=True)

options = parser.parse_args()

read_data(options.filename,options.species)
