ann = response.split('''FT   METAL           ''' +
                                 str(annotation) + '''
FT                   /note="''')[1].split(' ')[0].strip()
            ann = re.compile('[^a-zA-Z]').sub('', ann).replace('FT',
                                                               "").replace(
                                                                   '"', '')
            if annotation not in annotation_dict[ann]:
                annotation_dict[ann].append(annotation)

        except:
            with open(SEQ_DIR + "error.txt", "a") as f:
                print("error: " + uniprot_id + "\n")

    sequence = response.strip()

    sequence = sequence[(sequence.rindex(';') + 1):(-2)].strip().replace(
        ' ', '')

    sequences = ''
    for key, value in annotation_dict.items():
        print(key)
        with open(SEQ_DIR + "sequences/" + key + ".fasta", "a") as file_handle:
            sequence = sequence.replace("\n", '')
            sequences = aa.insert_annotations(sequence, value, '#')
            sequences = textwrap.fill(sequences, 60)
            if len(sequences) > 0:
                print('>' + uniprot_id + "\n")
                print(sequences + "\n")
                # file_handle.write('>'+uniprot_id +"\n")
                # file_handle.write(sequences+"\n")
                except IndexError:
                    print("nothing")

            if ('SEQRES') in records[i]:
                sequences += records[i]

aa_acid = aa.sort_aa_dict(annotations)
seq_rec = sequences.split('\n')   

for keys, values in aa_acid.items():
    chain = keys.split('_')
    print(pdb_id+'_'+chain[1]+':')
    seq = ""
    for line in seq_rec:
        seq_info = line.split()
        if len(seq_info) > 0:
            if seq_info[2] == chain[1].strip():
                seq = seq+(aa.aa_residue_list(' '.join(seq_info[4:]).strip()))
    if len(seq) > 0:
        seq = textwrap.fill(seq,60)
        position = [int(x.split('_')[2]) for x in values]
        position.sort()
        if not os.path.exists(SEQ_DIR):
            os.mkdir(SEQ_DIR)
        with open("../data/sequences/"+chain[0]+'.fasta',"a") as filehandle:
            seq = aa.insert_annotations(seq, position,'#')
            filehandle.write(pdb_id+'_'+chain[0]+'_'+chain[1]+':'+'\n'+seq+'\n')
        
        print((values))