-
Notifications
You must be signed in to change notification settings - Fork 5
/
extract-contig.pl
72 lines (63 loc) · 1.39 KB
/
extract-contig.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
#!/usr/bin/perl
use strict;
use warnings;
# Input parameters
my $scaffold_file = $ARGV[2];
my $type = $ARGV[0];
my $query = $ARGV[1];
# Save wanted fasta headers
my %list=();
if ($type eq "list") {
open(IN, $query);
while(<IN>) {
chomp($_);
$_=~s/>//;
$list{$_} = 1;
}
}
elsif($type eq "single") {
$query=~s/>//;
$list{$query}=1;
}
else {
die &usage();
}
#Go through fasta file, extract sequences
open(IN, $scaffold_file);
my $seq = "";
my $flag = "off";
while(<IN>) {
if($_ =~ m/^>/) {
my $head = $_;
chomp($head);
$head=~s/>//;
if(defined $list{$head}) {
print $_;
$flag = "on";
}
else {
if($type eq "single" && $flag eq "on") {
exit;
}
$flag = "off";
}
}
else {
if($flag eq "on") {
print $_;
}
}
}
sub usage {
print << "A";
\nextract-contig.pl
===============================================================================================
Extracts contigs from a fasta file. Either a single sequence name is given, or a text file with
a list of names (type must be given as input, either single contig or a list of desired contigs
================================================================================================
Usage: extract-contig.pl single|list contig_name|list SEQ.fasta > NEW-FILE.fasta
\nExample to extract a contig named contig1 from file SEQ.fasta: extract-contig.pl single contig1 SEQ.fasta > contig1.fasta
\n
A
exit;
}