Class: Minimap2::Native::FastxReader

Inherits:
Object
  • Object
show all
Defined in:
ext/ruby_minimap2/ruby_minimap2.c

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Object



900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'ext/ruby_minimap2/ruby_minimap2.c', line 900

static VALUE fastx_initialize(VALUE self, VALUE path)
{
    rb_mm2_fastx_t *reader;
    VALUE path_string = rb_get_path(path);
    const char *path_ptr = StringValueCStr(path_string);
    int first;
    TypedData_Get_Struct(self, rb_mm2_fastx_t, &fastx_type, reader);
    if (strcmp(path_ptr, "-") == 0) {
#ifdef _WIN32
        int fd = _dup(_fileno(stdin));
        if (fd >= 0) _setmode(fd, _O_BINARY);
#else
        int fd = dup(fileno(stdin));
#endif
        if (fd >= 0) {
            reader->file = gzdopen(fd, "r");
            if (!reader->file) {
#ifdef _WIN32
                _close(fd);
#else
                close(fd);
#endif
            }
        }
    } else {
        reader->file = gzopen(path_ptr, "r");
    }
    if (!reader->file) rb_raise(eMinimap2Error, "Cannot open FASTA/FASTQ file: %s", StringValueCStr(path_string));
    first = gzgetc(reader->file);
    if (first != '>' && first != '@') {
        gzclose(reader->file);
        reader->file = NULL;
        rb_raise(eMinimap2Error, "Invalid FASTA/FASTQ file: %s", path_ptr);
    }
    if (gzungetc(first, reader->file) == -1) {
        gzclose(reader->file);
        reader->file = NULL;
        rb_raise(eMinimap2Error, "Cannot read FASTA/FASTQ file: %s", path_ptr);
    }
    reader->seq = kseq_init(reader->file);
    if (!reader->seq) {
        gzclose(reader->file);
        reader->file = NULL;
        rb_raise(eMinimap2Error, "Cannot initialize FASTA/FASTQ reader");
    }
    reader->closed = 0;
#ifdef RUBY_MINIMAP2_TESTING
    ++live_fastx_readers;
#endif
    return self;
}

Instance Method Details

#closeObject



981
982
983
984
985
986
987
# File 'ext/ruby_minimap2/ruby_minimap2.c', line 981

static VALUE fastx_close(VALUE self)
{
    rb_mm2_fastx_t *reader;
    TypedData_Get_Struct(self, rb_mm2_fastx_t, &fastx_type, reader);
    fastx_close_native(reader);
    return Qnil;
}

#next_record(read_comment) ⇒ Object



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
# File 'ext/ruby_minimap2/ruby_minimap2.c', line 952

static VALUE fastx_next(VALUE self, VALUE read_comment)
{
    rb_mm2_fastx_t *reader;
    VALUE result;
    int status;
    TypedData_Get_Struct(self, rb_mm2_fastx_t, &fastx_type, reader);
    if (reader->closed) return Qnil;
    status = kseq_read(reader->seq);
    if (status == -1) {
        fastx_close_native(reader);
        return Qnil;
    }
    if (status < -1) {
        fastx_close_native(reader);
        rb_raise(eMinimap2Error, "Malformed FASTA/FASTQ record");
    }
    if (reader->seq->name.l == 0 || reader->seq->seq.l == 0) {
        fastx_close_native(reader);
        rb_raise(eMinimap2Error, "Malformed FASTA/FASTQ record");
    }
    result = rb_ary_new_capa(RTEST(read_comment) ? 4 : 3);
    rb_ary_push(result, rb_str_new(reader->seq->name.s, reader->seq->name.l));
    rb_ary_push(result, rb_str_new(reader->seq->seq.s, reader->seq->seq.l));
    rb_ary_push(result, reader->seq->qual.l > 0 ? rb_str_new(reader->seq->qual.s, reader->seq->qual.l) : Qnil);
    if (RTEST(read_comment))
        rb_ary_push(result, reader->seq->comment.l > 0 ? rb_str_new(reader->seq->comment.s, reader->seq->comment.l) : Qnil);
    return result;
}