forked from liulab-dfci/TRUST4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trust-smartseq.pl
231 lines (208 loc) · 5.19 KB
/
trust-smartseq.pl
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env perl
use strict ;
use warnings ;
use Cwd 'cwd' ;
use Cwd 'abs_path' ;
use File::Basename ;
die "TRUST4 SMART-seq pipeline usage: perl smartseq-process.pl [OPTIONS]:\n".
"\t-1 STRING: file containing the list of read 1 (or single-end) files\n".
"\t-2 STRING: file containing the list of read 2 files.\n".
"\t-f STRING: path to the fasta file coordinate and sequence of V/D/J/C genes\n".
"\t--ref STRING: path to detailed V/D/J/C gene reference file from IMGT database. (default: not used but recommended)\n".
"\t-o STRING: prefix of final output files. (default: TRUST)\n".
"\t-t INT: number of threads (default: 1)\n".
"\t--representative INT: number of representative for each detected chain (default: 1)\n"
#"\t--noclear: do not clear the intermediate results (default: clear)\n"
if (@ARGV == 0) ;
sub system_call
{
print STDERR "[".localtime()."] SYSTEM CALL: ".join(" ",@_)."\n";
system(@_) == 0
or die "system @_ failed: $?";
#print STDERR " finished\n";
}
sub GetPairChainType
{
foreach my $g (@_)
{
if ( $g =~ /^IGH/ )
{
return 0 ;
}
elsif ( $g =~ /^IGK/ )
{
return 1 ;
}
elsif ( $g =~ /^IGL/ )
{
return 1 ;
}
elsif ( $g =~ /^TRA/ )
{
return 2 ;
}
elsif ( $g =~ /^TRB/ )
{
return 3 ;
}
elsif ( $g =~ /^TRG/ )
{
return 4 ;
}
elsif ( $g =~ /^TRD/ )
{
return 5 ;
}
}
}
my $WD = dirname( abs_path( $0 ) ) ;
my $i ;
my $readFile1 = "" ;
my $readFile2 = "" ;
my $outputPrefix = "TRUST" ;
my $hasMate = 0 ;
my $trust4Args = "" ;
my $representativeN = 1 ;
for ( $i = 0 ; $i < @ARGV ; ++$i )
{
if ($ARGV[$i] eq "-1")
{
$readFile1 = $ARGV[$i + 1] ;
++$i ;
}
elsif ($ARGV[$i] eq "-2")
{
$readFile2 = $ARGV[$i + 1] ;
++$i ;
$hasMate = 1 ;
}
elsif ($ARGV[$i] eq "-o")
{
$outputPrefix = $ARGV[$i + 1] ;
++$i ;
}
elsif ($ARGV[$i] eq "-t" || $ARGV[$i] eq "-f" || $ARGV[$i] eq "--ref")
{
$trust4Args .= " ".$ARGV[$i]." ".$ARGV[$i + 1] ;
++$i ;
}
elsif ($ARGV[$i] eq "--representative")
{
$representativeN = $ARGV[$i + 1] ;
++$i ;
}
else
{
die "Unknown parameter ".$ARGV[$i]."\n" ;
}
}
die "Need to use -1 to specify the list of read 1 files.\n" if ($readFile1 eq "") ;
# Process the fastq file for each cell one by one
open FP1, $readFile1 ;
my $FP2 ;
open $FP2, $readFile2 if ($hasMate) ;
open FPfinalreport, ">${outputPrefix}_report.tsv" ;
open FPfinalannot, ">${outputPrefix}_annot.fa" ;
print FPfinalreport "#count\tfrequency\tCDR3nt\tCDR3aa\tV\tD\tJ\tC\tcid\tcid_full_length\n" ;
while (<FP1>)
{
chomp ;
my $file1 = $_ ;
my $file2 = "" ;
my $fname = (fileparse($file1))[0] ;
my $cellPrefix = (split /\./, $fname)[0] ; # use the content before the first "." as identifier.
if ($hasMate)
{
$file2 = <$FP2> ;
chomp $file2 ;
system("$WD/run-trust4 $trust4Args -1 $file1 -2 $file2 -o tmp_smartseq --skipMateExtension") ;
}
else
{
system("$WD/run-trust4 $trust4Args -u $file1 -o tmp_smartseq") ;
}
# Summary the output and extracting the assemblies for representative chains.
open FPreport, "tmp_smartseq_report.tsv" ;
my $header = <FPreport> ;
my $line = <FPreport> ;
if (!$line)
{
print STDERR "WARNING: no assemblies from $cellPrefix.\n" ;
next ;
}
chomp $line ;
my $report1 = $line ;
my @cols = split /\t/, $line ;
my $mainChainType = GetPairChainType($cols[4], $cols[6], $cols[7]) ;
my $report2 = "" ;
my @representativeCols ;
@{$representativeCols[0]} = @cols ;
my $representativeCols1Cnt = 1 ;
my $representativeCols2Cnt = 0 ;
my $representativeCnt = 1 ;
# Go throught the list to find the second representative.
while (<FPreport>)
{
chomp ;
$line = $_ ;
@cols = split /\t/, $line ;
my $chainType = GetPairChainType($cols[4], $cols[6], $cols[7]) ;
my $add = 0 ;
if ($chainType == $mainChainType)
{
if ($representativeCols1Cnt < $representativeN)
{
$add = 1 ;
++$representativeCols1Cnt ;
}
}
elsif ( int($chainType/2) == int($mainChainType/2) && $chainType%2 == 1 - ($mainChainType%2))
{
if ($representativeCols2Cnt < $representativeN)
{
$add = 1 ;
++$representativeCols2Cnt ;
}
}
if ($add == 1)
{
@{$representativeCols[$representativeCnt]} = @cols ;
++$representativeCnt ;
}
last if ($representativeCols1Cnt >= $representativeN && $representativeCols2Cnt >= $representativeN) ;
}
# Output the representative information
my %selectedContigs ;
for ($i = 0 ; $i < $representativeCols1Cnt + $representativeCols2Cnt ; ++$i)
{
my @cols = @{$representativeCols[$i]} ;
my $contigId = $cols[8] ;
$cols[8] = $cellPrefix."_".$contigId ;
$selectedContigs{$contigId} = 1 ;
print FPfinalreport join("\t", @cols), "\n" ;
}
close FPreport ;
# Process the annotation file for the assemblies
open FPannot, "tmp_smartseq_annot.fa" ;
while (<FPannot>)
{
chomp ;
my $header = $_ ;
my $seq = <FPannot> ;
chomp $seq ;
my @cols = split / /, $header ;
my $contigId = substr($cols[0], 1) ;
if (defined $selectedContigs{$contigId})
{
$cols[0] = ">$cellPrefix"."_".$contigId ;
print FPfinalannot join(" ", @cols), "\n$seq\n" ;
}
}
close FPannot ;
# remove the temporary files.
system("rm ./tmp_smartseq_*") ;
}
close FP1 ;
close $FP2 if ($hasMate) ;
close FPfinalreport ;
close FPfinalannot ;