-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_agpLike_to_fasta.pl
137 lines (120 loc) · 2.87 KB
/
parse_agpLike_to_fasta.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
## parse_agpLike_to_fasta.pl
#!/usr/bin/perl -w
#script to parse super-basic AGP-like files to chromosome FASTA files
#extensive usage of BioPerl
use warnings;
use strict;
use Bio::SeqIO;
use Bio::Seq;
use Bio::Index::Fasta;
#Chr1 scaffold118 -
#Chr1 gap 13000
my $agp = $ARGV[0];
my $contigs = $ARGV[1];
my $out = $ARGV[2];
my $prev_chr = 0;
unless (-e "$contigs.inx")
{
my $fasta_index = Bio::Index::Fasta->new(
-filename => "$contigs.inx",
-write_flag => 1 );
$fasta_index->make_index($contigs);
}
my $index = Bio::Index::Fasta->new("$contigs.inx");
open (OUT, ">$out");
open (FILE, $agp)
or die "impossible to open file $agp for reading !";
while (my $line = <FILE>)
{
chomp $line;
next if ($line =~ /^#/);
my ($chr, $type, $info) = split("\t", $line);
if ($chr ne $prev_chr)
{
print "\n";
print join ("\t", $chr, $type, $info),"\n";
print OUT "\n",">",$chr,"\n";
if ($type eq "gap")
{
print "writing a gap of length $info","\n";
my $string;
my $N = "N";
for (my $i = 0; $i < $info; $i++)
{
$string = $string . $N;
}
$string =~ s/(.{1,60})/$1\n/gs;
print OUT $string;
}
elsif ($type =~ /scaffold/)
{
my $seq = $index->fetch($type);
print $seq->display_id,"\n";
print "new or first chromosome, printing first scaffold $type","\n";
if ($info eq "+")
{
print "positive strand sequence $type","\n";
my $print_seq = $seq->seq;
$print_seq =~ s/(.{1,60})/$1\n/gs;
print OUT $print_seq;
}
elsif ($info eq "-")
{
print "reverse complementing sequence $type","\n";
my $seq_rev = $seq->revcom;
my $print_seq = $seq_rev->seq;
$print_seq =~ s/(.{1,60})/$1\n/gs;
print OUT $print_seq;
}
else
{
print "problem?","\n";
}
}
$prev_chr = $chr;
}
else
{
print join ("\t", $chr, $type, $info),"\n";
if ($type eq "gap")
{
print "writing a gap of length $info","\n";
my $string;
my $N = "N";
for (my $i = 0; $i < $info; $i++)
{
$string = $string . $N;
}
$string =~ s/(.{1,60})/$1\n/gs;
print OUT $string;
}
elsif ($type =~ /scaffold/)
{
my $seq = $index->fetch($type);
print $seq->display_id,"\n";
print "same chromosome, printing next scaffold $type","\n";
if ($info eq "+")
{
print "positive strand sequence $type","\n";
my $print_seq = $seq->seq;
$print_seq =~ s/(.{1,60})/$1\n/gs;
print OUT $print_seq;
}
elsif ($info eq "-")
{
print "reverse complementing sequence $type","\n";
my $seq_rev = $seq->revcom;
my $print_seq = $seq_rev->seq;
$print_seq =~ s/(.{1,60})/$1\n/gs;
print OUT $print_seq;
}
else
{
print "problem?","\n";
}
}
}
}
close FILE;
close OUT;
exit;