TIntArrayList stack = new TIntArrayList();
ArrayList<TIntArrayList> nodes = new ArrayList<TIntArrayList>();
ArrayList<RNANodeLabel> tmpLabelsList = new ArrayList<RNANodeLabel>();
nodes.add(new TIntArrayList());
tmpLabelsList.add(new RNANodeLabel(RNANodeLabel.ROOT,
new TCharArrayList("ROOT".toCharArray()), -1, -1, -1));
nodes.add(new TIntArrayList());
stack.add(1);
tmpLabelsList.add(new RNANodeLabel(RNANodeLabel.EXTERNAL,
new TCharArrayList("EXT".toCharArray()), -1, -1, -1));
// connect root to external
connectNodes(nodes, 0, 1);
TIntArrayList tmpStemSizes = new TIntArrayList();
TIntArrayList tmpIntervalSizes = new TIntArrayList();
tmpStemSizes.add(0);
int group = 0;
while (seqIx < seqEnd) {
if (structure[seqIx] == CLOSE_BRACKET) {
while (tmpLabelsList.get(peek(stack)).getType() != RNANodeLabel.BASE_PAIR) {
remove(stack);
}
remove(stack);
seqIx++;
} else if (structure[seqIx] == OPEN_BRACKET) {
nodes.add(new TIntArrayList());
// check if parent is ML or EXT then change the group id
if (tmpLabelsList.get(peek(stack)).getType() != RNANodeLabel.BASE_PAIR
&& tmpLabelsList.get(peek(stack)).getType() != RNANodeLabel.IL_LOOP) {
group++;
}
tmpLabelsList.add(new RNANodeLabel(RNANodeLabel.BASE_PAIR,
new TCharArrayList(new char[] { sequence[seqIx],
sequence[pairs[seqIx]] }), seqIx + 1,
pairs[seqIx] + 1, group));
tmpStemSizes.set(tmpStemSizes.size() - 1,
tmpStemSizes.get(tmpStemSizes.size() - 1) + 1);
// connect to head
connectNodes(nodes, nodeIx, peek(stack));
stack.add(nodeIx);
nodeIx++;
if (pairs[seqIx + 1] != pairs[seqIx] - 1) {
// not part of stack
nodes.add(new TIntArrayList());
int type = RNANodeLabel.HP_LOOP;
String typeName = "HP";
if (branches[seqIx] == 1) {
type = RNANodeLabel.IL_LOOP;
typeName = "IL";
} else if (branches[seqIx] > 1) {
type = RNANodeLabel.ML_LOOP;
typeName = "ML";
group++;
}
tmpStemSizes.add(0);
numOfStems++;
tmpLabelsList.add(new RNANodeLabel(type,
new TCharArrayList(typeName.toCharArray()), -1, -1,
group));
// connect to head
connectNodes(nodes, nodeIx, peek(stack));
stack.add(nodeIx);
nodeIx++;
}
seqIx++;
} else {
// base interval
int start = seqIx;
while (seqIx < sequence.length
&& (structure[seqIx] != OPEN_BRACKET && structure[seqIx] != CLOSE_BRACKET)) {
seqIx++;
}
// create an interval node
nodes.add(new TIntArrayList());
char[] label = new char[seqIx - start];
System.arraycopy(sequence, start, label, 0, label.length);
tmpLabelsList.add(new RNANodeLabel(RNANodeLabel.BASE_INTERVAL,
new TCharArrayList(label), start + 1, seqIx, group));
tmpIntervalSizes.add(label.length);
// connect to head
connectNodes(nodes, nodeIx, peek(stack));
nodeIx++;
}