Package net.sf.samtools

Examples of net.sf.samtools.SAMFileReader$AssertableIterator


    c2r.init(ref);
    c2r.setMinBaseQuality(20);
   
    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
   
    SAMFileReader abraReader = null;
    SAMFileReader origReader = null;
   
    long s = System.currentTimeMillis();
   
    String line = reader.readLine();
    while (line != null) {
View Full Code Here


  private SAMFileReader initReader(SAMFileReader old, String input) {
    if (old != null) {
      old.close();
    }
   
    SAMFileReader reader = new SAMFileReader(new File(input));
    reader.setValidationStringency(ValidationStringency.SILENT);

    return reader;
  }
View Full Code Here

    c2r.init(ref);
   
    qualityMismatches = new HashMap<Integer, Long>();
    qualityCounts = new HashMap<Integer, Long>();
   
    SAMFileReader reader = new SAMFileReader(new File(input));
   
    int min = 129;
    int max = -129;
   
    for (SAMRecord read : reader) {
      if (!read.getReadUnmappedFlag()) {
        List<Integer> mismatchPositions = c2r.mismatchPositions(read);
       
        for (int pos : mismatchPositions) {
          int qual = read.getBaseQualityString().charAt(pos) - 33;
          if (qual > max) {
            max = qual;
          }
         
          if (qual < min) {
            min = qual;
          }
         
          addMismatch(qual);
        }
       
        for (int i=0; i< read.getBaseQualityString().length(); i++) {
          int qual = read.getBaseQualityString().charAt(i) - 33;
         
          if (qual > max) {
            max = qual;
          }
         
          if (qual < min) {
            min = qual;
          }
         
          addCount(qual);
        }
      }
    }
   
    reader.close();
   
    for (int i=min; i<max; i++) {
      long count = getValue(qualityCounts, i);
      long mismatches = getValue(qualityMismatches, i);
      System.out.println(i + "," + count + "," +
View Full Code Here

public class BaseQualityByRegion {

  //TODO: Move to utils package
  public void run(String input, String regionsGtf) throws IOException {
   
    SAMFileReader reader = new SAMFileReader(new File(input));
   
    RegionLoader loader = new RegionLoader();
    List<Feature> regions = loader.load(regionsGtf);
   
    regions = ReAligner.splitRegions(regions);

    for (Feature region : regions) {
     
      long numReads = 0;
      long numBases = 0;
      long totalQuality = 0;
      long numBasesLt5 = 0;
      long numBasesLt10 = 0;
      long numBasesLt20 = 0;
      long numReadsLt5 = 0;
      long numReadsLt10 = 0;
      long numReadsLt20 = 0;
      long numReadsLt30 = 0;
      long numReads5X10 = 0;
      long numInternalReadsLt20 = 0;
      long numReadsWithAmbiguousBases = 0;
      long numReadsIntersectLt20Ambiguous = 0;
      long minMapq = 1000;
      long totalMapq = 0;
     
      CloseableIterator<SAMRecord> iter = reader.queryOverlapping(region.getSeqname(), (int) region.getStart(), (int) region.getEnd());
     
      while (iter.hasNext()) {
        SAMRecord read = iter.next();
       
        if (read.getMappingQuality() >= 10) {
          String qualStr = read.getBaseQualityString();
         
          boolean readLt5 = false;
          boolean readLt10 = false;
          boolean readLt20 = false;
          boolean readLt30 = false;
          boolean internalReadLt20 = false;
         
          numReads++;
          numBases += qualStr.length();
          int readBasesLt5 = 0;
 
          for (int i=0; i<qualStr.length(); i++) {
           
            // Assuming phred33
            int qual = qualStr.charAt(i) - '!';
            totalQuality += qual;
           
            if (qual < 5) {
              numBasesLt5++;
              readBasesLt5++;
              readLt5 = true;
            }
           
            if (qual < 10) {
              numBasesLt10++;
              readLt10 = true;
            }
           
            if (qual < 20) {
              numBasesLt20++;
              readLt20 = true;
             
              if ((i>=10) && (i<90)) {
                internalReadLt20 = true;
              }
            }
           
            if (qual < 30) {
              readLt30 = true;
            }
          }
         
          if (readLt5) {
            numReadsLt5++;
          }
         
          if (readLt10) {
            numReadsLt10++;
          }
         
          if (readLt20) {
            numReadsLt20++;
          }
         
          if (readLt30) {
            numReadsLt30++;
          }
         
          if (readBasesLt5 >= 10) {
            numReads5X10++;
          }
         
          if (internalReadLt20 == true) {
            numInternalReadsLt20++;
          }
         
          if (read.getReadString().contains("N")) {
            numReadsWithAmbiguousBases++;
          }
         
          if (readLt20 || read.getReadString().contains("N")) {
            numReadsIntersectLt20Ambiguous++;
          }
         
          if (read.getMappingQuality() < minMapq) {
            minMapq = read.getMappingQuality();
          }
         
          totalMapq += read.getMappingQuality();
        }
      }
     
      iter.close();
     
      System.out.println(region.getDescriptor() + "\t" +
          numBases + "\t" +
          numReads + "\t" +
          avg(numBasesLt5, numBases) + "\t" +
          avg(numBasesLt10, numBases) + "\t" +
          avg(numBasesLt20, numBases) + "\t" +
          avg(totalQuality, numBases) + "\t" +
          avg(numReadsLt5, numReads) + "\t" +
          avg(numReadsLt10, numReads) + "\t" +
          avg(numReadsLt20, numReads) + "\t" +
          avg(numReadsLt30, numReads) + "\t" +
          avg(numReads5X10, numReads) + "\t" +
          avg(numInternalReadsLt20, numReads) + "\t" +
          avg(numReadsWithAmbiguousBases, numReads) + "\t" +
          avg(numReadsIntersectLt20Ambiguous, numReads) + "\t" +
          avg(totalMapq, numReads) + "\t" +
          minMapq);
    }

    reader.close();
  }
View Full Code Here

    int[] thresholds = new int[] { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 255 };
   
   
    for (Feature region : regions) {
      SAMFileReader reader = new SAMFileReader(new File(input));
      reader.setValidationStringency(ValidationStringency.SILENT);

      int[] counts = new int[thresholds.length];
      System.err.println(region.getDescriptor());
      Iterator<SAMRecord> iter = reader.queryOverlapping(region.getSeqname(), (int) region.getStart(), (int) region.getEnd());
      while (iter.hasNext()) {
        SAMRecord read = iter.next();
        if (!read.getReadUnmappedFlag()) {
          for (int i=0; i<thresholds.length; i++) {
            if (read.getMappingQuality() <= thresholds[i]) {
              counts[i] += 1;
            }
          }
        }
      }
     
      StringBuffer output = new StringBuffer();
      output.append(region.getDescriptor());
     
      for (int count : counts) {
        output.append('\t');
        output.append(count);
      }
     
      System.out.println(output.toString());
      reader.close();
    }
  }
View Full Code Here

    private SAMFileReader inputSam;
   
    public SimpleSamReadPairReader(String filename) {
        File inputFile = new File(filename);
       
        inputSam = new SAMFileReader(inputFile);
        inputSam.setValidationStringency(ValidationStringency.SILENT);
 
        iter = inputSam.iterator();
    }
View Full Code Here

import net.sf.samtools.SAMRecord;

public class CalcReadMovement {

  public static void getReadDistance(String file) {
    SAMFileReader reader = new SAMFileReader(new File(file));
    reader.setValidationStringency(ValidationStringency.SILENT);
   
    int numReads = 0;
    for (SAMRecord read : reader) {
      numReads++;
      String yo = (String) read.getAttribute("YO");
View Full Code Here

*/
public class FixEditDistance {

  //TODO: Move to utils package
  public void fix(String input, String output, String reference) throws IOException {
    SAMFileReader reader = new SAMFileReader(new File(input));
    reader.setValidationStringency(ValidationStringency.SILENT);
   
    SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(
        reader.getFileHeader(), true, new File(output));
   
    CompareToReference c2r = new CompareToReference();
    c2r.init(reference);
   
    int i=0;
   
    for (SAMRecord read : reader) {
      int numMismatches = c2r.numMismatches(read);
      int numIndelBases = getNumIndelBases(read);
      int editDistance = numMismatches + numIndelBases;
     
      read.setAttribute("NM", editDistance);
     
      writer.addAlignment(read);
     
      if ((i % 1000000) == 0) {
        System.out.println("Processed: " + i + " reads.");
      }
      i++;
    }
   
    writer.close();
    reader.close();
   
    System.out.println("Done.");
  }
View Full Code Here

 
  private void copySam(String input, String output) {
   
    SAMFileWriterFactory writerFactory = new SAMFileWriterFactory();
   
    SAMFileReader reader = new SAMFileReader(new File(input));
    reader.setValidationStringency(ValidationStringency.SILENT);
   
    SAMFileWriter writer = writerFactory.makeSAMOrBAMWriter(
        reader.getFileHeader(), false, new File(output));
   
    for (SAMRecord read : reader) {
      writer.addAlignment(read);
    }
   
    reader.close();
    writer.close();
  }
View Full Code Here

      thread.join();
    }
  }
 
  private void discardMisalignedContigs(String inputSam, String outputSam) {
    SAMFileReader reader = new SAMFileReader(new File(inputSam));
    reader.setValidationStringency(ValidationStringency.SILENT);
   
    SAMFileWriter outputReadsBam = new SAMFileWriterFactory().makeSAMOrBAMWriter(
        samHeaders[0], true, new File(outputSam));

    for (SAMRecord contig : reader) {
      String[] fields = contig.getReadName().split("_");
     
      String regionChromosome = "";
     
      // Loop through fields in case the chromosome name contains
      // an underscore.
      for (int i=0; i<fields.length-3; i++) {
        regionChromosome += fields[i];
        if (i+1 < fields.length-3) {
          regionChromosome += "_";
        }
      }
     
      int regionStart = Integer.parseInt(fields[fields.length-3]) - 1000;
      int regionStop = Integer.parseInt(fields[fields.length-2]) + 1000;
           
      if ((contig.getReferenceName().equals(regionChromosome)) &&
        (contig.getAlignmentStart() >= regionStart) &&
        (contig.getAlignmentEnd() <= regionStop)) {
     
//        // Remove XP tags and other attributes, the semi-colons interfere with downstream processing
//        contig.clearAttributes();
//        contig.setAttribute("XP", null);
       
        outputReadsBam.addAlignment(contig);
      } else {
        System.out.println("Discarding: " + contig);
      }
    }
   
    outputReadsBam.close();
    reader.close();
  }
View Full Code Here

TOP

Related Classes of net.sf.samtools.SAMFileReader$AssertableIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.