File size: 7,886 Bytes
5032722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import os
from tqdm import tqdm

from utils.utils import (make_orig_folder, 
                   move_file_to_originals, 
                   rename_file_in_place)


def find_repeats(seed_alignment_dir: str):
    """
    find names and sequences of repeats WITHIN and ACROSS seed files
    
    Will iterate through every line of every seed file, so this might
      take a while...
     
    inputs:
    -------
        - seed_alignment_dir (str): where the .seed files are
    
    returns:
    --------
        (None)
    
    outputs:
    --------
        - repeats_ACROSS_families.tsv: names and sequences of repeats across 
                                       multiple pfams
        - repeats_ACROSS_families.tsv: names and sequences of repeats within 
                                       single pfam
    """
    ### a container for all the within-family repeats
    # key = prot seq (ungapped)
    # value = pfam/seqname
    RAW_repeats = {}
    seen_seqs = set()
    
    ### scan all msa files for repeats
    for idx,filename in tqdm(enumerate(os.listdir('seed_alignments'))):            
        if filename.endswith('.seed'):    
            pfam_name = filename.replace('.seed','')
            
            with open(f'./seed_alignments/{filename}','r', 
                      encoding='latin') as f:
                for line in f:
                    if not line.startswith('#'):
                        samp_name, gapped_seq = line.strip().split()
                        seq = gapped_seq.replace('.','')
                        valname = f'{pfam_name}:{samp_name}'
                        
                        # check if ungapped sequence is seen WITHIN this MSA
                        if seq in seen_seqs:
                            RAW_repeats[seq].append(valname)
                        else:
                            RAW_repeats[seq] = [valname]
                            seen_seqs.add(seq)
    
    
    ### remove anything that doesn't repeat
    repeats = {}
    for key, val_lst in RAW_repeats.items():
        if len(val_lst) > 1:
            repeats[key] = val_lst
    
    # remove the raw dict and the intermediate set
    del RAW_repeats, seen_seqs
    
    
    ### keep repeats across families in their own dictionary
    ###   otherwise, place in repeats within familities
    repeats_within_fams = {}
    repeats_across_fams = {}
    
    for key, val_lst in repeats.items():
        pfams_in_lst = set()
        for entry in val_lst:
            pfam, _ = entry.split(':')
            pfams_in_lst.add(pfam)
        
        if len(pfams_in_lst) > 1:
            repeats_across_fams[key] = val_lst
        
        else:
            repeats_within_fams[key] = val_lst
    
    del repeats
    
    
    ### separately output these
    if len(repeats_within_fams) > 0:
        with open('repeats_WITHIN_families.tsv', 'w') as g:
            for key, val_lst in repeats_within_fams.items():
                pfam_name = val_lst[0].split(':')[0]
                val_lst_without_pfam = ';'.join([elem.split(':')[1] for elem in val_lst])
                
                g.write(f'{pfam_name}\t{val_lst_without_pfam}\t{key}\n')
    
    if len(repeats_across_fams) > 0:
        with open('repeats_ACROSS_families.tsv', 'w') as g:
            for key, val_lst in repeats_across_fams.items():
                to_write = ';'.join(val_lst)
                g.write(f'{to_write}\t{key}\n')



def parse_within_families_file():
    """
    read "repeats_WITHIN_families" to figure out which repeats to remove
    
    inputs:
    -------
        (None)
    
    returns:
    --------
        - to_remove_dict: dictionary of pfam values to remove
    """
    # don't do anything if you don't generate this file
    if 'repeats_WITHIN_families.tsv' not in os.listdir():
        return dict()
    
    all_pfams = []
    all_samp_names = []
    with open('repeats_WITHIN_families.tsv','r') as f:
        for line in f:
            pfam, samp_names, _ = line.strip().split('\t')
            all_pfams.append(pfam)
            all_samp_names.append(samp_names.split(';'))
    
    # build dictionary from lists
    to_remove_dict = {}
    for i in range(len(all_pfams)):
        pfam = all_pfams[i]
        all_duplicates = all_samp_names[i]
        
        # only keep the first instance
        remove_samps = all_duplicates[1:]
        
        if pfam in to_remove_dict.keys():
            to_remove_dict[pfam] = to_remove_dict[pfam] + remove_samps
        elif pfam not in to_remove_dict.keys():
            to_remove_dict[pfam] = remove_samps
    
    return to_remove_dict
    
    
    
def parse_across_families_file():
    """
    read "repeats_ACROSS_families" to figure out which repeats to remove
    
    inputs:
    -------
        (None)
    
    returns:
    --------
        - to_remove_dict: dictionary of pfam values to remove
    """
    # don't do anything if you don't generate this file
    if 'repeats_ACROSS_families.tsv' not in os.listdir():
        return dict()
    
    to_remove_dict = {}
    with open('repeats_ACROSS_families.tsv', 'r') as f:
        for line in f:
            line = line.strip().split('\t')[0]
            raw_lst = line.split(';')
            
            # only keep the first instance
            remove_samps = raw_lst[1:]
            
            for entry in remove_samps:
                pfam, sample = entry.split(':')
                
                if pfam not in to_remove_dict.keys():
                    to_remove_dict[pfam] = [sample]
                
                elif pfam in to_remove_dict.keys():
                    to_remove_dict[pfam].append(sample)
        
    return to_remove_dict
    
    
def remove_samples(seed_alignment_dir: str,
                   to_remove_dict: dict):
    """
    given a list of pfams and samples to remove, trim samples from seed files
    
    
    inputs:
    -------
        - seed_alignment_dir (str): where the .seed files are
        - to_remove_dict: samples to remove from every pfam
            > keys: pfam
            > values: list of samples to remove
    
    returns:
    --------
        (None)
    
    outputs:
    --------
        - de-duplicated pfam files, new 'originals' folder
        
    """
    ### quit function, if there's nothing to be done
    if len(to_remove_dict) == 0:
        print('No duplicates found')
        return

    # make a folder to store originals, if it doesn't already exist
    make_orig_folder(in_dir = seed_alignment_dir)

    ### start iterating through
    for pfam, to_remove in tqdm(to_remove_dict.items()):
        to_remove = set(to_remove)
        
        # get the filenames
        msa_file = f'{pfam}.seed'
        assert msa_file in os.listdir(seed_alignment_dir), f'{msa_file} missing!'
        
        # open the msa and remove duplicate sequences
        with open(f'./{seed_alignment_dir}/DEDUPED_{msa_file}','w') as g_new:
            with open(f'./{seed_alignment_dir}/{msa_file}','r') as f_msa:
                for line in f_msa:
                    # filter
                    if line.startswith('#'):
                        g_new.write(line)
                    
                    else:
                        this_samp_name = line.split()[0]
                        if this_samp_name not in to_remove:
                            g_new.write(line)
        
        # move the original file, if it's not already there
        move_file_to_originals(filename = msa_file,
                               in_dir = seed_alignment_dir)
        
        # rename the new version (will overwrite original, if that's left 
        #   in the folder)
        rename_file_in_place(filename = msa_file,
                             in_dir = seed_alignment_dir,
                             prefix = 'DEDUPED')