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