annotate baseline/script_xlsx.py @ 67:ba33b94637ca draft

Uploaded
author davidvanzessen
date Tue, 29 Jan 2019 03:54:09 -0500 (2019-01-29)
parents c33d93683a09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
67
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
1 import xlrd
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
2 import argparse
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
3
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
4 parser = argparse.ArgumentParser()
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
5 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: 0
diff changeset
6 parser.add_argument("--ref", help="Reference file")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
7 parser.add_argument("--output", help="Output file")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
8
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
9 args = parser.parse_args()
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
10
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
11 gene_column = 6
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
12 id_column = 7
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
13 seq_column = 8
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
14 LETTERS = [x for x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
15
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
16
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
17 refdic = dict()
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
18 with open(args.ref, 'r') as ref:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
19 currentSeq = ""
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
20 currentId = ""
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
21 for line in ref.readlines():
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
22 if line[0] is ">":
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
23 if currentSeq is not "" and currentId is not "":
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
24 refdic[currentId[1:]] = currentSeq
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
25 currentId = line.rstrip()
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
26 currentSeq = ""
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
27 else:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
28 currentSeq += line.rstrip()
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
29 refdic[currentId[1:]] = currentSeq
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
30
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
31 currentSeq = ""
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
32 currentId = ""
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
33 with xlrd.open_workbook(args.input, 'r') as wb:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
34 with open(args.output, 'a') as o:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
35 for sheet in wb.sheets():
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
36 if sheet.cell(1,gene_column).value.find("IGHV") < 0:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
37 print "Genes not in column " + LETTERS[gene_column] + ", skipping sheet " + sheet.name
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
38 continue
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
39 o.write(">>>" + sheet.name + "\n")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
40 outputdic = dict()
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
41 for rowindex in range(1, sheet.nrows):
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
42 ref = sheet.cell(rowindex, gene_column).value.replace(">", "")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
43 if ref in outputdic:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
44 outputdic[ref] += [(sheet.cell(rowindex, id_column).value.replace(">", ""), sheet.cell(rowindex, seq_column).value)]
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
45 else:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
46 outputdic[ref] = [(sheet.cell(rowindex, id_column).value.replace(">", ""), sheet.cell(rowindex, seq_column).value)]
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
47 #print outputdic
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
48
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
49 for k in outputdic.keys():
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
50 if k in refdic:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
51 o.write(">>" + k + "\n")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
52 o.write(refdic[k] + "\n")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
53 for seq in outputdic[k]:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
54 #print seq
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
55 o.write(">" + seq[0] + "\n")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
56 o.write(seq[1] + "\n")
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
57 else:
ba33b94637ca Uploaded
davidvanzessen
parents: 0
diff changeset
58 print k + " not in reference, skipping " + k