I'm executing this command in two different folders and in two different terminals:

for i in *_RG.bam; do k=`echo $i | sed  "s/.bam/_Reordered.bam/"` java -jar /home/Programas/picard-tools-1.107/ReorderSam.jar R=/local/Referencias/Homo_sapiens/UCSC/hg19/Sequence/Bowtie2Index/genome.fa  I= $i O= "$k" ; done

In one terminal it works ok, but on the other terminal the code is not working at all. It gives the next error.

To get help, see http://picard.sourceforge.net/index.shtml#GettingHelp
Exception in thread "main" net.sf.samtools.util.RuntimeIOException: File not found: 
    at net.sf.samtools.util.BinaryCodec.<init>(BinaryCodec.java:102)
    at net.sf.samtools.util.BlockCompressedOutputStream.<init>(BlockCompressedOutputStream.java:127)
    at net.sf.samtools.BAMFileWriter.<init>(BAMFileWriter.java:50)
    at net.sf.samtools.SAMFileWriterFactory.makeBAMWriter(SAMFileWriterFactory.java:154)
    at net.sf.samtools.SAMFileWriterFactory.makeBAMWriter(SAMFileWriterFactory.java:136)
    at net.sf.samtools.SAMFileWriterFactory.makeSAMOrBAMWriter(SAMFileWriterFactory.java:246)
    at net.sf.picard.sam.ReorderSam.doWork(ReorderSam.java:118)
    at net.sf.picard.cmdline.CommandLineProgram.instanceMain(CommandLineProgram.java:179)
    at net.sf.picard.cmdline.CommandLineProgram.instanceMainWithExit(CommandLineProgram.java:120)
    at net.sf.picard.sam.ReorderSam.main(ReorderSam.java:77)
Caused by: java.io.FileNotFoundException:  (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
    at net.sf.samtools.util.BinaryCodec.<init>(BinaryCodec.java:95)
    ... 9 more

When I call the program as java -jar /home/ktroule/Programas/picard-tools-1.107/ReorderSam.jar -h in a new terminal, it prints the help as I would expect.

After this I stopped using ctrl+c the terminal that was working properly and I exchanged the terminals to prove if the problem was related to the files or to the terminal. And the same happened, only one terminal worked (the same that was working previously). I closed the terminal that wasn't working and opened a new one, but only on the original one the code works.

I have also compared on the terminal that works and the one that doesn't the output of printenv by usinf diff and both are the same excepet for the WINDOWID line.

Any guess on the problem?

I'm not closing the terminal that runs the code as I need it and I'm afraid of not being able to run the code.

  • not a java expert here but File not found and (No such file or directory) messages are telling me either you are in the wrong directory for this thing to run, or your running program is deleting some necessary files (sort of a file locking mechanism) and preventing your second instance from running. You may want to ask this in stackoverflow, as it is more coding than unix related. – MelBurslan Feb 10 '16 at 15:31
  • Thanks, I'll ask in stackoverflow. But as I said in the original post. The code and program works, but just in one terminal. I have executed several times de code on the original terminal and it works. – HeyHoLetsGo Feb 10 '16 at 15:34
  • @biorunner88: You're missing a ; after k=... You would likely also want to quote I= $i as in I="$i" – Runium Feb 10 '16 at 15:44
  • It worked well without the ;. Now for some reason is working properly. What happens if I just do I=$i, it always worked correctly. I'll add the ; after the k. – HeyHoLetsGo Feb 10 '16 at 15:54

Missing semicolon after assignment to k, k=...

In your first terminal you likely did some testing before hand such as:

k=`echo blah_RG.bam | sed  "s/.bam/_Reordered.bam/"`
echo $k

Now k is set in your shell.

When you then expand it to include the Java processing the output file would be that $k.

Then you open a new Terminal. Here $k is not set. Then you try to run the same command, but, as $k is not set the Java program complains about out file not found. O= "$k" would be O= ""

This also match with the error messages that mention various ...Writer and FileOutput... as opposed to ...Reader and FileInput.... It simply can't open a file with no / empty name.

As for comparing printenv or env note that that command does not include shell variables. Have a look at e.g. (set -o posix; set)

Simple sample:

$ touch 1.foo 2.foo 3.foo
$ for i in *.foo; do k="$(echo $i | sed 's/.foo/.bar/')"; done

# Now k is set to last *.foo

$ for i in *.foo; do k="$(echo $i | sed 's/.foo/.bar/')" printf "i=%s k=%s\n" "$i" "$k"; done
i=1.foo k=3.bar
i=2.foo k=3.bar
i=3.foo k=3.bar

Then try with ; after assignment to $k:

$ for i in *.foo; do k="$(echo $i | sed 's/.foo/.bar/')"; printf "i=%s k=%s\n" "$i" "$k"; done
i=1.foo k=1.bar
i=2.foo k=2.bar
i=3.foo k=3.bar

You could change it to something like:

for fn in *_RG.bam
    printf "Processing: %s\n" "$fn"

    java -jar /home/Programas/picard-tools-1.107/ReorderSam.jar \
    R=/local/Referencias/Homo_sapiens/UCSC/hg19/Sequence/Bowtie2Index/genome.fa \
    I="$fn" O="${fn%.bam}_Reordered.bam"

Note: here as multi-line and with the use of bash substitution:

    | |  |    |
    | |  |    +--------- Append _Reordered.bam
    | |  +-------------- Remove .bam
    | +----------------- Remove substring from end
    +------------------- Variable name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.