/**
   * Builds an {@link AFPChain} from already-matched arrays of atoms and residues.
   *
   * @param ca1 An array of atoms in the first structure
   * @param ca2 An array of atoms in the second structure
   * @param residues1 An array of {@link ResidueNumber ResidueNumbers} in the first structure that
   *     are aligned. Only null ResidueNumbers are considered to be unaligned
   * @param residues2 An array of {@link ResidueNumber ResidueNumbers} in the second structure that
   *     are aligned. Only null ResidueNumbers are considered to be unaligned
   * @throws StructureException
   */
  private static AFPChain buildAlignment(
      Atom[] ca1, Atom[] ca2, ResidueNumber[] residues1, ResidueNumber[] residues2)
      throws StructureException {

    // remove any gap
    // this includes the ones introduced by the nullifying above
    List<ResidueNumber> alignedResiduesList1 = new ArrayList<ResidueNumber>();
    List<ResidueNumber> alignedResiduesList2 = new ArrayList<ResidueNumber>();
    for (int i = 0; i < residues1.length; i++) {
      if (residues1[i] != null && residues2[i] != null) {
        alignedResiduesList1.add(residues1[i]);
        alignedResiduesList2.add(residues2[i]);
      }
    }

    ResidueNumber[] alignedResidues1 =
        alignedResiduesList1.toArray(new ResidueNumber[alignedResiduesList1.size()]);
    ResidueNumber[] alignedResidues2 =
        alignedResiduesList2.toArray(new ResidueNumber[alignedResiduesList2.size()]);

    AFPChain afpChain = AlignmentTools.createAFPChain(ca1, ca2, alignedResidues1, alignedResidues2);
    afpChain.setAlgorithmName("unknown");

    AlignmentTools.updateSuperposition(afpChain, ca1, ca2);

    afpChain.setBlockSize(new int[] {afpChain.getNrEQR()});
    afpChain.setBlockRmsd(new double[] {afpChain.getTotalRmsdOpt()});
    afpChain.setBlockGap(new int[] {afpChain.getGapLen()});

    return afpChain;
  }
  public void testOldSecOutput() throws Exception {

    String fileName = "/ce_1fdo.A_2iv2.X.out";
    InputStream inStream = this.getClass().getResourceAsStream(fileName);
    assertNotNull("Could not find file " + fileName + " in resource path. Config error?", inStream);
    String xml = StringManipulationHelper.convertStreamToString(inStream);

    AtomCache cache = new AtomCache();
    String name1 = "1FDO.A";
    String name2 = "2IV2.X";
    Atom[] ca1 = cache.getAtoms(name1);
    Atom[] ca2 = cache.getAtoms(name2);

    assertEquals(715, ca1.length);
    assertEquals(697, ca2.length);

    AFPChain afpChainOrig = AFPChainXMLParser.fromXML(xml, ca1, ca2);

    assertNotNull("Could not get AfpChain object from flat file!", afpChainOrig);

    assertEquals(
        "Could not find alignment string for prot 1",
        "MKKVVTVCPYCASGCKINLVVDNGKIVRAEAAQGKTNQGTLCLKGYYGWDFINDTQILTPRLKTPMIRRQRGGKLEPVSWDEALNYVAERLSAIKEKYGPDAIQTTGSSRGTGNETNYVMQKFARAVIGTNNVDCCARVUHGPSVA-----GLHQSVGNGAMSNAINEIDNTDLVFVFGYNPADSHPIVANHVINAKRNGAKIIVCDPRKIETARIADMHIALKNGSNIALLNAMGHVIIEENLYDKAFVASRTEGFEEYRKIVEGYTPESVEDITGVSASEIRQAARMYAQAKSAAILWGMGVTQFYQGVETVRSLTSLAMLTGNLGKPHAGVNPVRGQNNVQGACDMGALPDTYPGYQYVKDPANREKFAKAWGVESLPAHTGYRISELPHRAAHGEVRAAYIMGEDPLQTDAELSAVRKAFEDLELVIVQDIFMTKTASAADVILPSTSWGEHEGVFTAADRGFQRFFKAVEPKWDLKTDWQIISEIATRMGYPMHYNNTQEIWDELRHLCPDFYGATYEKMGELGFIQWPCRDTSDADQGTSYLFKEKFDTPNGLAQFFTCDWVAPIDKLTDEYPMVLSTVREVGHYSCRSMTGNCAALAALADEPGYAQINTEDAKRLGIEDEALVWVHSRKGKIITRAQVSDRPNKGAIYMTYQWWIGACNELVTENLSPITKTPEYKYCAVRVEPIADQRAAEQYVIDEYNKLKTRLREAALA",
        new String(afpChainOrig.getAlnseq1(), 0, afpChainOrig.getAlnLength()));
    assertEquals(
        "Could not find alignment string for prot 2",
        "MKKVVTVCPYCASGCKINLVVDNGKIVRAEAAQGKTNQGTLCLKGYYGWDFINDTQILTPRLKTPMIRRQRGGKLEPVSWDEALNYVAERLSAIKEKYGPDAIQTTGSSRGTGNETNYVMQKFARAVIGTNNVDCCAR-----VUHGPSVAGLHQSVGNGAMSNAINEIDNTDLVFVFGYNPADSHPIVANHVINAKRNGAKIIVCDPRKIETARIADMHIALKNGSNIALLNAMGHVIIEENLYDKAFVASRTEGFEEYRKIVEGYTPESVEDITGVSASEIRQAARMYAQAKSAAILWGMGVTQFYQGVETVRSLTSLAMLTGNLGKPHAGVNPVRGQNNVQGACDMGALPDTYPGYQYVKDPANREKFAKAWGVESLPAHTGYRISELPHRAAHGEVRAAYIMGEDPLQTDAELSAVRKAFEDLELVIVQDIFMTKTASAADVILPSTSWGEHEGVFTAADRGFQRFFKAVEPKWDLKTDWQIISEIATRMGYPMHYNNTQEIWDELRHLCPDFYGATYEKMGELGFIQWPCRDTSDADQGTSYLFKEKFDTPNGLAQFFTCDWVAPIDKLTDEYPMVLSTVREVGHYSCRSMTGNCAALAALADEPGYAQINTEDAKRLGIEDEALVWVHSRKGKIITRAQVSDRPNKGAIYMTYQWW------------------PEYKYCAVRVEPIADQRAAEQYVIDEYNKLKTRLREAALA",
        new String(afpChainOrig.getAlnseq2(), 0, afpChainOrig.getAlnLength()));

    // calc time is hardware dependent.... overwrite...
    afpChainOrig.setCalculationTime(-1);

    assertEquals(
        "alnLength is wrong! (" + afpChainOrig.getAfpChainLen() + ")",
        720,
        afpChainOrig.getAlnLength());
    assertEquals(
        "gapLength is wrong! (" + afpChainOrig.getGapLen() + ")", 28, afpChainOrig.getGapLen());

    // identity should be 0.9569
    assertTrue(
        "alinment ID is < 0.95 ! (" + afpChainOrig.getIdentity() + ")",
        afpChainOrig.getIdentity() > 0.95);
    assertTrue(
        "alignment ID is > 0.96 ! (" + afpChainOrig.getIdentity() + ")",
        afpChainOrig.getIdentity() < 0.96);

    String xmlComp = AFPChainXMLConverter.toXML(afpChainOrig, ca1, ca2);

    FlipAFPChainTest t = new FlipAFPChainTest();
    t.printFirstMismatch(xml, xmlComp);
    StringManipulationTestsHelper.assertEqualsIgnoreEndline(xml, xmlComp);
    StructureAlignment ce = StructureAlignmentFactory.getAlgorithm(CeMain.algorithmName);

    AFPChain afpChainNew = ce.align(ca1, ca2);
    afpChainNew.setCalculationTime(-1);
    afpChainNew.setName1(name1);
    afpChainNew.setName2(name2);

    String xmlNew = AFPChainXMLConverter.toXML(afpChainNew, ca1, ca2);

    StringManipulationTestsHelper.assertEqualsIgnoreEndline(xml, xmlNew);
  }