I have a list of genes and the following information:
- Their name 'XLOC_0000...'
- The genomic scaffold on which they're located 'Scaffold...'
- The location of each feature on its Scaffold ('start', 'stop')
I've written a piece of Perl code that finds each gene in the genomic scaffolds and saves it to a file. Briefly, first I put each gene in a hash of arrays, e.g.
my %geneID = map { $xloc[$_] => [ $scaffold[$_], $start[$_], $stop[$_] ] } (0 .. $#xloc);
I then make a hash of the fasta file containing scaffolds:
open FASTA, '<', 'genome.fasta' || die "Can't open 'genome.fasta'\n"; #Read in 'fasta' file
my (@head, @sequence);
while (<FASTA>) {
chomp;
push @head, $_ if /^>/;
push @sequence, $_ if /^[A-Z]/;
}
my %scaf;
@scaf{@head} = @sequence; # All scaffolds, as ordered in FH.
Then I assign the elements of the first HoA, and using substr, find the gene's start and stop position within the scaffold of the same name
foreach my $xloc (sort keys %geneID) {
print "gene sequence for $xloc is: ";
my $chm = @{$geneID{$xloc}}[0];
my $start = @{$geneID{$xloc}}[1];
my $end = @{$geneID{$xloc}}[2];
my $seq = substr($scaf{$chm},$start-1,$end-($start-1));
print "$seq\n";
}
The problem with this is that if I have xlocs with the same name, e.g. XLOC_00001, the the hash key only takes the last value. I want to be able to add multiple 'sub-values' to each hash, find their locations using substr, and essentially join them together at the end.
Any suggestions on how to do this?
UPDATE:
This is a test example showing the sort of results I get:
'GENOME' FASTA FILE
>Scaffold1
ONEATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold2
TWOATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold3
THREEATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold4
FOURATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold5
FIVEATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold6
SIXATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold7
SEVENATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold8
EIGHTATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold9
NINEATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
>Scaffold10
TENATCGCGCTTAGTGCAGTACGTAGCTACGTGACTACTGA
KEYS and values for %geneID:
Key: XLOC_000027 contains the values: >Scaffold1 1 10
Key: XLOC_000037 contains the values: >Scaffold2 1 15
Key: XLOC_000038 contains the values: >Scaffold3 2 9
Key: XLOC_000051 contains the values: >Scaffold4 6 8
Key: XLOC_000077 contains the values: >Scaffold5 2 7
Key: XLOC_000079 contains the values: >Scaffold6 4 16
Key: XLOC_000096 contains the values: >Scaffold7 4 9
Key: XLOC_000100 contains the values: >Scaffold8 3 20
Key: XLOC_000117 contains the values: >Scaffold9 6 8
Key: XLOC_000119 contains the values: >Scaffold10 7 14
Results, showing 'gene' as substring of scaffold on which it's located for each XLOC:
gene sequence for XLOC_000027 is: ONEATCGCG
gene sequence for XLOC_000037 is: TWOATCGCGCTTAG
gene sequence for XLOC_000038 is: HREEATCG
gene sequence for XLOC_000051 is: TCGCGCT
gene sequence for XLOC_000077 is: IVEATC
gene sequence for XLOC_000079 is: ATCGCGCTTAGTGCA
gene sequence for XLOC_000096 is: ENATCGCG
gene sequence for XLOC_000100 is: GHTATCGCGCTTAGTGCAG
gene sequence for XLOC_000117 is: TCGCGCT
gene sequence for XLOC_000119 is: GCGCTTAGTGCAG