annotate baseline/script_imgt.py @ 81:b6f9a640e098 draft

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