Skip to content
This repository
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor

Showing 3 changed files with 25 additions and 17 deletions. Show diff stats Hide diff stats

  1. +20 5 ext/faster.c
  2. +1 7 lib/bio/faster.rb
  3. +4 5 spec/parser_spec.rb
25 ext/faster.c
@@ -46,6 +46,14 @@ int check_bad_chars(char *invalid_chars, char *string_to_check) {
46 46 return 0;
47 47 }
48 48
  49 +static char* initialize(char *ptr) {
  50 + if(ptr!=NULL){
  51 + free(ptr);
  52 + ptr = NULL;
  53 + }
  54 + return ptr;
  55 +}
  56 +
49 57 int check_header(char *header, char *firstline) {
50 58 if (*header == *firstline)
51 59 return 1;
@@ -56,7 +64,9 @@ int check_header(char *header, char *firstline) {
56 64
57 65 int fastQ_iterator(FastQRecord *seq) {
58 66
59   - // initialization of file stream, line buffer and bad string characters.
  67 + // initialization of structure elements.
  68 + char *header = "@"; // FastQ header
  69 +
60 70 if (!seq->stream)
61 71 seq->stream = fopen(seq->filename,"r");
62 72 if (!seq->line)
@@ -64,24 +74,29 @@ int fastQ_iterator(FastQRecord *seq) {
64 74 if (!seq->bad_chars)
65 75 seq->bad_chars = " \x1F\x7F\t\v\e";
66 76
67   - char *header = "@"; // FastQ header
  77 + // this is done to wipe out data from previous iteration
  78 + seq->id = initialize(seq->id);
  79 + seq->seq = initialize(seq->seq);
  80 + seq->quality = initialize(seq->quality);
  81 +
68 82 for (int i = 0; i < 4; i++)
69 83 {
70 84 if (fgets(seq->line, _BSIZE, seq->stream) == NULL) {
  85 + // if either sequence or quality is missing the record is truncated
71 86 if((seq->seq != NULL && seq->quality == NULL) || (seq->quality != NULL && seq->seq == NULL)) return -2;
72 87 else return 0;
73 88 }
74 89
75 90 if (i==0) {
76   - if (!check_header(header,seq->line)) return -1;
  91 + if (!check_header(header,seq->line)) return -1; // check if the header format is correct
77 92 seq->id = alloc_and_copy(seq->id, seq->line);
78 93 }
79 94 else {
80   - if (check_bad_chars(seq->bad_chars,seq->line)) return -1;
  95 + if (check_bad_chars(seq->bad_chars,seq->line)) return -1; // check if quality or sequence includes bad characters
81 96 if (i==1) seq->seq = alloc_and_copy(seq->seq, seq->line);
82 97 if (i==3) {
83 98 seq->quality = alloc_and_copy(seq->quality, seq->line);
84   - if(strlen(seq->seq) != strlen(seq->quality)) return -2;
  99 + if(strlen(seq->seq) != strlen(seq->quality)) return -2; // if sequence and quality are of different length the record is truncated
85 100 }
86 101
87 102 }
8 lib/bio/faster.rb
@@ -33,13 +33,7 @@ def each_record
33 33 record = FastQRecord.new
34 34 record[:filename] = FFI::MemoryPointer.from_string self.file
35 35 while (result = Bio::Faster.fastQ_iterator(record)) == 1
36   - sequence = [record[:id].read_string,record[:seq].read_string, record[:quality].read_string]
37   - yield sequence
38   - #new_record = FastQRecord.new
39   - #new_record[:stream] = record[:stream]
40   - #new_record[:line] = record[:line]
41   - #new_record[:bad_chars] = record[:bad_chars]
42   - #record = new_record
  36 + yield [record[:id].read_string,record[:seq].read_string, record[:quality].read_string]
43 37 end
44 38 case result
45 39 when -1 then raise RuntimeError, "Bad formatted FastQ file!"
9 spec/parser_spec.rb
@@ -47,13 +47,12 @@
47 47 end
48 48
49 49 it "should raise an error if sequence and quality are truncated or differ in length" do
50   - puts "Trunctaed at Qual"
51   - #expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_at_qual.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
52   - #expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_at_seq.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
  50 + expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_at_qual.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
  51 + expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_at_seq.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
53 52 expect {Bio::Faster.new(File.join(TEST_DATA,"error_qual_null.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
54 53 expect {Bio::Faster.new(File.join(TEST_DATA,"error_long_qual.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
55   - #expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_in_seq.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
56   - #expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_in_qual.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
  54 + expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_in_seq.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
  55 + expect {Bio::Faster.new(File.join(TEST_DATA,"error_trunc_in_qual.fastq")).each_record {|seq|}}.to raise_error(RuntimeError)
57 56
58 57 end
59 58

No commit comments for this range

Something went wrong with that request. Please try again.