Rearranging Folders
[arvados-tutorial.git] / src / annotation / new / generatereport.py
1 import numpy as np
2 import scipy as scipy
3 import pandas as pd
4 import io
5 import argparse
6
7 def generatereport():
8
9     parser = argparse.ArgumentParser()
10     parser.add_argument('txtfilename', metavar='VCF2TXTFILENAME', help='text file of info to annotate')
11     parser.add_argument('samplename', metavar='SAMPLENAME', help='name of sample to use on report')
12     parser.add_argument('headfile', metavar='REPORTHEADHTML', help='head html for report')
13     parser.add_argument('tailfile', metavar='REPORTTAILHTML', help='tail html for report')
14     args = parser.parse_args()
15
16     pd.set_option("display.max_colwidth", 10000)
17
18 #    filename = "reportdata.txt"
19 #    samplename = "hu34D5B9_var-GS000015891-ASM"
20 #    headfile = "head.html"
21 #    tailfile = "tail.html"
22
23     filename = args.txtfilename
24     samplename = args.samplename
25     headfile = args.headfile
26     tailfile = args.tailfile
27
28     # reading data into dataframe
29     headerlist = ["Variant ID", "Chromosome", "Position", "Ref","Alt","Allele ID", "Clinical Significance","Disease Name","Frequency GO-ESP", "Frequency EXAC", "Frequency 1000 Genomes Project","GT"]
30     reportdata = pd.read_csv(filename,header=0,names=headerlist,sep='\t')
31     reportdata['Zygosity']="."
32
33     # defining zygosity
34     idxHOM1 = reportdata.GT=='1|1' 
35     idxHOM2 = reportdata.GT=='1/1'
36     idxHET1 = reportdata.GT=='1|0'
37     idxHET2 = reportdata.GT=='1/0'
38     idxHET3 = reportdata.GT=='0|1'
39     idxHET4 = reportdata.GT=='0/1'
40     idxHOM = idxHOM1 | idxHOM2
41     idxHET = idxHET1 | idxHET2 | idxHET3 | idxHET4
42     reportdata.Zygosity[idxHOM]='HOM'
43     reportdata.Zygosity[idxHET]='HET'
44
45     # creating url from variant ID
46     reportdata['URL'] = '<a href="http://www.mywebsite.com/about.html">About</a>'
47     reportdata.to_json('test.json',orient='records')
48     str_io = io.StringIO()
49
50     # creating html table from dataframe
51     reportdatasub = reportdata[["Variant ID", "Allele ID", "Clinical Significance","Disease Name", "Frequency EXAC", "Frequency 1000 Genomes Project","Zygosity","URL"]]
52     reportdatasub.to_html(buf=str_io, classes='table table-bordered',index_names=False,index=False)
53     html_str = str_io.getvalue()
54     html_str_encoded = unicode(html_str).encode('utf8')
55     html_str_encoded = html_str_encoded.replace('&lt;','<')
56     html_str_encoded = html_str_encoded.replace('&gt;','>')
57     html_str_encoded = html_str_encoded.replace('|','<br/>')
58
59     html_file = open(headfile, 'r')
60     source_code_head = html_file.read() 
61     html_file.close()
62
63     html_file = open(tailfile, 'r')
64     source_code_tail = html_file.read()
65     html_file.close()
66
67     # combine html table with head and tail html for total report
68     total_html = source_code_head + html_str_encoded + source_code_tail
69   
70     # write out report html
71     f = open(samplename+'.html','wb')
72     f.write(total_html)
73     f.close()
74
75 if __name__ == '__main__':
76     generatereport()