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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
#! @PERL@
# $Id: gmap_process.pl.in,v 1.8 2010-07-21 22:02:18 twu Exp $
use warnings;
use IO::File;
use Getopt::Std;
undef($opt_c); # coord file
undef($opt_g); # gunzip each file
undef($opt_f); # File with input FASTA source filenames
undef($opt_Q); # FASTQ file
getopts("c:gf:Q");
if (!defined($coord_file = $opt_c)) {
if (-e "coords.txt") {
$coord_file = "coords.txt";
} else {
die "Must specify coordinate file (created by md_coords or fa_coords) with -c flag."
}
}
read_coords($coord_file);
if (defined($opt_f)) {
# Source files given in a file
@sourcefiles = ();
$SOURCES = new IO::File($opt_f) or die "Cannot open file $opt_f";
while (defined($line = <$SOURCES>)) {
chop $line;
push @sourcefiles,$line;
}
close($SOURCES);
if (defined($opt_Q)) {
process_fastq(\@sourcefiles);
} else {
process_fasta(\@sourcefiles);
}
} elsif ($#ARGV < 0) {
# FASTA is piped via stdin
@streams = ();
push @streams,"<&STDIN";
if (defined($opt_Q)) {
process_fastq(\@streams);
} else {
process_fasta(\@streams);
}
} else {
# Source files given on command line
if (defined($opt_Q)) {
process_fastq(\@ARGV);
} else {
process_fasta(\@ARGV);
}
}
check_processed();
exit;
sub read_coords {
my ($coord_file) = @_;
my $universal_coord = 0;
$FP = new IO::File($coord_file) or die "Cannot open coord file $coord_file";
print STDERR "Reading coordinates from file $coord_file\n";
while (defined($line = <$FP>)) {
if ($line =~ /^#/) {
#if ($line =~ /Reference strain:\s*(\S+)/) {
#$refstrain = $1;
#}
} else {
$line =~ s/\r\n/\n/;
chop $line;
@fields = split /\s+/,$line;
$contig = $fields[0];
#if (!defined($strain{$contig} = $fields[2])) {
#$strain{$contig} = "";
#} elsif (defined($refstrain) && $strain{$contig} eq $refstrain) {
#$strain{$contig} = "";
#}
$coords{$contig} = $fields[1];
$universal_coord{$contig} = $universal_coord;
($chrstart,$chrend) = $coords{$contig} =~ /\S+:(\d+)\.\.(\d+)/;
if (!defined($fields[2])) {
$universal_coord += $chrend - $chrstart + 1;
} elsif ($fields[2] eq "circular") {
$circularp{$contig} = 1;
$universal_coord += $chrend - $chrstart + 1;
$universal_coord += $chrend - $chrstart + 1;
} elsif ($fields[2] eq "primary") {
# Linear and primary
$universal_coord += $chrend - $chrstart + 1;
} else {
# Altloc
$primary{$contig} = $fields[2];
$universal_coord += $chrend - $chrstart + 1;
}
}
}
close($FP);
return;
}
sub find_contig_name {
my ($contiginfo, $coords) = @_;
if ($contiginfo !~ /\|/) {
if (defined($ {$coords}{$contiginfo})) {
return $contiginfo;
} elsif ($contiginfo =~ /(\S+)/ && defined($ {$coords}{$1})) {
return $1;
} else {
# Failed
return $contiginfo;
}
} else {
# Old code for FASTA files that contained pipe delimiters
@parts = split /\|/,$contiginfo;
foreach $part (@parts) {
if (defined($ {$coords}{$part})) {
return $part;
} elsif ($part =~ /(\S+)/ && defined($ {$coords}{$1})) {
return $1;
}
}
# Failed
return $contiginfo;
}
}
sub process_fasta {
my ($argv) = @_;
my $printp = 0;
foreach $arg (@ {$argv}) {
if (defined($opt_g)) {
$FP = new IO::File("gunzip -c \"$arg\" |") or die "Cannot open file $arg";
} else {
$FP = new IO::File($arg) or die "Cannot open file $arg";
}
while (defined($line = <$FP>)) {
$line =~ s/\r\n/\n/;
chomp $line;
if ($line !~ /\S/) {
# Skip blank lines
} elsif ($line =~ /^>(\S+)/) {
# $contig = find_contig_name($1,\%coords);
$contig = $1;
if (!defined($coords{$contig})) {
print STDERR "No coordinates defined for contig $contig. Skipping.\n";
$printp = 0;
} else {
# ($chr,$chrpos1,$chrpos2) = $coords{$contig} =~ /(\S+):(\d+)\D+(\d+)/;
# If $chrpos2 < $chrpos1, then contig needs to be reverse complement.
# However, gmapindex knows how to handle this
$processedp{$contig} = 1;
#printf (">%s\t%s\t%s\n",$contig,$coords{$contig},$strain{$contig});
printf (">%s\t%s\t%u",$contig,$coords{$contig},$universal_coord{$contig});
if (defined($circularp{$contig})) {
print "\tcircular";
} elsif (defined($primary{$contig})) {
print "\t" . $primary{$contig};
}
print "\n";
$printp = 1;
}
} elsif ($printp == 1) {
print $line . "\n";
}
}
close($FP);
}
return;
}
sub process_fastq {
my ($argv) = @_;
foreach $arg (@ {$argv}) {
if (defined($opt_g)) {
$FP = new IO::File("gunzip -c \"$arg\" |") or die "Cannot open file $arg";
} else {
$FP = new IO::File($arg) or die "Cannot open file $arg";
}
while (defined($header = <$FP>) && defined($line = <$FP>)) {
$line =~ s/\r\n/\n/;
chomp $line;
if ($header =~ /^\+/) {
# Skip quality string
} elsif ($header !~ /^@(\S+)/) {
die "Expected FASTQ line to start with @";
} else {
# $contig = find_contig_name($1,\%coords);
$contig = $1;
if (!defined($coords{$contig})) {
print STDERR "No coordinates defined for contig $contig. Skipping.\n";
} else {
# ($chr,$chrpos1,$chrpos2) = $coords{$contig} =~ /(\S+):(\d+)\D+(\d+)/;
# If $chrpos2 < $chrpos1, then contig needs to be reverse complement.
# However, gmapindex knows how to handle this
$processedp{$contig} = 1;
#printf (">%s\t%s\t%s\n",$contig,$coords{$contig},$strain{$contig});
$printed_contig = $contig;
# $printed_contig =~ s/:/_/g;
printf (">%s\t%s\t%u",$printed_contig,$coords{$contig},$universal_coord{$contig});
print "\n";
print $line . "\n";
}
}
}
close($FP);
}
return;
}
sub check_processed {
$nwarnings = 0;
foreach $contig (keys %coords) {
if (!defined($processedp{$contig})) {
print STDERR "Warning!! Contig $contig was listed in coords file, but not seen in a FASTA file\n";
$nwarnings++;
}
}
if ($nwarnings > 0) {
print STDERR "Warning: A total of $nwarnings contigs were listed in coords file, but not seen in a FASTA file\n";
print STDERR "Should provide the correct FASTA files, or comment out these contigs in the coords file $coord_file\n";
}
return;
}
|