File: sort_fastq.pl

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (63 lines) | stat: -rwxr-xr-x 1,176 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env perl

use strict;
use warnings;

use lib ("/usr/lib/trinityrnaseq/PerlLib");
use Fastq_reader;

my $usage = "usage: $0 file.fastq\n\n";

my $fastq_file = $ARGV[0] or die $usage;

main: {

    my $fastq_reader = new Fastq_reader($fastq_file);

    my $tab_tmp_file = "$fastq_file.tab";

    open (my $ofh, ">$tab_tmp_file") or die $!;

    while (my $fq_entry = $fastq_reader->next()) {

        my $fq_record = $fq_entry->get_fastq_record();
        chomp $fq_record;
        
        my @lines = split(/\n/, $fq_record);
        
        print $ofh join("\t", @lines) . "\n";

    }
    close $ofh;

    ## sort by read name

    my $cmd = "sort -S 4G -T . -k1,1 $tab_tmp_file > $tab_tmp_file.sort";
    &process_cmd($cmd);
    
    unlink($tab_tmp_file);
    
    ## convert back to fastq file format

    $cmd = "cat $tab_tmp_file.sort | sed s/\\\\t/\\\\n/g > $fastq_file.sorted.fq";
    &process_cmd($cmd);

    unlink("$tab_tmp_file.sort");
    
    exit(0);
}

####
sub process_cmd {
    my ($cmd) = @_;

    print STDERR "CMD: $cmd\n";
    my $ret = system($cmd);

    if ($ret) {
        die "Error, cmd: $cmd died with ret $ret";
    }

    return;
}