/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.tsl.etherington.piculus.fasta;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import mathsutils.RandomUtils;
import org.biojava.bio.symbol.Alphabet;
import org.biojava.bio.symbol.AlphabetManager;
import org.biojava3.core.sequence.DNASequence;
import org.biojava3.core.sequence.compound.AmbiguityDNACompoundSet;
import org.biojava3.core.sequence.compound.NucleotideCompound;
import org.biojava3.core.sequence.io.DNASequenceCreator;
import org.biojava3.core.sequence.io.FastaReader;
import org.biojava3.core.sequence.io.GenericFastaHeaderParser;
import org.biojava3.data.sequence.FastaSequence;
import org.biojava3.data.sequence.SequenceUtil;
import org.biojavax.SimpleNamespace;
import org.biojavax.bio.seq.RichSequence;
import org.biojavax.bio.seq.RichSequenceIterator;

/**
 *
 * @author ethering
 */
public class RandomFasta
{
    /**
     * 
     * @param fasta
     * @param numberOfSeqsRequired
     * @param outfile
     * @throws Exception 
     */
    public void selectRandomSequences(File fasta, int numberOfSeqsRequired, File outfile) throws Exception
    {
        //read in the fasta sequences to a List
        List<FastaSequence> seqs = SequenceUtil.readFasta(new FileInputStream(fasta));
        //get the random ints
        ArrayList<Integer> randomInts = RandomUtils.getRandomArray(numberOfSeqsRequired, seqs.size());
        //a holder
        ArrayList<FastaSequence> dnaSeqs = new ArrayList<>();

        for (Integer i : randomInts)
        {
            dnaSeqs.add(seqs.get(i));
        }
        FileOutputStream fop = new FileOutputStream(outfile);
        SequenceUtil.writeFasta(fop, dnaSeqs);
    }
        
   }

@@ -158,378 +158,105 @@
 .io.
-BufferedInputStream;%0Aimport java.io.BufferedReader;%0Aimport java.io.BufferedWriter;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.FileReader;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.LineNumberReader;%0Aimport java.util.ArrayList;%0Aimport java.util.LinkedHashMap
+File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.util.ArrayList
 ;%0Aim
@@ -280,30 +280,8 @@
 st;%0A
-import java.util.Map;%0A
 impo
@@ -329,649 +329,85 @@
 java
-.bio.symbol.Alphabet;%0Aimport org.biojava.bio.symbol.AlphabetManager;%0Aimport org.biojava3.core.sequence.DNASequence;%0Aimport org.biojava3.core.sequence.compound.AmbiguityDNACompoundSet;%0Aimport org.biojava3.core.sequence.compound.NucleotideCompound;%0Aimport org.biojava3.core.sequence.io.DNASequenceCreator;%0Aimport org.biojava3.core.sequence.io.FastaReader;%0Aimport org.biojava3.core.sequence.io.GenericFastaHeaderParser;%0Aimport org.biojava3.data.sequence.FastaSequence;%0Aimport org.biojava3.data.sequence.SequenceUtil;%0Aimport org.biojavax.SimpleNamespace;%0Aimport org.biojavax.bio.seq.RichSequence;%0Aimport org.biojavax.bio.seq.RichSequenceIterator
+3.data.sequence.FastaSequence;%0Aimport org.biojava3.data.sequence.SequenceUtil
 ;%0A%0A/
@@ -498,16 +498,39 @@
 am fasta
+ the multi-fasta infile
 %0A     * 
@@ -556,16 +556,56 @@
 Required
+ the number of random sequences required
 %0A     * 
@@ -897,24 +897,275 @@
 am(fasta));%0A
+        if (seqs.size() %3C numberOfSeqsRequired)%0A        %7B%0A            System.err.println(%22The number of random sequences requested is greater than that available.%5CnThere are %22+seqs.size()+%22 sequences available%22);%0A            System.exit(1);%0A        %7D%0A
         //ge
@@ -1571,16 +1571,37 @@
 aSeqs);%0A
+        fop.close();%0A
     %7D%0A  

Handle situation where number of seqs requested is greater than that available