Jump to content

Module:ImportProtein

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 01:57, 17 March 2013 (Maybe span will keep the label motif from going down uncontrollably? but...). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p.main (frame)
   local args=frame.args
   local parent=frame.getParent(frame)
   local pargs={}
   if parent then pargs=parent.args end
   local height=args.height or pargs.height or "200"
   local width=args.width or pargs.width or "600"
   local tableoutput=args.tableoutput or nil -- usually not showing the full table; this is mostly a graphic module
   local include=args.include or pargs.include or "all"
   if include == "all" then include = nil end -- anything but nil means to include only certain listed things.
   local tinclude={}
   if include then
      for i in mw.ustring.gmatch(include,[[%"(.-)%"]]) do
         tinclude[i]=1
      end
   end
   local exclude=args.exclude or pargs.exclude or "" -- for these empty arrays will be ignored later.
   local texclude={}
   for i in mw.ustring.gmatch(exclude,[[%"(.-)%"]]) do
      texclude[i]=1
   end
   local usenotes=args.usenotes or pargs.usenotes or ""
   local tusenotes={}
   for i in mw.ustring.gmatch(usenotes,[[%"(.-)%"]]) do
      tusenotes[i]=1
   end
   local substitute=args.substitute or pargs.substitute or ""
   local tsubstitute={}
   for i,j in mw.ustring.gmatch(substitute,[[%"(.-)%":%"(.-)%"]]) do
      tsubstitute[i]=j
   end
   local file=args.file or pargs.file
   if not(file) then return "error: use 'file=some cut-and-pasted NCBI protein sequence' to input a protein to be diagrammed" end
   local cdsstart, cdsend = mw.ustring.match(file,"Protein%s-(%d+)%.%.(%d+)")
   cdsstart=tonumber(cdsstart);cdsend=tonumber(cdsend)
   if ((cdsstart<1) or (cdsend<1)) then return [[error: the module expected a line "Protein: ''start amino acid''..''end amino acid''" to define the CDS.]] end
   local cdswidth=cdsend-cdsstart
   file = mw.ustring.gsub(file,"Site%s+","|##|S") -- there are no pipe characters in the input or it would have choked
   file = mw.ustring.gsub(file,"Region%s+","|##|R")
   file = mw.ustring.gsub(file,"$","|##|") --- close last feature at the EOF
   local output=[[{| class="wikitable" \n]]
   local graphics=[[<div style="position:relative;border-style:solid;border-color: #0077ff;width:]] .. width .. [[px;height:]] .. height .. [[px;">]]
   local feature,s,e,nextcolor
   nextcolor=1
   local color={"#000055","#000099","#0000CC","#0000FF","#550055","#550099","#5500CC","#5500FF","#990055","#990099","#9900CC","#9900FF","#CC0055","#CC0099","#CC00CC","#CC00FF","#FF0000","#FF0055","#FF0099","#FF00CC","#FF00FF","#005555","#005599","#0055CC","#0055FF","#55555","#555599","#5555CC","#5555FF","#995555","#995599","#9955CC","#9955FF","#CC5555","#CC5599","#CC55CC","#CC55FF","#FF5500","#FF5555","#FF5599","#FF55CC","#FF55FF"}
   local claim={} -- takes a name, stakes out a color as the returned value
   for feature, range in mw.ustring.gmatch(file,"#|(.-)|#") do
       local t=mw.ustring.match(feature,"^(%a)")
       local s=mw.ustring.match(feature,"(%d+)")
       local e=mw.ustring.match(feature,"^.%s-%d+%.%.(%d+)") or s
 
       if s then
          local n,c
          if t=="R" then n=mw.ustring.match(feature,[[/region_name=%"(.-)%"]]) end
          if t=="S" then n=mw.ustring.match(feature,[[/site_type=%"(.-)%"]]) end
          n=tostring(n)
          if tusenotes[n] then n=mw.ustring.match(feature,[[/note=%"(.-)%"]]) or n end
          n=tostring(n)
          n=mw.ustring.match(n,"^%s+(.+)%s+$") or n -- kill white space
          n=mw.ustring.gsub(n,"\n"," ") or n -- remove line feeds
          n=tostring(n) -- am I paranoid?
          if tsubstitute[n] then n=tostring(tsubstitute[n]) end
          n = mw.ustring.match(n,"(.+)%.") or n -- Don't need the ubiquitous final periods
          local nkey=mw.ustring.match(n,"(.+)[%.;,%(%[]") or n
          if claim[nkey] then c=claim[nkey] else c=color[nextcolor];claim[nkey]=c;nextcolor=nextcolor+1 end
          local showthismotif=true
          if include then if not (tinclude[n]) then showthismotif=nil end end -- if include is set, and n isn't in it, don't add to table or graphic
          if exclude then if texclude[n] then showthismotif=nil end end -- if exclude is set and n is in it don't add
          if showthismotif then
             if tostring(t)=="R" then output = output .. "\n|region\n|" else output = output .. "\n|site\n|" end
             output = output .. tostring(s) .. "\n|" .. tostring(e) .. "\n|" .. n .. "\n|-"
             local boxleft=math.floor(width*tonumber(s)/cdswidth)
             local boxwidth=math.floor(width*tonumber(e)/cdswidth)-boxleft
             graphics = graphics .. [[<div style="position:absolute;overflow:hidden;z-index:]] .. -1*boxwidth .. [[;left:]] .. boxleft .. [[px;border:2px;border-style:solid;border-color:]].. c .. [[;background-color:]].. c .. [[;width:]] .. boxwidth .. [[px;height:]] .. height .. [=[px;"></div><span style="position:absolute;z-index:100;left:]=] .. math.floor(boxleft+boxwidth/2) .. [[;down:]] .. math.floor(height/2) .. [[;>]] .. nkey .. [[</div>]]
          end
       end
   end
   tableoutput="yes" -- for debug, we'll leave the table
   if not(tableoutput) then output = "" end
   
   -- graphics is the next thing to implement!
   local output = graphics .. "</div>\n" .. output
   return output

end

return p