<commit_before>/*
 *  Groovy NGS Utils - Some simple utilites for processing Next Generation Sequencing data.
 *
 *  Copyright (C) 2013 <NAME>, <EMAIL><at><EMAIL>
 *
 *  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 (at your option) 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.  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gngs

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import groovy.stream.Stream;
import groovy.transform.CompileStatic;
import htsjdk.samtools.BAMIndexer
import htsjdk.samtools.BAMRecord;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SamReader;
import htsjdk.samtools.SamReaderFactory
import htsjdk.samtools.SAMFileWriter;
import htsjdk.samtools.SAMFileWriterFactory
import htsjdk.samtools.SAMFormatException;
import htsjdk.samtools.SAMReadGroupRecord;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.SAMRecordIterator;
import htsjdk.samtools.SAMSequenceRecord;
import htsjdk.samtools.SamInputResource
import htsjdk.samtools.ValidationStringency;


/*
 * An alternate alignment for a read. 
 * eg: constructed from the XA tag as output by bwa aln.
 * 
 * @author Simon
 */
class XA {
    String chr
    int pos
    String cigar
    int nm

    public String toString() {
        "[$chr:$pos $cigar NM=$nm]"
    }
}

class SAMRecordCategory {
    @CompileStatic
    static List<XA> getAlternateAlignments(BAMRecord record) {
        String xas = record.getAttribute("XA")
        if(!xas)
            return null
        return xas.split(';').collect { String xa ->
            String [] parts = xa.split(',')
            new XA( chr: parts[0], pos: parts[1].substring(1) as Integer, cigar:parts[2],  nm : parts[-1] as Integer)
        }
    }
    
    @CompileStatic
    static Object asType(SAMRecord r, Class clazz) {
        new Region(r.referenceName, Math.min(r.alignmentStart, r.alignmentEnd)..Math.max(r.alignmentStart, r.alignmentEnd))
    }
    
    @CompileStatic
    static Object asType(BAMRecord r, Class clazz) {
        new Region(r.referenceName, Math.min(r.alignmentStart, r.alignmentEnd)..Math.max(r.alignmentStart, r.alignmentEnd))
    } 
    
    @CompileStatic
    static Object toRegion(BAMRecord r) {
        new Region(r.referenceName, Math.min(r.alignmentStart, r.alignmentEnd)..Math.max(r.alignmentStart, r.alignmentEnd))
    }  
}

@CompileStatic
class ReadWindow {
    
    int pos
    
    TreeMap<Integer, List<SAMRecord>> window = new TreeMap()
}

/**
 * Adds various Groovy idioms and convenience features to the <commit_msg>add useful tostring on read window<commit_after>/*
 *  Groovy NGS Utils - Some simple utilites for processing Next Generation Sequencing data.
 *
 *  Copyright (C) 2013 <NAME>, ssadedin<at>gmail.com
 *
 *  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 (at your option) 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.  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gngs

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import groovy.stream.Stream;
import groovy.transform.CompileStatic;
import htsjdk.samtools.BAMIndexer
import htsjdk.samtools.BAMRecord;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SamReader;
import htsjdk.samtools.SamReaderFactory
import htsjdk.samtools.SAMFileWriter;
import htsjdk.samtools.SAMFileWriterFactory
import htsjdk.samtools.SAMFormatException;
import htsjdk.samtools.SAMReadGroupRecord;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.SAMRecordIterator;
import htsjdk.samtools.SAMSequenceRecord;
import htsjdk.samtools.SamInputResource
import htsjdk.samtools.ValidationStringency;


/*
 * An alternate alignment for a read. 
 * eg: constructed from the XA tag as output by bwa aln.
 * 
 * @author Simon
 */
class XA {
    String chr
    int pos
    String cigar
    int nm

    public String toString() {
        "[$chr:$pos $cigar NM=$nm]"
    }
}

class SAMRecordCategory {
    @CompileStatic
    static List<XA> getAlternateAlignments(BAMRecord record) {
        String xas = record.getAttribute("XA")
        if(!xas)
            return null
        return xas.split(';').collect { String xa ->
            String [] parts = xa.split(',')
            new XA( chr: parts[0], pos: parts[1].substring(1) as Integer, cigar:parts[2],  nm : parts[-1] as Integer)
        }
    }
    
    @CompileStatic
    static Object asType(SAMRecord r, Class clazz) {
        new Region(r.referenceName, Math.min(r.alignmentStart, r.alignmentEnd)..Math.max(r.alignmentStart, r.alignmentEnd))
    }
    
    @CompileStatic
    static Object asType(BAMRecord r, Class clazz) {
        new Region(r.referenceName, Math.min(r.alignmentStart, r.alignmentEnd)..Math.max(r.alignmentStart, r.alignmentEnd))
    } 
    
    @CompileStatic
    static Object toRegion(BAMRecord r) {
        new Region(r.referenceName, Math.min(r.alignmentStart, r.alignmentEnd)..Math.max(r.alignmentStart, r.alignmentEnd))
    }  
}

@CompileStatic
class ReadWindow {
    
    int pos
    
    TreeMap<Integer, List<SAMRecord>> window = new TreeMap()
    
    String toString() {
        "Reads at ${window.count { it.value.size() > 1 }} positions: " + 
            window.grep { Map.Entry e -> ((List)e.value).size() > 1 }
    }
}

/**
 * Adds various Groovy idioms and convenience features to the 
{'fraction_non_alphanumeric': 0.09180378286944488, 'fraction_numerical': 0.007073658311548516, 'mean_word_length': 3.4701030927835053, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '222820', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1967, 'n_words': 604}