2009-05-22 03:30:44 +08:00
|
|
|
package org.broadinstitute.sting.utils.sam;
|
|
|
|
|
|
|
|
|
|
import net.sf.samtools.*;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2009-06-03 15:40:34 +08:00
|
|
|
import java.util.*;
|
|
|
|
|
|
2009-06-05 06:37:51 +08:00
|
|
|
import org.broadinstitute.sting.gatk.iterators.PeekingStingIterator;
|
2009-06-03 15:40:34 +08:00
|
|
|
import org.broadinstitute.sting.gatk.Reads;
|
2009-06-12 04:18:13 +08:00
|
|
|
import org.broadinstitute.sting.utils.StingException;
|
2009-05-22 03:30:44 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* User: aaron
|
|
|
|
|
* Date: May 21, 2009
|
|
|
|
|
* Time: 2:57:48 PM
|
|
|
|
|
*
|
|
|
|
|
* The Broad Institute
|
|
|
|
|
* SOFTWARE COPYRIGHT NOTICE AGREEMENT
|
|
|
|
|
* This software and its documentation are copyright 2009 by the
|
|
|
|
|
* Broad Institute/Massachusetts Institute of Technology. All rights are reserved.
|
|
|
|
|
*
|
|
|
|
|
* This software is supplied without any warranty or guaranteed support whatsoever. Neither
|
|
|
|
|
* the Broad Institute nor MIT can be responsible for its use, misuse, or functionality.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author aaron
|
|
|
|
|
* @version 1.0
|
|
|
|
|
*/
|
2009-06-12 01:46:06 +08:00
|
|
|
public class ArtificialSAMUtils {
|
2009-06-03 15:40:34 +08:00
|
|
|
public static final int DEFAULT_READ_LENGTH = 50;
|
|
|
|
|
|
2009-05-22 03:30:44 +08:00
|
|
|
/**
|
|
|
|
|
* create an artificial sam file
|
2009-06-03 15:40:34 +08:00
|
|
|
*
|
|
|
|
|
* @param filename the filename to write to
|
2009-05-22 03:30:44 +08:00
|
|
|
* @param numberOfChromosomes the number of chromosomes
|
2009-06-03 15:40:34 +08:00
|
|
|
* @param startingChromosome where to start counting
|
|
|
|
|
* @param chromosomeSize how large each chromosome is
|
|
|
|
|
* @param readsPerChomosome how many reads to make in each chromosome. They'll be aligned from position 1 to x (which is the number of reads)
|
2009-05-22 03:30:44 +08:00
|
|
|
*/
|
2009-06-05 06:37:51 +08:00
|
|
|
public static void createArtificialBamFile( String filename, int numberOfChromosomes, int startingChromosome, int chromosomeSize, int readsPerChomosome ) {
|
2009-05-22 03:30:44 +08:00
|
|
|
SAMFileHeader header = createArtificialSamHeader(numberOfChromosomes, startingChromosome, chromosomeSize);
|
|
|
|
|
File outFile = new File(filename);
|
|
|
|
|
|
|
|
|
|
SAMFileWriter out = new SAMFileWriterFactory().makeBAMWriter(header, false, outFile);
|
|
|
|
|
|
|
|
|
|
for (int x = startingChromosome; x < startingChromosome + numberOfChromosomes; x++) {
|
|
|
|
|
for (int readNumber = 0; readNumber < readsPerChomosome; readNumber++) {
|
|
|
|
|
out.addAlignment(createArtificialRead(header, "Read_" + readNumber, x - startingChromosome, readNumber, 100));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create an artificial sam file
|
2009-06-03 15:40:34 +08:00
|
|
|
*
|
|
|
|
|
* @param filename the filename to write to
|
2009-05-22 03:30:44 +08:00
|
|
|
* @param numberOfChromosomes the number of chromosomes
|
2009-06-03 15:40:34 +08:00
|
|
|
* @param startingChromosome where to start counting
|
|
|
|
|
* @param chromosomeSize how large each chromosome is
|
|
|
|
|
* @param readsPerChomosome how many reads to make in each chromosome. They'll be aligned from position 1 to x (which is the number of reads)
|
2009-05-22 03:30:44 +08:00
|
|
|
*/
|
2009-06-05 06:37:51 +08:00
|
|
|
public static void createArtificialSamFile( String filename, int numberOfChromosomes, int startingChromosome, int chromosomeSize, int readsPerChomosome ) {
|
2009-05-22 03:30:44 +08:00
|
|
|
SAMFileHeader header = createArtificialSamHeader(numberOfChromosomes, startingChromosome, chromosomeSize);
|
|
|
|
|
File outFile = new File(filename);
|
|
|
|
|
|
|
|
|
|
SAMFileWriter out = new SAMFileWriterFactory().makeSAMWriter(header, false, outFile);
|
|
|
|
|
|
|
|
|
|
for (int x = startingChromosome; x < startingChromosome + numberOfChromosomes; x++) {
|
|
|
|
|
for (int readNumber = 0; readNumber < readsPerChomosome; readNumber++) {
|
|
|
|
|
out.addAlignment(createArtificialRead(header, "Read_" + readNumber, x - startingChromosome, readNumber, 100));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an artificial sam header, matching the parameters, chromosomes which will be labeled chr1, chr2, etc
|
2009-06-03 15:40:34 +08:00
|
|
|
*
|
2009-05-22 03:30:44 +08:00
|
|
|
* @param numberOfChromosomes the number of chromosomes to create
|
2009-06-03 15:40:34 +08:00
|
|
|
* @param startingChromosome the starting number for the chromosome (most likely set to 1)
|
|
|
|
|
* @param chromosomeSize the length of each chromosome
|
2009-06-05 06:37:51 +08:00
|
|
|
*
|
2009-05-22 03:30:44 +08:00
|
|
|
* @return
|
|
|
|
|
*/
|
2009-06-05 06:37:51 +08:00
|
|
|
public static SAMFileHeader createArtificialSamHeader( int numberOfChromosomes, int startingChromosome, int chromosomeSize ) {
|
2009-05-22 03:30:44 +08:00
|
|
|
SAMFileHeader header = new SAMFileHeader();
|
|
|
|
|
SAMSequenceDictionary dict = new SAMSequenceDictionary();
|
|
|
|
|
// make up some sequence records
|
|
|
|
|
for (int x = startingChromosome; x < startingChromosome + numberOfChromosomes; x++) {
|
2009-06-05 06:37:51 +08:00
|
|
|
SAMSequenceRecord rec = new SAMSequenceRecord("chr" + ( x ), chromosomeSize /* size */);
|
2009-06-03 15:40:34 +08:00
|
|
|
rec.setSequenceLength(chromosomeSize);
|
2009-05-22 03:30:44 +08:00
|
|
|
dict.addSequence(rec);
|
|
|
|
|
}
|
|
|
|
|
header.setSequenceDictionary(dict);
|
|
|
|
|
return header;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an artificial read based on the parameters. The cigar string will be *M, where * is the length of the read
|
2009-06-03 15:40:34 +08:00
|
|
|
*
|
|
|
|
|
* @param header the SAM header to associate the read with
|
|
|
|
|
* @param name the name of the read
|
|
|
|
|
* @param refIndex the reference index, i.e. what chromosome to associate it with
|
2009-05-22 03:30:44 +08:00
|
|
|
* @param alignmentStart where to start the alignment
|
2009-06-03 15:40:34 +08:00
|
|
|
* @param length the length of the read
|
2009-06-05 06:37:51 +08:00
|
|
|
*
|
2009-05-22 03:30:44 +08:00
|
|
|
* @return the artificial read
|
|
|
|
|
*/
|
2009-06-05 06:37:51 +08:00
|
|
|
public static SAMRecord createArtificialRead( SAMFileHeader header, String name, int refIndex, int alignmentStart, int length ) {
|
2009-06-12 04:18:13 +08:00
|
|
|
if( alignmentStart == 0 )
|
|
|
|
|
throw new StingException("Invalid alignment start for artificial read");
|
2009-05-22 03:30:44 +08:00
|
|
|
SAMRecord record = new SAMRecord(header);
|
|
|
|
|
record.setReadName(name);
|
|
|
|
|
record.setReferenceIndex(refIndex);
|
2009-06-12 04:18:13 +08:00
|
|
|
record.setAlignmentStart(alignmentStart);
|
2009-05-22 03:30:44 +08:00
|
|
|
List<CigarElement> elements = new ArrayList<CigarElement>();
|
2009-06-03 15:40:34 +08:00
|
|
|
elements.add(new CigarElement(length, CigarOperator.characterToEnum('M')));
|
2009-05-22 03:30:44 +08:00
|
|
|
record.setCigar(new Cigar(elements));
|
|
|
|
|
record.setProperPairFlag(false);
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-03 15:40:34 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create an iterator containing the specified read piles
|
2009-06-05 06:37:51 +08:00
|
|
|
*
|
2009-06-03 15:40:34 +08:00
|
|
|
* @param startingChr the chromosome (reference ID) to start from
|
2009-06-05 06:37:51 +08:00
|
|
|
* @param endingChr the id to end with
|
|
|
|
|
* @param readCount the number of reads per chromosome
|
|
|
|
|
*
|
2009-06-03 15:40:34 +08:00
|
|
|
* @return StingSAMIterator representing the specified amount of fake data
|
|
|
|
|
*/
|
2009-06-05 06:37:51 +08:00
|
|
|
public static PeekingStingIterator unmappedReadIterator( int startingChr, int endingChr, int readCount ) {
|
|
|
|
|
SAMFileHeader header = createArtificialSamHeader(( endingChr - startingChr ) + 1, startingChr, readCount + DEFAULT_READ_LENGTH);
|
|
|
|
|
|
|
|
|
|
return new ArtificialSAMIterator(startingChr, endingChr, readCount, header);
|
2009-06-03 15:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-06-05 06:37:51 +08:00
|
|
|
* create an iterator containing the specified read piles
|
|
|
|
|
*
|
|
|
|
|
* @param startingChr the chromosome (reference ID) to start from
|
|
|
|
|
* @param endingChr the id to end with
|
|
|
|
|
* @param readCount the number of reads per chromosome
|
|
|
|
|
* @param unmappedReadCount the count of unmapped reads to place at the end of the iterator, like in a sorted bam file
|
|
|
|
|
*
|
|
|
|
|
* @return StingSAMIterator representing the specified amount of fake data
|
2009-06-03 15:40:34 +08:00
|
|
|
*/
|
2009-06-05 06:37:51 +08:00
|
|
|
public static PeekingStingIterator unmappedReadIterator( int startingChr, int endingChr, int readCount, int unmappedReadCount ) {
|
|
|
|
|
SAMFileHeader header = createArtificialSamHeader(( endingChr - startingChr ) + 1, startingChr, readCount + DEFAULT_READ_LENGTH);
|
2009-06-03 15:40:34 +08:00
|
|
|
|
2009-06-05 06:37:51 +08:00
|
|
|
return new ArtificialSAMIterator(startingChr, endingChr, readCount, unmappedReadCount, header);
|
2009-06-03 15:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
2009-06-05 06:37:51 +08:00
|
|
|
/**
|
|
|
|
|
* create an ArtificialSAMQueryIterator containing the specified read piles
|
|
|
|
|
*
|
|
|
|
|
* @param startingChr the chromosome (reference ID) to start from
|
|
|
|
|
* @param endingChr the id to end with
|
|
|
|
|
* @param readCount the number of reads per chromosome
|
|
|
|
|
*
|
|
|
|
|
* @return StingSAMIterator representing the specified amount of fake data
|
|
|
|
|
*/
|
|
|
|
|
public static ArtificialSAMQueryIterator queryReadIterator( int startingChr, int endingChr, int readCount ) {
|
|
|
|
|
SAMFileHeader header = createArtificialSamHeader(( endingChr - startingChr ) + 1, startingChr, readCount + DEFAULT_READ_LENGTH);
|
|
|
|
|
|
|
|
|
|
return new ArtificialSAMQueryIterator(startingChr, endingChr, readCount, 0, header);
|
2009-06-03 15:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
2009-06-05 06:37:51 +08:00
|
|
|
/**
|
|
|
|
|
* create an ArtificialSAMQueryIterator containing the specified read piles
|
|
|
|
|
*
|
|
|
|
|
* @param startingChr the chromosome (reference ID) to start from
|
|
|
|
|
* @param endingChr the id to end with
|
|
|
|
|
* @param readCount the number of reads per chromosome
|
|
|
|
|
* @param unmappedReadCount the count of unmapped reads to place at the end of the iterator, like in a sorted bam file
|
|
|
|
|
*
|
|
|
|
|
* @return StingSAMIterator representing the specified amount of fake data
|
|
|
|
|
*/
|
|
|
|
|
public static ArtificialSAMQueryIterator queryReadIterator( int startingChr, int endingChr, int readCount, int unmappedReadCount ) {
|
|
|
|
|
SAMFileHeader header = createArtificialSamHeader(( endingChr - startingChr ) + 1, startingChr, readCount + DEFAULT_READ_LENGTH);
|
2009-06-03 15:40:34 +08:00
|
|
|
|
2009-06-05 06:37:51 +08:00
|
|
|
return new ArtificialSAMQueryIterator(startingChr, endingChr, readCount, unmappedReadCount, header);
|
2009-06-03 15:40:34 +08:00
|
|
|
}
|
2009-05-22 03:30:44 +08:00
|
|
|
}
|
2009-06-05 06:37:51 +08:00
|
|
|
|