// Path: src/libra/common/helpers/PathHelper.java
// public class PathHelper {
//     
//     private static final String[] COMPRESSED_EXT = {"gz", "bz2"};
//     
//     public static String getParent(String path) {
//         // check root
//         if(path.equals("/")) {
//             return null;
//         }
//         
//         int lastIdx = path.lastIndexOf("/");
//         if(lastIdx > 0) {
//             return path.substring(0, lastIdx);
//         } else {
//             return "/";
//         }
//     }
//     
//     public static String concatPath(String path1, String path2) {
//         StringBuffer sb = new StringBuffer();
//         
//         if(path1 != null && !path1.isEmpty()) {
//             sb.append(path1);
//         }
//         
//         if(!path1.endsWith("/")) {
//             sb.append("/");
//         }
//         
//         if(path2 != null && !path2.isEmpty()) {
//             if(path2.startsWith("/")) {
//                 sb.append(path2.substring(1, path2.length()));
//             } else {
//                 sb.append(path2);
//             }
//         }
//         
//         return sb.toString();
//     }
//     
//     public static String getExtensionOfDecompressedFile(String name) {
//         String myname = name;
//         int idx = myname.lastIndexOf(".");
//         if(idx > 0) {
//             String ext = myname.substring(idx + 1);
//             for(String cext : COMPRESSED_EXT) {
//                 if(cext.equalsIgnoreCase(ext)) {
//                     // compressed
//                     myname = myname.substring(0, idx);
//                     break;
//                 }
//             }
//         } else {
//             return null;
//         }
//         
//         idx = myname.lastIndexOf(".");
//         if(idx > 0) {
//             return myname.substring(idx + 1);
//         }
//         return null;
//     }
// }
// 
// Path: src/libra/preprocess/common/PreprocessorConstants.java
// public class PreprocessorConstants {
//     public static final String FILE_TABLE_FILENAME_EXTENSION = "ftbl";
//     public static final String KMER_FILTER_TABLE_FILENAME_EXTENSION = "kflt";
//     public static final String KMER_INDEX_TABLE_FILENAME_EXTENSION = "kidx";
//     public static final String KMER_INDEX_DATA_FILENAME_EXTENSION = "kidxc";
//     public static final String KMER_STATISTICS_TABLE_FILENAME_EXTENSION = "kstat";
//     
//     public static final String FILE_TABLE_DIRNAME = "filetable";
//     public static final String KMER_FILTER_DIRNAME = "filter";
//     public static final String KMER_INDEX_DIRNAME = "kmerindex";
//     public static final String KMER_STATISITCS_DIRNAME = "statistics";
// }
// 
// Path: src/libra/preprocess/common/kmerstatistics/KmerStatisticsPartTablePathFilter.java
// public class KmerStatisticsPartTablePathFilter implements PathFilter {
// 
//     @Override
//     public boolean accept(Path path) {
//         return KmerStatisticsHelper.isKmerStatisticsPartTableFile(path);
//     }
// }


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import libra.common.helpers.PathHelper;
import libra.preprocess.common.PreprocessorConstants;
import libra.preprocess.common.kmerstatistics.KmerStatisticsPartTablePathFilter;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/*
 * Copyright 2016 iychoi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package libra.preprocess.common.helpers;


/**
 *
 * @author iychoi
 */
public class KmerStatisticsHelper {
    
    private final static String KMER_STATISTICS_TABLE_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION + "$";
    private final static Pattern KMER_STATISTICS_TABLE_PATH_PATTERN = Pattern.compile(KMER_STATISTICS_TABLE_PATH_EXP);
    
    private final static String KMER_STATISTICS_PART_TABLE_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION + "\\.\\d+$";
    private final static Pattern KMER_STATISTICS_PART_TABLE_PATH_PATTERN = Pattern.compile(KMER_STATISTICS_PART_TABLE_PATH_EXP);
    
    public static String makeKmerStatisticsTableFileName(String filename) {
        return filename + "." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION;
    }
    
    public static String makeKmerStatisticsPartTableFileName(String filename, int taskID) {
        return filename + "." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION + "." + taskID;
    }
    
    public static String makeKmerStatisticsDirPath(String rootPath) {
// Path: src/libra/common/helpers/PathHelper.java
// public class PathHelper {
//     
//     private static final String[] COMPRESSED_EXT = {"gz", "bz2"};
//     
//     public static String getParent(String path) {
//         // check root
//         if(path.equals("/")) {
//             return null;
//         }
//         
//         int lastIdx = path.lastIndexOf("/");
//         if(lastIdx > 0) {
//             return path.substring(0, lastIdx);
//         } else {
//             return "/";
//         }
//     }
//     
//     public static String concatPath(String path1, String path2) {
//         StringBuffer sb = new StringBuffer();
//         
//         if(path1 != null && !path1.isEmpty()) {
//             sb.append(path1);
//         }
//         
//         if(!path1.endsWith("/")) {
//             sb.append("/");
//         }
//         
//         if(path2 != null && !path2.isEmpty()) {
//             if(path2.startsWith("/")) {
//                 sb.append(path2.substring(1, path2.length()));
//             } else {
//                 sb.append(path2);
//             }
//         }
//         
//         return sb.toString();
//     }
//     
//     public static String getExtensionOfDecompressedFile(String name) {
//         String myname = name;
//         int idx = myname.lastIndexOf(".");
//         if(idx > 0) {
//             String ext = myname.substring(idx + 1);
//             for(String cext : COMPRESSED_EXT) {
//                 if(cext.equalsIgnoreCase(ext)) {
//                     // compressed
//                     myname = myname.substring(0, idx);
//                     break;
//                 }
//             }
//         } else {
//             return null;
//         }
//         
//         idx = myname.lastIndexOf(".");
//         if(idx > 0) {
//             return myname.substring(idx + 1);
//         }
//         return null;
//     }
// }
// 
// Path: src/libra/preprocess/common/PreprocessorConstants.java
// public class PreprocessorConstants {
//     public static final String FILE_TABLE_FILENAME_EXTENSION = "ftbl";
//     public static final String KMER_FILTER_TABLE_FILENAME_EXTENSION = "kflt";
//     public static final String KMER_INDEX_TABLE_FILENAME_EXTENSION = "kidx";
//     public static final String KMER_INDEX_DATA_FILENAME_EXTENSION = "kidxc";
//     public static final String KMER_STATISTICS_TABLE_FILENAME_EXTENSION = "kstat";
//     
//     public static final String FILE_TABLE_DIRNAME = "filetable";
//     public static final String KMER_FILTER_DIRNAME = "filter";
//     public static final String KMER_INDEX_DIRNAME = "kmerindex";
//     public static final String KMER_STATISITCS_DIRNAME = "statistics";
// }
// 
// Path: src/libra/preprocess/common/kmerstatistics/KmerStatisticsPartTablePathFilter.java
// public class KmerStatisticsPartTablePathFilter implements PathFilter {
// 
//     @Override
//     public boolean accept(Path path) {
//         return KmerStatisticsHelper.isKmerStatisticsPartTableFile(path);
//     }
// }


// Path: src/libra/preprocess/common/helpers/KmerStatisticsHelper.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import libra.common.helpers.PathHelper;
import libra.preprocess.common.PreprocessorConstants;
import libra.preprocess.common.kmerstatistics.KmerStatisticsPartTablePathFilter;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/*
 * Copyright 2016 iychoi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package libra.preprocess.common.helpers;


/**
 *
 * @author iychoi
 */
public class KmerStatisticsHelper {
    
    private final static String KMER_STATISTICS_TABLE_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION + "$";
    private final static Pattern KMER_STATISTICS_TABLE_PATH_PATTERN = Pattern.compile(KMER_STATISTICS_TABLE_PATH_EXP);
    
    private final static String KMER_STATISTICS_PART_TABLE_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION + "\\.\\d+$";
    private final static Pattern KMER_STATISTICS_PART_TABLE_PATH_PATTERN = Pattern.compile(KMER_STATISTICS_PART_TABLE_PATH_EXP);
    
    public static String makeKmerStatisticsTableFileName(String filename) {
        return filename + "." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION;
    }
    
    public static String makeKmerStatisticsPartTableFileName(String filename, int taskID) {
        return filename + "." + PreprocessorConstants.KMER_STATISTICS_TABLE_FILENAME_EXTENSION + "." + taskID;
    }
    
    public static String makeKmerStatisticsDirPath(String rootPath) {
        return PathHelper.concatPath(rootPath, PreprocessorConstants.KMER_STATISITCS_DIRNAME);