--
-- functions for bio sequences
--

DECLARE FUNCTION reverse_complement(
   residues TEXT)
 RETURNS TEXT;
COMMENT ON FUNCTION reverse_complement(TEXT) IS
 '
Calculates the reverse complement of a DNA sequence. The value of the argument must an an IUPAC compliant string
 ';

-- Requires: gencode module; see modules/sequence/gencode
DECLARE FUNCTION translate_dna(
   residues TEXT)
 RETURNS TEXT;
COMMENT ON FUNCTION translate_dna(TEXT) IS
 '
Translates a DNA sequence to AA sequence based on standard genetic code
 ';

-- Requires: gencode schema; see modules/sequence/gencode
DECLARE FUNCTION translate_dna(
   residues TEXT,
   gencode_id INT)
 RETURNS TEXT;
COMMENT ON FUNCTION translate_dna(TEXT,INT) IS
 '
Translates a DNA sequence to AA sequence based on NCBI genetic code ID (1=standard). See gencode schema module for details
 ';

DECLARE FUNCTION share_exons()
  RETURNS void;
COMMENT ON FUNCTION share_exons() IS
'
This is a function to 'unshare' exons.  It looks for exons that have the same fmin and fmax and belong to the same gene and only keeps one.  The other, redundant exons are marked obsolete in the feature table.  Nothing is done with those features' entries in the featureprop, feature_dbxref, feature_pub, or feature_cvterm tables.  For the moment, I'm assuming that any annotations that they have when this script is run are identical to their non-obsoleted doppelgangers.  If that's not the case, they could be merged via query.  The bulk of this code was contributed by Robin Houston at GeneDB/Sanger Centre.
'

DECLARE FUNCTION order_exons(
    parent_type_id INT)
  RETURNS void;
COMMENT ON FUNCTION order_exons(INT) IS 
 '
This is a function to seek out exons of transcripts and orders them, using feature_relationship.rank, in "transcript order" numbering from 0, taking strand into account. It will not touch transcripts that already have their exons ordered (in case they have a non-obvious ordering due to trans splicing). It takes as an argument the feature.type_id of the parent transcript type (typically, mRNA, although non coding transcript types should work too).
 ';
