Coming soon ...

Let's start building a library here for:

-replacements for the missing standard library functions

- game-specific library functions

We can upload library functions to the regular foldit recipe page and simply link to them from here.

Or make new pages for each function (users can cut and paste into their game clients).

## Integer

```function int(x) -- integer function

temp=x-x%1

if temp<1 then temp=temp+1 end

return temp

end
```

## proportional function

```function proportional(a,b,x) -- If a is proportional to b, what is x? use b=100 for percent. temp=x*b/a

return temp

end
```

## Rescale function

```function rescale(x,x1,x2,y1,y2) -- x is a value in a scale between x1-x2. what would x be within a scale of y1-y2?  y=(y2-y1)/(x2-x1)*(x-x1)+y1 return y

end
```

For example, if there is a value x which only moves between 130 and 297, and we want to stretch it to 0 and 100, we can use rescale(x,130,297,1,100)

```Useful for percentage when you run a loop running backwards and/or starting at with an offset.
```

You can also use it to morph values: Morph -100 to 236, where x=0 -> y=-100; and at x=1 -> y=236 use

rescale (x,0,1,-100,236

```Increasing x in fine values form 0 to 1 will give you values between -100 and 236
```

## Get Structure functions

These two functions return information about secondary structures.  getStructIndices takes an index and returns the start and end points of that index's structure.  For example, if we have a loop from segments 5 -10 and you call getStructIndices(3), the result would be 5, 10 (note that in Lua we can return multiple results!).

```function getStructIndices (i)   -- i is an index to any segment in the protein	                s = i     -- initialize start segment        e = i     -- initialize end segment      	-- look forward   to find end point  	for j = i+1, get_segment_count() do  	   if get_ss(j) ~= get_ss(i) then  	      break              -- we hit a new structure type; we're done.  	   end           e = j	  	  	end  	-- look back   to find start point  	for k = i-1, 1, -1 do  	     if get_ss(k) ~= get_ss(i) then  	      break  	   end  	   s = k  	  	end  	return s, e    end
```

To use getStructIndices, simply call it with a whole number as a parameter (index to a segment in the protein).  It will return two values, a start index and and end index.  Below are some examples of how you might use the functions and the values it returns.

```-- Example of use for getStructIndices()

s, e = getStructIndices(5)            -- get indices for struct at index 5 and store as s (start) and e (end)
print("start = " , s , " end = " , e) -- print start and end points
select_index_range(s, e)              -- another way to use s and e.  Send them to a function of your choice.
do_global_wiggle_all(5)               -- you can wiggle that whole structure by itself!
```

Next, we have a related function: getStructTable().  Note that getStructTable depends on getStructIndices, so that means you'll need to paste both functions into your script if you wish to use getStructTable() (note that if you just want getStructIndices, it will work fine by itself).  getStructTable() takes no parameters and iterates through the whole protein, calling getStructIndices() several times to compile a table of all of the start and end points of structures.  For example, a table is returned with values {1, 5, 6, 9, 10, 10} for a protein that has a loop in indices 1 - 5 inclusive, a helix structure in 6 - 9 inclusive, and another loop at segment 10.  Note this is a bit redundant and we might want to improve the efficiency (we don't need both start and end points for each).  But it is useful if we want to feed the table into a function that wiggles each structure in turn, for example.

```function getStructTable()

structTable = {}       -- our empty table of values
curSeg, tableIndex = 1

while curSeg <= get_segment_count() do
s, e = getStructIndices(curSeg)                                -- dependent on getStructIndices
structTable[tableIndex], structTable[tableIndex + 1] = s, e
tableIndex = tableIndex + 2                                    -- we're filling pairs of values
curSeg = e + 1		                                       -- next point to look at is seg after e
end

return structTable
end
```

To use getStructTable, make sure to include the code for the function it's dependent on, getStructIndices().  It needs no parameters and returns a table.  In Lua, tables are similar to arrays.  The examples below will show you how to use getStructTable and access its table.

```myTable = getStructTable(
)             -- it's convenient to assign the table to a variable so we can easily access it.
printStr = " "

-- print table
for i = 1, # myTable do
-- # means "size of"
printStr =  printStr .. "table entry " .. i .. " " .. myTable[i]
-- .. means "concatenate"
end

print(printStr)

-- wiggle each structure in turn
for i = 1, # myTable, 2 do
select_index_range(myTable[i], myTable[i+1])
do_global_wiggle_all(10)
deselect_all()
end
```

## Some really simple functions

These ones fit into the "duh" category, but are useful to some!

### abs(n)

Return the absolute value of n.

```function abs(n)
if n >= 0 then
return n
else
return -n
end
end
```

### printTable(t)

print out contents of a table (formatted nicely!)

```function printTable(t)
txt = ""
for i = 1, # t do
txt = txt .. "  " .. i .. "  " .. t[i] .. "\n"
end
print(txt)
end
```

## Randomization Functions

I'm just listing some functions we already have (implemented in existing scripts) -- will go in and fill in code later.

### getRandomSegment()

gets a random seg between 1 and end of protein.

### getRandomInt(a, b)

gets a random integer in the range of a and b. This should already be handled by math.random with two integer arguments. E.g. math.random(1,100)

### setRandomBand([s1][,e1][,s2],[e2])

sets a random band somewhere in the segment.  If called with no parameters, sets a random band between any two segs.  If called with s1, e1, sets a random band between two segments in the range s1, e1.  If called with all parameters, sets a random band between a segment in the range s1, e1 and a segment in the range s2, e2.

## Ptable Functions

We can build a "Ptable" which is a table filled with all the information we can pull out of a protein, segment by segment.  Included will be amino acid,  secondary structure, segment score, all the segment score parts (backbone, clashing, packing, etc.).  We can also derive some other data, such as distance to start/end

## bandEveryN(n [,s][,e])

bands every n segments.  If s and e are specified, bands every n segments between s and e.  Otherwise, does it to the whole protein.

```function bandEveryN(n, s, e)
if (s==null) or (e==null) then
s = 1
e = get_segment_count()
end

for i = s, e, n do
if (i+n <= get_segment_count()) then