ThomasDickey / original-mawk

bug-reports for mawk (originally on GoogleCode)
http://invisible-island.net/mawk/mawk.html
17 stars 2 forks source link

type error in arg(2) in call to <custom function> #33

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

# Running with mawk fails:
$ mawk -f split_meme_motif_file.awk mawk_test.meme
mawk: split_meme_motif_file.awk: line 31: type error in arg(2) in call to 
print_splitted_motif_file
mawk: split_meme_motif_file.awk: line 85: type error in arg(3) in call to 
split_meme_motif_file

# Running with gawk works:
$ gawk -f split_meme_motif_file.awk mawk_test.meme
Creating 'mawk_test.meme.part_0.meme'...
Creating 'mawk_test.meme.part_1.meme'...
Creating 'mawk_test.meme.part_2.meme'...
Creating 'mawk_test.meme.part_3.meme'...
Creating 'mawk_test.meme.part_4.meme'...
Creating 'mawk_test.meme.part_5.meme'...
Creating 'mawk_test.meme.part_6.meme'...
Creating 'mawk_test.meme.part_7.meme'...
Creating 'mawk_test.meme.part_8.meme'...
Creating 'mawk_test.meme.part_9.meme'...
Creating 'mawk_test.meme.part_10.meme'...
Creating 'mawk_test.meme.part_11.meme'...
Creating 'mawk_test.meme.part_12.meme'...
Creating 'mawk_test.meme.part_13.meme'...
Creating 'mawk_test.meme.part_14.meme'...
Creating 'mawk_test.meme.part_15.meme'...

$ cat split_meme_motif_file.awk
#!/usr/bin/awk -f

function print_splitted_motif_file(meme_header, motifs_array, motif_idx_start, 
motif_idx_end, motif_output_filename,    motif_idx) {
    print "Creating '" motif_output_filename "'..." >> "/dev/stderr";

    # Write MEME header to output file.
    print meme_header > motif_output_filename;

    # Write selected motifs to output file.
    for (motif_idx = motif_idx_start; motif_idx <= motif_idx_end; motif_idx++) {
        print motifs_array[motif_idx] >> motif_output_filename;
    }
}

function split_meme_motif_file(motif_input_filename, meme_header, motifs_array, 
number_of_splits,    number_of_motifs, motifs_per_splitted_file, split_number, 
motif_idx_start, motif_idx_end) {
    # Get the number of motifs.
    number_of_motifs = length(motifs_array)

    # Calculate the number of motifs for each spitted file (add one additional count to compensate for rounding issues).
    motifs_per_splitted_file = int(number_of_motifs / number_of_splits) + 1;

    for (split_number = 0; split_number < number_of_splits; split_number++) {
        motif_idx_start = motifs_per_splitted_file * split_number;
        motif_idx_end = motif_idx_start + motifs_per_splitted_file - 1;

        if (motif_idx_start <= number_of_motifs - 1) {
            if (motif_idx_end > number_of_motifs - 1) {
                motif_idx_end = number_of_motifs - 1;
            }

            print_splitted_motif_file(meme_header, motifs_array, motif_idx_start, motif_idx_end, motif_input_filename ".part_" split_number ".meme");
        }
    }
}

BEGIN {
    # Split records on each new MOTIF section.
    RS = "\nMOTIF ";

    FS = "NOTHING";

    # Number of splitted files to create.
    number_of_splits = 20;

    # Set error code to 1.
    error_code = 1;
}

{
    if (NR == 1) {
        if (FILENAME == "-") {
            # Print error message in END block as this code is not executed when stdin is empty.
            error_code = 1;
            exit(1);
        }

        error_code = 0;

        motif_input_filename = FILENAME;

        # Get MEME header part of the input file.
        meme_header = $0;

        if (meme_header !~ /^MEME /) {
            print "Input file '" motif_input_filename "' does not look like a MEME motif file." > "/dev/stderr";

            error_code = 2;
            exit(2);
        }
    } else {
        # Store each motif record in an array.
        motifs_array[NR -2] = "MOTIF " $0;
    }
}

END {
    if (error_code != 0) {
        if (error_code == 1) {
            print "ERROR: Input must be given by passing a filename, not by passing input via stdin." > "/dev/stderr";
        }
        exit(error_code);
    }

    # Split MEME motifs over multiple output files.
    split_meme_motif_file(motif_input_filename, meme_header, motifs_array, number_of_splits);
}

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by hulselma...@gmail.com on 16 Jul 2015 at 6:22

Attachments: