next up previous contents
Next: Error Checking Up: Structured Types Previous: Structured Types

   
Defining New Structured Types: An Example

As a concrete example of how to go about creating a new structured data type, we will build a structure to hold a protein sequence and some information about it. In particular, our data type should have fields to contain the name of the sequence, which database the sequence is located in, the accession number of the sequence, the organism from where it was derived, the length of the protein sequence and, of course, the protein sequence itself. We begin by creating a data type ProEntry as follows.

> ProEntry := proc( )
>   description
>   '  Data type to contain information about a protein sequence
>   Parameters:
>     Name, DB, AC, Organism, Sequence, Length
>   ';

>   if ( nargs=0 ) then
>     return(noeval(ProEntry('', '', '', '', '', -1)));
>   elif ( nargs=6 ) then
>     return(noeval(ProEntry(args)));
>   else
>     print( ProEntry );
>     error('Invalid ProEntry format');
>   fi;
> end:
Let us follow through this definition line by line.  

Recall that nargs is a system name within the scope of a routine which contains the number of arguments passed to it on that invocation. The args name is assigned the expression sequence consisting of all arguments passed to the routine.

The body of ProEntry consists of a single if-then-fi clause. If ProEntry is called with no arguments, Darwin creates a data structure ProEntry and initializes the six fields appropriately. The noeval statement delays Darwin from attempting to evaluate ProEntry by invoking it as it would normally do when it encounters a procedure or function name. Instead upon encountering a noeval command, Darwin treats the whole segment in the same manner it would treat a expression sequence and simply returns it.

If ProEntry is called with exactly six arguments, Darwin creates a new data structure ProEntry, it assigns the ith entry of args to the ith element of ProEntry for i from $1\ldots6$, and returns it unevaluated.

If ProEntry is called with any other number of arguments, the description header for the structured type is issued and an error is reported (see Chapter [*] for an explanation of the error function).

We are now in a position to create a structure of type ProEntry. There are at least two ways to accomplish this: we can either pass the items of data for a protein sequence as arguments to ProEntry or we may create an empty ProEntry structure and assign to the elements of ProEntry ourselves.5.2

> #  The first method

> protentry1 := ProEntry('ABL1_CAEEL', 'SwissProt', 'P03949', 
>               'C. ELEGANS',  
>               'NNEWCEARLYSTRKNDASNQRRLGEIGWVPSNFIAPYNSLDK', 42);

protentry1 := ProEntry(ABL1_CAEEL,SwissProt,P03949,C. ELEGANS,
NNEWCEARLYSTRKNDASNQRRLGEIGWVPSNFIAPYNSLDK,42)

> #  The second method
 
> protentry2 :=ProEntry();            # first initialize an empty 
                                      # structure
protentry2 := ProEntry(,,,,,-1)

> protentry2[1] := 'ABL1_CAEEL':
> protentry2[2] := 'SwissProt':
> protentry2[3] := 'P03949':
> protentry2[4] := 'C. ELEGANS':
> protentry2[5] := 'NNEWCEARLYSTRKNDASNQRRLGEIGWVPSNFIAPYNSLDK':
> protentry2[6] := 42:
> print(protentry2);
ProEntry(ABL1_CAEEL,SwissProt,P03949,C. ELEGANS,
NNEWCEARLYSTRKNDASNQRRLGEIGWVPSNFIAPYNSLDK,42)

Now we may examine each item of data contained in protentry1 by indexing into it in the same manner we would index into the elements of an array.

> protentry1[1];                 # the name of the entry
ABL1_CAEEL
> protentry1[2];                 # the accession number for the entry
SwissProt
> protentry1[3..5];              # organism, sequence, length
P03949, C. ELEGANS, NNEWCEARLYSTRKNDASNQRRLGEIGWVPSNFIAPYNSLDK

Of course, we can define as many structures of type ProEntry as we would like and each structure will have the same format. This is already a marginal improvement over using, for example, the list type to manage our data. However, the real power manifested in Darwin's structured types still lies ahead. We can improve our type definition to include three powerful tools which reduce the amount of error, simplify our programs and help keep our data stored in a uniform fashion. We modify type ProEntry throughout the next three subsections to include these features.



 
next up previous contents
Next: Error Checking Up: Structured Types Previous: Structured Types
Gaston Gonnet
1998-09-15