private HaploStruct fillGamete(HaploStruct pattern) throws Exception {
        HaploStruct gamete;
        if (pattern.segmentCount()==1) {
            gamete = (HaploStruct) haplostruct[pattern.getFounder().get(0)].clone();
        }
        else {
            gamete = new HaploStruct(getChrom(),pattern.getFounder().get(0));
            gamete.getFounder().clear();
            gamete.getRecombPos().clear();
            int patseg = 0; //the pattern segment currently being translated
            while (patseg<pattern.segmentCount()) {
                /* get the list of partial and complete segments
                 * of the haplostruct corresponding to this pattern segment;
                 * the start of the first (partial) segment is replaced
                 * by the start of the pattern segment
                 * and all the haplostruct segments are added to gamete
                 */
                double patstart = pattern.getRecombPos().get(patseg);
                double patend = patseg < pattern.segmentCount()-1 ?
                    pattern.getRecombPos().get(patseg+1) :
                    getChrom().getTailPos();
                int pf = pattern.getFounder().get(patseg); //always in 0..3 for Quadrivalent, 0..1 for Bivalent (?)
                HaploStruct hs = haplostruct[pf];
                //find the hsseg in which patstart falls:
                int hsseg = hs.findSegment(patstart);
                gamete.getRecombPos().add(patstart);
                gamete.getFounder().add(hs.getFounder().get(hsseg));
                hsseg++;
                while (hsseg<hs.segmentCount() &&
                        hs.getRecombPos().get(hsseg)<patend) {
                    gamete.getRecombPos().add(hs.getRecombPos().get(hsseg));
                    gamete.getFounder().add(hs.getFounder().get(hsseg));
                    hsseg++;
                }
                patseg++;
            }
        }
        return gamete;
    } //fillGamete