Advertisement
Crashguard303

CG303 Structure Sections V5.10

Oct 12th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.65 KB | None | 0 0
  1. --[[
  2. Structure Sections by Crashguard303
  3. Finds sections of cohesive secondary structures in a puzzle.
  4. Improved table-handling.
  5. Creates a flag-table marking found letters of the secondary structure
  6. and a 3-dimensional section table with dimensions:
  7. structure letter,
  8. section number (with this letter),
  9. start & end segment (with this section number and letter).
  10.  
  11. Example:
  12. structure_section.H[2].first: first segment of 2nd helix section (if existing)
  13. structure_section.H[2].last: last segment of 2nd helix section (if existing)
  14.  
  15. See script text for details.
  16. ]]--
  17. function find_structure_sections()
  18.  -- Phase 1:
  19.  -- by using table found, store in each line of this table:
  20.  -- another secondary structure letter (.s) (must be different to last in line),
  21.  -- first (.first)
  22.  -- and last (.last) segment with structure (.s)
  23.  
  24.  local found={} -- initialize table
  25.  
  26.  found=found_new(found,structure.GetSecondaryStructure(1),1)
  27.  -- the very first segment is also a new letter
  28.  -- expand found-list by 1, at new found-list position: write new letter and current segment number as first-segment-information
  29.  
  30.  local i
  31.  for i=2,structure.GetCount() do -- by variable i, cycle through all segments of the puzzle
  32.   local s=structure.GetSecondaryStructure(i) -- and fetch secondary structure letter at segment i
  33.   if s~=found[#found].s -- if actual letter is unequal to actual one in our found-list
  34.    then found[#found].last=i-1
  35.    -- actual section has ended one segment before,
  36.    -- so at current found-list position: write previous segment number as last-segment-information
  37.     found=found_new(found,s,i)
  38.     -- expand found-list by 1, at new found-list position: write new letter and current segment number as first-segment-information
  39.   end -- if
  40.  end -- i
  41.  found[#found].last=structure.GetCount()
  42.  -- the very last segment closes the found-list with a last-segment-information
  43.  -- at current found-list position: write last segment number as last-segment-information
  44.  
  45.  -- uncomment this to show found table contents
  46.  -- for i=1,#found do
  47.  -- print(found[i].s,found[i].first,found[i].last)
  48.  -- end -- i
  49.  -- print()
  50.  
  51.  -- Phase 2:
  52.  -- by using table structure_section, fetch information from table found
  53.  -- store structure sections sorted by letter and position
  54.  local structure_created_flag={} -- initialize flag table, so we can see if we have already created a table for this letter
  55.  local structure_section={} -- initialize basic table for letters
  56.  
  57.  local i
  58.  for i=1,#found do -- by variable i, cycle trough all lines in our found-list which have letters, first and last segment information
  59.   s=found[i].s -- fetch s = letter at current found-list line
  60.   if structure_created_flag[s]==nil -- if s is not flagged as created
  61.    then structure_section[#structure_section+1]=s -- expand basic table and append s as found letter
  62.         structure_section[s]={} -- in s, create a subtable for all sections which have s
  63.         structure_created_flag[s]=true -- flag s as created
  64.   end -- if
  65.  
  66.   structure_section[s][#structure_section[s]+1]={} -- expand subtable at s
  67.   structure_section[s][#structure_section[s]].first=found[i].first -- at new position, write first
  68.   structure_section[s][#structure_section[s]].last=found[i].last -- and last segment position of section
  69.  
  70.   -- uncomment this to watch table conversion process
  71.   -- print(s,#structure_section[s],structure_section[s][#structure_section[s]].first,structure_section[s][#structure_section[s]].last)
  72.  
  73.  end -- i
  74.  
  75.  return structure_created_flag,structure_section
  76.  -- return flag table marking found letters with true and
  77.  -- return section table created in phase 2
  78.  -- old table found is obsolete and won't be returned
  79. end -- function
  80.  
  81. function found_new(found,s,i)
  82. -- expand found-list table
  83.  local found=found -- copy current found-table (I'm not sure if it necessary, but to me it's the safe way)
  84.  found[#found+1]={} -- expand found-list by 1
  85.  found[#found].s=s -- at new found-list position: write new letter
  86.  found[#found].first=i -- and current segment number as first-segment-information
  87.  return found -- return expanded table, which is now 1 line more
  88. end -- function
  89.  
  90. structure_found_flag,structure_section=find_structure_sections()
  91. -- fetch structure found flag table marking found letters with true and section table
  92.  
  93. print("Found structures:")
  94.  
  95. local i
  96. for i=1,#structure_section do -- by variable i, cycle through all letters which have been found
  97.  local s=structure_section[i] -- fetch letter s at position i
  98.  print()
  99.  print(s..": "..#structure_section[s].." times") -- show how many sections with s are there
  100.  for j = 1,#structure_section[s] do -- by variable j, cycle through all sections with letter s
  101.   print(structure_section[s][j].first.." to "..structure_section[s][j].last)
  102.   -- show first and last segment of each section j with letter s
  103.  end -- j
  104. end -- i
  105.  
  106. --[[Table usage (examples):
  107. structure_found_flag: true if a structure (given by letter) was found
  108.                       nil if it was not found
  109. structure_found_flag["E"] or
  110. structure_found_flag.E: true if sheets were found
  111. structure_found_flag["H"] or
  112. structure_found_flag.H: true if helices were found
  113. structure_found_flag["L"] or
  114. structure_found_flag.L: true if loops were found
  115.  
  116. #structure_section: number of DIFFERENT structures which have been found.
  117.                     if there are just loops, it will be 1,
  118.                     if there are loops and sheets, it will be 2
  119.                     if there are loops, sheets and helices, it will be 3 etc.
  120.  
  121. structure_section[1]: letter of first structure which has been found
  122. structure_section[2]: letter of second structure which has been found
  123. structure_section[n]: letter of nth structure which has been found (n shouldn't be bigger than #structure_section)
  124.  
  125. For example, if there were loops found at first and then helices,
  126. structure_section[1] will be "L",
  127. structure_section[2] will be "H",
  128.  
  129. #structure_section["E"] or
  130. #structure_section.E: number of sheet sections found (if existing)
  131. #structure_section["H"] or
  132. #structure_section.H: number of helix sections found (if existing)
  133. #structure_section["L"] or
  134. #structure_section.L: number of loop sections found (if existing)
  135.  
  136. structure_section["E"][1]["first"] or
  137. structure_section.E[1].first: first segment of 1st sheet section (if existing)
  138. structure_section.E[1].last: last segment of 1st sheet section (if existing)
  139.  
  140. structure_section.H[2].first: first segment of 2nd helix section (if existing)
  141. structure_section.H[2].last: last segment of 2nd helix section (if existing)
  142.  
  143. structure_section.L[3].first: first segment of 3rd loop section (if existing)
  144. structure_section.L[3].last: last segment of 3rd loop section (if existing)
  145. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement