annotate baseline/script_imgt.py @ 4:5ffd52fc35c4 draft

Uploaded
author davidvanzessen
date Mon, 12 Dec 2016 05:22:37 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
1 #import xlrd #avoid dep
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
2 import argparse
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
3 import re
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
4
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
5 parser = argparse.ArgumentParser()
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
6 parser.add_argument("--input", help="Excel input file containing one or more sheets where column G has the gene annotation, H has the sequence id and J has the sequence")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
7 parser.add_argument("--ref", help="Reference file")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
8 parser.add_argument("--output", help="Output file")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
9 parser.add_argument("--id", help="ID to be used at the '>>>' line in the output")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
10
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
11 args = parser.parse_args()
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
12
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
13 refdic = dict()
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
14 with open(args.ref, 'r') as ref:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
15 currentSeq = ""
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
16 currentId = ""
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
17 for line in ref:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
18 if line[0] is ">":
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
19 if currentSeq is not "" and currentId is not "":
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
20 refdic[currentId[1:]] = currentSeq
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
21 currentId = line.rstrip()
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
22 currentSeq = ""
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
23 else:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
24 currentSeq += line.rstrip()
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
25 refdic[currentId[1:]] = currentSeq
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
26
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
27
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
28 vPattern = [r"(IGHV[0-9]-[0-9ab]+-?[0-9]?D?\*\d{1,2})"]#,
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
29 # r"(TRBV[0-9]{1,2}-?[0-9]?-?[123]?)",
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
30 # r"(IGKV[0-3]D?-[0-9]{1,2})",
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
31 # r"(IGLV[0-9]-[0-9]{1,2})",
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
32 # r"(TRAV[0-9]{1,2}(-[1-46])?(/DV[45678])?)",
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
33 # r"(TRGV[234589])",
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
34 # r"(TRDV[1-3])"]
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
35
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
36 #vPattern = re.compile(r"|".join(vPattern))
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
37 vPattern = re.compile("|".join(vPattern))
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
38
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
39 def filterGene(s, pattern):
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
40 if type(s) is not str:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
41 return None
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
42 res = pattern.search(s)
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
43 if res:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
44 return res.group(0)
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
45 return None
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
46
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
47
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
48
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
49 currentSeq = ""
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
50 currentId = ""
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
51 first=True
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
52 with open(args.input, 'r') as i:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
53 with open(args.output, 'a') as o:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
54 o.write(">>>" + args.id + "\n")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
55 outputdic = dict()
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
56 for line in i:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
57 if first:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
58 first = False
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
59 continue
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
60 linesplt = line.split("\t")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
61 ref = filterGene(linesplt[1], vPattern)
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
62 if not ref or not linesplt[2].rstrip():
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
63 continue
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
64 if ref in outputdic:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
65 outputdic[ref] += [(linesplt[0].replace(">", ""), linesplt[2].replace(">", "").rstrip())]
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
66 else:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
67 outputdic[ref] = [(linesplt[0].replace(">", ""), linesplt[2].replace(">", "").rstrip())]
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
68 #print outputdic
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
69
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
70 for k in outputdic.keys():
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
71 if k in refdic:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
72 o.write(">>" + k + "\n")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
73 o.write(refdic[k] + "\n")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
74 for seq in outputdic[k]:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
75 #print seq
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
76 o.write(">" + seq[0] + "\n")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
77 o.write(seq[1] + "\n")
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
78 else:
5ffd52fc35c4 Uploaded
davidvanzessen
parents:
diff changeset
79 print k + " not in reference, skipping " + k