/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package motifkmer;

/**
 *
 * @author Rahul Semwal
 */
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Scanner;
import java.util.regex.*; 
import java.util.stream.Stream;
public class FileIo 
{

    /**
     *
     * @param Filename
     * @return
     * @throws Exception
     */
    public String totalSequence(String Filename,int KmerLength,double frequency[]) throws Exception
    {
        File file = new File(Filename);
        int count = 0;
        int maxKmer=0;
        int totalChar=0;
        List<List<String>> homo=new ArrayList<>();
        try (Scanner sc = new Scanner(file)) 
        {
            
            Pattern p = Pattern.compile("^>.*");
            Pattern sn = Pattern.compile("([\\s*\\n\\r]*)");
            String line;
            String content="";
            count = 0;
            String kmer;
            
            File ofile =new File("/home/rahul/NetBeansProjects/MotifKmer/Files/OutputKmer.txt");
            if (ofile.exists() && ofile.isFile())
            {
                ofile.delete();
            }   ofile.createNewFile();
            FileWriter fileWriter = new FileWriter(ofile,true);
            try (BufferedWriter bw = new BufferedWriter(fileWriter)) 
            {
                while (sc.hasNextLine())
                {
                    line = sc.nextLine();
                    Matcher m = p.matcher(line);
                    if(m.matches())
                    {
                        count++;
                        if(content.length()>0)
                        {
                            maxKmer += content.length()-KmerLength+1;
                            totalChar +=content.length();
                            for(int i=0;i<content.length();i++)
                                frequency[(int)content.charAt(i)-65]+=1;
                            content+="\r";
                            bw.write(content);
                            content="";
                        }
                    }
                    else
                    {
                        m=sn.matcher(line);
                        if(!m.matches())
                        {
                            line =line.replace("\n", "");
                            content +=line;
                        }
                    }
                    
                }   
                bw.write(content);
                bw.close();
                ofile=null;
                fileWriter=null;
            }catch (Exception e){}
        }catch (Exception e){}
        file=null;
        System.out.println("TotalChar ="+totalChar);
        for(int i=0;i<26;i++)
            frequency[i]=frequency[i]/totalChar;
         homo.forEach(System.out::println);
         return(count+","+maxKmer);
    }
    
    public BinList parseFile(int kmerLength,int totalSequence,FileIo io,double frequency[],int maxKmer) throws Exception
    {
        File file = new File("/home/rahul/NetBeansProjects/MotifKmer/Files/OutputKmer.txt");
        String currentSequence;
        Scanner sc = new Scanner(file);
        Utility u = new Utility();
        Score s =new Score();
        LinkedHashMap<String,KMER> c;
        int SequenceCount=0;
        int counter=0;
        BinList Bin=new BinList();
        while (sc.hasNextLine())
        {
           currentSequence=sc.nextLine();
           
           c=u.getKmer(currentSequence,kmerLength,totalSequence,s,frequency,maxKmer,Bin.getHead(),SequenceCount);
           
           if(!c.isEmpty())
           {
              Bin.addNode(c, SequenceCount);
              counter++;
           }
           SequenceCount++;
        }
        s=null;
        u=null;
        file=null;
        s=null;
        return(Bin);  
    }
    
    public String getSequence(int n) throws IOException
    {
        String line;
        try(Stream<String> lines =Files.lines(Paths.get("/home/rahul/NetBeansProjects/MotifKmer/Files/OutputKmer.txt")))
        {
            line = lines.skip(n).findFirst().get();
        }
        return(line);
    }
}
