package japsadev.tools;
import java.io.IOException;

import org.graphstream.graph.Edge;
import org.graphstream.graph.Node;
import org.graphstream.ui.view.Viewer;

import japsa.util.CommandLine;
import japsa.util.deploy.Deployable;
import japsadev.bio.hts.newscarf.BidirectedGraph;
import japsadev.bio.hts.newscarf.HybridAssembler;

@Deployable(
		scriptName = "jsa.dev.newScarf", 
		scriptDesc = "New npscarf"
		)
public class NewScarfCmd extends CommandLine{
	public NewScarfCmd(){
		super();
		Deployable annotation = getClass().getAnnotation(Deployable.class);		
		setUsage(annotation.scriptName() + " [options]");
		setDesc(annotation.scriptDesc()); 

		addString("fastg", null, "Assembly graph fastg file",true);		
		addString("sam", null, "Sam file alignment of assembly graph to long reads",true);

		addStdHelp();
	}
		    	
	public static void main(String[] args) throws IOException{
		CommandLine cmdLine = new NewScarfCmd ();
		args = cmdLine.stdParseLine(args);

		String fastgFile = cmdLine.getStringVal("fastg");
		String samFile = cmdLine.getStringVal("sam");
		String styleSheet =
			        "node {" +
			        "	fill-color: black; z-index: 0;" +
			        "}" +
			        "edge {" +
			        "	text-alignment: along;" +
			        "}" +
			        "node.marked {" +
			        "	fill-color: red;" +
			        "}" +
			        "edge.marked {" +
			        "	fill-color: red;" +
			        "}";
		HybridAssembler hbAss = new HybridAssembler(fastgFile);
		//For SAM file, run bwa first on the edited assembly_graph.fastg by running:
		//awk -F '[:;]' -v q=\' 'BEGIN{flag=0;}/^>/{if(index($1,q)!=0) flag=0; else flag=1;}{if(flag==1) print $1;}' ../EcK12S-careful/assembly_graph.fastg > Eck12-careful.fasta
		//TODO: need to make this easier
		BidirectedGraph graph= hbAss.simGraph;
		
        //graph.addAttribute("ui.quality");
        //graph.addAttribute("ui.antialias");
        graph.addAttribute("ui.stylesheet", styleSheet);
        Viewer viewer = graph.display();
        // Let the layout work ...
        
        System.out.println("Node: " + graph.getNodeCount() + " Edge: " + graph.getEdgeCount());

        
        for (Node node : graph) {
            node.addAttribute("ui.label", node.getId());
            node.setAttribute("ui.style", "text-offset: -10;"); 
            node.addAttribute("layout.weight", 100); 

            if(BidirectedGraph.isUnique(node))
            	node.setAttribute("ui.class", "marked");
        }


        try {
        	hbAss.reduceFromSPAdesPaths(fastgFile);
        	hbAss.assembly(samFile, 30);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        

        for (Edge edge: graph.getEdgeSet()){
        	if(edge.hasAttribute("isReducedEdge"))
        		edge.addAttribute("layout.weight", 10); 
        }
        
        
        HybridAssembler.promptEnterKey();
        viewer.disableAutoLayout();
        System.out.println("Node: " + graph.getNodeCount() + " Edge: " + graph.getEdgeCount());
	}
}

@@ -798,16 +798,70 @@
 ,true);%0A
+%09%09addString(%22path%22, null, %22SPAdes contigs path file%22);
 %0A%09%09addSt
@@ -1118,24 +1118,77 @@
 Val(%22sam%22);%0A
+%09%09String pathFile = cmdLine.getStringVal(%22path%22);%0A%09%09%0A
 %09%09String sty
@@ -2587,24 +2587,53 @@
  %7B%0A        %09
+if(pathFile!=null)%0A        %09%09
 hbAss.reduce
@@ -2648,21 +2648,20 @@
 esPaths(
-fastg
+path
 File);%0A 

Add contigs.path to option