This is a test version of Biostars. For the public version, visit https://www.biostars.org.
linux command in a perl script problem (sam to fastq)

I'm trying to run this linux command which converts a sam file to fastq reads file and works on the command line to work from a perl script (the sam to fastq tool I found does not work in this instance hence why using the command line) but I think I'm having some issues getting the perl version to work:

cat A.sam | grep -v ^@ | awk '{print "@"$1"\n"$10"\n+\n"$11}' > A.fastq
system("cat $SAMdata[$i].sam | grep -v ^\@ | awk \'{print \"\@\"\$1\"\n\"\$10\"\n+\n\"\$11}\' > $SAMdata[$i].fastq ") == 0 or die "system convert sam to Fastq: $?";

I've been staring at this for a while and can't get it to work and tried a few things but I think I'm missing something obvious, so hopefully someone will see the mistake that I'm missing which I can correct?

perl

2 answers

instead of using grep+awk you could use awk only:

/^@/ { printf("@%s\n%s\n+\n%s\n",$1,$10,$11);}");}

why on earth calling awk inside perl?

sometimes when calling system, your software doesn't know the value of ${PATH}

don't you need to escape the '$' in '$1, $10, $11'

just corrected the escaping $ plus a few others but now got this error:

awk: {print "@"$1"
awk:             ^ unterminated string

I'll have a go a just awk.

worked it out, need to escape \n so \\n and for } instead \}

Thanks for the input it helped me to think

Or simply use Perl for everything:

cat A.sam | perl -lane 'print "\@$F[0]\n$F[9]\n+\n$F[10]" unless (/^@/)' > A.fastq

Log in to answer this question.