samtools / htsjdk

A Java API for high-throughput sequencing data (HTS) formats.
http://samtools.github.io/htsjdk/
283 stars 242 forks source link

SamReader's close method doesn't seem to work #1591

Open wangyuxiang985 opened 2 years ago

wangyuxiang985 commented 2 years ago

Description

  1. When I read the corresponding BAM file through Sam Reader, I tried to delete the folder and found it failed. The corresponding index file bam. Bai is always occupied until AFTER I gc
  2. The BAI is still occupied after the flow is closed

Environment:

Steps to reproduce

  1. The specified BAM and BAI exist in the destination directory
  2. The test code is as follows
    
    import htsjdk.samtools.SAMRecordIterator;
    import htsjdk.samtools.SamReader;
    import htsjdk.samtools.SamReaderFactory;
    import htsjdk.samtools.util.CloserUtil;
    import htsjdk.samtools.util.IOUtil;

import java.io.File; import java.io.IOException; import java.util.UUID;

public class MinTestRemove { public static void main(String[] args) { String baseBamPath = "D:\test_closebam\0001"; String targetBamPath01 = "D:\bam\0001" + UUID.randomUUID().toString(); String targetBamPath02 = "D:\bam\0001" + UUID.randomUUID().toString(); String targetBamPath03 = "D:\bam\0001" + UUID.randomUUID().toString(); System.out.println("-------callGcRemove--------"); callGcRemove(baseBamPath,targetBamPath01); System.out.println("-------notCallGcRemove--------"); notCallGcRemove(baseBamPath, targetBamPath02);

    System.out.println("===========close===========");
    afterCloseOperation(baseBamPath, targetBamPath03);
}

private static void callGcRemove(String baseBamPath, String targetBamPath) {

    String finalBamPath = targetBamPath + File.separator + "aln" + File.separator + "page" + File.separator + "12-B28-20200221_S12" + File.separator + "12-B28-20200221_S12.clean.virus.page0.sorted.bam";

    IOUtil.copyDirectoryTree(new File(baseBamPath), new File(targetBamPath));
    System.out.println("INFO: **callGcRemove** copy success path: " + targetBamPath);
    testReadSam(finalBamPath);
    System.gc();
    System.out.println("**callGcRemove** call the gc()");
    boolean b = IOUtil.deleteDirectoryTree(new File(targetBamPath));
    System.out.println("**callGcRemove** delete result:\t" + b);
}

private static void notCallGcRemove(String baseBamPath, String targetBamPath) {

    String finalBamPath = targetBamPath + File.separator + "aln" + File.separator + "page" + File.separator + "12-B28-20200221_S12" + File.separator + "12-B28-20200221_S12.clean.virus.page0.sorted.bam";

    IOUtil.copyDirectoryTree(new File(baseBamPath), new File(targetBamPath));
    System.out.println("INFO: ##notCallGcRemove## copy success path: " + targetBamPath);
    testReadSam(finalBamPath);
    boolean b = IOUtil.deleteDirectoryTree(new File(targetBamPath));
    System.out.println("##notCallGcRemove## delete result:\t" + b);
}

private static void afterCloseOperation(String baseBamPath, String targetBamPath) {
    String finalBamPath = targetBamPath + File.separator + "aln" + File.separator + "page" + File.separator + "12-B28-20200221_S12" + File.separator + "12-B28-20200221_S12.clean.virus.page0.sorted.bam";

    IOUtil.copyDirectoryTree(new File(baseBamPath), new File(targetBamPath));
    System.out.println("INFO: **afterCloseOperation** copy success path: " + targetBamPath);

    File file = new File(finalBamPath);
    SamReader reader = null;
    SAMRecordIterator iterator = null;
    try{
        reader = SamReaderFactory.makeDefault().open(file);
        iterator = reader.query("SARS-COV-2", 0, 40000, true);
        System.out.println("over");
    } finally {
        CloserUtil.close(iterator);
        CloserUtil.close(reader);
        try {
            System.out.println("after close iterator:\t" + iterator.hasNext());
        } catch (Throwable e) {
            System.out.println("Error: iterator\t" + e.getMessage());
        }
        try {
            System.out.println("after close reader:\t" + reader.getResourceDescription());
        } catch (Throwable e) {
            System.out.println("ERROR: reader\t" + e.getMessage());
        }
    }
}

private static void testReadSam(String filePath) {
    File file = new File(filePath);
    try (SamReader reader = SamReaderFactory.makeDefault().open(file);
         SAMRecordIterator iterator = reader.query("SARS-COV-2", 0, 40000, true)) {
        System.out.println("over");
    } catch (IOException ie) {
        ie.printStackTrace();
    }
}

}



### Expected behaviour
Specified folders can be deleted without invoking GC

### Actual behaviour
![image](https://user-images.githubusercontent.com/42695753/149311123-257b510d-4919-4758-bd91-ecf1fff5375a.png)