tcarver/Artemis — pull request #2 by erasche (2013-08-22 21:41:13+00:00)
Title: Fixes bug opening odd length GBKs with a space at end

This is going to be a bit long, my apologies--it was a very odd bug to track down.

In the edge case of a Genbank file with an odd length sequence and a blank line after the sequence*, preceding the final set of double slashes, the commented out function was called. However, the array "dna" passed to it was empty, triggering an ArrayIndexOutOfBoundsException. I could see no other cases when this if was triggered (in even and odd length sequences, with and without a newline before the end), and so commented it out. This fixes that issue.

As far as I can tell, this comes as a result of

```java
int filledLastUnit = sequence_length & 0x0001;
```

whenever the length of the last line of sequence put onto the array is odd, `filledLastUnit=1`.

**Case 1: No blank line**:
If there is not a blank line at the end, we go straight to `setFromChar`. However, `filledLastUnit` is set to zero, as the length of sequence_length was even before we added the last (odd) number of characters.

**Case 2: Blank Line**:
If there is a blank line, then we set `filledLastUnit` to be 1, as the now-complete sequence length is odd. This only happens in the case of an odd length sequence, AND if there is a blank line at the end that gets parsed (which further only happens in the Genbank Parser). This happens because in `GenbankStreamSequence.java`, it will called `appendChar` on empty lines. When the empty array is encountered, the `filledLastUnit` check becomes positive and this issue occurs.

**Other possible cases**
I've checked the other calls to `appendChar` and found some.
* `EmblStreamSequence.java`, however due to the check of ` !line.startsWith(" ")` (line 159), this case never occurs.
* `RawStreamSequence.java`, however due to `if(line.length() > 0)` (line 186), this case never occurs.

I'd already written this up as a removal of that section of code (since it isn't called), however, it could equivalently be rewritten as a similar check in `GenbankStreamSequence.java` to check that the array isn't empty. Please feel free to accept/reject based on your preference there.

Cheers, Eric

* (as are apparently produced by some BioPerl scripts I use)

**Test Files**
Some example files. You will find that the last file (`odd+space.gbk`) causes artemis to crash.

even-space.gbk
```text
LOCUS 82 bp dna linear UNK
ACCESSION unknown
FEATURES Location/Qualifiers
ORIGIN
1 cttgtagttc tcactgtggg gtaatggggc tattatgccc cgagaatacg ttttgaaata
61 acgtttttct atagcattca tn
//
```

even+space.gbk
```text
LOCUS 82 bp dna linear UNK
ACCESSION unknown
FEATURES Location/Qualifiers
ORIGIN
1 cttgtagttc tcactgtggg gtaatggggc tattatgccc cgagaatacg ttttgaaata
61 acgtttttct atagcattca tn

//
```

odd-space.gbk
```text
LOCUS 83 bp dna linear UNK
ACCESSION unknown
FEATURES Location/Qualifiers
ORIGIN
1 cttgtagttc tcactgtggg gtaatggggc tattatgccc cgagaatacg ttttgaaata
61 acgtttttct atagcattca tnn
//
```

odd+space.gbk
```text
LOCUS 83 bp dna linear UNK
ACCESSION unknown
FEATURES Location/Qualifiers
ORIGIN
1 cttgtagttc tcactgtggg gtaatggggc tattatgccc cgagaatacg ttttgaaata
61 acgtttttct atagcattca tnn

//
```