-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone_line_fasta.pl
45 lines (37 loc) · 1.2 KB
/
one_line_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
#!/usr/bin/perl
use strict;
use warnings;
open (INP_FASTA, "<$ARGV[0]") or die "Cannot open $ARGV[0] file: $!";
## Open the output file to save the reference genome ID matching the
## input strain
my $cut_name = $ARGV[0];
$cut_name =~ s/\..*//;
my $out_name = "$cut_name" . "_one_line.fa";
open(OUT, ">>$out_name") or die "Cannot create the output file: $!";
my @oneline_fasta;
my $sequence;
my $linecount = 1;
while (<INP_FASTA>){
chomp;
if (m/^>/){ # If the line is a header line
if ($linecount == 1){ # If this is the first header line,
push (@oneline_fasta, $_); # just add the header to the oneline_fasta array
$linecount++; # Increase the linecount
}
else{
push (@oneline_fasta, $sequence); # print the concatenated sequence to output
push (@oneline_fasta, $_); # print the new header to oneline_fasta
$sequence = ''; # empty the sequence string
}
}
else{ # not a header line? - must be sequence
chomp; # remove newline at end
$sequence .= $_; # append additional sequence
}
}
push (@oneline_fasta, $sequence); # print the last sequence to the file
foreach my $element (@oneline_fasta){
print OUT "$element\n";
}
close OUT;
close INP_FASTA;