File: nameSorted_SAM_to_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 (84 lines) | stat: -rwxr-xr-x 1,724 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env perl

use strict;
use warnings;

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

use SAM_reader;
use SAM_entry;
use Nuc_translator;

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


my $sam_file = $ARGV[0] or die $usage;
my $out_prefix = $ARGV[1] or die $usage;



main: {

    my $sam_reader = new SAM_reader($sam_file);
        
    my %fhs;
 
    while (my $sam_entry = $sam_reader->get_next()) {
        
        if ($sam_entry->is_query_unmapped()) {
            next;
        }

        my $outfh = &get_output_file($sam_entry, \%fhs);

        my $seq = $sam_entry->get_sequence();
        my $quals = $sam_entry->get_quality_scores();

        my $strand = $sam_entry->get_query_strand();
        if ($strand eq '-') {
            $seq = &reverse_complement($seq);
            $quals = join("", reverse(split(//, $quals)));
        }
        
        my $read_name = $sam_entry->reconstruct_full_read_name();

        print $outfh join("\n", "\@$read_name", $seq, "+", $quals) . "\n";
    }

    exit(0);
}
        
####
sub get_output_file {
    my ($sam_entry, $fhs_href) = @_;

    my $filename = "$out_prefix.single.fq";

    if ($sam_entry->is_paired()) {
        
        if ($sam_entry->is_first_in_pair()) {

            $filename = "$out_prefix.left.fq";
        }
        elsif ($sam_entry->is_second_in_pair()) {

            $filename = "$out_prefix.right.fq";
        }
        else {
            die "Error, read is paired but neither first or second in pair!";
        }
    }

    my $fh = $fhs_href->{$filename};

    unless ($fh) {
        
        open ($fh, ">$filename") or die "Error, cannot write to $filename";
        $fhs_href->{$filename} = $fh;
    }


    return($fh);
}