 <%
import sys
import os
import re
import py_semtools.cli_manager as cli_manager

def show_n_exec(args, command='semtools', out='std'):
	plotter.execute_command(args, command, out=out, string2table = True, module = cli_manager, func_name = command )
	results = plotter.table(id='results', text=True, header=False, row_names=False, styled='bs', attrib = {'class' : 'table table-striped table-bordered table-compact'} )
	html=f'''
	<div class="code"> {command} {args} </div>
	<details>
		<summary class="click-data"> Click to see results</summary>
		<div class="results">
			{results}
		</div>
	</details>
	'''
	return html

def write_n_show_table(table, input_file):
	plotter.hash_vars['terms'] = table
	input_file = 'terms.txt'
	with open(input_file, 'w') as f:
			for r in table: f.write("\t".join(r) + "\n")
	formated_table = plotter.table(id='terms', text=True, header=False, row_names=False, styled='bs', attrib = {'class' : 'table table-striped table-bordered table-compact'})
	return formated_table
%>

<div class="main">
	<h1> semtools </h1>
	<h2> Translate term lists </h2>
		<div class="lvl1">	
			<h3>Take a list of code terms and pas to name terms</h3>
				<p> <b>Input</b> </p>
				<%
					table = [['GO:0005499'], ['GO:0005502'], ['GO:0016918']]
					input_file = 'terms.txt'
				%>
				${ write_n_show_table(table, input_file) }
				<p> The script takes a input file with one code term per line and translates the list to term names </p>
				<% args = f"-O GO -i {input_file} -l names" %>
				${show_n_exec(args)}
		</div>

	<h2> Get children terms </h2>
		<div class="lvl1">	
			<h3>Use a list of parent terms to obtain a list of child terms</h3>
				<p> We use -C to specify whic terms use as parents to obtain all childs (from the next level to the finale leafs in the ontology). Then, all childs for the specified parents are unified in one list. </p>		
				<% args = f"-C GO:0019842,GO:0002060 -O GO" %>
				${show_n_exec(args)}

			<h3>Use a list of parent terms to obtain a list of parent-child term names</h3>
				<p> With '/' character previous to the term list specified with -C flag we an use modificators such 'r' to obtain pairs parent-child and 'n' to translate term codes to term names. </p>		
				<% args = f"-C 'rn/GO:0019842,GO:0002060' -O GO" %>
				${show_n_exec(args)}

			<h3>Use a list of child terms to obtain a list of parent-child term names limiting the levels</h3>
				<p> 
					Others -C modificators are 'a' which assumes that the given terms are childs and that the user desires the parent terms of these childs. 
					The numbers of levels checked (parents or childs without 'a' or not) could be controlled with 'hN' where N must be an integrate with the number of hops (i.e. 'h2' means that the command only must check the next/previous two levels of the ontology) 
				</p>
				
				<% args = f"-C 'rah2n/GO:0019842,GO:0002060' -O GO"%>
				${show_n_exec(args)}

			<h3>Take a list of code terms and remove whose terms with a given parent</h3>
				<details>
					<summary class="click-data"> <b>Input:</b> Show </summary>
					<%
						table = [['GO:0005499'],['GO:0005502'],['GO:0016918'],['GO:0005503'],['GO:0005542'],['GO:0008431'],['GO:0009374'],['GO:0019841'],['GO:0030170'],['GO:0070279'],['GO:0030975'],['GO:0030976'],['GO:0031177'],['GO:0031418'],['GO:0031419'],['GO:0070280'],['GO:0070281'],['GO:0070282'] ]
						input_file = 'terms.txt'
					%>
					${ write_n_show_table(table, input_file) }

				</details>

				<p> To filter a list of code terms the -F flag must be used with a string that with specifices white lists (p, to keep terms with a given parent) or black list (n, to remove terms with a given parent). Parents are listed as comma separated term list flanked by '(' and ')'. The string can include several especifications of this estructure. The flag '--list' specifies that the input file is one item per line list.</p>
			
				<% args = f"-i {input_file} -F 'n(GO:0070279)' --list -O GO"%>
				${show_n_exec(args)}

		</div>
</div>
