fastq-lex.l 7.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
/*===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
*/

%{
    #include <sysalloc.h>
    #include <ctype.h>

    #include <klib/rc.h>
    #include <klib/log.h>

    #include "fastq-parse.h"
    #include "fastq-tokens.h"

    #define YYSTYPE FASTQToken

    /* code to populate token with internal values */
	static void ConsumeToken(FASTQToken* token, FASTQParseBlock* pb);
    #define YY_USER_ACTION ConsumeToken(yylval, yyextra);

    #define ENDLINE \
        yyextra -> column=1;\
        return fqENDLINE;

    #define YY_INPUT(buf, result, max_size) \
        result = yyextra->input(yyextra, buf, max_size)

    #define ECHO

    static void FASTQ_fatal_error(yyconst char* msg);
    #define YY_FATAL_ERROR(msg) FASTQ_fatal_error(msg)

%}

%option never-interactive nounistd yylineno reentrant bison-bridge stack noyywrap

%option extra-type="FASTQParseBlock*"

 /*%option debug*/

%x TAG_LINE
%x SEE_COORDS
%x PAST_COORDS
%x READ_NUMBER
%x NAMEVALUE
%x IN_SEQUENCE
%x QTAG_LINE
%x IN_QUALITY
%x INLINE_SEQUENCE
%x INLINE_QUALITY
%x SKIP_TO_EOL

ws          [ \t]+
digits      [0-9]+
alphanum    [A-Za-z0-9\-]+
eol         (\r\n|\r|\n)

/* there are more: =ACMGRSVTWYHKDBN. plus lowercase */
base [ACGTacgtNn.]
cskey [ACGTacgt]
color   [0-3.]

ascqual   [\x0E-\x1F \x21-\x7F]+

%%

^@                              { BEGIN TAG_LINE; return yytext[0]; }
^>                              { BEGIN TAG_LINE; return yytext[0]; }

^\+                             { BEGIN QTAG_LINE; return yytext[0]; }

<INITIAL,TAG_LINE>
{
    :{digits}:{digits}:{digits}:{digits}                    { return fqCOORDS; }
    [SDE]RR{digits}\.{digits}   { return fqRUNDOTSPOT; }
    #[A-Za-z0-9\-_]*            { return fqSPOTGROUP; }
    {digits}                    { return fqNUMBER; }
    {alphanum}                  { return fqALPHANUM; }
    {ws}                        { return fqWS; }
    ^{base}+/{eol}              { return fqBASESEQ; }
    ^{cskey}{color}+/{eol}      { return fqCOLORSEQ; }
    .                           { return yytext[0]; }
}

<TAG_LINE>[ \t]*{eol}           { BEGIN IN_SEQUENCE; ENDLINE; }

<IN_SEQUENCE>
{
    ^{base}+/{eol}              { BEGIN 0; return fqBASESEQ; }
    ^{cskey}{color}+/{eol}      { BEGIN 0; return fqCOLORSEQ; }
    ^{ascqual}/{eol}            { BEGIN 0; return fqASCQUAL; }
    ^.*$                        { BEGIN 0; return fqUNRECOGNIZED; }
}

<INLINE_SEQUENCE>
{
    {base}+                     { BEGIN 0; return fqBASESEQ; }
    {cskey}{color}+             { BEGIN 0; return fqCOLORSEQ; }
    {digits}                    { BEGIN 0; return fqNUMBER; }
    {eol}                       { BEGIN 0; ENDLINE; }
}

<QTAG_LINE>
{
    .+$                         { return fqTOKEN; /* do not bother to parse the inside of the line */ }
    {eol}                       { BEGIN IN_QUALITY; ENDLINE }
}

<IN_QUALITY,INLINE_QUALITY>
{
    {ws}                        { }
}

<IN_QUALITY>
{
    ^{ascqual}/{eol}            { return fqASCQUAL; }
    {eol}                       {   /* if read was split across several lines, expect the same number of lines in quality */
                                    if (yyextra->expectedQualityLines <= 1)
                                        BEGIN 0;
                                    else
                                        --yyextra->expectedQualityLines;
                                    ENDLINE;
                                }
    .                           { return yytext[0]; }
}

<INLINE_QUALITY>
{
    {ascqual}/{eol}             { return fqASCQUAL; }
    {eol}                       { BEGIN 0; ENDLINE; }
    .                           { return yytext[0]; }
}

<SKIP_TO_EOL>
{
    .*{eol}                     { BEGIN 0; ENDLINE; }
    .*                          { BEGIN 0; return fqENDOFTEXT; }
}

{eol}                           { ENDLINE; }

%%

rc_t CC FASTQScan_yylex_init(FASTQParseBlock* sb, bool debug)
{
    if (yylex_init_extra(sb, &sb->scanner) != 0)
    {
        return RC ( rcApp, rcFile, rcParsing, rcMemory, rcExhausted );
    }

    sb->length = 0;
    sb->lastToken = NULL;
    sb->record = NULL;
    sb->column = 1;

    sb->expectedQualityLines = 0;

    yyset_debug(debug ? 1 : 0, sb->scanner);

    yy_push_state(INITIAL, sb->scanner);

    return 0;
}

int FASTQ_lex(FASTQToken* tok, FASTQParseBlock * pb)
{
    return yylex ( tok, pb -> scanner );
}

void CC FASTQScan_yylex_destroy(FASTQParseBlock* sb)
{
    if (sb->scanner)
    {
        yylex_destroy(sb->scanner);
    }
    sb->scanner=0;
}

void CC FASTQScan_inline_sequence(FASTQParseBlock* pb)
{
    yy_pop_state(pb->scanner);
    yy_push_state(INLINE_SEQUENCE, pb->scanner);
}

void CC FASTQScan_inline_quality(FASTQParseBlock* pb)
{
    yy_pop_state(pb->scanner);
    yy_push_state(INLINE_QUALITY, pb->scanner);
}

void CC FASTQScan_skip_to_eol(FASTQParseBlock* pb)
{
    struct yyguts_t* yyg = (struct yyguts_t*)pb->scanner;
    if ( yytext[0] != '\n' && yytext[0] != '\r' ) /* not at an EOL already */
    {
        yy_push_state(SKIP_TO_EOL, pb->scanner);
    }
}

void CC FASTQ_unlex(FASTQParseBlock* pb, FASTQToken* token)
{
    size_t i;
    struct yyguts_t* yyg = (struct yyguts_t*)pb->scanner;
    for (i = 0; i < token->tokenLength; ++i)
    {
        yyunput(TokenTextPtr(pb, token)[token->tokenLength - 1 - i], yyg->yytext_r, pb->scanner);
    }
    yyextra -> column -= token->tokenLength;
    yyextra -> length -= token->tokenLength;
	KDataBufferResize( & pb->record->source, KDataBufferBytes( & pb->record->source ) - token->tokenLength );
}

void FASTQ_fatal_error(yyconst char* msg)
{
    rc_t rc = RC ( rcApp, rcFile, rcParsing, rcError, rcUnexpected );
    if (strcmp(msg, "input buffer overflow, can't enlarge buffer because scanner uses REJECT") == 0)
        pLogErr(klogErr, rc, "line is too long (maximum line length = $(B) bytes)", "B=%d", YY_BUF_SIZE);
    else
        LogErr(klogErr, rc, msg);
    exit(rc);
}

void ConsumeToken(FASTQToken* token, FASTQParseBlock* pb)
{
    struct yyguts_t* yyg = (struct yyguts_t*)pb->scanner;

	token -> tokenStart = KDataBufferBytes( & pb->record->source );
	token -> tokenLength   = yyleng;
	token -> line_no       = yylineno;
	token -> column_no     = pb -> column;

	KDataBufferResize( & pb->record->source, KDataBufferBytes( & pb->record->source ) + token->tokenLength );
	strncpy((char*)TokenTextPtr(pb, token), yytext, token->tokenLength);

	pb -> column += token->tokenLength;
	pb -> length += token->tokenLength;
	pb -> lastToken = token;
}