public static void writeGenotypesFile(String fName, boolean founders, PopulationData popdata)
            throws IOException, Exception {
        System.out.println("write file "+fName);
        //File outFile = new File(fName);
        //PrintWriter out = new PrintWriter(new FileWriter(outFile));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fName)));
        // write header line:
        out.print("marker");
        for (int i=0; i<popdata.indivCount(); i++) {
            for (int h=0; h<popdata.ploidy; h++) {
                out.print("\t"+ popdata.getIndiv(i).getIndivName()+"_"+(h+1));
            }
        }
        out.println();
        for (int c=0; c<popdata.chromCount(); c++) {
            Chromosome chrom = popdata.getChrom(c);
            System.out.println("chrom "+chrom.getChromName());
            ArrayList<Locus> loci = chrom.getLocus();
            for (int loc=0; loc<loci.size(); loc++) {
                //instead of building a line as a string in memeory and printing
                //it at once it is much faster to print every element separately:
                out.print(loci.get(loc).getLocusName());
                for (int i=0; i<popdata.indivCount(); i++) {
                    if (founders) {
                        for (int h=0; h<popdata.ploidy; h++) {
                            out.print("\t"+ popdata.getIndiv(i).getHaploStruct(c,h).
                                    getFounderAt(loci.get(loc).getPosition()));
                        }
                    } else { //not founders but locus alleles
                        String[] indall = popdata.getIndiv(i).getLocusAllele(c, loc);
                        for (int h=0; h<popdata.ploidy; h++) {
                            out.print("\t"+ indall[h]);
                        }
                    }
                }
                out.println();
            }
        }
        out.flush();
    } //writeGenotypesFile