You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "John Wagenleitner (JIRA)" <ji...@apache.org> on 2017/05/17 15:40:04 UTC
[jira] [Resolved] (GROOVY-4851) NullPointerException on iterator()
call
[ https://issues.apache.org/jira/browse/GROOVY-4851?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
John Wagenleitner resolved GROOVY-4851.
---------------------------------------
Resolution: Fixed
Assignee: John Wagenleitner
Fix Version/s: (was: 3.0.0-alpha-1)
2.5.0-beta-1
Thanks Michal for reporting the issue and thanks to Jochen K. for confirming the fix.
> NullPointerException on iterator() call
> ---------------------------------------
>
> Key: GROOVY-4851
> URL: https://issues.apache.org/jira/browse/GROOVY-4851
> Project: Groovy
> Issue Type: Bug
> Components: groovy-runtime
> Affects Versions: 1.7.10, 1.8-rc-3, 1.8-rc-4, 1.8.0
> Environment: Linux Mint 10
> Reporter: Michal
> Assignee: John Wagenleitner
> Fix For: 2.5.0-beta-1
>
> Attachments: ex1.bam, ex1.bam.bai, GROOVY-4851.patch
>
>
> I got with the following code which use http://sourceforge.net/projects/picard/files/picard-tools/
> {code}
> import net.sf.picard.util.Interval
> import net.sf.picard.util.IntervalList
> import net.sf.picard.util.SamLocusIterator
> import net.sf.picard.util.SamLocusIterator.RecordAndOffset
> import net.sf.samtools.SAMFileHeader
> import net.sf.samtools.SAMFileReader
> import net.sf.samtools.SAMFileReader.ValidationStringency
> bamFile = new File("/media/trx/workspace/Picard/test/ex1.bam")
> baiFile = new File(bamFile.getAbsolutePath() + ".bai")
> sam = new SAMFileReader(bamFile, baiFile, true)
> sam.setValidationStringency(ValidationStringency.SILENT)
> if(!sam.hasIndex()) throw new Exception("Missing index")
> SAMFileHeader header = sam.getFileHeader()
> sequences = header.getSequenceDictionary().getSequences()
> for ( i in sequences) {
> println("${i.getSequenceName()} = ${i.getSequenceLength()}")
> }
> IntervalList il = new IntervalList(header)
> il.add(new Interval("chr1",0, 157))
> final SamLocusIterator sli = new SamLocusIterator(sam, il, true)
> sli.setEmitUncoveredLoci(false)
> sli.each { li ->
> println li.getSequenceName()
> }
> {code}
> I got the following error:
> {code}
> chr1 = 1575
> chr2 = 1584
> WARNING 2011-04-30 12:10:29 SamLocusIterator SamLocusIterator constructed with samReader that has SortOrder == unsorted. Assuming SAM is coordinate sorted, but exceptions may occur if it is not.
> Exception in thread "main" java.lang.NullPointerException
> at net.sf.picard.util.SamLocusIterator.samHasMore(SamLocusIterator.java:223)
> at net.sf.picard.util.SamLocusIterator.hasNext(SamLocusIterator.java:231)
> at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1219)
> at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1196)
> at org.codehaus.groovy.runtime.dgm$110.invoke(Unknown Source)
> at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:270)
> at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
> at pileup.run(pileup.groovy:33)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:616)
> at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
> at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
> at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:793)
> at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:776)
> at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:394)
> at org.codehaus.groovy.runtime.InvokerHelper$runScript.call(Unknown Source)
> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
> at pileup.main(pileup.groovy)
> {code}
> The following code is written completely in Java and uses the same input file without any error:
> {code}
> import java.io.File;
> import java.util.List;
> import net.sf.picard.util.Interval;
> import net.sf.picard.util.IntervalList;
> import net.sf.picard.util.SamLocusIterator;
> import net.sf.picard.util.SamLocusIterator.LocusInfo;
> import net.sf.picard.util.SamLocusIterator.RecordAndOffset;
> import net.sf.samtools.SAMFileHeader;
> import net.sf.samtools.SAMFileReader;
> import net.sf.samtools.SAMRecord;
> import net.sf.samtools.SAMRecordIterator;
> import net.sf.samtools.SAMSequenceRecord;
> import net.sf.samtools.SAMFileReader.ValidationStringency;
> public class ReadReferenceName {
> /**
> * @param args
> * @throws Exception
> */
> public static void main(String[] args) throws Exception {
> // TODO Auto-generated method stub
>
> File bamFile = new File("/media/trx/workspace/Picard/test/ex1.bam");
> File baiFile = new File(bamFile.getAbsolutePath() + ".bai");
>
> SAMFileReader sam = new SAMFileReader(bamFile, baiFile, true);
> sam.setValidationStringency(ValidationStringency.SILENT);
> if(!sam.hasIndex()) throw new Exception("Missing index");
>
> SAMFileHeader header = sam.getFileHeader();
> List<SAMSequenceRecord> sequences = header.getSequenceDictionary().getSequences();
>
> for(SAMSequenceRecord rec: sequences) {
> System.out.println(rec.getSequenceName() + " = " +
> rec.getSequenceLength());
> }
>
> IntervalList il = new IntervalList(header);
> il.add(new Interval("chr1",0, 157));
> final SamLocusIterator sli = new SamLocusIterator(sam, il, true);
> sli.setEmitUncoveredLoci(false);
>
> for (final SamLocusIterator.LocusInfo li : sli) {
> String sequenceName = li.getSequenceName();
> int pos = li.getPosition();
> int coverage = li.getRecordAndPositions().size();
> System.out.println(sequenceName + " coverage at base " + pos + " = "
> + coverage);
> for (int i = 0; i< coverage; i++){
> RecordAndOffset rec = li.getRecordAndPositions().get(i);
> char base = (char)rec.getReadBase();
> String readName = rec.getRecord().getReadName();
> System.out.println(" base in read " + readName + " = " + base);
> }
> }
> }
> }
> {code}
> and it produced this output:
> {code}
> chr1 = 1575
> chr2 = 1584
> WARNING 2011-04-30 19:30:07 SamLocusIterator SamLocusIterator constructed with samReader that has SortOrder == unsorted. Assuming SAM is coordinate sorted, but exceptions may occur if it is not.
> chr1 coverage at base 100 = 1
> base in read EAS56_57:6:190:289:82 = A
> chr1 coverage at base 101 = 1
> ...
> {code}
> Tim Yates wrote the following work around for this problem:
> {code}
> import net.sf.picard.util.*
> import net.sf.picard.util.SamLocusIterator.RecordAndOffset
> import net.sf.samtools.*
> import net.sf.samtools.SAMFileReader.ValidationStringency
> import net.sf.picard.filter.*
> import net.sf.samtools.util.CloseableIterator
> bamFile = new File("ex1.bam")
> baiFile = new File("ex1.bam.bai")
> sam = new SAMFileReader(bamFile, baiFile, true)
> sam.validationStringency = ValidationStringency.SILENT
> if(!sam.hasIndex()) throw new Exception("Missing index")
> SAMFileHeader header = sam.fileHeader
> sequences = header.sequenceDictionary.sequences
> sequences.each { i ->
> println "$i.sequenceName = $i.sequenceLength"
> }
> SamLocusIterator.metaClass.iterator = {
> if ( delegate.@samIterator != null ) {
> throw new IllegalStateException("Cannot call iterator() more than once on SamLocusIterator");
> }
> CloseableIterator<SAMRecord> tempIterator;
> if (delegate.@intervals != null) {
> tempIterator = new SamRecordIntervalIteratorFactory().makeSamRecordIntervalIterator(delegate.@samReader, delegate.@intervals, delegate.@useIndex);
> } else {
> tempIterator = delegate.@samReader.iterator();
> }
> if (delegate.@samFilters != null) {
> tempIterator = new FilteringIterator(tempIterator, new AggregateFilter(delegate.@samFilters));
> }
> delegate.@samIterator = new PeekableIterator<SAMRecord>( tempIterator )
> delegate
> }
> IntervalList il = new IntervalList( header )
> il.add( new Interval( "chr1", 0, 157 ) )
> SamLocusIterator sli = new SamLocusIterator( sam, il, true )
> sli.emitUncoveredLoci = false
> sli.each { li ->
> println "$li.sequenceName coverage at base $li.position = ${li.recordAndPositions.size()}"
> (0..<li.recordAndPositions.size()).each { i ->
> rec = li.recordAndPositions.get( i )
> println " base in read $rec.record.readName = $rec.readBase"
> }
> }
> {code}
> More information can be found here ( http://groovy.329449.n5.nabble.com/NullPointerException-problem-td4360426.html )
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)