Search in sources :

Example 1 with KnownSequenceFragmentData

use of io.repseq.dto.KnownSequenceFragmentData in project repseqio by repseqio.

the class VDJCLibraryRegistry method registerLibrary.

/**
 * Creates and registers single library from VDJCLibraryData
 *
 * @param context context to use for resolution of sequences
 * @param name    library name
 * @param data    library data
 * @return created library
 */
public synchronized VDJCLibrary registerLibrary(Path context, String name, VDJCLibraryData data) {
    // Creating library object
    VDJCLibrary library = new VDJCLibrary(data, name, this, context);
    // Getting library id
    VDJCLibraryId rootId = library.getLibraryIdWithoutChecksum();
    // Check if such library is already registered
    if (libraries.containsKey(rootId))
        throw new RuntimeException("Duplicate library: " + rootId);
    // Loading known sequence fragments from VDJCLibraryData to current SequenceResolver
    SequenceResolver resolver = getSequenceResolver();
    for (KnownSequenceFragmentData fragment : data.getSequenceFragments()) resolver.resolve(new SequenceAddress(context, fragment.getUri())).setRegion(fragment.getRange(), fragment.getSequence());
    // Adding genes
    for (VDJCGeneData gene : data.getGenes()) VDJCLibrary.addGene(library, gene);
    // Adding common species names
    Long taxonId = data.getTaxonId();
    for (String speciesName : data.getSpeciesNames()) {
        String cSpeciesName = canonicalizeSpeciesName(speciesName);
        if (speciesNames.containsKey(cSpeciesName) && !speciesNames.get(cSpeciesName).equals(taxonId))
            throw new IllegalArgumentException("Mismatch in common species name between several libraries. " + "(Library name = " + name + "; name = " + speciesName + ").");
        speciesNames.put(cSpeciesName, taxonId);
        List<String> names = speciesNamesReverse.get(taxonId);
        if (names == null)
            speciesNamesReverse.put(taxonId, names = new ArrayList<>());
        names.add(speciesName);
    }
    // Adding this library to collection
    libraries.put(library.getLibraryIdWithoutChecksum(), library);
    return library;
}
Also used : SequenceResolver(io.repseq.seqbase.SequenceResolver) KnownSequenceFragmentData(io.repseq.dto.KnownSequenceFragmentData) SequenceAddress(io.repseq.seqbase.SequenceAddress) VDJCGeneData(io.repseq.dto.VDJCGeneData)

Example 2 with KnownSequenceFragmentData

use of io.repseq.dto.KnownSequenceFragmentData in project repseqio by repseqio.

the class CompileAction method compile.

public static void compile(Path source, Path destination, int surroundings) throws IOException {
    VDJCLibraryRegistry.resetDefaultRegistry();
    VDJCLibraryRegistry reg = VDJCLibraryRegistry.getDefault();
    reg.registerLibraries(source, "lib");
    List<VDJCLibraryData> result = new ArrayList<>();
    for (VDJCLibrary lib : reg.getLoadedLibraries()) {
        VDJCDataUtils.FragmentsBuilder fragmentsBuilder = new VDJCDataUtils.FragmentsBuilder();
        for (KnownSequenceFragmentData fragment : lib.getData().getSequenceFragments()) fragmentsBuilder.addRegion(fragment);
        for (VDJCGene gene : lib.getGenes()) {
            if (!gene.getData().getBaseSequence().isPureOriginalSequence())
                throw new IllegalArgumentException("Don't support mutated sequences yet.");
            URI uri = gene.getData().getBaseSequence().getOrigin();
            Range region = gene.getPartitioning().getContainingRegion();
            region = region.expand(surroundings);
            NucleotideSequence seq;
            try {
                seq = gene.getSequenceProvider().getRegion(region);
            } catch (SequenceProviderIndexOutOfBoundsException e) {
                region = e.getAvailableRange();
                if (region == null)
                    throw new IllegalArgumentException("Wrong anchor points for " + gene.getName() + " ?");
                seq = gene.getSequenceProvider().getRegion(region);
            }
            fragmentsBuilder.addRegion(uri, region, seq);
        }
        result.add(new VDJCLibraryData(lib.getTaxonId(), lib.getData().getSpeciesNames(), lib.getData().getGenes(), lib.getData().getMeta(), fragmentsBuilder.getFragments()));
    }
    VDJCDataUtils.writeToFile(result, destination, true);
    log.info("{} compiled successfully.", source);
}
Also used : VDJCDataUtils(io.repseq.dto.VDJCDataUtils) ArrayList(java.util.ArrayList) Range(com.milaboratory.core.Range) URI(java.net.URI) KnownSequenceFragmentData(io.repseq.dto.KnownSequenceFragmentData) SequenceProviderIndexOutOfBoundsException(com.milaboratory.core.sequence.provider.SequenceProviderIndexOutOfBoundsException) VDJCLibraryData(io.repseq.dto.VDJCLibraryData) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCLibrary(io.repseq.core.VDJCLibrary) VDJCGene(io.repseq.core.VDJCGene) VDJCLibraryRegistry(io.repseq.core.VDJCLibraryRegistry)

Aggregations

KnownSequenceFragmentData (io.repseq.dto.KnownSequenceFragmentData)2 Range (com.milaboratory.core.Range)1 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)1 SequenceProviderIndexOutOfBoundsException (com.milaboratory.core.sequence.provider.SequenceProviderIndexOutOfBoundsException)1 VDJCGene (io.repseq.core.VDJCGene)1 VDJCLibrary (io.repseq.core.VDJCLibrary)1 VDJCLibraryRegistry (io.repseq.core.VDJCLibraryRegistry)1 VDJCDataUtils (io.repseq.dto.VDJCDataUtils)1 VDJCGeneData (io.repseq.dto.VDJCGeneData)1 VDJCLibraryData (io.repseq.dto.VDJCLibraryData)1 SequenceAddress (io.repseq.seqbase.SequenceAddress)1 SequenceResolver (io.repseq.seqbase.SequenceResolver)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1