annotate baseline/script_imgt.py @ 77:58d2377b507d draft

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