File size: 3,446 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 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
def find_missing_info(seed_alignment_dir: str,
tree_dir: str):
"""
find if pfams are missing either tree or MSA seed alignment files
inputs:
-------
- seed_alignment_dir: contains seed alignments
- tree_dir: contains trees
returns:
--------
"""
pfams_in_msas = set( [f.replace('.seed','') for f in os.listdir(seed_alignment_dir)
if f.startswith('PF') and f.endswith('.seed')] )
pfams_in_trees = set( [f.replace('.tree','') for f in os.listdir(tree_dir)
if f.startswith('PF') and f.endswith('.tree')] )
missing_trees = pfams_in_msas - pfams_in_trees
missing_msas = pfams_in_trees - pfams_in_msas
return list(missing_trees), list(missing_msas)
def rename_file_in_place(filename: str,
in_dir:str,
prefix: str):
"""
rename a file in place
inputs:
-------
- filename: name of file (usually pfam.seed or pfam.tree)
- in_dir: name of folder
- prefix: the temporary prefix
returns:
--------
(None)
outputs:
--------
- renames file in place
"""
os.rename(f'./{in_dir}/{prefix}_{filename}',
f'./{in_dir}/{filename}')
def move_file_to_originals(filename: str,
in_dir: str):
"""
move the file from in_dir to in_dir/originals
inputs:
-------
- filename: name of file (usually pfam.seed or pfam.tree)
- in_dir: name of folder
returns:
--------
(None)
outputs:
--------
- new directory at {in_dir}/originals
"""
if filename not in os.listdir(f'{in_dir}/originals'):
os.rename(f'./{in_dir}/{filename}',
f'./{in_dir}/originals/{filename}')
def make_sub_folder(in_dir, sub_folder):
"""
try making a folder inside in_dir
inputs:
-------
- in_dir: name of directory to contain sub_folder
- sub_folder: new sub_folder
returns:
--------
(None)
outputs:
--------
- new directory at {in_dir}/{sub_folder}
"""
if sub_folder not in os.listdir(in_dir):
os.mkdir(f'{in_dir}/{sub_folder}')
def make_orig_folder(in_dir: str):
make_sub_folder(in_dir = in_dir,
sub_folder = 'originals')
def msa_dimensions(pfam_seed_file: str):
"""
Open a pfam seed file and count the width and depth
Doesn't depend on info in given PFam annotation line, since sequences
could be removed during processing
inputs:
-------
- pfam_seed_file: the single Pfam seed file to split
returns:
--------
- out_dict: dictionary of MSA dimensions
"""
pfam_name = pfam_seed_file.split('/')[-1].replace('.seed','')
msa_width = -1
num_seqs = 0
with open(pfam_seed_file, 'r', encoding='latin') as f:
for line in f:
if not line.startswith('#'):
num_seqs += 1
if msa_width == -1:
msa_width = len( line.strip().split()[-1] )
out_dict = {'name': pfam_name,
'num_seqs': num_seqs,
'msa_width': msa_width}
return out_dict
|