Module:Sandbox/Mensis Mirabilis/gene

local p = { }

local function check_values(f,args) --local u= table.upack(args) local exist, val = pcall(f, unpack(args)) if exist and val ~= nil then return(val) else -- Leaking some debugging info won't hurt.... return("VALUE_ERROR (" .. tostring(val) .. ")")		end end

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" --actually ortholog property additional orthologs can exist 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, "P31") 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 local qualifierID = "P703" --found in taxon 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 -- Don't have to go on if we already got it						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							--get the taxon qualifier id							local qual_obj_id = "Q"..qv.datavalue.value["numeric-id"] if qual_obj_id == mouse_qual then --check if this is mouse or other mm_qid = mouse_itemID entity_mouse = mw.wikibase.getEntity(mouse_itemID) checkOrtholog = 1 break end end end end 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 chr = check_values(p.trimChromosome, {entity}) --local drug = check_values(p.getDrug, {entity_protein, "P129"}) --define Global Color Scheme rowBGcolor = '#eee' titleBGcolor = '#ddd' sideTitleBGcolor = '#c3fdb8'

return tostring(root) --return table.concat(drug_pqid) else return "An Error has occurred retrieving Wikidata item for infobox" 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.trimChromosome = function(entity) local string_to_trim = p.getValue(entity, "P1057") local out = '' --"mitochondrion" and "chromosome MT" is used for mitochondrial DNA. --See d:Special:WhatLinksHere/Q18694495 if string.find(string_to_trim, 'chromosome MT') or string.find(string_to_trim, 'mitochondri') then --match both 'mitochondrio'/'mitochondrial' out = "MT" elseif string.find(string_to_trim, 'chromosome') then out = string.match(string_to_trim, "%d+")--extract number from string if out == nil then out = string.match(string_to_trim, "X") or string.match(string_to_trim, "Y") end end return out end

local chr = check_values(p.trimChromosome, {entity}) local category_chromosome = ''

function p._main(args)

return chr end

return p