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