annotate imgt_locus_split.py @ 3:9aa036caa8e9 draft default tip

Uploaded
author davidvanzessen
date Tue, 05 Mar 2019 05:27:37 -0500
parents 4bb8f6523130
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
1 import argparse
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
2 import logging
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
3 import os
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
4 import re
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
5 import shutil
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
6 import sys
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
7 import tarfile
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
8 import tempfile
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
9 import zipfile
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
10
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
11 import magic
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
12
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
13 imgt_file_regex = re.compile("^\d+_[^P]")
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
14
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
15
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
16 def sniff_imgt_type(input_file):
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
17 file_type = magic.from_file(input_file)
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
18 logging.debug("File type of {0} is {1}".format(input_file, file_type))
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
19 return file_type
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
20
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
21
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
22 def unpack_imgt_zip(input_file, output_dir):
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
23 imgt_type = sniff_imgt_type(input_file)
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
24 if imgt_type.startswith("Zip"):
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
25 with zipfile.ZipFile(input_file) as inf:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
26 inf.extractall(output_dir)
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
27 elif imgt_type.startswith("XZ"):
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
28 with tarfile.open(input_file) as inf:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
29 inf.extractall(output_dir)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
30 else:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
31 raise IOError("Unsuppported file type: {0}".format(imgt_type))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
32 logging.debug("Extracted {0} to {1}".format(input_file, output_dir))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
33 check = os.listdir(output_dir)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
34 if len(check) == 1:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
35 check = os.path.join(output_dir, check[0])
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
36 if os.path.isdir(check):
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
37 logging.info("{0} is an older IMGT zip, removing extra dir".format(input_file))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
38 files = os.listdir(check)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
39 for file in files:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
40 new_file = os.path.join(output_dir, file)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
41 file = os.path.join(check, file)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
42 shutil.move(file, new_file)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
43 shutil.rmtree(check)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
44
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
45
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
46 def filter_tabular_file(old_file, new_file, column, regex):
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
47 logging.debug("Filtering {0} with {1}".format(old_file, regex.pattern))
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
48 first = True
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
49 total = 0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
50 remain = 0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
51 with open(old_file, 'r') as of, open(new_file, 'w') as nf:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
52 column_index = -1
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
53 for line in of:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
54 splt = line.split("\t")
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
55 if first:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
56 column_index = splt.index(column)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
57 first = False
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
58 nf.write(line)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
59 continue
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
60 total += 1
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
61 if len(splt) >= column_index and regex.search(splt[column_index]):
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
62 remain += 1
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
63 nf.write(line)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
64 return total, remain
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
65
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
66
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
67 def all_same_in_list(l):
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
68 return all(l[0] == x for x in l[1:])
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
69
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
70
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
71 def filter_imgt_dir(imgt_dir, loci):
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
72 logging.info("Filtering {0} with {1}".format(imgt_dir, loci))
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
73 imgt_files = [f for f in os.listdir(imgt_dir) if imgt_file_regex.match(f)]
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
74 tmp_file = os.path.join(imgt_dir, "tmp.txt")
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
75 totals = []
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
76 remains = []
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
77 loci_regex = re.compile("|".join(loci))
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
78 for imgt_file in imgt_files:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
79 imgt_file = os.path.join(imgt_dir, imgt_file)
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
80 total, remain = filter_tabular_file(imgt_file, tmp_file, "V-GENE and allele", loci_regex)
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
81 totals.append(total)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
82 remains.append(remain)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
83 logging.debug("{0} rows, {1} after filtering".format(total, remain))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
84 shutil.move(tmp_file, imgt_file)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
85 if not (all_same_in_list(totals) and all_same_in_list(remains)):
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
86 logging.warning("Not all files had the same number of sequences remaining for {0}: {1}".format(imgt_dir, remains))
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
87 return totals[0], remains[0]
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
88
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
89
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
90 def make_new_xz_file(input_dir, output_file):
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
91 logging.info("Creating new IMGT zip for {0} at {1}".format(input_dir, output_file))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
92 imgt_files = [f for f in os.listdir(input_dir)]
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
93 with tarfile.open(output_file, 'w:xz') as out:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
94 for imgt_file in imgt_files:
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
95 logging.debug("Writing {0} to new IMGT zip".format(imgt_file))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
96 imgt_file = os.path.join(input_dir, imgt_file)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
97 out.add(imgt_file, arcname=os.path.basename(imgt_file))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
98
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
99
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
100 def main():
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
101 parser = argparse.ArgumentParser()
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
102 parser.add_argument("-i", "--input", help="The input IMGT file", required=True)
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
103 parser.add_argument("-l", "--loci", help="The Loci to filter on", required=True)
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
104 parser.add_argument("-o", "--output", help="The output file for the new IMGT zip with just the filtered sequences", required=True)
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
105
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
106 logging.basicConfig(filename="./log.html", level=logging.DEBUG, format="%(asctime)s:&emsp;%(message)s <br />",
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
107 datefmt='%Y/%m/%d %H:%M:%S')
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
108 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
109 logging.info("Started IMGT locus split")
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
110
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
111 args = parser.parse_args()
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
112 input_file = args.input
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
113 loci = args.loci.split(",")
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
114 output_file = args.output
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
115
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
116 logging.debug("All Parameters:")
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
117 logging.debug("Input: {0}".format(input_file))
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
118 logging.debug("Loci: {0}".format(loci))
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
119 logging.debug("Output: {0}".format(output_file))
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
120
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
121 if len(loci) == 0:
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
122 raise Exception("No locus selected, nothing to do")
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
123
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
124 work_dir = tempfile.mkdtemp()
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
125 original_files_dir = os.path.join(work_dir, "original")
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
126 os.mkdir(original_files_dir)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
127
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
128 unpack_imgt_zip(input_file, original_files_dir)
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
129
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
130 total, remain = filter_imgt_dir(original_files_dir, loci)
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
131 logging.info("{0}\t{1}".format(total, remain))
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
132
2
4bb8f6523130 Uploaded
davidvanzessen
parents: 1
diff changeset
133 make_new_xz_file(original_files_dir, output_file)
0
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
134
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
135
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
136 if __name__ == "__main__":
b00c257f0a67 Uploaded
davidvanzessen
parents:
diff changeset
137 main()