shamshersingh:
Here's what I tried:
#!/usr/bin/perl
use strict;
use warnings;
my %H;
open my $FH, '<', 'DNA_strings.dat' or die $!;
while (<$FH>) {
s/\s+$//;
for my $i (0 .. length($_)-1) {
my $k = $_;
substr($k,$i,1) = '*';
push @{$H{$k}}, $_;
}
}
for my $k (sort keys %H) {
if ($#{$H{$k}} > 1) {
print "$k\t", join(",\n\t\t", @{$H{$k}}), "\n";
}
}
A quick experiment with a short file:
$ cat DNA_strings.dat
CTGAG
CGAGT
ACGCT
TATAC
CTGAA
GGAGC
ATACA
AAAAA
ACAAA
AGAAA
AATAA
AAAGA
ACCAA
AGCAC
CCACG
GCCAT
AGCAA
GGCAT
GTTTG
$ perl DNA_cmp.pl
A*AAA: AAAAA, ACAAA, AGAAA
A*CAA: ACCAA, AGCAA
AA*AA: AAAAA, AATAA
AAA*A: AAAAA, AAAGA
AC*AA: ACAAA, ACCAA
AG*AA: AGAAA, AGCAA
AGCA*: AGCAC, AGCAA
CTGA*: CTGAG, CTGAA
G*CAT: GCCAT, GGCAT
It seems pretty fast, too. It took less than a minute to scan through 100,000 20-character strings, but it didn't find anything. (gen_random_strings.pl is on my my scratchpad)
$ perl gen_random_strings.pl 100000 20 20 ACGT >DNA_strings.dat
$ time perl DNA_cmp.pl
real 0m47.659s
user 0m46.395s
sys 0m1.252s
Update: I tried to make this node a reply to the OP, but for some reason, it kept failing on me, and when I'd refresh, it would show my node replacing BrowserUk's node. It was odd looking...
...roboticus
When your only tool is a hammer, all problems look like your thumb.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.