Skip to content

Commit

Permalink
added summary table, users can now interactively pass/fail samples ba…
Browse files Browse the repository at this point in the history
…sed on the displayed graph
  • Loading branch information
nilesh-tawari committed Oct 3, 2017
1 parent 75e4796 commit 648c016
Show file tree
Hide file tree
Showing 10 changed files with 895 additions and 162 deletions.
18 changes: 9 additions & 9 deletions chronqc/chronqc_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main(args):
db = op.abspath(op.join(op.dirname(__file__), 'db','chronqc.annotations.db'))
connection = sqlite3.connect(db)
mycursor = connection.cursor()
curr = mycursor.execute('''SELECT Run, Panel, Annotation from Run_Annotations''')
curr = mycursor.execute('''SELECT Run, Panel, Status, Annotation, GraphID, Date1, Date2 from Run_Annotations''')
anndata = curr.fetchall()
anndata = pd.DataFrame(anndata)
anndata.rename(columns={0: "Run", 1: "Panel", 2: "Annotation"}, inplace=True)
Expand All @@ -39,7 +39,7 @@ def main(args):
connection.commit()
datedata = pd.DataFrame(datedata)
datedata.rename(columns={0: "Annotated_date", 1: "Notes", 2: "Panel"}, inplace=True)
datedata["Annotated_date"] = pd.to_datetime(datedata.Annotated_date, dayfirst=True)
#datedata["Annotated_date"] = pd.to_datetime(datedata.Annotated_date, dayfirst=True)


###############################################################################
Expand All @@ -55,16 +55,16 @@ def enable_cors():
# MUST to be written inside the lesser/greater than signs ( <parameter_name> )
'''Update Run Annotations '''

@route('/dataQuery/<Runname>/<Panel>/<annotation>')
def myQuery(Runname, Panel, annotation):
@route('/dataQuery/<Runname>/<Panel>/<status>/<chartid>/<startdate>/<enddate>/<annotation>')
def myQuery(Runname, Panel, status, chartid,startdate,enddate,annotation):
print("Enter")
todate = date.today().strftime('%Y-%m-%d')
annotation = '{0} : {1}'.format(todate, annotation)
mycursor.execute("UPDATE Run_Annotations SET Annotation = Annotation || '<br>' || ? WHERE Run = ? and Panel = ?;", (annotation, Runname, Panel))
mycursor.execute("UPDATE Run_Annotations SET Annotation = Annotation || '<br>' || ? , Status = ?, GraphID = ?, Date1 = ?, Date2 = ? WHERE Run = ? and Panel = ?;", (annotation.decode('utf8'),status,chartid,startdate,enddate,Runname,Panel))
connection.commit()
print(mycursor.rowcount, " rows")
if mycursor.rowcount == 0:
mycursor.execute("Insert into Run_Annotations (Annotation, Run, Panel) values( ?, ?,?);", (annotation, Runname, Panel))
mycursor.execute("Insert into Run_Annotations (Annotation, Run, Panel, Status, GraphID, Date1, Date2) values(?,?,?,?,?,?,?);", (annotation.decode('utf8'), Runname, Panel, status,chartid,startdate,enddate))
connection.commit()
return

Expand All @@ -83,11 +83,11 @@ def myDateQuery(Date, Panel, Notes):

@route('/runannotation')
def runannotation():
curr = mycursor.execute('''SELECT Run, Panel, Annotation from Run_Annotations''')
curr = mycursor.execute('''SELECT Run, Panel, Status, Annotation, GraphID, Date1, Date2 from Run_Annotations''')
anndata = curr.fetchall()
connection.commit()
anndata = pd.DataFrame(anndata)
anndata.rename(columns={0: "Run", 1:"Panel", 2: "Annotation"},inplace=True)
anndata.rename(columns={0: "Run", 1:"Panel", 2: "Status", 3: "Annotation", 4: "GraphID", 5: "Date1", 6: "Date2"},inplace=True)
runannotation = anndata.to_json(orient="records")
return runannotation

Expand All @@ -101,7 +101,7 @@ def dateannotation():
connection.commit()
datedata = pd.DataFrame(datedata)
datedata.rename(columns={0: "Annotated_date", 1: "Notes", 2: "Panel"}, inplace=True)
datedata["Annotated_date"] = pd.to_datetime(datedata.Annotated_date, dayfirst=True)
#datedata["Annotated_date"] = pd.to_datetime(datedata.Annotated_date, dayfirst=True)
dateannotation = datedata.to_json(orient="records")
return dateannotation

Expand Down
44 changes: 31 additions & 13 deletions chronqc/chronqc_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from datetime import date
from dateutil.relativedelta import relativedelta
import warnings
import io
try:
from . import utils
except (ValueError, SystemError, ImportError):
Expand Down Expand Up @@ -396,7 +397,7 @@ def stacked_bar_plot(df, column_name):
return df_dup_all, df_dup_all_cumsum


def create_dir(vals, df_chart, chart_id, chart_title, y_label, startdate, enddate, categories, ylabel2, df_chart_cumsum, per_sample):
def create_dir(vals, df_chart, chart_id, chart_title, y_label, startdate, enddate, categories, ylabel2, df_chart_cumsum, per_sample, column_name):
'''
df, dir -> dir
'''
Expand Down Expand Up @@ -439,6 +440,7 @@ def create_dir(vals, df_chart, chart_id, chart_title, y_label, startdate, enddat
# for per_sample
vals[chart_id + 'htmltemplates']['id_perSample_bool'] = per_sample
vals[chart_id + 'htmltemplates']['id_perSample'] = chart_id + '_per_sample'
vals[chart_id + 'htmltemplates']['download_title'] = column_name

return vals

Expand All @@ -456,6 +458,19 @@ def process_y(column_name):
return column_name.replace('_', ' ').title().replace('Percent Gc', '% GC Content').replace('Tstv', 'Ts/Tv')


def process_title(column_name):
"""
(str) -> str
Do following processing in string
1. replace _ with ""
2. replace percent with %
3. replace pct with %
4. capitalize words
"""
# .replace('percent', '%').replace('pct', '%')
return column_name.replace(' ', '_').replace('>=', 'ge').replace('<=', 'le').replace('>', 'gt').replace('/', '_').replace('<', 'lt').replace('(', '').replace(')', '').replace('[', '').replace(']', '').replace(',', '').replace('%', 'Percent')


###############################################################################
# Get parameters
def main(args):
Expand Down Expand Up @@ -517,24 +532,26 @@ def main(args):
# add ch to logger
logger.addHandler(ch)
logger.info("Started chronqc {0}".format(day))

# read plot config
with open(op.abspath(args.json), 'r') as f:
try:
config = json.load(f)
logger.info("Got required parameters for chronqc")
except ValueError:
e = sys.exc_info()[1]
logger.critical("FATAL: Error in JSON file {0}:{1}".format(e, op.abspath(args.json)))
sys.exit(1)
f = op.abspath(args.json)
try:
config = json.load(io.open(f, 'r', encoding = 'utf-8-sig'),strict=False)
logger.info("Got required parameters for chronqc")
except ValueError:
e = sys.exc_info()[1]
logger.critical("FATAL: Error in JSON file {0}:{1}".format(e, op.abspath(args.json)))
sys.exit(1)

# enddate = date.today() + relativedelta(months=+1)
# enddate = enddate.strftime('%Y-%m-%d')
# Create dictionary of data tobe filled in html file
datetime = date.today()
vals = {'htmltemplates': '', 'calendartemplates': '',
'javascripttemplate': '', 'sidebartemplates': '',
'j': '$j', 'panel': panel, 'startdate': '$startdate',
'enddate': '$enddate', 'datetime': datetime, 'pdfname': '$pdfname'}
'enddate': '$enddate', 'datetime': datetime, 'pdfname': '$pdfname',
'table': '$table','headers': '$headers', 'rows':'$rows', 'row':'$row',
'cols':'$cols', 'col':'$col', 'text':'$text'}
i = 1
chart_ids = []
group_ids = {}
Expand Down Expand Up @@ -755,8 +772,9 @@ def main(args):
logger.critical("For {0}: No suitable chart_type is defined check JSON".format(chart_id))
sys.exit(1)
# keep data in dir
download_title = process_title(chart_title)
vals = create_dir(vals, df_chart, chart_id, chart_title, y_label,
startdate, enddate, categories=category_str, ylabel2=ylabel2, df_chart_cumsum=df_chart_cumsum, per_sample=per_sample)
startdate, enddate, categories=category_str, ylabel2=ylabel2, df_chart_cumsum=df_chart_cumsum, per_sample=per_sample, column_name=download_title)
# html template
html_tmpl = string.Template(open(op.join(templates_dir, "html.txt")).read())
vals[chart_id + '_html'] = html_tmpl.substitute(**vals[chart_id + 'htmltemplates'])
Expand All @@ -775,7 +793,7 @@ def main(args):
vals['pdfname'] = "%s.pdf" % prefix
# substitute vals in main template
tmpl = string.Template(tmpl).substitute(**vals)
with open(out_file, "w") as fh:
with io.open(out_file, "w", encoding='utf8') as fh:
fh.write(tmpl)
logger.info("Finished creating {0} chronqc plots: {1}".format(i-1, out_file))
print("Finished creating {0} chronqc plots: {1}".format(i-1, out_file))
39 changes: 22 additions & 17 deletions chronqc/templates/absolute_threshold.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
document.getElementById("$divname"),
$dataname,
{
labels: [ "Date","Upper","Lower","Scatter"],
labels: [ "Date","Threshold","Threshold","$ylabel"],
labelsDiv: document.getElementById("$labeldiv"),
labelsSeparateLines: true,
showRangeSelector: true,
Expand Down Expand Up @@ -50,15 +50,13 @@
sample = $samplename[row];

if ($id_perSample == "False"){
text = "<b>Run ID: </b>".fontcolor("#800000") + run[0]
+ "<br/><b>Sample ID (y-value): </b>".fontcolor("#008080") + "<font size='-1.5'>" + sample + "</font>"
+ "<br><b>Date:</b> ".fontcolor("#800080") + moment(d).format("DD/MM/YYYY")
+ "<br/><b>$ylabel: </b>".fontcolor("#000080") + data[3]
text = "<b>Run ID: </b>".fontcolor("#000") + run[0]
+ "<br/><b>Sample ID (y-value): </b>".fontcolor("#000") + "<font size='-1.5'>" + sample + "</font>"
+ "<br><b>Date:</b> ".fontcolor("#000") + moment(d).format("DD/MM/YYYY")
}else{
text = "<b>Sample ID: </b>".fontcolor("#800000") + run[0]
+ "<br/><b>Run ID: </b>".fontcolor("#008080") + "<font size='-1.5'>" + sample + "</font>"
+ "<br><b>Date:</b> ".fontcolor("#800080") + moment(d).format("DD/MM/YYYY")
+ "<br/><b>$ylabel: </b>".fontcolor("#000080") + data[3]
text = "<b>Sample ID: </b>".fontcolor("#000") + run[0]
+ "<br/><b>Run ID: </b>".fontcolor("#000") + "<font size='-1.5'>" + sample + "</font>"
+ "<br><b>Date:</b> ".fontcolor("#000") + moment(d).format("DD/MM/YYYY")
}

if (data[1]==null){
Expand All @@ -69,6 +67,8 @@
+ "<br/><b>Lower Threshold: </b>".fontcolor("#ff0000") + data[2]
}

text = text + "<br/><b>$ylabel: </b>".fontcolor("#0080ff") + data[3]

if (run[1].length != 0){
note = "<br/><b>Notes: </b><br/>" + run[1]
text = text + note.fontcolor("red");
Expand All @@ -81,17 +81,12 @@
},
},
series: {
"Upper" : {
strokePattern: Dygraph.DASHED_LINE ,
color: "#ff0000",
strokeWidth: 2,
},
"Lower" : {
"Threshold" : {
strokePattern: Dygraph.DASHED_LINE ,
color: "#ff0000",
strokeWidth: 2,
},
"Scatter" : {
"$ylabel" : {
strokeWidth: 0,
drawPoints: true,
pointSize: 3,
Expand Down Expand Up @@ -142,10 +137,20 @@
date = $dataname[point.idx][0]
date = moment(date).format("DD-MM-YYYY")
//copyToClipboard(run)
makeQuery(run,date)
chartid = "$chart_id"
makeQuery(run,date, chartid)
},
}
);
var upper=1, lower =1;
for(i = 1; i < $dataname.length; i++){
if($dataname[i][1]==null){upper += 1;}
if($dataname[i][2]==null){lower += 1;}
}
if(upper==$dataname.length && lower==$dataname.length){$graphvar.updateOptions({labels: ["Date","","","$ylabel"]});}
else if (lower==$dataname.length){$graphvar.updateOptions({labels: ["Date","Threshold","","$ylabel"]});}
else if (upper ==$dataname.length){$graphvar.updateOptions({labels: ["Date","","Threshold","$ylabel"]});}

document.getElementById("$restorebutton").onclick = function() { restorePositioning($graphvar); };
document.getElementById("$yearbutton").onclick = function() {$graphvar.updateOptions({dateWindow: [Date.parse('$startdate'),Date.parse('$enddate')]});};
gs.push($graphvar);
Expand Down
6 changes: 3 additions & 3 deletions chronqc/templates/bar_line_plot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

for (i=0;i<$graphvar_labels.length;i++){
if ($graphvar_labels[i]=="Total"){
$graphvar_labels[i]="Total Mutation";
$graphvar_series[$graphvar_labels[i]]={color: "#000000",axis: 'y2',plotter: Dygraph.Plotters.linePlotter,strokeWidth: 1.5,independentTicks: true}
}
else{
Expand Down Expand Up @@ -57,10 +58,9 @@
var text = [],
data = $dataname[row],

text = "<br><b>Month:</b> ".fontcolor("#800080") + moment(d).format("MMMM YYYY")

text = "<br><b>Month:</b> ".fontcolor("#000") + moment(d).format("MMMM YYYY")
for (i=1;i<$graphvar_labels.length; i++){
if($graphvar_labels[i]=="Total"){text = text + "<br><b>Total Mutations:</b> ".fontcolor("#000000") + data[i] }
if($graphvar_labels[i]=="Total Mutation"){text = text + "<br><b>Total Mutations:</b> ".fontcolor("#000000") + data[i] }
else {
name= $graphvar_labels[i]
color = barchart_Colors[i]
Expand Down

0 comments on commit 648c016

Please sign in to comment.