package com.astrazeneca.seq2c;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.cli.*;

public class Seq2c {

    public static void main(String[] args) throws Exception {

        CommandLine cmd = new BasicParser().parse(buildCmdOptions(), args);

        String[] cmdArgs = cmd.getArgs();
        String sam2bamFile = cmdArgs[0];
        final String bedFile = cmdArgs[1];
        String control = "";

        if (cmdArgs.length > 2) {
            control = cmdArgs[2];
        }

        Dispatcher.init(getThreadsCount(cmd));
        try {

            final Map<String, String> sam2bam = Bam2Reads.parseFile(sam2bamFile);

            final Collection<Gene> sqrlist = new ArrayList<>();
            for (final Map.Entry<String, String> entry : sam2bam.entrySet()) {
                Seq2cov sec2cov = new Seq2cov(bedFile, entry.getValue(), entry.getKey());
                Collection<Gene> cov = sec2cov.process();
                sqrlist.addAll(cov);
            }

            Map<String, Long> stat = Bam2Reads.printStatsToFile(sam2bamFile);

            Cov2lr cov2lr = new Cov2lr(true, stat, sqrlist, control);
            List<Sample> cov = cov2lr.doWork();

            Lr2gene lr2gene = new Lr2gene(cov);
            lr2gene.init(cmd);
            lr2gene.setUseControl(cov2lr.isUseControlSamples());
            lr2gene.process();

        } finally {
            Dispatcher.shutdown();
        }
    }

    private static Options buildCmdOptions() {
        Options options = Lr2gene.getOptions();
        options.addOption(OptionBuilder.withArgName("number of threads")
                .hasOptionalArg()
                .withType(Number.class)
                .isRequired(false)
                .create("i"));
        return options;
    }

    private static int getThreadsCount(CommandLine cmd) throws ParseException {
        int threads = 0;
        if (cmd.hasOption("i")) {
            Object value = cmd.getParsedOptionValue("i");
            if (value == null) {
                threads = Runtime.getRuntime().availableProcessors();
            } else {
                threads = ((Number)value).intValue();
            }
        }
        return threads;

    }

}

@@ -25,16 +25,105 @@
 seq2c;%0A%0A
+import org.apache.commons.cli.*;%0A%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0A
 import j
@@ -138,24 +138,24 @@
 .ArrayList;%0A
-
 import java.
@@ -221,42 +221,8 @@
 p;%0A%0A
-import org.apache.commons.cli.*;%0A%0A
 publ
@@ -505,16 +505,59 @@
 rgs%5B1%5D;%0A
+        final String covFile = cmdArgs%5B2%5D;%0A
         
@@ -862,24 +862,61 @@
 ayList%3C%3E();%0A
+            boolean rewrite = false;%0A
             
@@ -1126,24 +1126,155 @@
 .process();%0A
+                //print coverage to the file%0A                printCoverage(cov, covFile, rewrite);%0A                rewrite = true;%0A
             
@@ -1305,32 +1305,33 @@
 %0A            %7D%0A%0A
+%0A
             Map%3C
@@ -1708,16 +1708,16 @@
 nally %7B%0A
-
         
@@ -1752,32 +1752,1065 @@
        %7D%0A    %7D%0A%0A
+    private static void printCoverage(Collection%3CGene%3E sqrlist, String covFile, boolean rewrite) %7B%0A        try (FileWriter writer = new FileWriter(covFile, rewrite)) %7B%0A            writer.write(%22Sample%5CtGene%5CtChr%5CtStart%5CtEnd%5CtTag%5CtLength%5CtMeanDepth%5Cn%22);%0A            writer.flush();%0A            for (Gene gene : sqrlist) %7B%0A                StringBuilder str = new StringBuilder();%0A                str.append(gene.getSample()).append(%22%5Ct%22);%0A                str.append(gene.getName()).append(%22%5Ct%22);%0A                str.append(gene.getChr()).append(%22%5Ct%22);%0A                str.append(gene.getStart()).append(%22%5Ct%22);%0A                str.append(gene.getEnd()).append(%22%5Ct%22);%0A                str.append(gene.getTag()).append(%22%5Ct%22);%0A                str.append(gene.getLen()).append(%22%5Ct%22);%0A                str.append(String.format(%22%25.2f%22, gene.getDepth())).append(%22%5Cn%22);%0A                writer.write(str.toString());%0A                writer.flush();%0A            %7D%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A
     private stat

print of coverage file added, the output file for coverage should be passed as third command line parameter