Module:Sandbox/Jts1882/CladeSVG
Appearance
From Wikipedia, the free encyclopedia
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
--NOTE: this module contains functions for generating the table structure of the clade tree in two ways: -- (1) the original method was generated by a number of function calls from the template using {{invoke}} -- function p.openTable(frame) - creates wikitext code to open the HTML table -- function p.node(frame) - deals with the first node (|1,|label1) and creates wikitext for top row of the table -- function p.nodeN(frame) - deals with a sister nodes (|2-17), adding a row each time it is called -- function p.closeTable() - closes the HTML table -- (2) the revised method is called by the template with one {{invoke}} instruction; it has three functions: -- p.main(frame) - opens and closes table, loops through the children of node, main is invoked once and controls the rest, calling ... -- p.addTaxon(childNumber, nodeLeaf) - the nuts and bolts; code dealing with each child node -- p.addLabel(childNumber) - adds the label text localp={} --============================== main function (for Method 2) =========================== -- main function, which will --[[Main function to generate the table structure of the tree Usage: {{#invoke:Module:Sandbox/Jts1882/CladeN|main|style={{{STYLE}}} }} Template: User:Jts1882/sandbox/templates/CladeN ]] functionp.main(frame) localcladeString="" localmaxChildren=20-- currently 17 in the clade/cladex templates localchildNumber=0 locallastNode=0 localnodeCount=0-- total leafs plus new clade branches localleafCount=0-- just the terminal leaves localcladeCount=0-- new clade calls (each with a table) localchildCount=0-- number of leaves in the clade (can use to set bottom of bracket in addTaxon() localtotalCount=0 --[[Note: this preprocessing loop gets information about the whole structure (number of nodes, leaves etc) it makes a redundant calls to the templates through transclusion, but doen't affect the template depths; it would probably be better to store the transcluded nodes in a table (a job for later) ]] --[[ disable proprocessing loop while childNumber < maxChildren do -- preprocessing loop childNumber = childNumber + 1 -- so we start with 1 local nodeLeaf,data = mw.getCurrentFrame():getParent().args[tostring(childNumber)] or "" -- get data from |N= if nodeLeaf ~= "" then childCount = childCount + 1 -- this counts child elements in this clade for i in string.gmatch(nodeLeaf, "|| rowspan") do -- count number of rows started (transclusion) nodeCount = nodeCount + 1 end for i in string.gmatch(nodeLeaf, "{| cellspacing") do -- count number of tables started (transclusion) cladeCount = cladeCount + 1 end -- _, cladeCount = string.gsub(nodeLeaf, "{| cellspacing", "") --_, nodeCount = string.gsub(nodeLeaf, "|| rowspan", "") --totalCount = totalCount + nodeCount lastNode = childNumber -- if data ~= nil then totalCount = totalCount + tonumber(data) else totalCount = totalCount + 1 end end --cladeString = cladeString .. '\n' .. ' count= ' .. totalCount end ]] nodeCount=nodeCount+childCount--(elements passed down by transduction plus current child elements) cladeCount=cladeCount+1--(elements passed down by transduction plus current clade) leafCount=nodeCount-cladeCount+1 totalCount=leafCount-- cladeCount --nodeCount localtesting=false --testing = true -- COMMENT OUT TO GET MODULE RUNNING WITHOUT THE TEST iftestingthen localdataString=p.test(1) cladeString=cladeString..dataString returncladeString end localtableStyle=frame.args.styleor"" localtableStyleString=' style="'..tableStyle..'" ' iftableStyle=='{{{style}}}'thentableStyleString=""end localcaptionName=mw.getCurrentFrame():getParent().args['caption']or"" localcaptionStyle=mw.getCurrentFrame():getParent().args['captionstyle']or"" -- open table --cladeString = cladeString .. '{| cellspacing=0 cellpadding=0 border=0 style="' .. tableStyle .. '"' cladeString=cladeString..'{|cellspacing=0 cellpadding=0 '..tableStyleString --cladeString = cladeString .. '{| cellspacing=0 ' .. tableStyleString -- NOTE: cellpadding supposedly not supported in HTML5 -- add caption ifcaptionName~=""then cladeString=cladeString..'\n|+style="'..captionStyle..'"|'..captionName end -- global nodeParameters (unnumber, i.e. color, thickness, state) apply to whole node bracket, -- but can be overrriden by branchParameters (numbered, e.g. color2, thickness2, state2) nodeColor=mw.getCurrentFrame():getParent().args['color']or"black" nodeThickness=tonumber(mw.getCurrentFrame():getParent().args['thickness'])or1 nodeState=mw.getCurrentFrame():getParent().args['state']or"solid" localmoreNeeded=true childNumber=0 lastNode=0 --[[get child elements (add more rows for each child of node; each child is two rows) the function addTaxon is called to add the rows for each child element; each child add two rows: the first cell of each row contains the label or sublabel (below the line label), respectively; the second cell spans both rows and contains the leaf name or a new clade structure a third cell on the top row is sometimes added to contain a group to the right ]] whilechildNumber<maxChildrendo-- disable moreNeeded -- while childNumber < 17 and moreNeeded == true do childNumber=childNumber+1-- so we start with 1 localnodeLeaf=mw.getCurrentFrame():getParent().args[tostring(childNumber)]or""-- get data from |N= localnodeLabel=mw.getCurrentFrame():getParent().args['label'..tostring(childNumber)]or""-- get data from |labelN= localnewickString=mw.getCurrentFrame():getParent().args['newick'..tostring(childNumber)]or""-- get data from |labelN= ifnewickString~=""then-- if using a newick string instead of a clade structure ifnodeLabel==""then-- use labelN by default, otherwise use root name from Newick string nodeLabel=p.getNewickOuterterm(newickString)-- need to use terminal part of newick string for label end cladeString=cladeString..'\n'..p.addTaxon(childNumber,p.newick(0,newickString),nodeLabel) lastNode=childNumber elseifnodeLeaf~=""then-- if the node contains a leaf name or clade structue cladeString=cladeString..'\n'..p.addTaxon(childNumber,nodeLeaf,nodeLabel) lastNode=childNumber else moreNeeded=false-- no |N= so don't look for more -- Note: this changes the behaviour. If there is no |2, it won't look for |3 or |4, which clade does (is this desirable?) -- this has been disabled to allow consistent behaviour with clade/cladex (it will check to the limit set) end end --[[finish last row by adding cell with no left border note: the row was started in addTaxon(), but the cell not added as left border yet to be determined; here the cell is added to the last child node with no left border as it is below the bracket this will be moved to addTaxon() when the number of the last child is known ]] localsubLabel=mw.getCurrentFrame():getParent().args['sublabel'..tostring(lastNode)]or""-- request in addLabel --use subLabel for annotating the clade structues to use structure information (DEBUGGIING ONLY) --subLabel= '(N=' .. totalCount .. ')' --cladeString = cladeString .. '\n' .. '| style="border: 0; padding: 0; vertical-align: top;" | <br/> ' cladeString=cladeString..'\n'..'|style="border:0;vertical-align:top;text-align:center;"|'..p.addLabel(lastNode-1,subLabel) --[[ footer description (add addition row spanning two columns at bottom of table) this is designed to provide the description below the whole clade structure (mimicking cladogram); however, it can also be used to add labels and spacing to the whole clade structre ]] localfooterText=mw.getCurrentFrame():getParent().args['footer']or"" localfooterStyle=mw.getCurrentFrame():getParent().args['footerstyle']or"" iffooterText~=""then --cladeString = cladeString .. '<tr><td>Test <br/><br/><br/><br/><br/></td></tr>' cladeString=cladeString..'\n|-style="'..footerStyle..'"\n|colspan="2"|<p>'..footerText..'</p>||' end -- close table (wikitext to close table) cladeString=cladeString..'\n|}' returncladeString end --[[ function to add child elements adds wikitext for two rows of the table for each child node, the first cell in each is used for the label and sublabel; the bottom border forms the horizonal branch of the bracket the second cell is used for the leafname or a transcluded clade structure and spans both rows note that the first and last child nodes need to be handled differently from the middle elements the middle elements (|2, |3 ...) use a left border to create the vertical line of the bracket the first child element doesn't use a left border for the first cell in the top row (as it is above the bracket) the last child doesn't use a left border for the first cell in the second row (as it is above the bracket) a complication is that the number of the last child node is not known; as a result the cells will be added to the row on the next iteration or after the main loop finishes ]] functionp.addTaxon(childNumber,nodeLeaf,nodeLabel) -- get leaf and labels --local nodeLeaf = mw.getCurrentFrame():getParent().args[tostring(childNumber)] or "" -- used passed variable to avoid redundancy --local nodeLabel = mw.getCurrentFrame():getParent().args['label'..tostring(childNumber)] or "" -- request in addLabel -- get formating parameters for branch (default to global nodeParameters) -- - the branch parameters have a number, e.g. |colorN, |thicknessN, |stateN -- - the node parameters have no number, e.g. |color, |thickness, |state localbranchThickness=tonumber(mw.getCurrentFrame():getParent().args['thickness'..tostring(childNumber)])ornodeThickness localbranchColor=mw.getCurrentFrame():getParent().args['color'..tostring(childNumber)]ornodeColor--"black" localbranchStyle=mw.getCurrentFrame():getParent().args['style'..tostring(childNumber)]or"" localbranchState=mw.getCurrentFrame():getParent().args['state'..tostring(childNumber)]ornodeState-- "solid" ifbranchState=='double'thenifbranchThickness<2thenbranchThickness=3endend-- need thick line for double -- the left border takes node parameters, the bottom border takes branch parameters localbottomBorder=tostring(branchThickness)..'px '..branchState..' '..branchColor localleftBorder=tostring(nodeThickness)..'px '..nodeState..' '..nodeColor -- variables for right hand bar or bracket --local barColor = "" localbarRight=mw.getCurrentFrame():getParent().args['bar'..tostring(childNumber)]or"0" localbarBottom=mw.getCurrentFrame():getParent().args['barend'..tostring(childNumber)]or"0" localbarTop=mw.getCurrentFrame():getParent().args['barbegin'..tostring(childNumber)]or"0" localbarLabel=mw.getCurrentFrame():getParent().args['barlabel'..tostring(childNumber)]or"" localgroupLabel=mw.getCurrentFrame():getParent().args['grouplabel'..tostring(childNumber)]or"" localgroupLabelStyle=mw.getCurrentFrame():getParent().args['labelstyle'..tostring(childNumber)]or"" --replace colours with format string; need right bar for all three options ifbarRight~="0"thenbarRight="2px solid "..barRightend ifbarTop~="0"thenbarRight="2px solid "..barTopend ifbarBottom~="0"thenbarRight="2px solid "..barBottomend ifbarTop~="0"thenbarTop="2px solid "..barTopend ifbarBottom~="0"thenbarBottom="2px solid "..barBottomend -- now construct wikitext localstyleString='' localcladeString='' --[[ first add cell to the spacing row (for 2-17); it completes the previous row why is this here rather than at the end of each row? To allow left border (here while extending bracket) or not (end of bracket) ]] ifchildNumber~=1then localsubLabel=mw.getCurrentFrame():getParent().args['sublabel'..tostring(childNumber-1)]or""-- request in addLabel styleString=' style="border-left:'..leftBorder..';border-top:0;border-right:0;border-bottom:0;vertical-align:top;text-align:center;"' --cladeString = cladeString .. '| ' .. styleString .. ' | <br/> ' -- for sublabel ' | y<br/> ' cladeString=cladeString..'|'..styleString..'|'..p.addLabel(childNumber-1,subLabel) cladeString=cladeString..'\n|-' else --cladeString = cladeString .. '\n|-' end -- note: the row is started here (there is no need too to start first row (the wikitext for table handles that) styleString='' -- now add cell with label ifchildNumber==1then -- the width gives minimum spacing when all labels are empty (was 1.5em) styleString=' style="width:1em;border-bottom:'..bottomBorder..';border-left:0;border-top:0;border-right:0;padding:0 0.2em;vertical-align:bottom;text-align:center;'..branchStyle..'"' --cladeString = cladeString .. '| ' .. styleString .. '| ' .. p.addLabel(childNumber,nodeLabel) -- p.addLabel(nodeLabel) cladeString=cladeString..'\n|'..styleString..'|'..p.addLabel(childNumber,nodeLabel)-- p.addLabel(nodeLabel) -- the addition linefeed doesn't seem to make a difference so can move line outside if statement (leave for now) else-- for 2-17 styleString=' style="border-left:'..leftBorder..';border-top:0;border-right:0;border-bottom:'..bottomBorder..';padding:0 0.2em;vertical-align:bottom;text-align:center;'..branchStyle..'"' cladeString=cladeString..'\n|'..styleString..'|'..p.addLabel(childNumber,nodeLabel)-- p.addLabel(nodeLabel) end styleString='' -- add cell with leaf (which may be a table with transluded content) --cladeString = cladeString .. '|| rowspan=2 style="border: 0; padding: 0; border-right: ' .. barRight .. ';border-bottom: ' .. barBottom .. ';border-top: ' .. barTop .. ';' .. branchStyle .. ' " | ' ifbarRight~="0"then styleString='style="border:0;padding:0;border-right:'..barRight..';border-bottom:'..barBottom..';border-top:'..barTop..';'..branchStyle..'"' else styleString='style="border:0;padding:0;'..branchStyle..'"' end -- styleString = ' style="padding: 0; border-style: '.. barTop .. ' ' .. barRight .. ' ' .. barBottom .. ' none;' .. branchStyle .. '" ' cladeString=cladeString..'||rowspan=2 '..styleString..'|' -- note: this is where leading spaces can be added to labels and leaves. This was commented out in the clade template. Most trees seem to have added spaces, to would need to check for existing spaces; however, cladex uses it cladeString=cladeString..'\n'..nodeLeaf -- stuff for right-hand bar-bracket ifbarRight~="0"then cladeString=cladeString..' '-- add spaces between leaf text and bar ifbarLabel~=""then -- cladeString = cladeString .. '\n| ' .. barLabel end -- experiment for labeling the bar cladeString=cladeString..'<td style="vertical-align:middle;">'..barLabel..'</td>' end end ifgroupLabel~=""then --cladeString = cladeString .. ' ' -- add spaces between leaf text and bar cladeString=cladeString..'<td style="'..groupLabelStyle..'">'..groupLabel..'</td>' end -- add row (needed because of rowspan=2); -- note: the cell will be added on the next iteration of addTaxon (with left border) or after the main loop (without left border) localbranchStyleString='style="'..branchStyle..'" ' ifbranchStyle==''thenbranchStyleString=''end-- avoid empty style elements --cladeString = cladeString .. '\n|- style="' .. branchStyle .. '" ' cladeString=cladeString..'\n|-'..branchStyleString -- TODO code to put whole row here instead of completing on next call to addTaxon() or after main loop returncladeString end --[[ adds text for label or sublabel to a cell ]] functionp.addLabel(childNumber,nodeLabel) --local nodeLabel = mw.getCurrentFrame():getParent().args['label'..tostring(childNumber)] or "" localfirstChars=string.sub(nodeLabel,1,2)-- get first two characters; will be {{ if no parameter (for Old method?) --if nodeLabel ~= "{{{label1}}}" then iffirstChars=="{{{"ornodeLabel==""then return'<br/>'--' <br/>' -- remove space to reduce post-expand include size (the width=1.5em handles spacing) else return'<span class="nowrap">'..nodeLabel..'</span>' end end --[[=================== Newick string handling function ============================= ]] functionp.getNewickOuterterm(newickString) returnstring.gsub(newickString,"%b()","")-- delete parenthetic term end functionp.newick(count,newickString) localcladeString="" count=count+1 --start table cladeString=cladeString..'{| cellspacing=0 cellpadding=0 border=0 ' localj,k j,k=string.find(newickString,'%(.*%)')-- find location of outer parenthesised term localinnerTerm=string.sub(newickString,j+1,k-1)-- select content in parenthesis localouterTerm=string.gsub(newickString,"%b()","")-- delete parenthetic term ifouterTerm=='panthera'thenouterTerm="x"end-- how is this set in local variable for inner nodes? outerTerm=tostring(count) -- need to remove commas in bracket terms before split, so temporarily replace commas between brackets localinnerTerm2=string.gsub(innerTerm,"%b()",function(n) returnstring.gsub(n,",%s*","XXX")-- also strip spaces after commas here end) --cladeString = cladeString .. '\n' .. p.addTaxon(1, innerTerm2, "") locals=strsplit(innerTerm2,",") locali=1 whiles[i]do restoredString=string.gsub(s[i],"XXX",",")-- convert back to commas --restoredString = s[i] localouterTerm=string.gsub(restoredString,"%b()","") ifstring.find(restoredString,'%(.*%)')then --cladeString = cladeString .. '\n' .. p.addTaxon(i, restoredString, "x") cladeString=cladeString..'\n'..p.addTaxon(i,p.newick(count,restoredString),outerTerm) -- p.addTaxon(2, p.newick(count,newickString2), "root") else cladeString=cladeString..'\n'..p.addTaxon(i,restoredString,"")--count) end i=i+1 end -- close table cladeString=cladeString..'\n'..'| style="border: 0; padding: 0; vertical-align: top;" | <br/> \n|}' returncladeString end -- emulate a standard split string function functionstrsplit(inputstr,sep) ifsep==nilthen sep="%s" end localt={};i=1 forstrinstring.gmatch(inputstr,"([^"..sep.."]+)")do t[i]=str i=i+1 end returnt end -- Bits from Peter Coxhead's module -- https://en.wikipedia.org/w/index.php?title=Module:Autotaxobox&action=edit --[[ Utility function primarily intended for use in checking and debugging. Returns number of levels in a taxonomic hierarchy, starting from the supplied taxon as level 1. Usage: {{#invoke:Autotaxobox|nLevels|TAXON}} ]] functionp.nLevels(frame) localcurrTaxon=frame.args[1] locali=1 localmaxN=2 localsearching=true whilesearchingandi<maxNdo --local parent = frame:expandTemplate{ title = 'Template:Taxonomy/' .. currTaxon, args = {['machine code'] = 'parent' } } localparent=frame:expandTemplate{title=currTaxon,args={['machine code']='parent'}} ifparent~=''then currTaxon=parent i=i+1 else searching=false end end ifsearchingthenreturncurrTaxon--maxN .. '+' elsereturni end end --[[ Utility function primarily intended for use in checking and debugging. Returns a comma separated list of a taxonomic hierarchy, starting from the supplied taxon. Usage: {{#invoke:Autotaxobox|listAll|TAXON}} ]] functionp.listAll(frame) localcurrTaxon=frame.args[1] locali=1 localsearching=true localmaxN=100 locallst=currTaxon whilei<maxNandsearchingdo localcurrCall='Template:Taxonomy/'..currTaxon localparent=frame:expandTemplate{title=currCall,args={['machine code']='parent'}} ifparent~=''then currTaxon=parent lst=lst..', '..currTaxon i=i+1 else searching=false end end ifsearchingthenlst=lst..'...'end returnlst..' ('..i..')' end -- =================== experimental Newick to clade parser function ============================= --[[Function of convert Newick strings to clade format Usage: {{#invoke:Module:Sandbox/Jts1882/CladeN|newickConverter|newickstring={{{NEWICK_STRING}}} }} ]] functionp.newickConverter(frame) localnewickString=frame.args['newickstring'] ifnewickString=='{{{newickstring}}}'thenreturnnewickStringend -- show the Newick string localcladeString='' locallevelNumber=1-- for depth of iteration localchildNumber=1-- number of sister elements on node (always one for root) -- converted the newick string to the clade structure cladeString=cladeString..'{{clade' cladeString=cladeString..p.newickParseLevel(newickString,levelNumber,childNumber) cladeString=cladeString..'\r}}' localresultString='' localoption=mw.getCurrentFrame():getParent().args['option']or'' ifoption=='tree'then --show the transcluded clade diagram resultString=cladeString else -- show the Newick string resultString='<pre>'..newickString..'</pre>' -- show the converted clade structure resultString=resultString..'<pre>'..cladeString..'</pre>' end --resultString = frame:expandTemplate{ title = 'clade', frame:preprocess(cladeString) } returnresultString end --[[ Parse one level of Newick sting This function recieves a Newick string, which has two components 1. the right hand term is a clade label: |labelN=labelname 2. the left hand term in parenthesis has common delimited child nodes, each of which can be i. a taxon name which just needs: |N=leafname ii. a Newick string which needs further processing through reiteration ]] functionp.newickParseLevel(newickString,levelNumber,childNumber) localcladeString="" localindent=p.getIndent(levelNumber) --levelNumber=levelNumber+1 localj=0 localk=0 j,k=string.find(newickString,'%(.*%)')-- find location of outer parenthesised term localinnerTerm=string.sub(newickString,j+1,k-1)-- select content in parenthesis localouterTerm=string.gsub(newickString,"%b()","")-- delete parenthetic term cladeString=cladeString..indent..'|label'..childNumber..'='..outerTerm cladeString=cladeString..indent..'|'..childNumber..'='..'{{clade' levelNumber=levelNumber+1 indent=p.getIndent(levelNumber) -- protect commas in inner parentheses from split; temporarily replace commas between parentheses localinnerTerm2=string.gsub(innerTerm,"%b()",function(n) returnstring.gsub(n,",%s*","XXX")-- also strip spaces after commas here end) locals=strsplit(innerTerm2,",") locali=1 whiles[i]do restoredString=string.gsub(s[i],"XXX",",")-- convert back to commas localouterTerm=string.gsub(restoredString,"%b()","") ifstring.find(restoredString,'%(.*%)')then --cladeString = cladeString .. indent .. '|y' .. i .. '=' .. p.newickParseLevel(restoredString,levelNumber+1,i) cladeString=cladeString..p.newickParseLevel(restoredString,levelNumber,i) else cladeString=cladeString..indent..'|'..i..'='..restoredString--.. '(level=' .. levelNumber .. ')' end i=i+1 end -- end -- end splitting of strings cladeString=cladeString..indent..'}}' returncladeString end functionp.getIndent(levelNumber) localindent="\r" localextraIndent=mw.getCurrentFrame():getParent().args['indent']or0 whiletonumber(extraIndent)>0do indent=indent.." "-- an extra indent to make aligining compound trees easier extraIndent=extraIndent-1 end whilelevelNumber>1do indent=indent.." " levelNumber=levelNumber-1 end returnindent end functionp.newickstuff(newickString) end ------------------------------------------------------------------------------------------ functionp.test(dummy) localdataString="" dataString="NEWCLADE:" localchildNumber=0 locallastNode=0 whilechildNumber<17do-- disable moreNeeded childNumber=childNumber+1-- so we start with 1 localnodeLeaf=mw.getCurrentFrame():getParent().args[tostring(childNumber)]or""-- get data from |N= --local nodeLabel = mw.getCurrentFrame():getParent().args['label'..tostring(childNumber)] or "" -- get data from |labelN= ifnodeLeaf~=""then dataString=dataString..tostring(childNumber) localfirstChars=string.sub(nodeLeaf,1,2)-- get first two characters --if nodeLabel ~= "{{{label1}}}" then iffirstChars=="{{"then dataString=dataString..'XXX'..nodeLeaf end dataString=dataString..'LEAF:'..nodeLeaf lastNode=childNumber end end localnumClades=0 localnumBranches=0 -- _, count = string.gsub(dataString, "NEWCLADE", "NEXT") foriinstring.gmatch(dataString,"NEWCLADE")do numClades=numClades+1 end dataString=dataString..'<br/>(numClades='..numClades..')' foriinstring.gmatch(dataString,"LEAF")do numBranches=numBranches+1 end dataString=dataString..'<br/>(numClades='..numClades..')' dataString=dataString..'<br/>(numBranches='..numBranches..')' mw:log(numClades) --return 'numClades=' .. numClades .. '; numBranches=' .. numBranches returndataString end functionp.test2(target) localtarget="User:Jts1882/sandbox/templates/Template:Passeroidea" localresult=mw.getCurrentFrame():expandTemplate{title=target,args={['style']=''}} returnresult end -- this must be at end returnp