next up previous contents
Next: Simplification Up: Defining New Structured Types: Previous: Defining New Structured Types:

   
Error Checking

We can code error checking into our type definitions in two ways: (1) type checking and (2) data verification.

Type Checking: Since our function declaration for ProEntry allows for a variable number of arguments we can not implement type checking in the manner described in Chapter [*] Procedures where we included the type of the parameter after the name of the parameter in the proc declaration line. Instead, we must delay checking until the body of the proc. We modify our definition of ProEntry so that it verifies that the information passed to it is of the correct type.

Data Verification: Certainly, the calculation of the Length field would be better carried out by a computer than by an assistant with better things to do with his or her time. The computer would also be less prone to error (and the monitor screen free of fingerprints!). We can make ProEntry more robust by exploiting the flexibility the args, nargs built-in variables offer by allowing ProEntry to also accept only five arguments without Length. This last field will be calculated within the ProEntry function. If the user insists on supplying the length of the protein sequence, our procedure will merely check their work and report a warning if it does not match that supplied by the user.

>  ProEntry := proc( )
>    description
>    '  Data type to contain information about a protein sequence
>    Parameters:
>      Name     : string   ('' indicates field is uninitialized)
>      DB       : string   ('' indicates field is uninitialized)
>      AC       : string   ('' indicates field is uninitialized)
>      Organism : string   ('' indicates field is uninitialized)
>      Sequence : string   ('' indicates field is uninitialized)
>      Length   : integer  (-1 indicates field is uninitialized)
>    ';
>
>    if (nargs=0) then
>      return(noeval(ProEntry('', '', '', '', '', -1)));
>    fi;
>
>    if not (type( args[1], string) and type(args[2], string) 
>         and type( args[3], string) and type(args[4], string) 
>         and type( args[5], string)) then
>      print( ProEntry );
>      error('Invalid types in ProEntry format');
>    fi;
>
>    if (nargs=6) then
>      if not (type(Length, integer)) then 
>        print(ProEntry);
>        error('Invalid types in ProEntry format');
>      else
>        if (args[6]<>length(args[5])) then
>          print('Warning: you counted the length incorrectly', args);
>        fi;
>        return(noeval(ProEntry(args)));
>      fi;
>    elif (nargs=5) then
>      return(noeval( ProEntry(args, length(args[5]))));
>    else
>      print( ProEntry );
>      error('Invalid number of parameters to ProEntry');
>    fi;
>  end:
>
>  protentry1 := ProEntry('ABL1_CAEEL', 'SwissProt', 'P03949', 
>       'C. ELEGANS', 'NNEWCEARLYSTRKNDASNQRRLGEIGWVPSNFIAPYNSLDK');

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

We could extend our error checking in several ways. For example, it would be prudent to verify that each character of the Sequence field is one of the twenty amino acids. We omit these straightforward modifications here due to space considerations.


next up previous contents
Next: Simplification Up: Defining New Structured Types: Previous: Defining New Structured Types:
Gaston Gonnet
1998-09-15