// 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/kmerindex/KmerIndexDataPathFilter.java
// public class KmerIndexDataPathFilter implements PathFilter {
// 
//     @Override
//     public boolean accept(Path path) {
//         return KmerIndexHelper.isKmerIndexDataFile(path);
//     }
// }


import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
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.kmerindex.KmerIndexDataPathFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
/*
 * 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 KmerIndexHelper {
    
    private static final Log LOG = LogFactory.getLog(KmerIndexHelper.class);
    
    private final static String KMER_INDEX_TABLE_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_INDEX_TABLE_FILENAME_EXTENSION + "$";
    private final static Pattern KMER_INDEX_TABLE_PATH_PATTERN = Pattern.compile(KMER_INDEX_TABLE_PATH_EXP);
    private final static String KMER_INDEX_DATA_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_INDEX_DATA_FILENAME_EXTENSION + "\\.\\d+$";
    private final static Pattern KMER_INDEX_DATA_PATH_PATTERN = Pattern.compile(KMER_INDEX_DATA_PATH_EXP);
    
    public static String makeKmerIndexTableFileName(String filename) {
        return filename + "." + PreprocessorConstants.KMER_INDEX_TABLE_FILENAME_EXTENSION;
    }
    
    public static String makeKmerIndexDataFileName(Path filePath, int mapreduceID) {
// 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/kmerindex/KmerIndexDataPathFilter.java
// public class KmerIndexDataPathFilter implements PathFilter {
// 
//     @Override
//     public boolean accept(Path path) {
//         return KmerIndexHelper.isKmerIndexDataFile(path);
//     }
// }


// Path: src/libra/preprocess/common/helpers/KmerIndexHelper.java
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
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.kmerindex.KmerIndexDataPathFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;

/*
 * 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 KmerIndexHelper {
    
    private static final Log LOG = LogFactory.getLog(KmerIndexHelper.class);
    
    private final static String KMER_INDEX_TABLE_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_INDEX_TABLE_FILENAME_EXTENSION + "$";
    private final static Pattern KMER_INDEX_TABLE_PATH_PATTERN = Pattern.compile(KMER_INDEX_TABLE_PATH_EXP);
    private final static String KMER_INDEX_DATA_PATH_EXP = ".+\\." + PreprocessorConstants.KMER_INDEX_DATA_FILENAME_EXTENSION + "\\.\\d+$";
    private final static Pattern KMER_INDEX_DATA_PATH_PATTERN = Pattern.compile(KMER_INDEX_DATA_PATH_EXP);
    
    public static String makeKmerIndexTableFileName(String filename) {
        return filename + "." + PreprocessorConstants.KMER_INDEX_TABLE_FILENAME_EXTENSION;
    }
    
    public static String makeKmerIndexDataFileName(Path filePath, int mapreduceID) {
        return makeKmerIndexDataFileName(filePath.getName(), mapreduceID);