annotate baseline/script_xlsx.py @ 7:ad9be244b104 draft

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