/** Copyright (c) 2012 Memorial Sloan-Kettering Cancer Center.
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License as published
** by the Free Software Foundation; either version 2.1 of the License, or
** any later version.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
** MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
** documentation provided hereunder is on an "as is" basis, and
** Memorial Sloan-Kettering Cancer Center 
** has no obligations to provide maintenance, support,
** updates, enhancements or modifications.  In no event shall
** Memorial Sloan-Kettering Cancer Center
** be liable to any party for direct, indirect, special,
** incidental or consequential damages, including lost profits, arising
** out of the use of this software and its documentation, even if
** Memorial Sloan-Kettering Cancer Center 
** has been advised of the possibility of such damage.  See
** the GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
**/

package org.mskcc.cbio.cgds.scripts;

import org.mskcc.cbio.cgds.util.ConsoleUtil;
import org.mskcc.cbio.cgds.util.FileUtil;
import org.mskcc.cbio.cgds.util.ProgressMonitor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConvertCosmicVcfToMaf {
    private ProgressMonitor pMonitor;
    private File vcf, maf;

    public ConvertCosmicVcfToMaf(File vcf, File maf, ProgressMonitor pMonitor) {
        this.vcf = vcf;
        this.maf = maf;
        this.pMonitor = pMonitor;
    }

    public void convert() throws IOException {
        Pattern p = Pattern.compile(".+;CNT=([0-9]+)");
        FileWriter writer = new FileWriter(maf);
        BufferedWriter bufWriter = new BufferedWriter(writer);
        bufWriter.append("COSMIC_ID\tCOSMIC_COUNT\tChromosome\tStart_Position\tEnd_Position\t"
                + "Reference_Allele\tTumor_Seq_Allele1\tTumor_Seq_Allele2\tNCBI_Build\tCOSMIC_Info");
        
        FileReader reader = new FileReader(vcf);
        BufferedReader buf = new BufferedReader(reader);
        String line;
        while ((line = buf.readLine()) != null) {
            if (pMonitor != null) {
                pMonitor.incrementCurValue();
                ConsoleUtil.showProgress(pMonitor);
            }
            if (!line.startsWith("#")) {
                String parts[] = line.split("\t",-1);
                if (parts.length<8) {
                    System.err.println("Wrong line in cosmic: "+line);
                    continue;
                }
                
                String info = parts[7];
                Matcher m = p.matcher(info);
                if (m.find()) {
                    String id = parts[2];
                    String count = m.group(1);
                    String chr = parts[0];
                    long start = Long.parseLong(parts[1]);
                    String ref = parts[3];
                    String alt = parts[4];
                    long end = start + ref.length() -1;
                    
                    bufWriter.append(id).append("\t")
                            .append(count).append("\t")
                            .append(chr).append("\t")
                            .append(Long.toString(start)).append("\t")
                            .append(Long.toString(end)).append("\t")
                            .append(ref).append("\t")
                            .append(alt).append("\t")
                            .append(alt).append("\t")
                            .append("37\t")
                            .append(info);
                }
            }
        }
        buf.close(); 
        bufWriter.close();
    }

    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.out.println("command line usage:  importCosmicData.pl <CosmicCodingMuts.vcf> <CosmicCodingMuts.maf>");
            System.exit(1);
        }
        ProgressMonitor pMonitor = new ProgressMonitor();
        pMonitor.setConsoleMode(true);

        File vcf = new File(args[0]);
        System.out.println("Reading data from:  " + vcf.getAbsolutePath());
        int numLines = FileUtil.getNumLines(vcf);
        System.out.println(" --> total number of lines:  " + numLines);
        pMonitor.setMaxValue(numLines);
        ConvertCosmicVcfToMaf parser = new ConvertCosmicVcfToMaf(vcf, new File(args[1]), pMonitor);
        parser.convert();
        ConsoleUtil.showWarnings(pMonitor);
        System.err.println("Done.");
    }
}

@@ -2446,16 +2446,18 @@
 MIC_Info
+%5Cn
 %22);%0A    
@@ -4073,24 +4073,66 @@
 append(info)
+%0A                            .append(%22%5Cn%22)
 ;%0A          

add new lines