package abra;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Reference {

	private Map<String, StringBuffer> refMap = new HashMap<String, StringBuffer>();
	
	public Reference(String reference) throws IOException {
		
		BufferedReader refReader = new BufferedReader(new FileReader(reference));
		String line = refReader.readLine();
		
		String key = null;
		StringBuffer seq = null;
		
		while (line != null) {
			
			if (line.startsWith(">")) {
				if (key != null) {
					refMap.put(key, seq);
				}
				key = line.substring(1, line.length());
				seq = new StringBuffer();
			} else {
				seq.append(line);
			}
			
			line = refReader.readLine();
		}
		
		if (key != null) {
			refMap.put(key, seq);
		}
		
		refReader.close();
	}
	
	public String getSequence(String chromosome, int position, int length) {
		StringBuffer ref = refMap.get(chromosome);
		
		if (ref == null) {
			System.out.println("No ref for chromosome: " + chromosome);
		}
		
		position -= 1;
		
		return ref.substring(position, Math.min(position + length, ref.length()));
	}
}

@@ -596,16 +596,114 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%09%0A%09%09%09%09int endIdx = line.indexOf(' ');%0A%09%09%09%09if (endIdx %3C 0) %7B%0A%09%09%09%09%09endIdx = line.length();%0A%09%09%09%09%7D%0A
 %09%09%09%09key 
@@ -722,29 +722,22 @@
 ring(1, 
-line.length()
+endIdx
 );%0A%09%09%09%09s
@@ -1178,16 +1178,25 @@
 bstring(
+Math.max(
 position
@@ -1196,16 +1196,20 @@
 osition,
+ 0),
  Math.mi

Handle reference with description. Don't sub index past beginning of reference sequence.