[Bio] / FigKernelScripts / translate_fasta.c Repository:
ViewVC logotype

View of /FigKernelScripts/translate_fasta.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Mon Jun 11 20:08:03 2012 UTC (7 years, 5 months ago) by golsen
Branch: MAIN
CVS Tags: rast_rel_2014_0729, rast_rel_2014_0912, HEAD
Support for codon usage analyses.

/*
 *  translate_fasta.c
 *
 *  Usage: translate_fasta [options] < fasta > fasta
 *
 *  Options:
 *
 *      -f                   //  Flush the output after every sequence
 *      -g NCBI_code_number  //  Translate using given NCBI genetic code (D = 1)
 *      -m                   //  Start protein with Met
 *      -v                   //  Print version to standard output and exit
 *
 *  cc -O3 -o translate_fasta translate_fasta.c
 *
 *  Versions:
 *
 *      1.00 - Unnumbered first release
 *
 *      1.01 - Add -f, -g, -m and -v flags
 */

#include <stdio.h>
#include <stdlib.h>    // atoi

#include "gjoseqlib.h"
#include "gjoseqio.h"
#include "gjoseqlib.c"
#include "gjoseqio.c"

#define  VERSION  "1.01"

void usage( char * command );


int main( int argc, char ** argv )
{
    seq_t *entry, *xlation;
    int    flush     = 0;
    int    met       = 0;
    int    NCBI_code = 1;
    int    i;

    /* Read command flags */

    i = 1;
    while ( ( i < argc ) && ( argv[i][0] == '-' ) )
    {
        if ( argv[i][1] == 'f' )
        {
            flush = 1;
        }
        else if ( argv[i][1] == 'g' )
        {
            if ( argv[i][2] ) { argv[i] += 2; }
            else if ( ++i >= argc )
            {
                fprintf( stderr, "Missing genetic code number after -g\n" );
                usage( argv[0] );
                return 1;
            }
            NCBI_code = atoi( argv[i] );

            // NCODES (defined in gjoseqlib.c) includes a code number zero
            // Invalid codes between 1 and 23 (e.g., 7 and 20) default to 1.
            if ( NCBI_code <= 0 || NCBI_code > NCODES-1 )
            {
                fprintf( stderr, "Bad genetic code number '%s'.\n", argv[i] );
                fprintf( stderr, "Allowed values are 1 to %d.\n", NCODES-1 );
                usage( argv[0] );
                return 2;
            }
        }
        else if ( argv[i][1] == 'm' )
        {
            met = 1;
        }
        else if ( argv[i][1] == 'v' )
        {
            printf( "%s\n", VERSION );
            return 0;
        }
        else
        {
            fprintf( stderr, "Bad flag '%s'.\n", argv[i] );
            usage( argv[0] );
            return 3;
        }

        i++;
    }

    while ( ( entry = read_next_fasta( stdin ) ) )
    {
        if ( ( xlation = translate_entry_with_code( entry, met, NCBI_code ) ) )
        {
            write_fasta_entry( stdout, xlation );
            if ( flush ) fflush( stdout );
            free_sequence( xlation );
        }
        free_sequence( entry );
    }

    return( 0 );
}


/*-----------------------------------------------------------------------------
 *  Usage statement:
 *
 *      void usage( char * command )
 *
 *-----------------------------------------------------------------------------
 */

void usage( char * command )
{
    fprintf( stderr, "\n" );
    fprintf( stderr, "Usage: %s [options] < fasta > fasta\n", command );
    fprintf( stderr, "\n" );
    fprintf( stderr, "Options:\n\n" );
    fprintf( stderr, "\n" );
    fprintf( stderr, "    -f                   //  Flush the output stream after every sequence\n" );
    fprintf( stderr, "    -g NCBI_code_number  //  Translate using given NCBI genetic code (D = 1)\n" );
    fprintf( stderr, "    -m                   //  Start all translations with Met\n" );
    fprintf( stderr, "    -v                   //  Print version to standard output and exit\n" );
    fprintf( stderr, "\n" );

    return;
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3