| 1 | #!/usr/bin/env perl | ||
| 2 | # Last changed Time-stamp: <2014-12-10 13:05:43 mtw> | ||
| 3 | # AUTHOR: Joerg Fallmann <joerg.fallmann@univie.ac.at> | ||
| 4 | |||
| 5 | ############### | ||
| 6 | ###Use stuff | ||
| 7 | ############### | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Data::Dumper; | ||
| 11 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 12 | use Pod::Usage; | ||
| 13 | use PerlIO::gzip; | ||
| 14 | use Cwd; | ||
| 15 | use List::Util qw(sum); | ||
| 16 | use File::Path qw(make_path remove_tree); | ||
| 17 | use Bio::ViennaNGS; | ||
| 18 | |||
| 19 | ############### | ||
| 20 | ###Variables | ||
| 21 | ############### | ||
| 22 | |||
| 23 | my $VERBOSE = 0; | ||
| 24 | my ( $dir, $odir, $file, $klength, $type ); | ||
| 25 | |||
| 26 | ############### | ||
| 27 | ###Command Line Options | ||
| 28 | ############### | ||
| 29 | |||
| 30 | pod2usage(-verbose => 0) | ||
| 31 | unless GetOptions( | ||
| 32 | "dir|d=s" => \$dir, | ||
| 33 | "odir|o=s" => \$odir, | ||
| 34 | "file|f=s" => \$file, | ||
| 35 | "kmer|k=s" => \$klength, | ||
| 36 | "type|t=s" => \$type, | ||
| 37 | "help|h" => sub{pod2usage(-verbose => 1)}, | ||
| 38 | "man|m" => sub{pod2usage(-verbose => 2)}, | ||
| 39 | "verbose" => sub{ $VERBOSE++ } | ||
| 40 | ); | ||
| 41 | |||
| 42 | $dir = cwd() unless ($dir); | ||
| 43 | $odir = "$dir"."/Kmer_Analysis" unless $odir; | ||
| 44 | $dir =~ s/ //g; | ||
| 45 | $odir =~ s/ //g; | ||
| 46 | |||
| 47 | ############### | ||
| 48 | ###MAIN | ||
| 49 | ############### | ||
| 50 | |||
| 51 | if (!-d $odir){ | ||
| 52 | make_path($odir) or die "Error creating directory: $odir"; | ||
| 53 | } | ||
| 54 | |||
| 55 | chdir($dir) or die "Could not find directory $dir, $!\n"; | ||
| 56 | |||
| 57 | unless (-f $file) { | ||
| 58 | warn "Could not find input file $file given via -f option"; | ||
| 59 | pod2usage(-verbose => 0); | ||
| 60 | } | ||
| 61 | |||
| 62 | unless ($type || $file =~ /.fa$/ || $file =~ /.fastq/ || $file =~ /.fasta/) { | ||
| 63 | warn "Could not determine file type, please define using the -t option (fasta or fastq)"; | ||
| 64 | pod2usage(-verbose => 0); | ||
| 65 | } | ||
| 66 | |||
| 67 | open(IN,"<:gzip(autopop)","$file") || die ("Could not open $file!\n@!\n"); | ||
| 68 | |||
| 69 | my (%embeddings, %kmer, @seqs); | ||
| 70 | my $x=1; ## Line counter for fastq file | ||
| 71 | while(<IN>){ | ||
| 72 | if ($file =~ /\.fastq/ || $type eq "fastq"){ | ||
| 73 | # print STDERR "Processing fastq file!\n"; | ||
| 74 | if( (++$x)%2 && $x != 5){ | ||
| 75 | chomp (my $raw = $_); | ||
| 76 | push @seqs , $raw; | ||
| 77 | } | ||
| 78 | else{ | ||
| 79 | # print STDERR $x,"\n"; | ||
| 80 | $x = 1 if ($x >= 5); | ||
| 81 | } | ||
| 82 | # $embeddings{"$line[1] $line[2]"}= $line[5]; | ||
| 83 | } | ||
| 84 | elsif ($file =~ /\.fa$/ || $file =~ /\.fasta$/ || $type eq "fasta"){ | ||
| 85 | # print STDERR "Processing fasta file!\n"; | ||
| 86 | next if ($_=~m/^>/); | ||
| 87 | chomp (my $raw = $_); | ||
| 88 | push @seqs, $raw; | ||
| 89 | # $embeddings{"seq"}= $raw; | ||
| 90 | } | ||
| 91 | else{ | ||
| 92 | warn "Could not determine file type, please define using the -t option (fasta or fastq)"; | ||
| 93 | pod2usage(-verbose => 0); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | close(IN); | ||
| 97 | |||
| 98 | ###Investigate kmer_enrichment | ||
| 99 | %kmer = %{kmer_enrichment(\@seqs, $klength)}; | ||
| 100 | #foreach my $key (keys %embeddings){ | ||
| 101 | # %kmer = %{kmer_enrichment($embeddings{$key}, $klength)}; | ||
| 102 | # %kmer = %{kmer_enrichment(\@seqs, $klength)}; | ||
| 103 | #} | ||
| 104 | |||
| 105 | my $total = sum values %kmer; | ||
| 106 | chdir($odir) or die "Could not find directory $odir, $!\n"; | ||
| 107 | |||
| 108 | ### Print Output | ||
| 109 | open(KMER,">","$klength\_mers") or die "Could not open file $!\n"; | ||
| 110 | print KMER "$klength\-mer\tCount\tRatio\n"; | ||
| 111 | print KMER "TOTAL\t$total\t1\n"; | ||
| 112 | foreach my $key (sort {$kmer{$b} <=> $kmer{$a} } keys %kmer) { | ||
| 113 | my $ratio = $kmer{$key}/$total; | ||
| 114 | print KMER "$key\t$kmer{$key}\t$ratio\n"; | ||
| 115 | } | ||
| 116 | close(KMER); | ||
| 117 | |||
| 118 | chdir($dir) or die "Could not find directory $dir, $!\n"; | ||
| 119 | ############### | ||
| 120 | ###POD | ||
| 121 | ############### | ||
| 122 | |||
| 123 | __END__ |