Java Code Examples for htsjdk.samtools.util.StringUtil#isBlank()

The following examples show how to use htsjdk.samtools.util.StringUtil#isBlank() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastqToSam.java    From picard with MIT License 6 votes vote down vote up
/** Returns read baseName and asserts correct pair read name format:
 * <ul>
 * <li> Paired reads must either have the exact same read names or they must contain at least one "/"
 * <li> and the First pair read name must end with "/1" and second pair read name ends with "/2"
 * <li> The baseName (read name part before the /) must be the same for both read names
 * <li> If the read names are exactly the same but end in "/2" or "/1" then an exception will be thrown
 * </ul>
 */
String getBaseName(final String readName1, final String readName2, final FastqReader freader1, final FastqReader freader2) {
    String [] toks = getReadNameTokens(readName1, 1, freader1);
    final String baseName1 = toks[0] ;
    final String num1 = toks[1] ;

    toks = getReadNameTokens(readName2, 2, freader2);
    final String baseName2 = toks[0] ;
    final String num2 = toks[1];

    if (!baseName1.equals(baseName2)) {
        throw new PicardException(String.format("In paired mode, read name 1 (%s) does not match read name 2 (%s)", baseName1,baseName2));
    }

    final boolean num1Blank = StringUtil.isBlank(num1);
    final boolean num2Blank = StringUtil.isBlank(num2);
    if (num1Blank || num2Blank) {
        if(!num1Blank) throw new PicardException(error(freader1,"Pair 1 number is missing (" +readName1+ "). Both pair numbers must be present or neither."));       //num1 != blank and num2   == blank
        else if(!num2Blank) throw new PicardException(error(freader2, "Pair 2 number is missing (" +readName2+ "). Both pair numbers must be present or neither.")); //num1 == blank and num =2 != blank
    } else {
        if (!num1.equals("1")) throw new PicardException(error(freader1,"Pair 1 number must be 1 ("+readName1+")"));
        if (!num2.equals("2")) throw new PicardException(error(freader2,"Pair 2 number must be 2 ("+readName2+")"));
    }

    return baseName1 ;
}
 
Example 2
Source File: CreateSequenceDictionary.java    From picard with MIT License 4 votes vote down vote up
/**
 * Load the file ALT_NAMES containing the alternative contig names
 *
 * @return a <code>Map&lt;src_contig,Set&lt;new_names&gt;&gt;</code>. Never null. May be empty if ALT_NAMES is null.
 * @throws PicardException if there is any error in the file ALT_NAMES
 * @author Pierre Lindenbaum
 */
private Map<String, Set<String>> loadContigAliasesMap() throws PicardException {
    // return an empty map if no mapping file was provided
    if (this.ALT_NAMES == null) {
        return Collections.emptyMap();
    }
    // the map returned by the function
    final Map<String, Set<String>> aliasesByContig = new HashMap<>();
    try {
        for (final String line : IOUtil.slurpLines(this.ALT_NAMES)) {
            if (StringUtil.isBlank(line)) {
                continue;
            }
            final int tab = line.indexOf('\t');
            if (tab == -1) {
                throw new IOException("tabulation missing in " + line);
            }
            final String contigName = line.substring(0, tab);
            final String altName = line.substring(tab + 1);
            // check for empty values
            if (StringUtil.isBlank(contigName)) {
                throw new IOException("empty contig in " + line);
            }
            if (StringUtil.isBlank(altName)) {
                throw new IOException("empty alternative name in " + line);
            }
            if (altName.equals(contigName)) {
                continue;
            }
            try {
                SAMSequenceRecord.validateSequenceName(altName);
            } catch (final SAMException exception) {
                throw new IOException("Illegal alternative reference sequence name in " + line, exception);
            }
            // check alias not previously defined as contig
            if (aliasesByContig.containsKey(altName)) {
                throw new IOException("alternate name " + altName +
                        " previously defined as a contig in " + line);
            }
            // check contig not previously defined as alias
            if (aliasesByContig.keySet().stream()
                    // not an error if defined twice for same contig
                    .filter(K -> !K.equals(contigName))
                    .anyMatch(K -> aliasesByContig.get(K).contains(contigName))) {
                throw new IOException("contig  " + contigName +
                        " previously defined as an alternate name in " + line);
            }
            // add alias
            if (!aliasesByContig.containsKey(contigName)) {
                aliasesByContig.put(contigName, new HashSet<>());
            }
            aliasesByContig.get(contigName).add(altName);
        }
        return aliasesByContig;
    } catch (final IOException e) {
        throw new PicardException("Can't read alias file " + ALT_NAMES, e);
    }
}