public static File extractModelFromPDB(File aPdbFile,
			File anOutputDirectory, String aPdbId, int aModelNumber)
			throws IOException {
		if (!aPdbFile.exists()) {
			throw new FileNotFoundException("No PDB file found!.");
		}
		// get the total number of Models
		int tNM = PdbHelper.findNumberOfModels(aPdbFile);
		if ((tNM < 1) || (aModelNumber > tNM)) {
			throw new IndexOutOfBoundsException(
					"The model number provided is outside the permited range :[1-"
							+ tNM + "]\n");
		}
		// check if there is only one model
		if (tNM == 1) {
			return aPdbFile;
		}
		BufferedReader br = new BufferedReader(new FileReader(aPdbFile));
		File returnMe = new File(anOutputDirectory.getAbsolutePath(),
				aPdbId.toLowerCase() + "_M_" + Integer.toString(aModelNumber)
						+ ".ent");
		BufferedWriter out = new BufferedWriter(new FileWriter(returnMe));
		String pattern = "^MODEL\\s+(\\d+)";
		Pattern myPattern = Pattern.compile(pattern);
		String s = "";
		String aLine = "";
		outerLoop: while ((aLine = br.readLine()) != null) {
			// add the header to the output file
			out.write(PdbHelper.getHeaderFromPDBFile(aPdbFile));
			Matcher m = myPattern.matcher(aLine.trim());
			if (m.matches()) {
				if (Integer.parseInt(m.group(1)) == aModelNumber) {
					String pat2 = "^ENDMDL";
					Pattern p2 = Pattern.compile(pat2);
					String aLine2 = "";
					while ((aLine2 = br.readLine()) != null) {
						Matcher m2 = p2.matcher(aLine2.trim());
						if (m2.matches()) {
							break outerLoop;
						} else {
							out.write(aLine2 + "\n");
						}
					}
				}
			}
		}
		out.close();
		br.close();
		return returnMe;
	}