private BitSet selectElements(String str) {

		if ("selected".equalsIgnoreCase(str))
			return model.getParticleSelectionSet();

		BitSet bs = genericSelect(str);
		if (bs != null)
			return bs;

		boolean found = false;
		int nop = model.getNumberOfParticles();
		bs = new BitSet(nop);

		Matcher matcher = RANGE.matcher(str);
		if (matcher.find()) {
			found = true;
			String[] s = str.split("-");
			int start = Integer.valueOf(s[0].trim()).intValue();
			int end = Integer.valueOf(s[1].trim()).intValue();
			MolecularModel mm = (MolecularModel) model;
			for (int k = 0; k < nop; k++) {
				int id = mm.getAtom(k).getID();
				if (id >= start && id <= end)
					bs.set(k);
			}
		}

		if (!found) {
			matcher = INTEGER_GROUP.matcher(str);
			if (matcher.find()) {
				found = true;
				MolecularModel mm = (MolecularModel) model;
				String[] s = str.split(REGEX_SEPARATOR + "+");
				int index;
				for (int m = 0; m < s.length; m++) {
					index = Integer.valueOf(s[m]).intValue();
					for (int k = 0; k < nop; k++) {
						if (mm.getAtom(k).getID() == index)
							bs.set(k);
					}
				}
			}
		}

		if (!found) {
			matcher = INDEX.matcher(str);
			if (matcher.find() && str.indexOf("within") == -1) {
				int index = 0;
				found = true;
				try {
					index = Integer.valueOf(str.trim()).intValue();
				} catch (NumberFormatException nfe) {
					nfe.printStackTrace();
					out(ScriptEvent.FAILED, "Element index cannot be parsed as an integer: " + str);
					found = false;
				}
				if (found) {
					MolecularModel mm = (MolecularModel) model;
					for (int k = 0; k < nop; k++) {
						if (mm.getAtom(k).getID() == index)
							bs.set(k);
					}
				}
			}
		}

		if (!found) {
			matcher = RANGE_WITHIN_RECTANGLE.matcher(str);
			if (matcher.find()) {
				found = true;
				matcher = RANGE_LEADING.matcher(str);
				if (matcher.find()) {
					int iw = str.toLowerCase().indexOf("within");
					String[] s = str.substring(0, iw).split("-");
					int start = Integer.valueOf(s[0].trim()).intValue();
					int end = Integer.valueOf(s[1].trim()).intValue();
					str = str.substring(matcher.end()).trim();
					int lp = str.indexOf("(");
					if (lp == -1)
						lp = str.indexOf("[");
					int rp = str.indexOf(")");
					if (rp == -1)
						rp = str.indexOf("]");
					float x = 0, y = 0, w = 0, h = 0;
					try {
						str = str.substring(lp + 1, rp).trim();
						s = str.split(REGEX_SEPARATOR + "+");
						x = Float.valueOf(s[0].trim()).floatValue() * IR_CONVERTER;
						y = Float.valueOf(s[1].trim()).floatValue() * IR_CONVERTER;
						w = Float.valueOf(s[2].trim()).floatValue() * IR_CONVERTER;
						h = Float.valueOf(s[3].trim()).floatValue() * IR_CONVERTER;
					} catch (Exception e) {
						out(ScriptEvent.FAILED, "Script error at: " + str + "\n" + e);
						return null;
					}
					Atom p;
					for (int k = 0; k < nop; k++) {
						p = (Atom) model.getParticle(k);
						if (p.getID() <= end && p.getID() >= start && p.getRx() >= x && p.getRx() <= x + w && p.getRy() >= y && p.getRy() <= y + h)
							bs.set(k);
					}
				}
			}
		}

		if (!found) {
			matcher = INDEX_WITHIN_RECTANGLE.matcher(str);
			if (matcher.find()) {
				found = true;
				matcher = NNI.matcher(str);
				if (matcher.find()) {
					int id = 0;
					try {
						id = Integer.valueOf(str.substring(0, matcher.end()).trim()).intValue();
					} catch (NumberFormatException e) {
						out(ScriptEvent.FAILED, "Element type must be an integer: " + str);
						return null;
					}
					str = str.substring(matcher.end()).trim();
					int lp = str.indexOf("(");
					if (lp == -1)
						lp = str.indexOf("[");
					int rp = str.indexOf(")");
					if (rp == -1)
						rp = str.indexOf("]");
					float x = 0, y = 0, w = 0, h = 0;
					try {
						str = str.substring(lp + 1, rp).trim();
						String[] s = str.split(REGEX_SEPARATOR + "+");
						x = Float.valueOf(s[0].trim()).floatValue() * IR_CONVERTER;
						y = Float.valueOf(s[1].trim()).floatValue() * IR_CONVERTER;
						w = Float.valueOf(s[2].trim()).floatValue() * IR_CONVERTER;
						h = Float.valueOf(s[3].trim()).floatValue() * IR_CONVERTER;
					} catch (Exception e) {
						out(ScriptEvent.FAILED, "Script error at: " + str + "\n" + e);
						return null;
					}
					Atom p;
					for (int k = 0; k < nop; k++) {
						p = (Atom) model.getParticle(k);
						if (p.getID() == id && p.getRx() >= x && p.getRx() <= x + w && p.getRy() >= y && p.getRy() <= y + h)
							bs.set(k);
					}
				}
			}
		}

		if (!found) {
			matcher = WITHIN_RADIUS.matcher(str);
			if (matcher.find()) {
				found = true;
				String s = str.substring(matcher.end()).trim();
				s = s.substring(0, s.indexOf(")"));
				if (RANGE.matcher(s).find()) {
					int lp = str.indexOf("(");
					int rp = str.indexOf(")");
					s = str.substring(lp + 1, rp).trim();
					float r = Float.valueOf(s.substring(0, s.indexOf(",")).trim()).floatValue() * IR_CONVERTER;
					r *= r;
					s = s.substring(s.indexOf(",") + 1);
					int i0 = Integer.valueOf(s.substring(0, s.indexOf("-")).trim()).intValue();
					int i1 = Integer.valueOf(s.substring(s.indexOf("-") + 1).trim()).intValue();
					if (i1 >= i0) {
						MolecularModel mm = (MolecularModel) model;
						Atom at;
						for (int k = 0; k < nop; k++) {
							at = mm.getAtom(k);
							if (at.getID() >= i0 && at.getID() <= i1) {
								bs.set(k);
								for (int m = 0; m < nop; m++) {
									if (m == k || bs.get(m))
										continue;
									if (mm.getAtom(m).distanceSquare(at) < r)
										bs.set(m);
								}
							}
						}
					}
				} else {
					int lp = str.indexOf("(");
					int rp = str.indexOf(")");
					s = str.substring(lp + 1, rp).trim();
					float r = Float.valueOf(s.substring(0, s.indexOf(",")).trim()).floatValue() * IR_CONVERTER;
					r *= r;
					int center = 0;
					try {
						center = Integer.valueOf(s.substring(s.indexOf(",") + 1).trim()).intValue();
					} catch (NumberFormatException nfe) {
						out(ScriptEvent.FAILED, str + " is not an integer number.");
						return null;
					}
					Atom at;
					MolecularModel mm = (MolecularModel) model;
					for (int m = 0; m < nop; m++) {
						at = mm.getAtom(m);
						if (at.getID() == center) {
							bs.set(m);
							for (int k = 0; k < nop; k++) {
								if (k == m || bs.get(k))
									continue;
								if (mm.getAtom(k).distanceSquare(at) < r)
									bs.set(k);
							}
						}
					}
				}
			}
		}

		if (found) {
			model.setParticleSelectionSet(bs);
		} else {
			out(ScriptEvent.FAILED, "Unrecognized expression: " + str);
		}

		return found ? bs : null;

	}