annotate imgt_locus_split.py @ 0:b00c257f0a67 draft

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