#!/usr/bin/env python3

from sys import argv

seqns = dict()
weights = []
pIs = []
out = []

input_file = str(argv[1])
output_file = str(argv[2])

with open(input_file, 'rt') as arch:
    info = arch.read().split('\n')

for line in info:
    if line.startswith('PEPSTATS'):
        name = line.split(' ')[2]
    elif line.startswith('Molecular weight'):
        weight = float(line.split(' ')[3])/1000
        weights.append(weight)
    elif line.startswith('Isoelectric Point'):
        pI = float(line.split(' ')[3])
        pIs.append(pI)
        seqns[name] = (weight, pI)

print(f'Молекулярная масса принимает значения от {min(weights):.01f} кДа до {max(weights):.01f} кДа')
print(f'pI принимает значения от {min(pIs):.02f} до {max(pIs):.02f}')
weight_lims = list(map(float, input('Границы рассматриваемого диапазона молекулярных весов (кДа): ').split()))
pI_lims = list(map(float, input('Границы рассматриваемого диапазона pI: ').split()))

for name in seqns:
    if max(weight_lims) >= seqns[name][0] >= min(weight_lims) and max(pI_lims) >= seqns[name][1] >= min(pI_lims):
        out.append(name)

print(f'Условиям удовлетворяют {len(out)} белков')

with open(output_file, 'w') as output:
    for n in out:
        output.write(f'{n}\t{seqns[n][0]:.01f}\t{seqns[n][1]:.02f}\n')