انتقل إلى المحتوى

وحدة:Infobox gene

من ويكيبيديا، الموسوعه الحره

يمكن إنشاء صفحة توثيق الوحدة في وحدة:Infobox gene/شرح

local p = { }

local navbar = require('Module:Navbar')._navbar
local infobox = require('Module:Infobox3cols').infobox
local infoboxImage = require('Module:InfoboxImage').InfoboxImage



--on a page {{#invoke:Sandbox/genewiki/alllua|getTemplateData|QID=Q14865053}}
--in debug window 
--frame = mw.getCurrentFrame()
--frame.args = {QID="Q14865053"} Q18031325
--print(p.getTemplateData(frame))
p.getTemplateData = function(frame)

	--make some guesses about whether the provided QID is a good one
	--could expand here if we had some kind of error handling framework
	--did we get it from the page
	local root_qid = mw.text.trim(frame.args['QID']  or "") --try to get it from the args
	local mm_qid = ""
	--pull all the entity objects that we will need
	local	entity = {} 
	local	entity_protein = {}
	local	entity_mouse = {}
	local	entity_mouse_protein = {}
	local	checkOrtholog = "" --flag used to see if mouse data avaliable
	
	local mouse_propertyID = "P684"
	local protein_propertyID = "P688" 

	--get root gene entity
	if root_qid == "" then
		entity = mw.wikibase.getEntityObject()
		if entity then root_qid = entity.id else root_qid = "" end
			
	else
		--assuming we think its good make one call to retrieve and store its wikidata representation
		entity = mw.wikibase.getEntity(root_qid)
	end
	
  	--need to figure out if it is protein or gene here
	local subclass = p.getValue(entity, "P279") or ""
	if string.find(subclass, 'protein') then --if protein switch entity to gene
		if entity.claims then
	 		claims = entity.claims["P702"] --encoded by
	 	end
		if claims then
			--go through each index and reassign entity
			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 --this would be problematic if multiple genes for the protein
					local itemID = "Q" .. claims[#entity + 1].mainsnak.datavalue.value["numeric-id"]
					entity[#entity + 1] = mw.wikibase.getEntity(itemID)
					root_qid = itemID
				end
				
			end --will return nothing if no claims are found
		end
		 entity = mw.wikibase.getEntity(root_qid) 	
	 end
	
	
	--get the other related entities
	if entity then
		local claims = ""
	 	--get protein entity object
	 	if entity.claims then
	 		claims = entity.claims[protein_propertyID]
	 	end
		if claims then
			--go through each index and then make entity_protein indexed
			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 --will return nothing if no claims are found
		end
	
	 	--get mouse entity object
	 	if entity.claims then
			claims = entity.claims[mouse_propertyID]
		end
		if claims then
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				local mouse_itemID = "Q" .. claims[1].mainsnak.datavalue.value["numeric-id"]
				mm_qid = mouse_itemID 
				entity_mouse = mw.wikibase.getEntity(mouse_itemID)
				checkOrtholog = 1 
			end --will return nothing if no claims are found
		else
			checkOrtholog = 0
		end
	
	 	--get mouse protein entity object
	 	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 --will return nothing if no claims are found
	 	end	
	end
	
	
	
	if entity then --only require the main gene entity
		--a list variables of all the data in the info box
		local name = p.getLabel(entity)
		local entrez_gene = p.getValue(entity, "P351")
		local entrez_gene_mm = p.getValue(entity_mouse, "P351", "n/a")
		local image = p.getImage(entity, "P18", " ", "frameless") -- Use default image size
		local uniprotID_hs = p.getValueProtein(entity_protein, "P352", "n/a")
	    local uniprotID_mm = p.getValueProtein(entity_mouse_protein, "P352", "n/a")
	    local pdbIDs = p.getPDB(entity_protein) --makes a list with links to RCSB
	    local aliases = p.getAliases(entity)
	    local gene_symbol = p.getValue(entity, "P353")
	    local hgnc_id = p.getValue(entity, "P354")
	    local homologene_id = p.getValue(entity, "P593")
	    local omim_id = p.getValue(entity, "P492")
	    local mgi_id = p.getValue(entity_mouse, "P671")
	    local ChEMBL_id = p.getValue(entity_protein, "P592") 
	    local IUPHAR_id = p.getValue(entity_protein, "P595")
	    local ec_no = p.getValueProtein(entity_protein, "P591")
	    local mol_funct = p.getGO(entity_protein, "P680")
	    local cell_comp = p.getGO(entity_protein, "P681")
	    local bio_process = p.getGO(entity_protein, "P682")
	    local expression_images = p.getImage(entity,"P692","<br><br>","frameless")
	    local ensembl = p.getValue(entity, "P594", "n/a")
	    local ensembl_mm = p.getValue(entity_mouse, "P594", "n/a")
	    local refseq_mRNA = p.getRefseq_mRNA(entity, "P639", "n/a")
	    local refseq_mRNA_mm = p.getRefseq_mRNA(entity_mouse, "P639", "n/a")
	    local refseq_prot = p.getRefseq_protein(entity_protein, "P637", "n/a")
	    local refseq_prot_mm = p.getRefseq_protein(entity_mouse_protein, "P637", "n/a")
		local gstart = p.getChromosomeLoc(entity, "P644", "hg")
		local gend = p.getChromosomeLoc(entity, "P645", "hg")
		local chr = p.trimChromosome(entity) 
		local db = p.getAliasFromGenomeAssembly(entity,"hg")
		local gstart_mm = p.getChromosomeLoc(entity_mouse, "P644", "mm")
		local gend_mm = p.getChromosomeLoc(entity_mouse, "P645", "mm") 
		local db_mm = p.getAliasFromGenomeAssembly(entity_mouse,"mm")
		local chr_mm = p.trimChromosome(entity_mouse)
		
		--define Global Color Scheme
		rowBGcolor = '#eee'
		titleBGcolor = '#ffcfd2'
		sideTitleBGcolor = '#fbf8cc'

		p.createTable()
    	p.renderUpperTitle(name)
    	--p.renderCaption()
    	p.renderImage(image)
    	p.renderAvailableStructures(uniprotID_hs, uniprotID_mm, checkOrtholog, pdbIDs) --PDB info
		p.renderIdentifiers(aliases, hgnc_id, gene_symbol, homologene_id, omim_id, mgi_id, ChEMBL_id, IUPHAR_id, ec_no, entrez_gene)
		if (mol_funct ~= "" and cell_comp ~= "" and bio_process ~= "") then
			p.renderGeneOntology(mol_funct, cell_comp, bio_process, uniprotID_hs)
		end
		if expression_images ~= ""  then
			p.renderRNAexpression(expression_images, entrez_gene)
		end
		p.renderOrthologs(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)
		p.renderFooter(root_qid, mm_qid)        
   		
		return tostring(root)

	else return "An Error has occured retrieving Wikidata item for infobox"
	end	
end

p.createTable = function(subbox)

    if subbox == 'sub' then --doesn't work 
    	 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')
            :css('background-color', 'transparent')
           
    else
    	root = mw.html.create('table')
    	root
    		:addClass('infobox')
        	:css('width', '26.4em')
        	 :newline()
    end

end

--Title above image
p.renderUpperTitle = function(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)
                :newline()
end

--This is a place holder for the image caption, which is stored in wikicommons comments unsure how to access 
p.renderCaption = function(entity)
	--caption
end

--gets default image
p.renderImage = function(image)

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



p.renderAvailableStructures = function(uniprotID_hs, uniprotID_mm, checkOrtholog, pdbIDs)

    local title = 'بنى متوفرة'
    local pdb_link = "[[بنك بيانات البروتين]]"
    local searchTitle = "" 
    local listTitle = "قائمة رموز معرفات بنك بيانات البروتين"
    local PDBe_base = 'http://www.ebi.ac.uk/pdbe/searchResults.html?display=both&amp;term='
    local RCSB_base = 'http://www.rcsb.org/pdb/search/smartSubquery.do?smartSearchSubtype=UpAccessionIdQuery&amp;accessionIdList='
    local url_uniprot = " " 
    
    
    if checkOrtholog == 1 and uniprotID_mm ~= 'n/a' then
    	searchTitle = 'Ortholog search: '
    	url_uniprot = uniprotID_mm
    else
    	searchTitle = 'Human UniProt search: '
    	url_uniprot = uniprotID_hs
    end
    local PDBe_list = " " --create a list with " or " if there is more than one uniprot
    --get first uniprot in a list
	if url_uniprot:match("([^,]+),") then--first check if there is a list if not just assume one value
		PDBe_list = string.gsub(url_uniprot, ",", "%%20or%%20") --add or's inststead of commas
	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 --if there aren't any PDB_ID don't display this part of the infobox
    	--p.formatRow(title)---how to not close the tags is a mystery and I could condense code once I figure out
    	root
	 		:tag('tr')
        	:tag('td')
   				:attr('colspan', 4)
            	:css('text-align', 'center')
            	:css('background-color', rowBGcolor)
            	:newline()
            	--p.createTable('sub')
            	:tag('table') 
            		:css('padding', '0')
            		:css('border', 'none')
            		:css('margin', '0')
            		:css('width', '100%')
            		:css('text-align', 'left')
            		:newline()

            		:tag('tr')    --create title header
            			:tag('th')
            			:attr('colspan', '4')
            			:css('text-align', 'center')
            			:css('background-color',titleBGcolor)
            			:wikitext(title)
            			:done()
            			:newline()
            		
    			--p.rowLabel(pdb_link) 
    			:tag('tr')
        		:tag('th')
        			:attr('rowspan', '2')
        			:css('background-color', sideTitleBGcolor)
        			:css('width', '43px')
         			:wikitext(pdb_link)
        			:done()
        			
            			:tag('td')
            				:attr('colspan', '2')
            				:css('background-color', rowBGcolor)
            				:wikitext(searchTitle)
            				:tag('span')
            					:attr('class', 'plainlinks')
            					:wikitext(PDBe)
            					:wikitext(RCSB)
            					:done()
            				:done()
            		:done() --this may not be needed
            		:newline()
            		--new row for collapsible list of PDB codes
            		: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')
            			:newline()
            			:tag('tr')
            				:css('background-color',titleBGcolor)
            				:css('text-align', 'center')
            				:newline()
            				:tag('th')
            					:attr('colspan', '2')
            					:wikitext(listTitle)
            				:done()
            				:newline()
            			:done()
            			:tag('tr')
            			:tag('td')
            				:attr('colspan', '2')
            				:css('background-color', rowBGcolor)
            			:newline()
            			:tag('p')
            				:tag('span')
            					:attr('class', 'plainlinks')
            					:wikitext(pdbIDs)
            					:done()
            				 :newline()
    else
    	return ""
	end

end

p.renderIdentifiers = function(aliases, hgnc_id, gene_symbol, homologene_id, omim_id, mgi_id, ChEMBL_id, IUPHAR_id, ec_no, entrez_gene)
	local title = "معرفات"
	local label_aliases = "[https://ar.wikipedia.org/wiki/منظمة_جينوم_الإنسان أسماء بديلة]"
	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 = "[http://www.genenames.org/cgi-bin/gene_symbol_report?hgnc_id="..hgnc_id.." "..gene_symbol.."], "
		end
	end
	aliases = string.gsub(aliases, gene_symbol, "") --get rid of gene name if in aliases list
	aliases = string.gsub(aliases, ", ,", ",") --remove comma from middle
	aliases = string.gsub(aliases, ", $", "") --remove comma from end
	aliases = string.gsub(aliases, "^,", "") --remove comma from start
	local label_ext_id = "معرفات خارجية"
	local omim = ""
	if string.match(omim_id, '%w+') then
		omim = "[[الوراثة المندلية البشرية عبر الإنترنت]]".." ".."[https://omim.org/entry/"..omim_id.." "..omim_id.."] "
	end
	local homolo =""
	if string.match(homologene_id, '%w+') then
		homolo = "[[HomoloGene|HomoloGene:]]".." ".."[http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=homologene&dopt=HomoloGene&list_uids="..homologene_id.." "..homologene_id.."] "
	end
	local genecards = "" 
	if string.match(entrez_gene, '%w+') then
		genecards = "[[GeneCards|GeneCards:]]".." ".."[http://www.genecards.org/cgi-bin/carddisp.pl?id_type=entrezgene&id="..entrez_gene.." "..entrez_gene.."] "
	end
	local mgi = "" 
	if string.match(mgi_id, '%w+') then
		mgi = "[[Mouse_Genome_Informatics|MGI:]]".." ".."[http://www.informatics.jax.org/marker/MGI:"..mgi_id.." "..mgi_id.."] "
	end
	local ChEMBL = ""
	if string.match(ChEMBL_id, '%w+') then
		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 = "[[الاتحاد الدولي للصيدلة الأساسية والسريرية]]".." ".."[http://www.guidetopharmacology.org/GRAC/ObjectDisplayForward?objectId="..IUPHAR_id.." "..IUPHAR_id.."] "
	end
	local label_EC = "[[رقم التصنيف الإنزيمي]]"
	ec_no = string.gsub(ec_no, "%d%.%d+%.%d+%.%-,", "")--remove those with"-" in list
	ec_no = string.gsub(ec_no, "%d%.%d+%.%d+%.%-", "")--remove those with"-" not in list
	local link_ec_no = string.gsub(ec_no, "," ,"+") --create format for link

	local EC = "[http://www.genome.jp/dbget-bin/www_bget?enzyme+" .. link_ec_no .. " " .. ec_no .. "]"
	
	root
		:tag('tr')
        	:tag('th')
        		:attr('colspan', '4')
        		:css('text-align', 'center')
        		:css('background-color', titleBGcolor)
         		:wikitext(title)
        		:newline()

        	:tag('tr')
        		:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(label_aliases)
        			:done()
        			:newline()
        		:tag('td')
        			:attr('colspan','3')
        			:css('background', rowBGcolor)
        			:tag('span')
            		 	:attr('class', 'plainlinks')
        			 	:wikitext(symbol_url)
        				:done()
        			:wikitext(aliases)
        		:done()
         :newline()
         
         :tag('tr')
         	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
         		:wikitext(label_ext_id)
        		:done()
        		:newline()
        	:tag('td')
        		:attr('colspan', '3')
        		:css('background-color', rowBGcolor)
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(omim)
        			:wikitext(mgi)
        			:wikitext(homolo)
        			:wikitext(ChEMBL)
        			:wikitext(IUPHAR)
        			:wikitext(genecards)
        			:done()
        		 	:newline()
        		 :done()
	if ec_no ~= ""  then
	  root
         :tag('tr')
         	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
         		:wikitext(label_EC)
        		:done()
        		:newline()
        	:tag('td')
        		:attr('colspan', '3')
        		:css('background-color', rowBGcolor)
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(EC)
        			:done()
        		 	:newline()
        		 :done()
    end
end

p.renderGeneOntology = function(mol_funct, cell_comp, bio_process, uniprotID)
	local title = "[[علم الوجود الجيني]]"
	local mol_funct_title = "وظائف جزيئية"
	local cell_comp_title = "مكونات خلوية"
	local bio_process_title = "عمليات حيوية"
	local amigo_link = "[http://amigo.geneontology.org/" .. " Amigo]"
	local quickGO_link = "[http://www.ebi.ac.uk/QuickGO/" .. " QuickGO]"


	root
		:tag('tr')
        	:tag('td')
   				:attr('colspan', 4)
            	:css('text-align', 'center')
            	:css('background-color', rowBGcolor)
            	:newline()
            	--p.createTable('sub')
            	:tag('table')
            		:attr('class', 'collapsible collapsed')
            		:css('padding', '0')
            		:css('border', 'none')
            		:css('margin', '0')
            		:css('width', '100%')
            		:css('text-align', 'right')
            		:newline()
            		            		
            		:tag('tr') --create title bar
        				:tag('th')
        				:attr('colspan', '4')
        				:css('text-align', 'center')
        				:css('background-color', titleBGcolor)
         				:wikitext(title)
        				:done()
        				:newline()
            :tag('tr')
            	:tag('th')
            		:css('background-color', sideTitleBGcolor)
            		:wikitext(mol_funct_title)
            		:done()
            	:tag('td')
            		:css('background-color', rowBGcolor)
            		:newline()
            		:tag('span')
            			:attr('class', 'plainlinks')
            			:wikitext(mol_funct)
            		:done()
            		:newline()
            	:done()
            :tag('tr')
            	:tag('th')
            		:css('background-color', sideTitleBGcolor)
            		:wikitext(cell_comp_title)
            		:done()
            	:tag('td')
            		:css('background-color', rowBGcolor)
            		:newline()
            		:tag('span')
            			:attr('class', 'plainlinks')
            			:wikitext(cell_comp)
            		:done()
            		:newline()
            	:done()
              :tag('tr')
            	:tag('th')
            		:css('background-color', sideTitleBGcolor)
            		:wikitext(bio_process_title)
            		:done()
            	:tag('td')
            		:css('background-color', rowBGcolor)
            		:newline()
            		:tag('span')
            			:attr('class', 'plainlinks')
            			:wikitext(bio_process)
            		:done()
            		:newline()
            	:done()
            	:tag('tr')
            		:tag('td')
            			:css('background-color', rowBGcolor)
            			:css('text-align', 'center')
            			:attr('colspan', '4')
            			:wikitext("المصادر:")
            			:wikitext(amigo_link)
            			:wikitext(" / ")
            			:wikitext(quickGO_link)
            		:done()
            :done()
					
end 

p.renderRNAexpression = function(expression_images, entrez_gene)
	local title = "نمط التعبير عن الحمض النووي الريبوزي"
	local biogps_link = "[http://biogps.org/gene/"..entrez_gene.."/ المزيد من بيانات التعبير المرجعية]"
	root
		:tag('tr')
        	:tag('th')
        		:attr('colspan', '4')
        		:css('text-align', 'center')
        		:css('background-color', titleBGcolor)
         		:wikitext(title)
        		:done()
        		:newline()
        :tag('tr')
        	:tag('td')
        		:attr('colspan', '4')
        		:css('text-align', 'center')
        		:css('background-color', rowBGcolor)
        		:wikitext(expression_images)
        		:done()
        		:newline()
        :tag('tr')
        	:tag('td')
        		:attr('colspan', '4')
        		:css('text-align', 'center')
        		:css('background-color', rowBGcolor)
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(biogps_link)
        		:done()
        		:newline()
end

p.renderOrthologs = function(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) 
	local title = "تماثلات متسلسلة"
	local entrezTitle = "[[أنتريه]]"
	--check all values to see if exists so don't create nonsense links in infobox must have some alphanumberic character
	local entrez_link = ""
	if string.match(entrez_gene, '%w+') and entrez_gene ~= "n/a" then
		entrez_link = "[http://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
	local entrez_mm_link = entrez_gene_mm
	if string.match(entrez_gene_mm, '%w+') and entrez_gene_mm ~= "n/a" then
		entrez_mm_link = "[http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&amp;cmd=retrieve&amp;dopt=default&amp;list_uids="..entrez_gene_mm.."&amp;rn=1 "..entrez_gene_mm.."]"
	end
	local ensemblTitle = "[[مشروع قاعده بيانات جينوم انسنبل|انسنبل]]"
	local ensembl_link = ensembl --default to result so n/a defined
	if string.match(ensembl, '%w+') and ensembl ~= "n/a" then
		ensembl_link = "[http://www.ensembl.org/Homo_sapiens/geneview?gene="..ensembl..";db=core".." "..ensembl.."]"
	end
	local ensembl_mm_link  = ensembl_mm
	if string.match(ensembl_mm, '%w+') and ensembl_mm ~= "n/a" then
		ensembl_mm_link = "[http://www.ensembl.org/Mus_musculus/geneview?gene="..ensembl_mm..";db=core".." "..ensembl_mm.."]"
	end

	--create lists of uniprot ID
	local uniprotTitle = "[[يونيبروت]]"
	local uniprot_url = "http://www.uniprot.org/uniprot/"
	
	local uniprot_link = "n/a"
	local uniprot_collapse
	local uniprot_default = ""
	--split string and loop through concatenate by <br>
	local split_uniprot = mw.text.split(uniprot, ",")
	local uniprot_link_list = {}	
	local hash = {} --storage to look for duplicated values 
	for k,v in ipairs(split_uniprot) do 
		if not hash[v] then --only add if not found previously..some encodes uniprotID dup in different encodes
			local label = mw.text.trim(v)
			local concat_uniprot_link = uniprot_url .. label
			if string.match(uniprot, '%w+') and uniprot ~= "n/a" then
				uniprot_link_list[#uniprot_link_list+1] = "[" .. concat_uniprot_link .. " " ..label .. "]"
			end
			hash[v] = true
		end
	end

	--if less than 5 don't create collapsible list
	if  table.getn(uniprot_link_list) < 5 then
		uniprot_collapse = "none"
		if uniprot_default == nil and table.getn(uniprot_link_list) == 0 then uniprot_link = "n/a" 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>'--get first 5 elements in table and use for display
	end
	
	if uniprot_link_list[#uniprot_link_list] then
		uniprot_link = table.concat(uniprot_link_list, "<br>")
	end
	
    --mouse uniprot lists
	local uniprot_mm_link = "n/a"
	local uniprot_mm_collapse
	local uniprot_mm_default = ""
	--split string and loop through concatenate by <br>
	local split_uniprot_mm = mw.text.split(uniprot_mm, ",")
	local uniprot_mm_link_list = {}	
	for k,v in ipairs(split_uniprot_mm) do 
		local label = mw.text.trim(v)
		local concat_uniprot_link = uniprot_url .. label
		if string.match(uniprot_mm, '%w+') and uniprot_mm ~= "n/a" then
			uniprot_mm_link_list[#uniprot_mm_link_list+1] = "[" .. concat_uniprot_link .. " " ..label .. "]"
		end
	end

	--if less than 5 don't create collapsible list
	if  table.getn(uniprot_mm_link_list) < 5 then
		uniprot__mm_collapse = "none"
		if uniprot_mm_default == nil and table.getn(uniprot_mm_link_list) == 0 then uniprot_mm_link = "n/a" 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>' --get first 5 elements in table and use for display
	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 = "http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?val="
	local refseq_mRNATitle = "RefSeq ([[مرسال الحمض النووي الريبوزي|رنا مرسال.]])"
	
	--create list of links for refSeq mRNA
	local refseq_mRNA_link = "n/a"
	local refseq_mRNA_collapse
	local refseq_mRNA_default = ""
	--split string and loop through concatenate by <br>
	local split_refseq_mRNA = mw.text.split(refseq_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 ~= "n/a" then
			link_list[#link_list+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
		end
	end
	
	--if less than 5 don't create collapsible list
	if  table.getn(link_list) < 6 then
		refseq_mRNA_collapse = "none"
		if refseq_mRNA_default == nil and table.getn(link_list) == 0 then refseq_mRNA_link = "n/a" 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>'--get first 5 elements in table and use for display
	end
	
	if link_list[#link_list] then
		refseq_mRNA_link = table.concat(link_list, "<br>")
	end
	
	
	--create list of links for refSeq mRNA for mouse
	local refseq_mRNA_mm_link = "n/a"
	local refseq_mRNA_mm_collapse
	local refseq_mRNA_mm_default = ""
	local split_refseq_mRNA_mm = mw.text.split(refseq_mRNA_mm, ",")
	local link_list_mm = {}
	for k,v in ipairs(split_refseq_mRNA_mm) do
		if string.match(v, '%w+') and v ~= "n/a" then
			local label = mw.text.trim(v)
			local concat_ncbi_link = ncbi_link .. label
			if string.match(v, '%w+') and v ~= "n/a" then
				link_list_mm[#link_list_mm+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			end
		end
	end
	--if less than 5 don't create collapsible list
	if  table.getn(link_list_mm) < 6 then
		refseq_mRNA_mm_collapse = "none"
		if refseq_mRNA_mm_default == nil and table.getn(link_list_mm) == 0 then refseq_mRNA_mm_link = "n/a" 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>'--get first 5 elements in table and use for display
	end

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



	local refseq_protTitle = "[[ريفسيك]] (بروتين)"
	--create list of links for human refseq protein
	local refseq_prot_link = "n/a"
	local refseq_prot_collapse 
	local refseq_prot_default = ""
	local split_refseq_prot = mw.text.split(refseq_prot, ",")
	local link_list_prot = {}
	for k,v in ipairs(split_refseq_prot) do
		if string.match(v, '%w+') and v ~= "n/a" then
			local label = mw.text.trim(v)
			local concat_ncbi_link = ncbi_link .. label
			if string.match(v, '%w+') and v ~= "n/a" then
				link_list_prot[#link_list_prot+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			end
		end
	end
	--if less than 5 don't create collapsible list
	if  table.getn(link_list_prot) < 6 then
		refseq_prot_collapse  = "none"
		if refseq_prot_default == nil and table.getn(link_list_prot) == 0 then refseq_prot_link = "n/a" end
	else	
		refseq_prot_collapse = "collapsible collapsed"
		refseq_prot_default = table.remove(link_list_prot, 1) --get first element in table and use for display
	end

   
	if link_list_prot[#link_list_prot] then
		refseq_prot_link = table.concat(link_list_prot, "<br>")
	end
	
	
	--create list of links for mouse refseq protein
	local refseq_prot_mm_link = "n/a"
	local refseq_prot_mm_collapse
	local refseq_prot_mm_default = ""
	local split_refseq_prot_mm = mw.text.split(refseq_prot_mm, ",")
	local link_list_mm = {}
	for k,v in ipairs(split_refseq_prot_mm) do
		if string.match(v, '%w+') and v ~= "n/a" then
			local label = mw.text.trim(v)
			local concat_ncbi_link = ncbi_link .. label
			if string.match(v, '%w+') and v ~= "n/a" then
				link_list_mm[#link_list_mm+1] = "[" .. concat_ncbi_link .. " " ..label .. "]"
			end
		end
	end
	--if less than 5 don't create collapsible list
	if  table.getn(link_list_mm) < 6 then
		refseq_prot_mm_collapse  = "none"
		if refseq_prot_mm_default == nil and table.getn(link_list_mm) == 0 then refseq_prot_mm_link = "n/a" end
	else	
		refseq_prot_mm_collapse = "collapsible collapsed"
		refseq_prot_mm_default = table.remove(link_list_mm, 1) --get first element in table and use for display
	end
	if link_list_mm[#link_list_mm] then
		refseq_prot_mm_link = table.concat(link_list_mm, "<br>")
	end
	

	local locTitle = "الموقع ([[قاعدة بيانات جامعة كاليفورنيا (سانتا كروز)|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
		chr_loc_link = "[http://genome.ucsc.edu/cgi-bin/hgTracks?org=Human&db="..db.."&position=chr"..chr..":"..gstart.."-"..gend.." ".."Chr "..chr..": "..gstart_mb.." – "..gend_mb.." Mb]" 
	else
		chr_loc_link = "n/a"	
	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
	 chr_loc_mm_link =  "[http://genome.ucsc.edu/cgi-bin/hgTracks?org=Human&db="..db_mm.."&position=chr"..chr_mm..":"..gstart_mm.."-"..gend_mm.." ".."Chr "..chr_mm..": "..gstart_mm_mb.." – "..gend_mm_mb.." Mb]"
	else
		chr_loc_link = "n/a"	
	end
	local pubmedTitle = "بحث [[ببمد]]"
	local pubmed_link = entrez_gene
	if string.match(entrez_gene, '%w+') and entrez_gene ~= "n/a" then
		pubmed_link = "[http://www.ncbi.nlm.nih.gov/sites/entrez?db=gene&cmd=Link&LinkName=gene_pubmed&from_uid="..entrez_gene.."]"
	end
	local pubmed_mm_link = entrez_gene_mm
	if string.match(entrez_gene_mm, '%w+') and entrez_gene_mm ~= "n/a" then
		pubmed_mm_link = "[http://www.ncbi.nlm.nih.gov/sites/entrez?db=gene&cmd=Link&LinkName=gene_pubmed&from_uid="..entrez_gene_mm.."]"
	end
	
	root
		:tag('tr')
        	:tag('th')
        		:attr('colspan', '4')
        		:css('text-align', 'center')
        		:css('background-color', titleBGcolor)
         		:wikitext(title)
        		:done()
        		:newline()
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext("أنواع")
        		:done()
        		:newline()
        	:tag('td')
        		:wikitext("'''الإنسان'''")
        		:done()
        		:newline()
        	:tag('td')
        		:wikitext("'''الفأر'''")
        		:done()
        		:newline()
        	:done()
        	---this section would be nice to do in a loop if the tag closures were figured out
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext(entrezTitle)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(entrez_link)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(entrez_mm_link)
        		:done()
        		:newline()	
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext(ensemblTitle)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(ensembl_link)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(ensembl_mm_link)
        		:done()
        		:newline()
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext(uniprotTitle)
        		:done()
        		:newline()
        	: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')
            				:newline()
            				:tag('th')
            					:attr('colspan', '1')
            					:tag('span')
            						:attr('class', 'plainlinks')
            						:wikitext(uniprot_default)
            					:done()
            				:done()
            				:newline()
            			:done()
            			:tag('tr')
            			:tag('td')
            				:attr('colspan', '1')
            			:newline()
            			:tag('p')
            				:attr('class', 'plainlinks')
            				:wikitext(uniprot_link)
            				 :newline()
            				:done()
            			:done()
            		:done()
            	:done()
        		:newline()
        	    :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')
            				:newline()
            				:tag('th')
            					:attr('colspan', '1')
            					:tag('span')
            						:attr('class', 'plainlinks')
            						:wikitext(uniprot_mm_default)
            					:done()
            				:done()
            				:newline()
            			:done()
            			:tag('tr')
            			:tag('td')
            				:attr('colspan', '1')
            			:newline()
            			:tag('p')
            				:attr('class', 'plainlinks')
            				:wikitext(uniprot_mm_link)
            				 :newline()
            				:done()
            			:done()
            		:done()
            	:done()
        		:newline()
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext(refseq_mRNATitle)
        		:done()
        		:newline()
        	:tag('td') --RNASeq mRNA collapsible table 
        		: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')
            				:newline()
            				:tag('th')
            					:attr('colspan', '1')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_mRNA_default)
            				:done()
            				:newline()
            			:done()
            			:tag('tr')
            			:tag('td')
            				:attr('colspan', '1')
            			:newline()
            			:tag('p')
            				:tag('span')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_mRNA_link)
            					:done()
            				 :newline()
            				:done()
            			:done()
            		:done()
            :done()	
        	:tag('td') --RNASeq mRNA collapsible table for mouse 
        		: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')
            				:newline()
            				:tag('th')
            					:attr('colspan', '1')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_mRNA_mm_default)
            				:done()
            				:newline()
            			:done()
            			:tag('tr')
            			:tag('td')
            				:attr('colspan', '1')
            			:newline()
            			:tag('p')
            				:tag('span')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_mRNA_mm_link)
            					:done()
            				 :newline()
            				:done()
            			:done()
            		:done()
            :done()	
        		
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext(refseq_protTitle)
        		:done()
        		:newline()
        	:tag('td') --RNASeq protein collapsible table 
        		: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')
            				:newline()
            				:tag('th')
            					:attr('colspan', '1')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_prot_default)
            				:done()
            				:newline()
            			:done()
            			:tag('tr')
            			:tag('td')
            				:attr('colspan', '1')
            			:newline()
            			:tag('p')
            				:tag('span')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_prot_link)
            					:done()
            				 :newline()
            				:done()
            			:done()
            		:done()
            :done()	
			
			:tag('td') --RNASeq protein collapsible table for mouse
        		: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')
            				:newline()
            				:tag('th')
            					:attr('colspan', '1')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_prot_mm_default)
            				:done()
            			:done()
            			:tag('tr')
            			:tag('td')
            				:attr('colspan', '1')
            			:newline()
            			:tag('p')
            				:tag('span')
            					:attr('class', 'plainlinks')
            					:wikitext(refseq_prot_mm_link)
            					:done()
            				 :newline()
            				:done()
            			:done()
            		:done()
            :done()
    
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext(locTitle)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(chr_loc_link)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(chr_loc_mm_link)
        		:done()
        		:newline()
        :tag('tr')
        	:tag('th')
        		:attr('scope', 'row')
        		:css('background-color', sideTitleBGcolor)
        		:wikitext(pubmedTitle)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(pubmed_link)
        		:done()
        		:newline()
        	:tag('td')
        		:tag('span')
            		:attr('class', 'plainlinks')
        			:wikitext(pubmed_mm_link)
        		:done()
        		:newline()
end

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

p.renderFooter = function(Qid, Qid_mm)
 local text = "[[ويكي بيانات]]" --**lclz**
 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('font-size','x-small')
 			:css('background-color', rowBGcolor)
 			:wikitext(text)
 			:done() --end td
       :tag('tr')
 		:tag('td')
 			:attr('colspan', '4')
 			:css('text-align', 'center')
 			:css('font-size','x-small')
 			:css('background-color', rowBGcolor)
 		: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('text-align', 'center')
					:css('font-size','x-small')
					:wikitext(hs_link)
					:done() --end td
				:tag('td')
 					:attr('colspan', link_no_mm)
 					:css('background-color', rowBGcolor)
 					:css('text-align', 'center')
					:css('font-size','x-small')
					:wikitext(mm_link)
					:done() --end td
				:done() --end tr
			:done() --end table
		:done() --end tr
	root:done() --end root table
end

--this code isn't used was hoping could do some generalization of rows
p.rowLabel=function(label)
	root
	    :tag('tr')
        :tag('th')
        	:attr('rowspan', '2')
        	:css('background-color', sideTitleBGcolor)
        	:css('width', '43px')
         	:wikitext(label)
        	--:done()
end

-- look into entity object
p.getLabel = function(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



--general function to get value given an entity and property
p.getValue = function(entity, propertyID, return_val)

	local claims
	if return_val == nil then return_val = "" end
    local sep = " " --could ad as input parameter if need be
	if entity and entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		-- if wiki-linked value output as link if possible
		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("Q" .. v.mainsnak.datavalue.value["numeric-id"])
				if datav == nil then datav = " " end 
				out[#out + 1] = datav			
			end
			return table.concat(out, sep)
		else
		-- just return best values
			return entity:formatPropertyValues(propertyID).value
		end
	else
		return return_val
	end
end

p.getValueProtein = function(protein_entities, propertyID, return_val)
	if return_val == nil then return_val = "" end
	local sep = ","
    local overall_results = {} --should return empty if nothing assigned
	for key, val in pairs(protein_entities) do --in cases where there are multiple encodes we loop through each and return concatenated data as a whole
		local claims
		local entity = val --each protein in encodes
		if entity and entity.claims then
			claims = entity.claims[propertyID]
		end
		if claims then
			local results
			-- if wiki-linked value output as link if possible
			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("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 --individual propertyID value stored in this index	
		end
	end

	local str_overall_results = table.concat(overall_results, sep) --weirdness happens when add a sep = " " otherwise each value represented one time
	if string.match(str_overall_results, '%w+') then
		return str_overall_results 
	else
		return return_val
	end
end


--general function to get value given an entity and property
p.getQid = function(entity)
	local Qid
	if entity and entity.id then
		Qid = entity.id
		return Qid
	else
		return ""
	end
end

--get random value that is preferred ranked 
p.getRefseq_mRNA = function(entity, propertyID, return_val)
	if return_val == nil then return_val = "" end
	local input_rank = "RANK_PREFERRED" ---this is mostly like won't do anything because ranking isn't maintained in wikidata 
	local claims
	
	if entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		-- if wiki-linked value output as link if possible
		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.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])
				local label = mw.wikibase.label("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, ", ")
		else
			local results = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value 
			
			--loop through results until get a NP or NM or just return whatever is in first element
			--[[local results_split = mw.text.split(results, ",")
			
			local preffered_results = " "
			if results_split[1] then
				preferred_result = mw.text.trim(results_split[1]) --return first element if desired prefix not found and remove whitespace
			end
			local id --refseq id in question
			for i, id in ipairs(results_split) do
				local trim_id = mw.text.trim(id)
  				if string.match( trim_id, '^NM_%d+') then 
  					preferred_result = trim_id --overwrite each time found only need one to display
  				end
			end
			if preferred_result then
				return preferred_result --return a id starting with NP or NM
			else
				return return_val --return first element because desired prefix not found and remove whitespaces
			end
			--]]
			return results
		end
	else
		return return_val
	end
end

p.getRefseq_protein = function(protein_entities, propertyID, return_val)
local sep = ","
local overall_results = {} --should return empty if nothing assigned

	for key, val in pairs(protein_entities) do --in cases where there are multiple encodes we loop through each and return concatenated data as a whole
	
		local claims
		local entity = val --each protein in encodes
		if entity.claims then
			claims = entity.claims["P637"]
		end
		if claims then
			local results
			-- if wiki-linked value output as link if possible
			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("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("P637", mw.wikibase.entity.claimRanks).value 
			end
			overall_results[#overall_results+1] = results --a list is in each index 
		end	
		
	end
	--why are there duplicate results here
	local str_overall_results = table.concat(overall_results, sep)
	return str_overall_results

end
	--[[
	local results_split = mw.text.split(str_overall_results, sep) --split complete list so can loop through..probably a more direct way to do this
				--loop through results until get a NP or NM or just return whatever is in first element
	

	local preffered_result = results_split[1] or ""

	for i, id in ipairs(results_split) do
		local trim_id = mw.text.trim(id)
		--check of id starts with NP or NM
		if string.match( trim_id, '^NP_%d+') then 
			preferred_result = trim_id --overwrite each time found only need one to display
		end
	end
	--check if something in preffered_result if not get first element in result_split
	if p.isempty(preffered_result) then
		return return_val
	else
		return preferred_result --return a id starting with NP or NM
	end

end --]]

--gets an image
p.getImage = function(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] = "[[ملف:" .. filename .. "|" .. imgsize .. "]]" 
 			end   
 				return table.concat(out, sep)   
 		else   
 			return ""   
 		end   
	else   
 		return ""   
 	end   
end

p.getPDB = function(protein_entities)
	local pdb_propertyID = "P638"
	local overall_results = {}
	for key, val in pairs(protein_entities) do --in cases where there are multiple encodes we loop through each and return concatenated data as a whole
		local claims
		local entity = val
		if entity and entity.claims then
			claims = entity.claims[pdb_propertyID]
		end
		local sitelink = "http://www.rcsb.org/pdb/explore/explore.do?pdbId="
		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.label(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, ", ")
			else
				results = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
			end
			overall_results[#overall_results+1] = results --individual propertyID values stored in this index
		end
	end
	return table.concat(overall_results, ",%%s")
end

function p.getAliases(entity)
	a = ''
	if entity['aliases'] ~= nil then
        local test = entity['aliases']['en']
        if test then
			for key, value in ipairs(test) do
				a = a .. ', ' ..  value['value']
			end
			return a
		else
			return ""
		end
	else
		return ""
	end
	
end


--get a geneome start P644 or end P645
p.getChromosomeLoc = function(entity, propertyID, prefix)
	-- will contain the numeric value for the requested coordinate
	local output = ""
	local sep = " "
	-- can only be P644 (genomic start) or P645 (genomic end) for this to work
	-- should probably try to catch that.  Might also increase legibility to use specific variable names when possible
--	local propertyID = mw.text.trim(frame.args[1] or "") 
	-- this can really only be P659 right now.  I'm not sure of the value of including it as a parameter as other values will likely break this function
	local qualifierID = "P659" --mw.text.trim(frame.args[2] or "")
	-- Why do we include this here?  What should happen if FETCH_WIKIDATA is not included? 
	--local input_parm = mw.text.trim(frame.args[3] or "")
	-- this can needs to be fed to the function either by a call to {{#invoke:Wikidata|pageId}} or by setting it directly (e.g. if the function was applied on a page other than the targeted gene)
	--alert if this id is not a valid thing in wikidata, a Lua error will occur that says
	--The ID entered is unknown to the system. Please use a valid entity ID.
	--local itemID = mw.text.trim(frame.args[4] or "")
	-- will track the different builds pulled from the qualifiers
	local newest_build = "0"
	-- starts the process
	--local entity = mw.wikibase.getEntityObject(itemID)
	local claims
	--gets a table of claims on the (genomic start or end) property Q19847637
	if entity and entity.claims then
		claims = entity.claims[propertyID]
	end
	--will return nothing if no claims are found
	if claims then
		--checking to be sure claims is populated, not sure it its needed
		if (claims[1] ) then
			--useful for debugging
			--local out = {}
			--pulls the genome location from the claim
			for k, v in pairs(claims) do
				local location = v.mainsnak.datavalue.value
				--debugging
				--out[#out + 1] = k.." location:" .. location.. " || " 
				--gets the qualifiers linked to the current claim
				local quals 
				if v.qualifiers then
					quals = v.qualifiers.P659
				end
				--if there are any
				if quals then
					for qk, qv in pairs(quals) do
						local qual_obj_id = "Q"..qv.datavalue.value["numeric-id"]
						--get to the entity targeted by the qualifier property.  Genome builds are Items in wikidata
						local qual_obj = mw.wikibase.getEntityObject(qual_obj_id)
						local alias = ""
						--this uses the aliases to pull out version numbers
						--seems like there ought to be a better way to do this, but likely would need to change the data added by the bot
						if qual_obj["aliases"] ~= nil then
							local test = qual_obj["aliases"]["en"]
							for key, value in ipairs(test) do
								if string.match(value['value'], prefix) then
									alias = value['value']
									local build_no = alias:gsub(prefix,"")
									--report only the most location associated with the most recent build
									--if there is more than one location per build, just give one back as that is not our problem right now.
									if build_no > newest_build then
										output = location
										newest_build = build_no
									end
								end
							end
						end
					end
				--in case there are no qualifiers, but there is a location, might as well return it
				else output = location 
				end
			end
				return output
		else
			return ""
		end
	else
		return ""
		--debug
		--"no claims for "..itemID.." prop "..propertyID
	end
end

p.getAliasFromGenomeAssembly = function(entity, prefix)
	-- will contain the numeric value for the requested coordinate
	local output = ""
	local sep = " "
	local propertyID = "P644" --genomic start used 
	local qualifierID = "P659"

	local newest_build = "0"
	local claims
	if entity.claims then
	 claims = entity.claims[propertyID]
	end
	--will return nothing if no claims are found
	if claims then
		--checking to be sure claims is populated, not sure it its needed
		if (claims[1] ) then
			--useful for debugging
			--local out = {}
			--pulls the genome location from the claim
			for k, v in pairs(claims) do
				local quals
				if v.qualifiers then
					quals = v.qualifiers.P659
				end
				--if there are any
				if quals then
					for qk, qv in pairs(quals) do
						local qual_obj_id = "Q"..qv.datavalue.value["numeric-id"]
						--get to the entity targeted by the qualifier property.  Genome builds are Items in wikidata
						local qual_obj = mw.wikibase.getEntityObject(qual_obj_id)
						local alias = ""
						--this uses the aliases to pull out version numbers
						--seems like there ought to be a better way to do this, but likely would need to change the data added by the bot
						if qual_obj["aliases"] ~= nil then
							local test = qual_obj["aliases"]["en"]
							for key, value in ipairs(test) do
								if string.match(value['value'], prefix) then
									alias = value['value']
									local build_no = alias:gsub(prefix,"")
									--report only the most location associated with the most recent build
									--if there is more than one location per build, just give one back as that is not our problem right now.
									if build_no > newest_build then
										newest_build = build_no
									end
								end
							end
						end
					end
				--in case there are no qualifiers, but there is a location, might as well return it
				else output = location 
				end
			end
				return prefix..newest_build
		else
			return ""
		end
	else
		return ""
	end
end

p.trimChromosome = function(entity)
	local string_to_trim = p.getValue(entity, "P1057")
	local out = ''
	if string.find(string_to_trim, 'chromosome.') then
		out = string_to_trim:gsub("chromosome ", "")
	end 
	if string.find(string_to_trim, 'mouse.chromosome.') then
		out = string_to_trim:gsub("mouse chromosome ", "")
	end 
	return out	
end

p.locToMb = function(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

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


p.getGO = function(protein_entities, propertyID)
	--propertyID ie molecular, cellular, function
	
	local overall_results = {}
	local results = "" --string to return
	
	for key, val in pairs(protein_entities) do
	
		local claims
		local entity = val
		if entity.claims then
			claims = entity.claims[propertyID] -- ie molecular, cellular, function
		end
		local propertyID_child = "P686" -- Gene Ontology ID
		
		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 itemID_child = "Q" .. v.mainsnak.datavalue.value["numeric-id"] --get Qid of property item so can get the GOid
					local entity = mw.wikibase.getEntityObject(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).value
					else
						result_GOID = nil --no GO ID
					end
					local sitelink = "http://amigo.geneontology.org/amigo/term/"
					local label = mw.wikibase.label("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 = "<strong>•</strong> [" .. sitelink .. result_GOID .. " " .. label .."]<br>"
					else
						wiki_link = "<strong>•</strong> [[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<br>"
					end
					overall_results[#overall_results+1] = wiki_link
				end
				
			else
				results = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
			end
			
		end
		--overall_results[#overall_results+1] = results --each protein GO terms stored in this index, so table contains all the GO terms with duplicates 
	end

	local hash = {} --temp check
	local res = {} --no dups

	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


return p