مَوادَس کُن گٔژھِو

Module:Infobox gene

وِکیٖپیٖڈیا پؠٹھٕ، اَکھ آزاد اِنسایکلوپیٖڈیا

"یَمہٕ ماڈیوٗلُک دَستاویز ییٚہِ Module:Infobox gene/دَستاویز جاے بَناونہٕ"

local p = {}

local localSeparatorStr = "،" -- **lclz** Kashmiri comma
local localNotApplicableStr = "دٔستِیاب کِہیٖن" -- **lclz** n/a
local root

--define Global Color Scheme
local rowBGcolor = 'light-dark(#eee, #27292d) !important' --'var(--background-color-neutral)'
local titleBGcolor = 'light-dark(#ddd, #404244) !important' 
local sideTitleBGcolor = 'light-dark(#c3fdb8, #003500) !important'

-- wrapped "protected call", return "value error" with error info on error
local function check_values(f,args)
	local exist, val = pcall(f, unpack(args))
	if exist and val ~= nil then
		return(val)
	else
		return("'''VALUE_ERROR''' (" .. tostring(val) .. ")")
	end
end

local function trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function p.getTemplateData(frame)
	local root_qid = mw.text.trim(frame.args['QID'] or "")
	local mm_qid = ""
	local collapse_orthologs = mw.text.trim(frame.args['collapse_orthologs'] or '')
	local entity = {}
	local entity_protein = {}
	local entity_mouse = {}
	local entity_mouse_protein = {}
	local checkOrtholog = 0 -- fixed type

	local mouse_propertyID = "P684"
	local protein_propertyID = "P688"

	if root_qid == "" then
		entity = mw.wikibase.getEntity()
		if entity then root_qid = entity.id else root_qid = "" end
	else
		entity = mw.wikibase.getEntity(root_qid)
	end

	local subclass = p.getValue(entity, "P31") or ""
	if string.find(subclass, 'protein') or string.find(subclass, 'پرٛوٹیٖن') then -- **lclz**
		local claims
		if entity.claims then
			claims = entity.claims["P702"]
		end
		if claims then
			entity = {}
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				for k, v in pairs(claims) do
					local itemID = "Q" .. claims[#entity + 1].mainsnak.datavalue.value["numeric-id"]
					entity[#entity + 1] = mw.wikibase.getEntity(itemID)
					root_qid = itemID
				end
			end
		end
		entity = mw.wikibase.getEntity(root_qid)
	end

	if entity then
		local claims
		if entity.claims then
			claims = entity.claims[protein_propertyID]
		end
		if claims then
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				for k, v in pairs(claims) do
					local protein_itemID = "Q" .. claims[#entity_protein + 1].mainsnak.datavalue.value["numeric-id"]
					entity_protein[#entity_protein + 1] = mw.wikibase.getEntity(protein_itemID)
				end
			end
		end

		if entity.claims then
			claims = entity.claims[mouse_propertyID]
		end
		local qualifierID = "P703"
		local mouse_qual = "Q83310"
		if claims then
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				for k, v in pairs(claims) do
					if checkOrtholog == 1 then
						break
					end

					local mouse_itemID = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
					local quals
					if v.qualifiers then
						quals = v.qualifiers.P703
					end
					if quals then
						for qk, qv in pairs(quals) do
							local qual_obj_id = "Q"..qv.datavalue.value["numeric-id"]
							if qual_obj_id == mouse_qual then
								mm_qid = mouse_itemID
								entity_mouse = mw.wikibase.getEntity(mouse_itemID)
								checkOrtholog = 1
								break
							end
						end
					end
				end
			end
		else
			checkOrtholog = 0
		end

		if entity_mouse and entity_mouse.claims then
			claims = entity_mouse.claims[protein_propertyID]
		end
		if claims then
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				for k, v in pairs(claims) do
					local protein_itemID = "Q" .. claims[#entity_mouse_protein + 1].mainsnak.datavalue.value["numeric-id"]
					entity_mouse_protein[#entity_mouse_protein + 1] = mw.wikibase.getEntity(protein_itemID)
				end
			end
		end
	end

	if entity then
		local name = check_values(p.getLabel,{entity})
		local bgee_wikidata_id = 'Q54985720'
		local expressed_in_tissues = check_values(p.getValue, {entity, "P5572", localNotApplicableStr, localSeparatorStr, bgee_wikidata_id})
		local expressed_in_mouse_tissues = check_values(p.getValue, {entity_mouse, "P5572", localNotApplicableStr, localSeparatorStr, bgee_wikidata_id})
		local entrez_gene = check_values(p.getValue, {entity, "P351", localNotApplicableStr} )
		local entrez_gene_mm = check_values(p.getValue, {entity_mouse, "P351", localNotApplicableStr})
		local image = check_values( p.getImage, {entity, "P18", " ", "250px"})
		local uniprotID_hs = check_values(p.getValueProtein, {entity_protein, "P352", localNotApplicableStr})
		local uniprotID_mm = check_values(p.getValueProtein, {entity_mouse_protein, "P352", localNotApplicableStr})
		local pdbIDs = check_values(p.getPDB, {entity_protein})
		local aliases = check_values(p.getAliases, {entity})
		local gene_symbol = check_values(p.getValue, {entity, "P353"})
		local hgnc_id = check_values(p.getValue, {entity, "P354"})
		local homologene_id = check_values(p.getValue, {entity, "P593"})
		local omim_id = check_values(p.getValue, {entity, "P492"})
		local mgi_id = check_values(p.getValue, {entity_mouse, "P671"})
		local ChEMBL_id = check_values(p.getValue, {entity_protein, "P592"})
		local IUPHAR_id = check_values(p.getValue, {entity_protein, "P595"})
		local ec_no = check_values(p.getValueProtein, {entity_protein, "P591"})
		local mol_funct = check_values(p.getGO, {entity_protein, "P680"})
		local cell_comp = check_values(p.getGO, {entity_protein, "P681"})
		local bio_process = check_values(p.getGO, {entity_protein, "P682"})
		local expression_images = check_values(p.getImage, {entity,"P692","<br><br>","250px"})
		local ensembl = check_values(p.getValue, {entity, "P594", localNotApplicableStr})
		local ensembl_mm = check_values(p.getValue, {entity_mouse, "P594", localNotApplicableStr})
		local refseq_mRNA = check_values(p.getRefseq_mRNA, {entity, "P639", localNotApplicableStr})
		local refseq_mRNA_mm = check_values(p.getRefseq_mRNA, {entity_mouse, "P639", localNotApplicableStr})
		local refseq_prot = check_values(p.getRefseq_protein, {entity_protein, "P637", localNotApplicableStr})
		local refseq_prot_mm = check_values(p.getRefseq_protein, {entity_mouse_protein, "P637", localNotApplicableStr})
		local gstart = check_values(p.getChromosomeLoc, {entity, "P644", "hg"})
		local gend = check_values(p.getChromosomeLoc, {entity, "P645", "hg"})
		local chr = check_values(p.trimChromosome, {entity})
		local cytoband = check_values(p.getValue, {entity, "P4196", localNotApplicableStr})
		local db = check_values(p.getAliasFromGenomeAssembly, {entity,"hg"})
		local gstart_mm = check_values(p.getChromosomeLoc, {entity_mouse, "P644", "mm"})
		local gend_mm = check_values(p.getChromosomeLoc, {entity_mouse, "P645", "mm"})
		local chr_mm = check_values( p.trimChromosome, {entity_mouse})
		local db_mm = check_values(p.getAliasFromGenomeAssembly, {entity_mouse,"mm"})
		local cytoband_mm = check_values(p.getValue, {entity_mouse, "P4196", localNotApplicableStr})
		local disease, dis_ref = p.getDisease(entity, "P2293")
		local drug, drug_ref, drug_pqid, drug_pname = p.getDrug(entity_protein, "P129")

		p.createTable()
		p.renderUpperTitle(name)
		p.renderImage(image)
		p.renderAvailableStructures(uniprotID_hs, uniprotID_mm, checkOrtholog, pdbIDs)
		p.renderIdentifiers(aliases, hgnc_id, gene_symbol, homologene_id, omim_id, mgi_id, ChEMBL_id, IUPHAR_id, ec_no, entrez_gene, ensembl)

		if (chr ~= "" and gstart ~= "" and gend ~= "") or (chr_mm ~= "" and gstart_mm ~= "" and gend_mm ~= "") then
			p.renderGeneLocation(frame, chr, gstart, gend, db, cytoband, ensembl, chr_mm, gstart_mm, gend_mm, db_mm, cytoband_mm, ensembl_mm, name)
		end
		if expression_images ~= "" or expressed_in_tissues ~= localNotApplicableStr then
			p.renderRNAexpression(expression_images, entrez_gene, ensembl, expressed_in_tissues, ensembl_mm, expressed_in_mouse_tissues)
		end
		if (mol_funct ~= "" and cell_comp ~= "" and bio_process ~= "") then
			p.renderGeneOntology(mol_funct, cell_comp, bio_process, uniprotID_hs)
		end
		p.renderOrthologs(frame, entrez_gene, entrez_gene_mm, ensembl, ensembl_mm, uniprotID_hs, uniprotID_mm, refseq_mRNA, refseq_mRNA_mm, refseq_prot, refseq_prot_mm, db, chr, gstart, gend, db_mm, chr_mm, gstart_mm, gend_mm, collapse_orthologs)
		p.renderFooter(root_qid, mm_qid)

		return tostring(root)
	else
		return "An Error has occurred retrieving Wikidata item for infobox"
	end
end

function p.createTable(subbox)
	if subbox == 'sub' then
		root
			:tag('table')
			:css('padding', '0')
			:css('border', 'none')
			:css('margin', '0')
			:css('width', 'auto')
			:css('min-width', '100%')
			:css('font-size', '100%')
			:css('clear', 'none')
			:css('float', 'none')
	else
		root = mw.html.create('table')
		root
			:addClass('infobox')
			:css('width', '26.4em')
	end
end

function p.renderUpperTitle(name)
	local title = name
	if not title then return "error: failed to get label"; end

	root
		:tag('tr')
			:tag('th')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:css('font-size', '125%')
				:css('font-weight', 'bold')
				:wikitext(title)
				:done()
end

function p.renderCaption(entity)
	--caption
end

function p.renderImage(image)
	root
		:tag('tr')
			:tag('td')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:wikitext(image)
				:done()
end

function p.renderAvailableStructures(uniprotID_hs, uniprotID_mm, checkOrtholog, pdbIDs)
	local title = 'دٔستِیاب ڈانٛچہٕ' -- **lclz** Available structures
	local pdb_link = "[[Protein_Data_Bank|PDB]]"
	local searchTitle = ""
	local listTitle = "پی ڈی بی (PDB) کوڈَن ہُنٛد فِہرِست" -- **lclz** List of PDB id codes
	local PDBe_base = 'https://www.ebi.ac.uk/pdbe/searchResults.html?display=both&amp;term='
	local RCSB_base = 'https://www.rcsb.org/search?q='
					..'rcsb_polymer_entity_container_identifiers.reference_sequence_identifiers.database_name:UniProt%20AND%20'
					..'rcsb_polymer_entity_container_identifiers.reference_sequence_identifiers.database_accession:'
	local url_uniprot = " "

	if checkOrtholog == 1 and uniprotID_mm ~= 'n/a' then
		searchTitle = 'آرتھولاگ تَلاش: ' -- **lclz** Ortholog search
		url_uniprot = uniprotID_mm..','..uniprotID_hs
	else
		searchTitle = 'اِنسٲنی UniProt تَلاش: ' -- **lclz** Human UniProt search
		url_uniprot = uniprotID_hs
	end
	
	local PDBe_list = " "
	if url_uniprot:match("([^,]+),") then
		PDBe_list = string.gsub(url_uniprot, ",", "%%20or%%20")
	else
		PDBe_list = url_uniprot
	end

	local PDBe = "["..PDBe_base..PDBe_list.." PDBe] "
	local RCSB = "["..RCSB_base..url_uniprot.." RCSB] "

	if string.match(pdbIDs, '%w+') then
		root
			:tag('tr')
				:tag('td')
					:attr('colspan', 4)
					:css('text-align', 'center')
					:css('background-color', rowBGcolor)
					:css('color', 'inherit')
					:tag('table')
						:css('padding', '0')
						:css('border', 'none')
						:css('margin', '0')
						:css('width', '100%')
						:css('text-align', 'left')
						:tag('tr')
							:tag('th')
								:attr('colspan', '4')
								:css('text-align', 'center')
								:css('background-color', titleBGcolor)
							    :css('color', 'inherit')
								:wikitext(title)
								:done()
							:done()
						:tag('tr')
							:tag('th')
								:attr('rowspan', '2')
								:css('background-color', sideTitleBGcolor)
							    :css('color', 'inherit')
								:css('width', '43px')
								:wikitext(pdb_link)
								:done()
							:tag('td')
								:attr('colspan', '2')
								:css('background-color', rowBGcolor)
							    :css('color', 'inherit')
								:wikitext(searchTitle)
								:tag('span')
									:attr('class', 'plainlinks')
									:wikitext(PDBe)
									:wikitext(RCSB)
									:done()
								:done()
							:done()
						:tag('tr')
							:tag('td')
								:tag('table')
									:attr('class', 'collapsible collapsed')
									:css('padding', '0')
									:css('border', 'none')
									:css('margin', '0')
									:css('width', '100%')
									:css('text-align', 'left')
									:tag('tr')
  										:css('text-align', 'center')
										:tag('th')
										    :css('background-color', titleBGcolor)
	        						        :css('color', 'inherit')
											:attr('colspan', '2')
											:wikitext(listTitle)
											:done()
										:done()
									:tag('tr')
										:tag('td')
											:attr('colspan', '2')
											:css('background-color', rowBGcolor)
							                :css('color', 'inherit')
											:tag('p')
												:tag('span')
													:attr('class', 'plainlinks')
													:wikitext(pdbIDs)
													:done()
												:done()
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:done()
				:done()
	else
		return ""
	end
end

function p.renderIdentifiers(aliases, hgnc_id, gene_symbol, homologene_id, omim_id, mgi_id, ChEMBL_id, IUPHAR_id, ec_no, entrez_gene, ensembl)
	local title = "شِناخَتھ کَرَن وٲلؠ" -- **lclz** Identifiers
	local label_aliases = "[[Gene nomenclature|عُرف]]" -- **lclz** Aliases
	local symbol_url
	if gene_symbol == "" or gene_symbol == nil then
		symbol_url = ""
	else
		if hgnc_id == "" or hgnc_id == nil then
			symbol_url = gene_symbol
		else
			symbol_url = "[https://www.genenames.org/data/gene-symbol-report/#!/hgnc_id/"..hgnc_id.." "..gene_symbol.."]"
		end
	end

	aliases = string.gsub(aliases, '، '..gene_symbol..'$', '')
	aliases = string.gsub(aliases, gene_symbol..'، ', '')
	aliases = string.gsub(aliases, '، '..gene_symbol..'،', '،')
	aliases = string.gsub(aliases, "، ،", "،")
	aliases = string.gsub(aliases, "، $", "")
	
	local label_ext_id = "نؠبرِم شِناخَتھ" -- **lclz** External IDs
	omim_id = string.gsub(omim_id, "%s", "")
	local omim_list = mw.text.split(omim_id, localSeparatorStr)
	local omim = ""
	if (omim_id ~= nil and omim_id ~= "") then
		omim = "[[Mendelian_Inheritance_in_Man|OMIM]]"..": "
	end
	for i, v in ipairs(omim_list) do
		if string.match(v, '%w+') then
			omim = omim.."[https://omim.org/entry/"..v.." "..v.."]، "
		end
	end
	omim = trim(string.gsub(omim, "، $",""))
	
	homologene_id = string.gsub(homologene_id, "%s", "")
	local homolo_list = mw.text.split(homologene_id, localSeparatorStr)
	local homolo =""
	if (homologene_id ~= nil and homologene_id ~= "") then
		homolo = "[[HomoloGene]]"..": "
	end
	for i, v in ipairs(homolo_list) do
		if string.match(v, '%w+') then
			homolo = homolo.."[https://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=homologene&dopt=HomoloGene&list_uids="..v.." "..v.."] "
		end
	end
	homolo = trim(string.gsub(homolo, "، $",""))
	
	local genecards = "[[GeneCards]]"..": "
	genecards = genecards.."[https://www.genecards.org/cgi-bin/carddisp.pl?gene="..gene_symbol.." "..gene_symbol.."]"
	mgi_id = string.gsub(mgi_id, "%s", "")
	local mgi_list = mw.text.split(mgi_id, localSeparatorStr)
	local mgi = ""
	if (mgi_id ~= nil and mgi_id ~= "") then
		mgi = "[[Mouse_Genome_Informatics|MGI]]"..": "
	end
	for i, v in ipairs(mgi_list) do
		if string.match(v, '%w+') then
			local mgi_number = string.sub(mgi_id, 5)
			mgi = mgi.."[http://www.informatics.jax.org/marker/"..mgi_id.." "..mgi_number.."] "
		end
	end
	mgi = trim(string.gsub(mgi, "، $",""))
	
	local ChEMBL = ""
	if string.match(ChEMBL_id, '%w+') then
		ChEMBL = "[[ChEMBL]]"..": ".."[https://www.ebi.ac.uk/chembldb/index.php/target/inspect/CHEMBL"..ChEMBL_id.." "..ChEMBL_id.."] "
	end
	
	local IUPHAR = ""
	if string.match(IUPHAR_id, '%w+') then
		IUPHAR = "[[International_Union_of_Basic_and_Clinical_Pharmacology|IUPHAR]]"..": ".."[http://www.guidetopharmacology.org/GRAC/ObjectDisplayForward?objectId="..IUPHAR_id.." "..IUPHAR_id.."]"
	end
	
	local label_EC = "[[Enzyme_Commission_number|ای سی (EC) نَمبَر]]" -- **lclz** EC number
	ec_no = string.gsub(ec_no, "%d%.%d+%.%d+%.%-,", "")
	ec_no = string.gsub(ec_no, "%d%.%d+%.%d+%.%-", "")
	local link_ec_no = string.gsub(ec_no, "،" ,"+")
	local EC = "[https://www.genome.jp/dbget-bin/www_bget?enzyme+" .. link_ec_no .. " " .. ec_no .. "]"
	
	local oma = "[[Orthologous_MAtrix|OMA]]:"
	oma = oma.."[https://omabrowser.org/oma/vps/"..mw.text.split(ensembl,"،")[1].." "..gene_symbol.." - orthologs]"
	
	local external_id_table = {omim, mgi, homolo, ChEMBL, IUPHAR, genecards, oma}
	local external_id_processed_table = {}
	for i, v in ipairs(external_id_table) do
		if (v ~= "") then 
			external_id_processed_table[#external_id_processed_table + 1] = v 
		end
	end
	local external_ids = tostring(table.concat(external_id_processed_table, "؛ "))

	root
		:tag('tr')
			:tag('th')
				:attr('colspan', '4')
				:css('text-align', 'center')
				:css('background-color', titleBGcolor)
				:css('color', 'inherit')
				:wikitext(title)
				:done()
		:tag('tr')
			:tag('th')
				:attr('scope', 'row')
				:css('background-color', sideTitleBGcolor)
				:css('color', 'inherit')
				:tag('span')
					:attr('class', 'plainlinks')
					:wikitext(label_aliases)
					:done()
				:done()
				:tag('td')
					:attr('colspan','3')
					:css('background', rowBGcolor)
					:css('color', 'inherit')
					:tag('span')
						:attr('class', 'plainlinks')
						:wikitext(symbol_url)
						:done()
					:wikitext(mw.text.nowiki(aliases))
					:done()
			:done()
		:tag('tr')
			:tag('th')
				:attr('scope', 'row')
				:css('background-color', sideTitleBGcolor)
			    :css('color', 'inherit')
				:wikitext(label_ext_id)
				:done()
			:tag('td')
				:attr('colspan', '3')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('span')
					:attr('class', 'plainlinks')
					:wikitext(external_ids)
					:done()
				:done()
			:done()

	if ec_no ~= "" then
		root
			:tag('tr')
				:tag('th')
					:attr('scope', 'row')
					:css('background-color', sideTitleBGcolor)
			        :css('color', 'inherit')
					:wikitext(label_EC)
					:done()
				:tag('td')
					:attr('colspan', '3')
					:css('background-color', rowBGcolor)
			        :css('color', 'inherit')
				:tag('span')
						:attr('class', 'plainlinks')
						:wikitext(EC)
						:done()
					:done()
				:done()
	end
end

function p.renderDiseases(frame, disease, dis_ref, name, qid)
	local title = "جِنیٲتی بؠمٲرؠ" -- **lclz**

	local ref_flag_all = false
	local disease_name = ''
	for index,value in ipairs(disease) do
		if (dis_ref[index] ~= nil and dis_ref[index] ~= '') then
			if disease_name == '' then
				disease_name = value
			else
				disease_name = disease_name.."، "..value
			end
			ref_flag_all = true
		end
	end
	
	if ref_flag_all then
		root
			:tag('tr')
				:tag('td')
					:attr('colspan', 4)
					:css('text-align', 'center')
					:css('background-color', rowBGcolor)
			        :css('color', 'inherit')
					:tag('tr')
						:tag('th')
							:attr('colspan', '3')
						    :css('text-align', 'center')
							:css('background-color', titleBGcolor)
			                :css('color', 'inherit')
							:wikitext(title)
							:done()
						:done()
					:done()
				:done()

		local ref_url = "https://www.wikidata.org/wiki/"..qid.."#P2293"
		local title = "یِم بؠمٲرؠ یِم جِنیٲتی طور پٲٹھؠ " .. name .. " سٟتؠ وابَستہٕ چھِ، وِکیٖڈیٹا پؠٹھ وُچھِو/اؠڈِٹ کٔرِو" -- **lclz**
		local ref_link = disease_name..frame:extensionTag("ref",frame:expandTemplate{ title = 'cite_web', args = { title = title, url = ref_url} })

		root
			:tag('tr')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('td')
					:css('background-color', rowBGcolor)
			        :css('color', 'inherit')
					:attr('scope', 'row')
					:attr('colspan', '3')
					:wikitext(ref_link)
					:done()
				:done()
	end
end

function p.renderDrug(frame,drug, drug_ref, drug_pqid, drug_pname)
	local title = "دَواہُک مقصد" -- **lclz** Targeted by drug

	local ref_flag_all = false
	local drug_list_per_protein = {}
	
	for index,value in ipairs(drug) do
		if (drug_ref[index] ~= nil and drug_ref[index] ~= '') then
			local protein_qid = drug_pqid[index]
			if drug_list_per_protein[protein_qid] == '' or drug_list_per_protein[protein_qid] == nil then
				drug_list_per_protein[protein_qid] = value
			else
				drug_list_per_protein[protein_qid] = drug_list_per_protein[protein_qid]..'، '..value
			end
			ref_flag_all = true
		end
	end

	if ref_flag_all then
		root
			:tag('tr')
				:tag('td')
					:attr('colspan', 4)
					:css('text-align', 'center')
					:css('background-color', rowBGcolor)
			        :css('color', 'inherit')
					:tag('tr')
						:tag('th')
							:attr('colspan', '3')
							:css('text-align', 'center')
							:css('background-color', titleBGcolor)
			                :css('color', 'inherit')
							:wikitext(title)
							:done()
						:done()
					:done()
				:done()

		for k,v in pairs(drug_list_per_protein) do
			local drug_name = v
			local ref_url = "https://www.wikidata.org/wiki/"..k.."#P129"
			local title = "یِم دَوا یِم جِسمٲنی طور پٲٹھؠ " .. drug_pname[k] .. " سٟتؠ رَلان چھِ، وِکیٖڈیٹا پؠٹھ وُچھِو/اؠڈِٹ کٔرِو" -- **lclz**
			local ref_link = drug_name..frame:extensionTag("ref",frame:expandTemplate{ title = 'cite_web', args = { title = title, url = ref_url} })

			root
				:tag('tr')
					:attr('colspan', 4)
					:css('text-align', 'center')
					:css('background-color', rowBGcolor)
			        :css('color', 'inherit')
					:tag('td')
						:css('background-color', rowBGcolor)
			            :css('color', 'inherit')
						:attr('scope', 'row')
						:attr('colspan', '3')
						:wikitext(ref_link)
						:done()
				:done()
		end
	end
end

function p.renderGeneLocation(frame, chr, gstart, gend, db, cytoband, ensembl, chr_mm, gstart_mm, gend_mm, db_mm, cytoband_mm, ensembl_mm, name)
		local titleHuman = "جیٖنٕچ جاے ([[Human genome|اِنسان]])" -- **lclz**
		local titleMouse = "جیٖنٕچ جاے ([[Laboratory mouse|گَگُر]])" -- **lclz**
		local label_chr = "[[Chromosome|کرٛوموزوم]]" -- **lclz**
		local label_locus = "[[Locus (genetics)|بؠنٛڈ]]" -- **lclz**
		local label_gstart = "شُروٗع" -- **lclz**
		local label_gend = "خَتٕم" -- **lclz**
		local tooltip_arrowSign = "یَتھ خٲطرٕ جینومِک جاے "..name -- **lclz**
		local arrowSign_width = 14

	if chr ~= "" and gstart ~= "" and gend ~= "" then
		local chrLengthTable = {}
				chrLengthTable["1"] = 248956422
				chrLengthTable["2"] = 242193529
				chrLengthTable["3"] = 198295559
				chrLengthTable["4"] = 190214555
				chrLengthTable["5"] = 181538259
				chrLengthTable["6"] = 170805979
				chrLengthTable["7"] = 159345973
				chrLengthTable["8"] = 145138636
				chrLengthTable["9"] = 138394717
				chrLengthTable["10"] = 133797422
				chrLengthTable["11"] = 135086622
				chrLengthTable["12"] = 133275309
				chrLengthTable["13"] = 114364328
				chrLengthTable["14"] = 107043718
				chrLengthTable["15"] = 101991189
				chrLengthTable["16"] = 90338345
				chrLengthTable["17"] = 83257441
				chrLengthTable["18"] = 80373285
				chrLengthTable["19"] = 58617616
				chrLengthTable["20"] = 64444167
				chrLengthTable["21"] = 46709983
				chrLengthTable["22"] = 50818468
				chrLengthTable["X"] = 156040895
				chrLengthTable["Y"] = 57227415
				chrLengthTable["MT"] = 16569
		local chrLength = chrLengthTable[chr]

		local chrTextTable = {}
				chrTextTable["1"] = "کرٛوموزوم 1 (اِنسان)"
				chrTextTable["2"] = "کرٛوموزوم 2 (اِنسان)"
				chrTextTable["3"] = "کرٛوموزوم 3 (اِنسان)"
				chrTextTable["4"] = "کرٛوموزوم 4 (اِنسان)"
				chrTextTable["5"] = "کرٛوموزوم 5 (اِنسان)"
				chrTextTable["6"] = "کرٛوموزوم 6 (اِنسان)"
				chrTextTable["7"] = "کرٛوموزوم 7 (اِنسان)"
				chrTextTable["8"] = "کرٛوموزوم 8 (اِنسان)"
				chrTextTable["9"] = "کرٛوموزوم 9 (اِنسان)"
				chrTextTable["10"] = "کرٛوموزوم 10 (اِنسان)"
				chrTextTable["11"] = "کرٛوموزوم 11 (اِنسان)"
				chrTextTable["12"] = "کرٛوموزوم 12 (اِنسان)"
				chrTextTable["13"] = "کرٛوموزوم 13 (اِنسان)"
				chrTextTable["14"] = "کرٛوموزوم 14 (اِنسان)"
				chrTextTable["15"] = "کرٛوموزوم 15 (اِنسان)"
				chrTextTable["16"] = "کرٛوموزوم 16 (اِنسان)"
				chrTextTable["17"] = "کرٛوموزوم 17 (اِنسان)"
				chrTextTable["18"] = "کرٛوموزوم 18 (اِنسان)"
				chrTextTable["19"] = "کرٛوموزوم 19 (اِنسان)"
				chrTextTable["20"] = "کرٛوموزوم 20 (اِنسان)"
				chrTextTable["21"] = "کرٛوموزوم 21 (اِنسان)"
				chrTextTable["22"] = "کرٛوموزوم 22 (اِنسان)"
				chrTextTable["X"] = "X کرٛوموزوم (اِنسان)"
				chrTextTable["Y"] = "Y کرٛوموزوم (اِنسان)"
				chrTextTable["MT"] = "مایٹوکانٛڈرِیَل ڈی این اے (اِنسان)"
		local chrText = chrTextTable[chr]

		local markerWidth = ((gend - gstart) * 294.133 )/ chrLength
		if markerWidth < 2 then
			markerWidth = 2
		else
			markerWidth = math.ceil(markerWidth)
		end
		local markerLocation = (147.0666 * (gstart + gend) / chrLength ) + 1.6 - (markerWidth / 2)
		local arrowSignLocation = markerLocation + (markerWidth / 2) - (arrowSign_width / 2)
		markerLocation = math.floor( markerLocation * 10 + 0.5 ) / 10

		local source_link_chr, source_link_gstart, source_link_gend
		if( db == "hg38" ) then
			source_link_chr = frame:extensionTag("ref", "[http://May2017.archive.ensembl.org/Homo_sapiens/Gene/Summary?db=core;g="..ensembl.." GRCh38: Ensembl release 89: "..ensembl.."] &ndash; [[Ensembl genome database project|Ensembl]], May 2017", {name = "refGRCh38Ensembl"})
			source_link_gstart = frame:extensionTag("ref", "", {name = "refGRCh38Ensembl"})
			source_link_gend = frame:extensionTag("ref", "", {name = "refGRCh38Ensembl"})
		elseif( db == "hg37") then
			source_link_chr = frame:extensionTag("ref", "[http://grch37.ensembl.org/Homo_sapiens/Gene/Summary?db=core;&g="..ensembl.." GRCh37: Ensembl release 89: "..ensembl.."] &ndash; [[Ensembl genome database project|Ensembl]], May 2017", {name = "refGRCh37Ensembl"})
			source_link_gstart = frame:extensionTag("ref", "", {name = "refGRCh37Ensembl"})
			source_link_gend = frame:extensionTag("ref", "", {name = "refGRCh37Ensembl"})
		else
			source_link_chr = ""
			source_link_gstart = ""
			source_link_gend = ""
		end

		local wikitext_for_ideogram_image = ""
		if chr == "MT" then
		else
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."<div align=\"center\">"
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."<div style=\"position: relative; width: 300px;\">"
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."[[File:Human chromosome "..chr.." ideogram.svg|300px|"..chrText.."]]"
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."<div style=\"position: absolute; left: "..arrowSignLocation.."px; top: 2px; padding: 0;\">"
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."[[File:HSR 1996 II 3.5e.svg|"..arrowSign_width.."px|"..tooltip_arrowSign.."]]</div>"
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."<div style=\"position: absolute; left: "..markerLocation.."px; top: 19px; padding: 0;\">[[File:Red rectangle "..markerWidth.."x18.png|"..markerWidth.."px|"..tooltip_arrowSign.."]]</div>"
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."</div>"
			wikitext_for_ideogram_image = wikitext_for_ideogram_image.."</div>"
		end

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('table')
					:attr('class', 'collapsible collapsed')
					:css('padding', '0')
					:css('border', 'none')
					:css('margin', '0')
					:css('width', '100%')
					:css('text-align', 'left')
					:tag('tr')
						:tag('th')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', titleBGcolor)
			                :css('color', 'inherit')
							:wikitext(titleHuman)
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
				 			:wikitext("[[File:Ideogram human chromosome "..chr..".svg|300px|"..chrText.."]]")
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:attr('width', '15%')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_chr)
							:done()
						:tag('td')
							:attr('colspan', '3')
							:attr('width', '85%')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext("[["..chrText.."]]"..source_link_chr)
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:wikitext(wikitext_for_ideogram_image)
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:attr('rowspan', '2')
							:attr('width', '15%')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_locus)
							:done()
						:tag('td')
							:attr('rowspan', '2')
							:attr('width', '35%')
							:css('background-color', rowBGcolor)
						    :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(cytoband)
								:done()
							:done()
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_gstart)
							:done()
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(p.separateWithComma(gstart).." [[Base pair|بی پی (bp)]]"..source_link_gstart)
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_gend)
							:done()
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(p.separateWithComma(gend).." [[Base pair|بی پی (bp)]]"..source_link_gend)
								:done()
							:done()
						:done()
					:done()
				:done()
			:done()
	end

	if chr_mm ~= "" and gstart_mm ~= "" and gend_mm ~= "" then
		local chrLengthTable_mm = {}
				chrLengthTable_mm["1"] = 195471971
				chrLengthTable_mm["2"] = 182113224
				chrLengthTable_mm["3"] = 160039680
				chrLengthTable_mm["4"] = 156508116
				chrLengthTable_mm["5"] = 151834684
				chrLengthTable_mm["6"] = 149736546
				chrLengthTable_mm["7"] = 145441459
				chrLengthTable_mm["8"] = 129401213
				chrLengthTable_mm["9"] = 124595110
				chrLengthTable_mm["10"] = 130694993
				chrLengthTable_mm["11"] = 122082543
				chrLengthTable_mm["12"] = 120129022
				chrLengthTable_mm["13"] = 120421639
				chrLengthTable_mm["14"] = 124902244
				chrLengthTable_mm["15"] = 104043685
				chrLengthTable_mm["16"] = 98207768
				chrLengthTable_mm["17"] = 94987271
				chrLengthTable_mm["18"] = 90702639
				chrLengthTable_mm["19"] = 61431566
				chrLengthTable_mm["X"] = 171031299
				chrLengthTable_mm["Y"] = 91744698
				chrLengthTable_mm["MT"] = 16299
		local chrLength_mm = chrLengthTable_mm[chr_mm]

		local chrTextTable_mm = {}
				chrTextTable_mm["1"] = "کرٛوموزوم 1 (گَگُر)"
				chrTextTable_mm["2"] = "کرٛوموزوم 2 (گَگُر)"
				chrTextTable_mm["3"] = "کرٛوموزوم 3 (گَگُر)"
				chrTextTable_mm["4"] = "کرٛوموزوم 4 (گَگُر)"
				chrTextTable_mm["5"] = "کرٛوموزوم 5 (گَگُر)"
				chrTextTable_mm["6"] = "کرٛوموزوم 6 (گَگُر)"
				chrTextTable_mm["7"] = "کرٛوموزوم 7 (گَگُر)"
				chrTextTable_mm["8"] = "کرٛوموزوم 8 (گَگُر)"
				chrTextTable_mm["9"] = "کرٛوموزوم 9 (گَگُر)"
				chrTextTable_mm["10"] = "کرٛوموزوم 10 (گَگُر)"
				chrTextTable_mm["11"] = "کرٛوموزوم 11 (گَگُر)"
				chrTextTable_mm["12"] = "کرٛوموزوم 12 (گَگُر)"
				chrTextTable_mm["13"] = "کرٛوموزوم 13 (گَگُر)"
				chrTextTable_mm["14"] = "کرٛوموزوم 14 (گَگُر)"
				chrTextTable_mm["15"] = "کرٛوموزوم 15 (گَگُر)"
				chrTextTable_mm["16"] = "کرٛوموزوم 16 (گَگُر)"
				chrTextTable_mm["17"] = "کرٛوموزوم 17 (گَگُر)"
				chrTextTable_mm["18"] = "کرٛوموزوم 18 (گَگُر)"
				chrTextTable_mm["19"] = "کرٛوموزوم 19 (گَگُر)"
				chrTextTable_mm["X"] = "X کرٛوموزوم (گَگُر)"
				chrTextTable_mm["Y"] = "Y کرٛوموزوم (گَگُر)"
				chrTextTable_mm["MT"] = "مایٹوکانٛڈرِیَل ڈی این اے (گَگُر)"
		local chrText_mm = chrTextTable_mm[chr_mm]

		local markerWidth_mm = ((gend_mm - gstart_mm) * 294.133 )/ chrLength_mm
		if markerWidth_mm < 2 then
			markerWidth_mm = 2
		else
			markerWidth_mm = math.ceil(markerWidth_mm)
		end
		local markerLocation_mm = (147.0666 * (gstart_mm + gend_mm) / chrLength_mm ) + 1.6 - (markerWidth_mm / 2)
		local arrowSignLocation_mm = markerLocation_mm + (markerWidth_mm / 2) - (arrowSign_width / 2)
		markerLocation_mm = math.floor( markerLocation_mm * 10 + 0.5 ) / 10
		local source_link_chr_mm = ""
		local source_link_gstart_mm = ""
		local source_link_gend_mm = ""
		if( db_mm == "mm10" or db_mm == "mm0") then
			source_link_chr_mm = frame:extensionTag("ref", "[http://May2017.archive.ensembl.org/Mus_musculus/Gene/Summary?db=core;g="..ensembl_mm.." GRCm38: Ensembl release 89: "..ensembl_mm.."] &ndash; [[Ensembl genome database project|Ensembl]], May 2017", {name = "refGRCm38Ensembl"})
			source_link_gstart_mm = frame:extensionTag("ref", "", {name = "refGRCm38Ensembl"})
			source_link_gend_mm = frame:extensionTag("ref", "", {name = "refGRCm38Ensembl"})
			else
			source_link_chr_mm = ""
			source_link_gstart_mm = ""
			source_link_gend_mm = ""
		end
		local wikitext_for_ideogram_image_mm = ""
		if chr_mm == "MT" then
		else
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."<div align=\"center\">"
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."<div style=\"position: relative; width: 300px;\">"
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."[[File:Ideogram of house mouse chromosome "..chr_mm..".svg|300px|"..chrText_mm.."]]"
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."<div style=\"position: absolute; left: "..arrowSignLocation_mm.."px; top: 2px; padding: 0;\">"
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."[[File:HSR 1996 II 3.5e.svg|"..arrowSign_width.."px|"..tooltip_arrowSign.."]]</div>"
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."<div style=\"position: absolute; left: "..markerLocation_mm.."px; top: 19px; padding: 0;\">[[File:Red rectangle "..markerWidth_mm.."x18.png|"..markerWidth_mm.."px|"..tooltip_arrowSign.."]]</div>"
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."</div>"
			wikitext_for_ideogram_image_mm = wikitext_for_ideogram_image_mm.."</div>"
		end

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('table')
					:attr('class', 'collapsible collapsed')
					:css('padding', '0')
					:css('border', 'none')
					:css('margin', '0')
					:css('width', '100%')
					:css('text-align', 'left')
					:tag('tr')
						:tag('th')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', titleBGcolor)
			                :css('color', 'inherit')
							:wikitext(titleMouse)
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:wikitext("[[File:Ideogram house mouse chromosome "..chr_mm..".svg|260px|"..chrText_mm.."]]")
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:attr('width', '15%')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_chr)
							:done()
						:tag('td')
							:attr('colspan', '3')
							:attr('width', '85%')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(chrText_mm..source_link_chr_mm)
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:wikitext(wikitext_for_ideogram_image_mm)
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:attr('rowspan', '2')
							:attr('width', '15%')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_locus)
							:done()
						:tag('td')
							:attr('rowspan', '2')
							:attr('width', '35%')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(cytoband_mm)
								:done()
							:done()
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_gstart)
							:done()
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(p.separateWithComma(gstart_mm).." [[Base pair|بی پی (bp)]]"..source_link_gstart_mm)
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(label_gend)
							:done()
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(p.separateWithComma(gend_mm).." [[Base pair|بی پی (bp)]]"..source_link_gend_mm)
								:done()
							:done()
						:done()
					:done()
				:done()
			:done()
	end
end

function p.renderExpressionList(expressed_in_tissues, default_text)
	if default_text == nil then default_text = "" end
	local anatomic_entity_labels = localNotApplicableStr
	local collapse = "none"
	local split_values = mw.text.split(expressed_in_tissues, localSeparatorStr)
	local anatomic_entity_list = {}
	local anatomic_entities = {}
	local results
	for k,v in ipairs(split_values) do
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			anatomic_entity_list[#anatomic_entity_list+1] = "<li style='line-height: 137%;'>"..v.."</li>"
			anatomic_entities[#anatomic_entities+1] = v
		end
	end
	if #anatomic_entity_list < 11 then
		if #anatomic_entity_list == 0 then
			anatomic_entity_labels = localNotApplicableStr
			default_text = ""
		end
	else
		collapse = "collapsible collapsed"
		default_text = default_text .. '<br>' .. table.remove(anatomic_entities, 1) .. '<br>' .. table.remove(anatomic_entities, 1) .. '<br>' ..table.remove(anatomic_entities, 1) .. '<br>' .. table.remove(anatomic_entities, 1) .. '<br>' .. table.remove(anatomic_entities, 1) .. '<br>'
	end
	if anatomic_entity_list[#anatomic_entity_list] then
		anatomic_entity_labels = table.concat(anatomic_entity_list, "<br>")
	end
	results = {collapse, default_text, anatomic_entity_labels}
	return results
end

function p.renderRNAexpression(expression_images, entrez_gene,
		ensembl, bgee_expression, ensembl_mouse, bgee_expression_mouse)
	local bgee_gene_page = "https://www.bgee.org/gene/"
	local title = "[[Gene expression|آر این اے ظٲہِر گَژھنُک طٔریٖقہٕ]]" -- **lclz**
	local biogps_link = ""
	local biogps_title = "BioGPS"
	biogps_title = "[http://biogps.org/ " .. biogps_title .. "]"

	if expression_images ~= "" then
		biogps_link = "[http://biogps.org/gene/"..entrez_gene.."/ More reference expression data]"
	else
		expression_images = localNotApplicableStr
	end
	local ensembl_id = string.match(ensembl,"%a+%d+") or ""
	local ensembl_id_mouse = string.match(ensembl_mouse,"%a+%d+") or ""
	local bgee_title = "Bgee"
	bgee_title = "[https://www.bgee.org/ " .. bgee_title .. "]"
	local bgee_default = "["..bgee_gene_page..ensembl_id.." Top expressed in]"
	bgee_expression = p.renderExpressionList(bgee_expression, bgee_default)
	local bgee_collapse = bgee_expression[1]
	bgee_default = bgee_expression[2]
	local bgee_tissues = bgee_expression[3]
	local bgee_default_mm = "["..bgee_gene_page..ensembl_id_mouse.." Top expressed in]"
	bgee_expression_mouse = p.renderExpressionList(bgee_expression_mouse, bgee_default_mm)
	local bgee_collapse_mm = bgee_expression_mouse[1]
	bgee_default_mm = bgee_expression_mouse[2]
	local bgee_tissues_mm = bgee_expression_mouse[3]
	local bgee_more_link = ""
	if bgee_tissues ~= localNotApplicableStr then
		bgee_more_link = "["..bgee_gene_page..ensembl_id.." More reference expression data]"
	end

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('table')
					:attr('class', 'collapsible collapsed')
					:css('padding', '0')
					:css('border', 'none')
					:css('margin', '0')
					:css('width', '100%')
					:css('text-align', 'left')
					:tag('tr')
						:tag('th')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', titleBGcolor)
			                :css('color', 'inherit')
							:wikitext(title)
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(bgee_title)
							:done()
						:tag('td')
							:tag('table')
								:attr('class', 'none')
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'left')
								:tag('tr')
									:tag('th')
										:wikitext("'''[[Human genome|اِنسان]]'''") -- **lclz**
										:done()
									:tag('th')
										:wikitext("'''[[Laboratory mouse|گَگُر]] (آرتھولاگ)'''") -- **lclz**
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:tag('table')
											:attr('class', bgee_collapse)
											:css('padding', '0')
											:css('border', 'none')
											:css('margin', '0')
											:css('width', '100%')
											:css('text-align', 'center')
											:tag('tr')	
												:tag('td')
													:attr('colspan', '1')
													:tag('span')
														:attr('class', 'plainlinks')
														:css('margin', '-3px')
														:wikitext(bgee_default)
														:done()
													:done()
												:done()
											:tag('tr')
												:tag('td')
													:attr('colspan', '1')
													:tag('div')
														:css('margin', '-12px 0px -10px 0px')
														:attr('class', 'plainlinks')
														:tag('ul')
															:css('line-height', '15%')
															:css('margin', '9px')
															:wikitext(bgee_tissues)
															:done()
														:done()
													:done()
												:done()
											:done()
										:done()
									:tag('td')
										:tag('table')
											:attr('class', bgee_collapse_mm)
											:css('padding', '0')
											:css('border', 'none')
											:css('margin', '0')
											:css('width', '100%')
											:css('text-align', 'center')
											:tag('tr')	
												:tag('td')
													:attr('colspan', '1')
													:tag('span')
														:attr('class', 'plainlinks')
														:css('margin', '-3px')
														:wikitext(bgee_default_mm)
														:done()
													:done()
												:done()
											:tag('tr')
												:tag('td')
													:attr('colspan', '1')
													:tag('div')
														:css('margin', '-12px 0px -10px 0px')
														:attr('class', 'plainlinks')
														:tag('ul')
															:css('line-height', '15%')
															:css('margin', '9px')
															:wikitext(bgee_tissues_mm)
															:done()
														:done()
													:done()
												:done()
											:done()
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '4')
										:css('text-align', 'center')
										:css('background-color', rowBGcolor)
            			                :css('color', 'inherit')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(bgee_more_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(biogps_title)
							:done()
						:tag('td')
							:tag('table')
								:attr('class', bgee_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'left')
								:tag('tr')
									:tag('td')
										:attr('colspan', '4')
										:css('text-align', 'center')
										:css('background-color', rowBGcolor)
			                            :css('color', 'inherit')
										:wikitext(expression_images)
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '4')
										:css('text-align', 'center')
										:css('background-color', rowBGcolor)
			                            :css('color', 'inherit')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(biogps_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:done()
				:done()
			:done()
end

function p.renderGeneOntology(mol_funct, cell_comp, bio_process, uniprotID)
	local title = "[[Gene ontology|جیٖن آنٹولوجی]]" -- **lclz**
	local mol_funct_title = "مالیکیولَر کٲم" -- **lclz**
	local cell_comp_title = "سؠلُک حِصہٕ" -- **lclz**
	local bio_process_title = "حیٲتِیٲتی عَمَل" -- **lclz**
	local amigo_link = "[http://amigo.geneontology.org/" .. " Amigo]"
	local quickGO_link = "[https://www.ebi.ac.uk/QuickGO/" .. " QuickGO]"

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('table')
					:attr('class', 'collapsible collapsed')
					:css('padding', '0')
					:css('border', 'none')
					:css('margin', '0')
					:css('width', '100%')
					:css('text-align', 'left')
					:tag('tr')
						:tag('th')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', titleBGcolor)
			                :css('color', 'inherit')
							:wikitext(title)
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:css('font-weight', 'bold')
							:wikitext(mol_funct_title)
							:done()
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('div')
								:attr('class', 'plainlinks')
								:wikitext(mol_funct)
								:wikitext( '\n' )
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:css('font-weight', 'bold')
							:wikitext(cell_comp_title)
							:done()
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('div')
								:attr('class', 'plainlinks')
								:wikitext(cell_comp)
								:wikitext( '\n' )
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:css('font-weight', 'bold')
							:wikitext(bio_process_title)
							:done()
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:tag('div')
								:attr('class', 'plainlinks')
								:wikitext(bio_process)
								:wikitext( '\n' )
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('td')
							:css('background-color', rowBGcolor)
			                :css('color', 'inherit')
							:css('text-align', 'center')
							:attr('colspan', '4')
							:wikitext("ذَرایَع:") -- **lclz** Sources
							:wikitext(amigo_link)
							:wikitext(" / ")
							:wikitext(quickGO_link)
							:done()
						:done()
					:done()
				:done()
			:done()
end

function p.renderOrthologs(frame, entrez_gene, entrez_gene_mm, ensembl, ensembl_mm, uniprot, uniprot_mm, refseq_mRNA, refseq_mRNA_mm, refseq_prot, refseq_prot_mm, db, chr, gstart, gend, db_mm, chr_mm, gstart_mm, gend_mm, collapse_orthologs)
	local title = "[[Orthologs|آرتھولاگ]]" -- **lclz**
	local ortholog_class = collapse_orthologs ~= '' and 'collapsible collapsed' or 'collapsible'		

	local category_chromosome = '[[Category:اِنسٲنی کرٛوموزوم '..chr..' پؠٹھ جیٖن]]'
	if chr == "MT" then
		category_chromosome = '[[Category:اِنسٲنی مایٹوکانٛڈرِیَل جیٖن]]'
	end
	if chr == "" then
		category_chromosome = '[[Category:اِنسٲنی جیٖن]]'
	end
	if mw.title.getCurrentTitle().namespace ~= 0 then
		category_chromosome = ""
	end
	local entrezTitle = "[[Entrez]]"
	entrez_gene = string.gsub(entrez_gene, "%s", "")
	local entrez_link = localNotApplicableStr
	local entrez_collapse
	local entrez_default = ""
	local split_entrez = mw.text.split(entrez_gene, localSeparatorStr)
	local entrez_link_list = {}
	for k,v in ipairs(split_entrez) do
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			entrez_link_list[#entrez_link_list+1] = "[https://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&amp;cmd=retrieve&amp;dopt=default&amp;list_uids="..entrez_gene.."&amp;rn=1 "..entrez_gene.."]"
		end
	end
	
	if #entrez_link_list < 5 then
		entrez_collapse = "none"
		if entrez_default == nil and #entrez_link_list == 0 then entrez_link = localNotApplicableStr end
	else
		entrez_collapse = "collapsible collapsed"
		entrez_default = table.remove(entrez_link_list, 1) .. '<br>' .. table.remove(entrez_link_list, 1) .. '<br>' ..table.remove(entrez_link_list, 1) .. '<br>' .. table.remove(entrez_link_list, 1) .. '<br>' .. table.remove(entrez_link_list, 1) .. '<br>'
	end
	if entrez_link_list[#entrez_link_list] then
		entrez_link = table.concat(entrez_link_list, "<br>")
	end

	entrez_gene_mm = string.gsub(entrez_gene_mm, "%s", "")
	local entrez_mm_link = localNotApplicableStr
	local entrez_mm_collapse
	local entrez_mm_default = ""
	local split_entrez_mm = mw.text.split(entrez_gene_mm, localSeparatorStr)
	local entrez_mm_link_list = {}
	for k,v in ipairs(split_entrez_mm) do
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			entrez_mm_link_list[#entrez_mm_link_list+1] = "[https://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&amp;cmd=retrieve&amp;dopt=default&amp;list_uids="..v.."&amp;rn=1 "..v.."]"
		end
	end
	
	if #entrez_mm_link_list < 5 then
		entrez_mm_collapse = "none"
		if entrez_mm_default == nil and #entrez_mm_link_list == 0 then entrez_mm_link = localNotApplicableStr end
	else
		entrez_mm_collapse = "collapsible collapsed"
		entrez_mm_default = table.remove(entrez_mm_link_list, 1) .. '<br>' .. table.remove(entrez_mm_link_list, 1) .. '<br>' ..table.remove(entrez_mm_link_list, 1) .. '<br>' .. table.remove(entrez_mm_link_list, 1) .. '<br>' .. table.remove(entrez_mm_link_list, 1) .. '<br>'
	end
	if entrez_mm_link_list[#entrez_mm_link_list] then
		entrez_mm_link = table.concat(entrez_mm_link_list, "<br>")
	end

	local ensemblTitle = "[[Ensembl]]"
	ensembl = string.gsub(ensembl, "%s", "")
	local ensembl_link = localNotApplicableStr
	local ensembl_collapse
	local ensembl_default = ""
	local split_ensembl = mw.text.split(ensembl, localSeparatorStr)
	local ensembl_link_list = {}
	for k,v in ipairs(split_ensembl) do
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			ensembl_link_list[#ensembl_link_list+1] = "[http://www.ensembl.org/Homo_sapiens/geneview?gene="..v..";db=core".." "..v.."]"
		end
	end
	
	if #ensembl_link_list < 5 then
		ensembl_collapse = "none"
		if ensembl_default == nil and #ensembl_link_list == 0 then ensembl_link = localNotApplicableStr end
	else
		ensembl_collapse = "collapsible collapsed"
		ensembl_default = table.remove(ensembl_link_list, 1) .. '<br>' .. table.remove(ensembl_link_list, 1) .. '<br>' ..table.remove(ensembl_link_list, 1) .. '<br>' .. table.remove(ensembl_link_list, 1) .. '<br>' .. table.remove(ensembl_link_list, 1) .. '<br>'
	end
	if ensembl_link_list[#ensembl_link_list] then
		ensembl_link = table.concat(ensembl_link_list, "<br>")
	end

	ensembl_mm = string.gsub(ensembl_mm, "%s", "")
	local ensembl_mm_link = localNotApplicableStr
	local ensembl_mm_collapse
	local ensembl_mm_default = ""
	local split_ensembl_mm = mw.text.split(ensembl_mm, localSeparatorStr)
	local ensembl_mm_link_list = {}
	for k,v in ipairs(split_ensembl_mm) do
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			ensembl_mm_link_list[#ensembl_mm_link_list+1] = "[http://www.ensembl.org/Mus_musculus/geneview?gene="..v..";db=core".." "..v.."]"
		end
	end
	
	if #ensembl_mm_link_list < 5 then
		ensembl_mm_collapse = "none"
		if ensembl_mm_default == nil and #ensembl_mm_link_list == 0 then ensembl_mm_link = localNotApplicableStr end
	else
		ensembl_mm_collapse = "collapsible collapsed"
		ensembl_mm_default = table.remove(ensembl_mm_link_list, 1) .. '<br>' .. table.remove(ensembl_mm_link_list, 1) .. '<br>' ..table.remove(ensembl_mm_link_list, 1) .. '<br>' .. table.remove(ensembl_mm_link_list, 1) .. '<br>' .. table.remove(ensembl_mm_link_list, 1) .. '<br>'
	end
	if ensembl_mm_link_list[#ensembl_mm_link_list] then
		ensembl_mm_link = table.concat(ensembl_mm_link_list, "<br>")
	end

	local uniprotTitle = "[[UniProt]]"
	local uniprot_url = "https://www.uniprot.org/uniprot/"

	local uniprot_link = localNotApplicableStr
	local uniprot_collapse
	local uniprot_default = ""
	local split_uniprot = mw.text.split(uniprot, '%p')
	local uniprot_link_list = {}
	local uniprot_first = {}
	local uniprot_alternate = {}
	local hash = {}
	for k,v in ipairs(split_uniprot) do
		if not hash[v] then
			local label = mw.text.trim(v)
			local concat_uniprot_link = uniprot_url .. label
			if string.match(v, '%w+') and v ~= localNotApplicableStr then
				if string.match(v, '^O') or string.match(v,'^P') or string.match(v, '^Q') then
					uniprot_first[#uniprot_first+1] = "[" .. concat_uniprot_link .. " " ..label .. "]"
				else
					uniprot_alternate[#uniprot_alternate+1] = "[" .. concat_uniprot_link .. " " ..label .. "]"
				end
			end
			hash[v] = true
		end
	end
	if #uniprot_first > 0 then
		uniprot_link_list = uniprot_first
	else
		uniprot_link_list = uniprot_alternate
	end

	if #uniprot_link_list < 5 then
		uniprot_collapse = "none"
		if uniprot_default == nil and #uniprot_link_list == 0 then uniprot_link = localNotApplicableStr end
	else
		uniprot_collapse = "collapsible collapsed"
		uniprot_default = table.remove(uniprot_link_list, 1) .. '<br>' .. table.remove(uniprot_link_list, 1) .. '<br>' ..table.remove(uniprot_link_list, 1) .. '<br>' .. table.remove(uniprot_link_list, 1) .. '<br>' .. table.remove(uniprot_link_list, 1) .. '<br>'
	end

	if uniprot_link_list[#uniprot_link_list] then
		uniprot_link = table.concat(uniprot_link_list, "<br>")
	end

	local uniprot_mm_link = localNotApplicableStr
	local uniprot_mm_collapse
	local uniprot_mm_default = ""
	local split_uniprot_mm = mw.text.split(uniprot_mm, localSeparatorStr)
	local uniprot_mm_link_list = {}
	local uniprot_mm_first = {}
	local uniprot_mm_alternate = {}
	local hash2 = {}
	for k,v in ipairs(split_uniprot_mm) do
		if not hash2[v] then
			local label = mw.text.trim(v)
			local concat_uniprot_link = uniprot_url .. label
			if string.match(v, '%w+') and v ~= localNotApplicableStr then
				if string.match(v, '^O') or string.match(v,'^P') or string.match(v, '^Q') then
					uniprot_mm_first[#uniprot_mm_first+1] = "[" .. concat_uniprot_link .. " " ..label .. "]"
				else
					uniprot_mm_alternate[#uniprot_mm_alternate+1] = "[" .. concat_uniprot_link .. " " ..label .. "]"
				end
			end
			hash2[v] = true
		end
	end
	if #uniprot_mm_first > 0 then
		uniprot_mm_link_list = uniprot_mm_first
	else
		uniprot_mm_link_list = uniprot_mm_alternate
	end

	if #uniprot_mm_link_list < 5 then
		uniprot_mm_collapse = "none"
		if uniprot_mm_default == nil and #uniprot_mm_link_list == 0 then uniprot_mm_link = localNotApplicableStr end
	else
		uniprot_mm_collapse = "collapsible collapsed"
		uniprot_mm_default = table.remove(uniprot_mm_link_list, 1) .. '<br>' .. table.remove(uniprot_mm_link_list, 1) .. '<br>' ..table.remove(uniprot_mm_link_list, 1) .. '<br>' .. table.remove(uniprot_mm_link_list, 1) .. '<br>' .. table.remove(uniprot_mm_link_list, 1) .. '<br>'
	end

	if uniprot_mm_link_list[#uniprot_mm_link_list] then
		uniprot_mm_link = table.concat(uniprot_mm_link_list, "<br>")
	end

	local ncbi_link = "https://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?val="
	local refseq_mRNATitle = "RefSeq (mRNA)"

	local refseq_mRNA_link = localNotApplicableStr
	local refseq_mRNA_collapse
	local refseq_mRNA_default = ""
	local split_refseq_mRNA = mw.text.split(refseq_mRNA, localSeparatorStr)
	local link_list_first_mRNA = {}
	local link_list_alternate_mRNA = {}
	local link_list = {}
	for k,v in ipairs(split_refseq_mRNA) do
		local label = mw.text.trim(v)
		local concat_ncbi_link = ncbi_link .. label
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			if string.match(v, 'NM') or string.match(v, 'NP') then
				link_list_first_mRNA[#link_list_first_mRNA+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			elseif string.match(v, 'XM') or string.match(v, 'XP') then
				link_list_alternate_mRNA[#link_list_alternate_mRNA+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			end
		end
	end
	if #link_list_first_mRNA > 0 then
		link_list = link_list_first_mRNA
	else
		link_list = link_list_alternate_mRNA
	end

	if #link_list < 6 then
		refseq_mRNA_collapse = "none"
		if refseq_mRNA_default == nil and #link_list == 0 then refseq_mRNA_link = localNotApplicableStr end
	else
		refseq_mRNA_collapse = "collapsible collapsed"
		refseq_mRNA_default = table.remove(link_list, 1) .. '<br>' .. table.remove(link_list, 1) .. '<br>' ..table.remove(link_list, 1) .. '<br>' .. table.remove(link_list, 1) .. '<br>' .. table.remove(link_list, 1) .. '<br>'
	end

	if link_list[#link_list] then
		refseq_mRNA_link = table.concat(link_list, "<br>")
	end

	local refseq_mRNA_mm_link = localNotApplicableStr
	local refseq_mRNA_mm_collapse
	local refseq_mRNA_mm_default = ""
	local split_refseq_mRNA_mm = mw.text.split(refseq_mRNA_mm, localSeparatorStr)
	local link_list_mm = {}
	local link_list_first_mRNA_mm = {}
	local link_list_alternate_mRNA_mm = {}

	for k,v in ipairs(split_refseq_mRNA_mm) do
		local label = mw.text.trim(v)
		local concat_ncbi_link = ncbi_link .. label
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			if string.match(v, 'NM') or string.match(v, 'NP') then
				link_list_first_mRNA_mm[#link_list_first_mRNA_mm+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			elseif string.match(v, 'XM') or string.match(v, 'XP') then
				link_list_alternate_mRNA_mm[#link_list_alternate_mRNA_mm+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			end
		end
	end
	if #link_list_first_mRNA_mm > 0 then
		link_list_mm = link_list_first_mRNA_mm
	else
		link_list_mm = link_list_alternate_mRNA_mm
	end
	
	if #link_list_mm < 6 then
		refseq_mRNA_mm_collapse = "none"
		if refseq_mRNA_mm_default == nil and #link_list_mm == 0 then refseq_mRNA_mm_link = localNotApplicableStr end
	else
		refseq_mRNA_mm_collapse = "collapsible collapsed"
		refseq_mRNA_mm_default = table.remove(link_list_mm, 1) .. '<br>' .. table.remove(link_list_mm, 1) .. '<br>' ..table.remove(link_list_mm, 1) .. '<br>' .. table.remove(link_list_mm, 1) .. '<br>' .. table.remove(link_list_mm, 1) .. '<br>'
	end

	if link_list_mm[#link_list_mm] then
		refseq_mRNA_mm_link = table.concat(link_list_mm, "<br>")
	end

	local refseq_protTitle = "RefSeq (protein)"
	local refseq_prot_link = localNotApplicableStr
	local refseq_prot_collapse
	local refseq_prot_default = ""
	local split_refseq_prot = mw.text.split(refseq_prot, localSeparatorStr)
	local link_list_prot = {}
	local link_list_first_prot = {}
	local link_list_alternate_prot = {}
	for k,v in ipairs(split_refseq_prot) do
		local label = mw.text.trim(v)
		local concat_ncbi_link = ncbi_link .. label
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			if string.match(v, 'NM') or string.match(v, 'NP') then
				link_list_first_prot[#link_list_first_prot+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			elseif string.match(v, 'XM') or string.match(v, 'XP') then
				link_list_alternate_prot[#link_list_alternate_prot+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			end
		end
	end
	if #link_list_first_prot > 0 then
		link_list_prot = link_list_first_prot
	else
		link_list_prot = link_list_alternate_prot
	end
	
	if #link_list_prot < 6 then
		refseq_prot_collapse = "none"
		if refseq_prot_default == nil and #link_list_prot == 0 then refseq_prot_link = localNotApplicableStr end
	else
		refseq_prot_collapse = "collapsible collapsed"
		refseq_prot_default = table.remove(link_list_prot, 1) .. '<br>' .. table.remove(link_list_prot, 1) .. '<br>' ..table.remove(link_list_prot, 1) .. '<br>' .. table.remove(link_list_prot, 1) .. '<br>' .. table.remove(link_list_prot, 1) .. '<br>'
	end

	if link_list_prot[#link_list_prot] then
		refseq_prot_link = table.concat(link_list_prot, "<br>")
	end

	local refseq_prot_mm_link = localNotApplicableStr
	local refseq_prot_mm_collapse
	local refseq_prot_mm_default = ""
	local split_refseq_prot_mm = mw.text.split(refseq_prot_mm, localSeparatorStr)
	local link_list_prot_mm = {}
	local link_list_first_prot_mm = {}
	local link_list_alternate_prot_mm = {}

	for k,v in ipairs(split_refseq_prot_mm) do
		local label = mw.text.trim(v)
		local concat_ncbi_link = ncbi_link .. label
		if string.match(v, '%w+') and v ~= localNotApplicableStr then
			if string.match(v, 'NM') or string.match(v, 'NP') then
				link_list_first_prot_mm[#link_list_first_prot_mm+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			elseif string.match(v, 'XM') or string.match(v, 'XP') then
				link_list_alternate_prot_mm[#link_list_alternate_prot_mm+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			end
		end
	end
	if #link_list_first_prot_mm > 0 then
		link_list_prot_mm = link_list_first_prot_mm
	else
		link_list_prot_mm = link_list_alternate_prot_mm
	end
	
	if #link_list_prot_mm < 6 then
		refseq_prot_mm_collapse = "none"
		if refseq_prot_mm_default == nil and #link_list_prot_mm == 0 then refseq_prot_mm_link = localNotApplicableStr end
	else
		refseq_prot_mm_collapse = "collapsible collapsed"
		refseq_prot_mm_default = table.remove(link_list_prot_mm, 1) .. '<br>' .. table.remove(link_list_prot_mm, 1) .. '<br>' ..table.remove(link_list_prot_mm, 1) .. '<br>' .. table.remove(link_list_prot_mm, 1) .. '<br>' .. table.remove(link_list_prot_mm, 1) .. '<br>'
	end
	if link_list_prot_mm[#link_list_prot_mm] then
		refseq_prot_mm_link = table.concat(link_list_prot_mm, "<br>")
	end

	local locTitle = "Location (UCSC)"
	local gstart_mb = p.locToMb(gstart, 2)
	local gend_mb = p.locToMb(gend, 2)
	local chr_loc_link = ""
	if (string.match(db, '%w+') and string.match(chr, '%w+') and string.match(gstart, '%w+') and string.match(gend, '%w+') )then
		local chr_ucsc
		if chr == "MT" then
			chr_ucsc = "M"
		else
			chr_ucsc = chr
		end
		chr_loc_link = "[https://genome.ucsc.edu/cgi-bin/hgTracks?org=Human&db="..db.."&position=chr"..chr_ucsc..":"..gstart.."-"..gend.." ".."Chr "..chr_ucsc..": "..gstart_mb.." – "..gend_mb.." Mb]"
	else
		chr_loc_link = localNotApplicableStr
	end
	
	local gstart_mm_mb = p.locToMb(gstart_mm, 2)
	local gend_mm_mb = p.locToMb(gend_mm, 2)
	local chr_loc_mm_link = ""
	if (string.match(db_mm, '%w+') and string.match(chr_mm, '%w+') and string.match(gstart_mm, '%w+') and string.match(gend_mm, '%w+') )then
		local chr_mm_ucsc
		if chr_mm == "MT" then
			chr_mm_ucsc = "M"
		else
			chr_mm_ucsc = chr_mm
		end
		chr_loc_mm_link = "[https://genome.ucsc.edu/cgi-bin/hgTracks?org=Mouse&db="..db_mm.."&position=chr"..chr_mm_ucsc..":"..gstart_mm.."-"..gend_mm.." ".."Chr "..chr_mm_ucsc..": "..gstart_mm_mb.." – "..gend_mm_mb.." Mb]"
	else
		chr_loc_mm_link = localNotApplicableStr
	end

	local pubmedTitle = "[[PubMed]] search"
	local pubmed_link = entrez_gene
	if string.match(entrez_gene, '%w+') and entrez_gene ~= localNotApplicableStr then
		pubmed_link = frame:extensionTag("ref",frame:expandTemplate{ title = 'cite_web', args = { title ="Human PubMed Reference:" , url = "https://www.ncbi.nlm.nih.gov/sites/entrez?db=gene&cmd=Link&LinkName=gene_pubmed&from_uid="..entrez_gene, website = "National Center for Biotechnology Information, U.S. National Library of Medicine" } } )
	end
	
	local pubmed_mm_link = entrez_gene_mm
	if string.match(entrez_gene_mm, '%w+') and entrez_gene_mm ~= localNotApplicableStr then
		pubmed_mm_link = frame:extensionTag("ref",frame:expandTemplate{ title = 'cite_web', args = { title ="Mouse PubMed Reference:" , url ="https://www.ncbi.nlm.nih.gov/sites/entrez?db=gene&cmd=Link&LinkName=gene_pubmed&from_uid="..entrez_gene_mm, website = "National Center for Biotechnology Information, U.S. National Library of Medicine" } } )
	end

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', 4)
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('table')
					:attr('class', ortholog_class)
					:css('padding', '0')
					:css('border', 'none')
					:css('margin', '0')
					:css('width', '100%')
					:css('text-align', 'left')
					:tag('tr')
						:tag('th')
							:attr('colspan', '4')
							:css('text-align', 'center')
							:css('background-color', titleBGcolor)
			                :css('color', 'inherit')
							:wikitext(title)
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext("زاتھ") -- **lclz**
							:done()
						:tag('td')
							:wikitext("'''اِنسان'''") -- **lclz**
							:done()
						:tag('td')
							:wikitext("'''گَگُر'''") -- **lclz**
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(entrezTitle)
							:done()
						:tag('td')
							:tag('table')
								:attr('class', entrez_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(entrez_default)
											:done()
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:attr('class', 'plainlinks')
											:wikitext(entrez_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:tag('td')
							:tag('table')
								:attr('class', entrez_mm_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(entrez_mm_default)
											:done()
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:attr('class', 'plainlinks')
											:wikitext(entrez_mm_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(ensemblTitle)
							:done()
						:tag('td')
							:tag('table')
								:attr('class', ensembl_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(ensembl_default)
											:done()
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:attr('class', 'plainlinks')
											:wikitext(ensembl_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:tag('td')
							:tag('table')
								:attr('class', ensembl_mm_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(ensembl_mm_default)
											:done()
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:attr('class', 'plainlinks')
											:wikitext(ensembl_mm_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(uniprotTitle)
							:done()
						:tag('td')
							:tag('table')
								:attr('class', uniprot_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(uniprot_default)
											:done()
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:attr('class', 'plainlinks')
											:wikitext(uniprot_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:tag('td')
							:tag('table')
								:attr('class', uniprot_mm_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:tag('span')
											:attr('class', 'plainlinks')
											:wikitext(uniprot_mm_default)
											:done()
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:attr('class', 'plainlinks')
											:wikitext(uniprot_mm_link)
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(refseq_mRNATitle)
							:done()
						:tag('td')
							:tag('table')
								:attr('class', refseq_mRNA_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:attr('class', 'plainlinks')
										:wikitext(refseq_mRNA_default)
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:tag('span')
												:attr('class', 'plainlinks')
												:wikitext(refseq_mRNA_link)
												:done()
											:done()
										:done()
									:done()
								:done()
							:done()
						:tag('td')
							:tag('table')
								:attr('class', refseq_mRNA_mm_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:attr('class', 'plainlinks')
										:wikitext(refseq_mRNA_mm_default)
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:tag('span')
												:attr('class', 'plainlinks')
												:wikitext(refseq_mRNA_mm_link)
												:done()
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(refseq_protTitle)
							:done()
						:tag('td')
							:tag('table')
								:attr('class', refseq_prot_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:attr('class', 'plainlinks')
										:wikitext(refseq_prot_default)
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:tag('span')
												:attr('class', 'plainlinks')
												:wikitext(refseq_prot_link)
												:done()
											:done()
										:done()
									:done()
								:done()
							:done()
						:tag('td')
							:tag('table')
								:attr('class', refseq_prot_mm_collapse)
								:css('padding', '0')
								:css('border', 'none')
								:css('margin', '0')
								:css('width', '100%')
								:css('text-align', 'right')
								:tag('tr')
									:tag('th')
										:attr('colspan', '1')
										:attr('class', 'plainlinks')
										:wikitext(refseq_prot_mm_default)
										:done()
									:done()
								:tag('tr')
									:tag('td')
										:attr('colspan', '1')
										:tag('p')
											:tag('span')
												:attr('class', 'plainlinks')
												:wikitext(refseq_prot_mm_link)
												:done()
											:done()
										:done()
									:done()
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(locTitle)
							:done()
						:tag('td')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(chr_loc_link)
								:done()
							:done()
						:tag('td')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(chr_loc_mm_link)
								:done()
							:done()
						:done()
					:tag('tr')
						:tag('th')
							:attr('scope', 'row')
							:css('background-color', sideTitleBGcolor)
			                :css('color', 'inherit')
							:wikitext(pubmedTitle)
							:done()
						:tag('td')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(pubmed_link)
								:done()
							:done()
						:tag('td')
							:tag('span')
								:attr('class', 'plainlinks')
								:wikitext(pubmed_mm_link)
								:done()
							:wikitext(category_chromosome)
							:done()
						:done()
					:done()
				:done()
			:done()
end

function p.formatRow(title)
	root
		:tag('tr')
			:tag('td')
				:attr('colspan', '4')
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:tag('table')
					:css('padding', '0')
					:css('border', 'none')
					:css('margin', '0')
					:css('width', '100%')
					:css('text-align', 'left')
					:tag('tr')
						:css('background-color', titleBGcolor)
			            :css('color', 'inherit')
						:css('text-align', 'center')
						:tag('th')
							:attr('colspan',"2")
							:wikitext(title)
							:done()
						:done()
					:done()
				:done()
			:done()
end

function p.renderFooter(Qid, Qid_mm)
	local text = "[[Wikidata]]"
	local hs_link = "[[d:"..Qid.."|اِنسان وُچھِو/اؠڈِٹ کٔرِو]]" -- **lclz**
	local mm_link = ""
	local link_no_hs
	local link_no_mm

	if Qid_mm == "" then
		link_no_mm = 0
		link_no_hs = 4
	else
		link_no_mm = 2
		link_no_hs = 2
		mm_link = "[[d:"..Qid_mm.."|گَگُر وُچھِو/اؠڈِٹ کٔرِو]]" -- **lclz**
	end

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', '4')
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
				:wikitext(text)
				:done()
		:tag('tr')
			:tag('td')
				:attr('colspan', '4')
				:css('text-align', 'center')
				:css('background-color', rowBGcolor)
			    :css('color', 'inherit')
			:tag('table')
				:css('padding', '0')
				:css('border', 'none')
				:css('margin', '0')
				:css('width', '100%')
				:css('text-align', 'center')
				:tag('tr')
					:tag('td')
						:attr('colspan', link_no_hs)
						:css('background-color', rowBGcolor)
			            :css('color', 'inherit')
						:css('text-align', 'center')
						:wikitext(hs_link)
						:done()
					:tag('td')
						:attr('colspan', link_no_mm)
						:css('background-color', rowBGcolor)
			            :css('color', 'inherit')
						:css('text-align', 'center')
						:wikitext(mm_link)
						:done()
					:done()
				:done()
			:done()
	root:done()
end

function p.rowLabel(label)
	root
		:tag('tr')
		:tag('th')
			:attr('rowspan', '2')
			:css('background-color', sideTitleBGcolor)
			:css('color', 'inherit')
			:css('width', '43px')
			:wikitext(label)
end

function p.getLabel(entity)
	local data = entity
	local f = {'labels','ks','value'} -- **lclz** Attempt to pull Kashmiri first if set

	local i = 1
	while true do
		local index = f[i]
		if not index then
			if type(data) == "table" then
				return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)
			else
				return tostring(data)
			end
		end

		data = data[index] or data[tonumber(index)]
		if not data then
			return p.getLabelFallback(entity) -- fallback mechanism
		end

		i = i + 1
	end
end

-- Fallback to EN if KS label is missing
function p.getLabelFallback(entity)
	local data = entity
	local f = {'labels','en','value'}
	local i = 1
	while true do
		local index = f[i]
		if not index then
			if type(data) == "table" then
				return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)
			else
				return tostring(data)
			end
		end
		data = data[index] or data[tonumber(index)]
		if not data then
			return
		end
		i = i + 1
	end
end

function p.getValue(entity, propertyID, return_val, sep, stated_in)
	local claims
	if return_val == nil then return_val = "" end
	if sep == nil then sep = " " end
	if entity and entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
			local out = {}
			for k, v in pairs(claims) do
				local datav = mw.wikibase.label(v.mainsnak.datavalue.value["id"])
				if datav == nil then datav = " " end
				local is_from_given_source = true
				if stated_in ~= nil then
					is_from_given_source = false
					if v.references then
						for rk, rv in pairs(v.references) do
							local ref_val = rv.snaks.P248
							if ref_val then
								for stated_k, stated_v in pairs(ref_val) do
									if (stated_v and stated_v.snaktype == "value" and stated_v.datavalue.type == "wikibase-entityid") then
										local ref_stated_in_val = stated_v.datavalue.value["id"]
										if ref_stated_in_val == stated_in then is_from_given_source = true end
									end
								end
							end
						end
					end
				end
				if is_from_given_source then
					out[#out + 1] = datav
				end
			end
			return table.concat(out, sep)
		else
			return entity:formatPropertyValues(propertyID).value
		end
	else
		return return_val
	end
end

function p.getValueProtein(protein_entities, propertyID, return_val)
	if return_val == nil then return_val = "" end
	local sep = "،" -- **lclz**
	local overall_results = {}
	for key, val in pairs(protein_entities) do
		local claims
		local entity = val
		if entity and entity.claims then
			claims = entity.claims[propertyID]
		end
		if claims then
			local results
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				local out = {}
				for k, v in pairs(claims) do
					local datav = mw.wikibase.getLabel("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					if datav == nil then datav = " " end
					out[#out + 1] = datav
				end
				results = table.concat(out, sep)
			else
				results = entity:formatPropertyValues(propertyID).value
			end
			overall_results[#overall_results+1] = results
		end
	end

	local str_overall_results = table.concat(overall_results, sep)
	if string.match(str_overall_results, '%w+') then
		return str_overall_results
	else
		return return_val
	end
end

function p.getQid(entity)
	local Qid
	if entity and entity.id then
		Qid = entity.id
		return Qid
	else
		return ""
	end
end

function p.getRefseq_mRNA(entity, propertyID, return_val)
	if return_val == nil then return_val = "" end
	local input_rank = "RANK_PREFERRED"
	local claims

	if entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid" ) then
			local out = {}
			for k, v in pairs(claims) do
				local sitelink = mw.wikibase.getSitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])
				local label = mw.wikibase.getLabel("Q" .. v.mainsnak.datavalue.value["numeric-id"])
				if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end

				if sitelink then
					out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
				else
					out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]"
				end
			end
			return table.concat(out, "، ") -- **lclz**
		else
			local results = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
			return results
		end
	else
		return return_val
	end
end

function p.getRefseq_protein(protein_entities, propertyID, return_val)
local sep = localSeparatorStr
local overall_results = {}

	for key, val in pairs(protein_entities) do

		local claims
		local entity = val
		if entity.claims then
			claims = entity.claims[propertyID]
		end
		if claims then
			local results
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid" ) then
				local out = {}
				for k, v in pairs(claims) do
					local datav = mw.wikibase.getLabel("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					if datav == nil then datav = " " end
					out[#out + 1] = datav
				end
				results = table.concat(out, sep)
			else
				results = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
			end
			overall_results[#overall_results+1] = results
		end

	end
	local str_overall_results = table.concat(overall_results, sep)
	return str_overall_results

end

function p.getImage(entity, propertyID, sep, imgsize)
	local claims
	if entity and entity.claims then
		claims = entity.claims[propertyID]
	end

	if claims then
		if (claims[1] and claims[1].mainsnak.datatype == "commonsMedia") then
			local out = {}
			for k, v in pairs(claims) do
				local filename = v.mainsnak.datavalue.value
				out[#out + 1] = "[[File:" .. filename .. "|" .. imgsize .. "]]"
			end
				return table.concat(out, sep)
		else
			return ""
		end
	else
		return ""
	end
end

function p.getPDB(protein_entities)
	local pdb_propertyID = "P638"
	local overall_results = {}
	for key, val in pairs(protein_entities) do
		local claims
		local entity = val
		if entity and entity.claims then
			claims = entity.claims[pdb_propertyID]
		end
		local sitelink = "https://www.rcsb.org/structure/"
		if claims then
			local results
			if (claims[1] and claims[1].mainsnak.snaktype == "value") then

				local out = {}
				for k, v in pairs(claims) do
					local label = mw.wikibase.getLabel(v.mainsnak.datavalue.value)
					if label == nil then label = v.mainsnak.datavalue.value end

					if sitelink then
						out[#out + 1] = "[" .. sitelink .. label .. " " ..label .. "]"
					else
						out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value .. "|" .. label .. "]]"
					end
				end
				results = table.concat(out, "، ") -- **lclz**
			else
				results = entity:formatPropertyValues(pdb_propertyID, mw.wikibase.entity.claimRanks).value
			end
			overall_results[#overall_results+1] = results
		end
	end
	return table.concat(overall_results, "،%%s") -- **lclz**
end

function p.getAliases(entity)
	if entity['aliases'] ~= nil then
		local test = entity['aliases']['ks'] -- **lclz** Attempt to pull Kashmiri first
		if not test then test = entity['aliases']['en'] end
		if test then
			local a = ''
			for key, value in ipairs(test) do
				a = a .. '، ' .. value['value'] -- **lclz**
			end
			return a
		else
			return ""
		end
	else
		return ""
	end
end

function p.getChromosomeLoc(entity, propertyID, prefix)
	local output = ""
	local sep = " "
	local qualifierID = "P659"
	local newest_build = "0"
	local claims
	if entity and entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		if (claims[1] ) then
			for k, v in pairs(claims) do
				local location = v.mainsnak.datavalue.value
				local quals
				if v.qualifiers then
					quals = v.qualifiers.P659
				end
				if quals then
					for qk, qv in pairs(quals) do
						local qual_obj_id = "Q"..qv.datavalue.value["numeric-id"]
						local qual_obj = mw.wikibase.getEntity(qual_obj_id)
						local alias = ""
						if qual_obj["aliases"] ~= nil then
							local test = qual_obj["aliases"]["en"]
							if test then
								for key, value in ipairs(test) do
									if string.match(value['value'], prefix) then
										alias = value['value']
										local build_no = alias:gsub(prefix,"")
										if build_no > newest_build then
											output = location
											newest_build = build_no
										end
									end
								end
							end
						end
					end
				else
					output = location
				end
			end
			return output
		else
			return ""
		end
	else
		return ""
	end
end

function p.getAliasFromGenomeAssembly(entity, prefix)
	local output = ""
	local sep = " "
	local propertyID = "P644"
	local qualifierID = "P659"

	local newest_build = "0"
	local claims
	if entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		if (claims[1] ) then
			for k, v in pairs(claims) do
				local location = ''
				local quals
				if v.qualifiers then
					quals = v.qualifiers.P659
				end
				if quals then
					for qk, qv in pairs(quals) do
						local qual_obj_id = "Q"..qv.datavalue.value["numeric-id"]
						local qual_obj = mw.wikibase.getEntity(qual_obj_id)
						local alias = ""
						if qual_obj["aliases"] ~= nil then
							local test = qual_obj["aliases"]["en"]
							if test then
								for key, value in ipairs(test) do
									if string.match(value['value'], prefix) then
										alias = value['value']
										local build_no = alias:gsub(prefix,"")
										if build_no > newest_build then
											newest_build = build_no
										end
									end
								end
							end
						end
					end
				else
					output = location
				end
			end
			return prefix .. newest_build
		else
			return ""
		end
	else
		return ""
	end
end

function p.trimChromosome(entity)
	local string_to_trim = p.getValue(entity, "P1057")
	local out = ''

	if string.find(string_to_trim, 'chromosome MT') or string.find(string_to_trim, 'mitochondri') then
		out = "MT"
	elseif string.find(string_to_trim, 'chromosome') then
		out = string.match(string_to_trim, "%d+")
		if out == nil then
			out = string.match(string_to_trim, "X") or string.match(string_to_trim, "Y")
		end
	end
	return out
end

function p.locToMb(num, idp)
	num = tonumber(num)
	if num == nil then
		return ""
	else
		local mb = num/1000000
		local mult = 10^(idp or 0)
		return math.floor(mb * mult + 0.5) / mult
	end
end

function p.isempty(s)
	return s == nil or s == ''
end

function p.getGO(protein_entities, propertyID)
	local overall_results = {}
	local results = ""

	for key, val in pairs(protein_entities) do
		local claims
		local entity = val
		if entity.claims then
			claims = entity.claims[propertyID]
		end
		local propertyID_child = "P686"

		if claims then
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				for k, v in pairs(claims) do
					local itemID_child = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
					local entity = mw.wikibase.getEntity(itemID_child)
					local claims
					local result_GOID = ''
					if entity and entity.claims then claims = entity.claims[propertyID_child] end
					if claims then
						result_GOID = entity:formatPropertyValues(propertyID_child, {mw.wikibase.entity.claimRanks.RANK_NORMAL}).value
					else
						result_GOID = nil
					end
					local sitelink = "http://amigo.geneontology.org/amigo/term/"
					local label = mw.wikibase.getLabel("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
					local wiki_link = ""
					if sitelink and result_GOID ~= nil then
						wiki_link = "\n* [" .. sitelink .. result_GOID .. " " .. label .."]"
					else
						wiki_link = "\n* [[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]"
					end
					overall_results[#overall_results+1] = wiki_link
				end
			else
				results = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
			end
		end
	end

	local hash = {}
	local res = {}

	for _,v in ipairs(overall_results) do
		if (not hash[v]) then
			res[#res+1] = v
			hash[v] = true
		end
	end
	return table.concat(res, "")
end

local function getReference(qID, entity, property_id, ref_index)
	local f = {"claims",property_id, ref_index, "references"}
	local id = qID
	local data = entity
	if not data then
		return nil
	end

	local i = 1
	while true do
		local index = f[i]
		if not index then
			if type(data) == "table" then
				return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)
			else
				return tostring(data)
			end
		end

		data = data[index] or data[tonumber(index)]
		if not data then
			return ""
		end
		i = i + 1
	end
end

function p.getDisease(entity, propertyID)
	local claims
	local return_val = ""
	if entity and entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
			local out = {}
			local datasource = {}
			for k, v in pairs(claims) do
				local datav = mw.wikibase.getLabel("Q" .. v.mainsnak.datavalue.value["numeric-id"])
				if datav == nil then datav = " " end
				local id = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
				local linkTarget = mw.wikibase.getSitelink(id)
				local refLink = ""
				local ref = ""
				ref = getReference("", entity, "P2293", k)
				if (ref ~= nil and ref ~= '') then
					refLink = ref
				end

				if linkTarget then
					out[#out + 1] = "[["..linkTarget.."|"..datav.."]]"
				else
					out[#out + 1] = "[[:d:" .. id .. "|" .. datav .. "]]"
				end
				datasource[#out] = refLink
			end
			return out, datasource
		else
			return return_val, return_val
		end
	else
		return return_val
	end
	return return_val
end

function p.getDrug(protein_entities, propertyID)
	local out = {}
	local datasource = {}
	local pname = {}
	local pqid = {}

	for key, val in pairs(protein_entities) do
		local claims
		local entity = val
		local name = check_values(p.getLabel,{entity})
		if entity.claims then
			claims = entity.claims[propertyID]
		end
		local protein_id
		if entity then protein_id = entity.id else protein_id = "" end
		if claims then
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				for k, v in pairs(claims) do
					local datav = mw.wikibase.getLabel("Q" .. v.mainsnak.datavalue.value["numeric-id"])

					if datav == nil then datav = "" end
					local id = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
					local linkTarget = mw.wikibase.getSitelink(id)
					local refLink = ""
					local ref = getReference(protein_id, entity, "P129",k)
					if (ref ~= nil and ref ~= '') then
						refLink = ref
					end
					if linkTarget then
						out[#out + 1] = "[["..linkTarget.."|"..datav.."]]"
					else
						out[#out + 1] = "[[:d:" .. id .. "|" .. datav .. "]]"
					end
					pname[protein_id] = name
					pqid[#out] = protein_id
					datasource[#out] = refLink
				end
			end
		end
	end
	return out, datasource, pqid, pname
end

function p.separateWithComma(bp)
	local commaSeparated = bp
	while true do
		local k
		commaSeparated, k = string.gsub(commaSeparated, "^(-?%d+)(%d%d%d)", '%1،%2') -- **lclz**
		if k == 0 then
			break
		end
	end
	return commaSeparated
end

return p