/** * Changes the sequence itself. BaseSequence doesn't care what sequence you try feeding in here. */ public void changeSequence(String seq) throws SequenceException { StringBuffer buff = new StringBuffer(); // important step: is this sequence actually 'valid' (are all the brackets closed?) // // Also: convert round brackets into 'system-standard' (i.e. TaxonDNA specific) square brackets. // int count = 0; int length = 0; for (int x = 0; x < seq.length(); x++) { char ch = seq.charAt(x); // convert round brackets to square if (ch == '(') ch = '['; else if (ch == ')') ch = ']'; // count the brackets! if (ch == '[') count++; if (ch == ']') count--; // this way, we delete this *once* if (count == 0) length++; buff.append(ch); } if (count != 0) throw new SequenceException( getFullName(), "This sequence cannot be changed to the specified sequence, as the brackets are not properly closed. Please ensure that all brackets are closed, and try again."); seq = buff.toString(); // hey, if we _can_ ... synchronized (this) { this.id = new UUID(); this.seq = seq.toCharArray(); this.len = length; } }
/** * Returns a subsequence of this sequence. If the subsequence is completely 'valid', we might * return a Sequence! Otherwise, we return a BaseSequence. */ public Sequence getSubsequence(int from, int to) throws SequenceException { // make sure we're not being fed garbage if (from < 1 || // the first char is index = 1 to > getLength() // the 'to' field must not be greater than the length of the sequence ) throw new SequenceException( this.getFullName(), "There is no subsequence at (" + from + ", " + to + ") in sequence " + this); if (to < from) { throw new SequenceException( this.getFullName(), "I cannot generate the reverse-complement of this sequence, since I do not understand what sort of data it is."); } // so, now we have a 'from' and a 'to'. // Let's get cracking! java.io.StringReader r = new java.io.StringReader(getSequence()); StringBuffer output = new StringBuffer(); // System.err.println("Base.cut: from: " + from + " to: " + to); boolean writing = false; int char_at = 0; try { while (r.ready()) { int x = r.read(); if (x == -1) throw new java.io.EOFException(); if (char_at == from - 1) // off by one, here writing = true; if (char_at == to) // off by one, here too, but for a slightly different reason writing = false; if (x == '(' || x == '[') { int initial_char = x; int final_char = ' '; if (x == '(') final_char = ')'; else final_char = ']'; // convert brackets to the Real Thing if (writing) output.append('['); while (x != ']') { x = r.read(); if (x == final_char) x = ']'; if (x == -1) throw new java.io.EOFException(); if (writing) output.append((char) x); } } else { if (writing) output.append((char) x); } char_at++; } } catch (java.io.EOFException e) { // okay, done. // did you know that } catch (java.io.IOException e) { throw new RuntimeException("Why is reading from a string causing an IOException?! " + e); } String seq_str = output.toString(); // System.err.println("What've we got: " + seq_str); Sequence seq = BaseSequence.createSequence( getFullName() + " (segment:" + from + "-" + to + ":inclusive)", seq_str); // System.err.println("Ended up with: " + seq); return seq; }