All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencb.biodata.tools.variant.VcfUtils Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * 
 *
 */

package org.opencb.biodata.tools.variant;

import org.opencb.biodata.models.variant.VariantFileMetadata;
import org.opencb.biodata.models.variant.metadata.VariantFileHeaderComplexLine;

import java.util.*;

public class VcfUtils {

    private static final Map> SUPPORTED_CALLERS;

    static {
        SUPPORTED_CALLERS = new LinkedHashMap<>();
        SUPPORTED_CALLERS.put("CAVEMAN", Arrays.asList("ASMD", "CLPM"));
        SUPPORTED_CALLERS.put("PINDEL", Arrays.asList("PC", "VT"));
        SUPPORTED_CALLERS.put("BRASS", Arrays.asList("BAS", "BKDIST"));
    }

    public static String getCaller(VariantFileMetadata fileMetadata) {
        // Try to find the variant caller if it is not provided
        // Check if any of the supported callers contains the INFO fields needed
        String caller = null;
        for (Map.Entry> entry : SUPPORTED_CALLERS.entrySet()) {
            if (checkCaller(entry.getKey(), entry.getValue(), fileMetadata)) {
                caller = entry.getKey();
                break;
            }
        }
        return caller;
    }

    public static boolean checkCaller(String caller, VariantFileMetadata fileMetadata) {
        if (SUPPORTED_CALLERS.containsKey(caller)) {
            return checkCaller(caller, SUPPORTED_CALLERS.get(caller), fileMetadata);
        }
        return false;
    }

    /**
     * Checks if all header fields passed exists.
     * @param caller Variant caller name to check
     * @param keys VCF header fields to check
     * @return true if the name is found or all the field exist
     */
    private static boolean checkCaller(String caller, List keys, VariantFileMetadata fileMetadata) {
        // TODO we need to use caller param for some callers
        Set keySet = new HashSet<>(keys);
        int counter = 0;
        for (VariantFileHeaderComplexLine complexLine : fileMetadata.getHeader().getComplexLines()) {
            if (complexLine.getKey().equals("INFO") && keySet.contains(complexLine.getId())) {
                counter++;

                // Return when all needed keys have been found
                if (keys.size() == counter) {
                    return true;
                }
            }
        }
        return false;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy