Quartets

AUTHOR

L. Grondin

http://rosalind.info/problems/qrt/

Sample input

cat dog elephant ostrich mouse rabbit robot
    01xxx00
    x11xx00
    111x00x

Sample output

{elephant, dog} {rabbit, robot}
    {cat, dog} {mouse, rabbit}
    {mouse, rabbit} {cat, elephant}
    {dog, elephant} {mouse, rabbit}
use v6;



proto combine (Int, @) {*}
multi combine (0,  @)  { [] }
multi combine ($,  []) { () }
multi combine ($n, [$head, *@tail]) {
    gather {
        take [$head, @$_] for combine($n-1, @tail);
        take [ @$_ ] for combine($n, @tail);
    }
}
Ā 
sub ncombine($n, $k) {
    (state@)[$n][$k] //= combine($k, [^$n])
}

my $default-input = q:to/END/;
    cat dog elephant ostrich mouse rabbit robot
    01xxx00
    x11xx00
    111x00x
    END

sub MAIN($input-file = Nil) {
    my $input = $input-file ?? $input-file.IO.slurp !! $default-input;
    my @data = $input.lines;

    my %seen;
    my @taxa = @data.shift.words;
    for @data -> $line {
        my %h = @taxa Z=> $line.comb;
        my @classif =
        classify( { %h{$_} }, @taxa )<0 1>;
        next if any(@classif) < 2;
        note "processing $line";
        my @a = map { [ @classif[0][@$_] ] }, ncombine(@classif[0].elems, 2)[];
        my @b = map { [ @classif[1][@$_] ] }, ncombine(@classif[1].elems, 2)[];
        for @a X @b -> $a, $b {
            my $left = $a.sort.join(', ');
            my $right = $b.sort.join(', ');
            next if %seen{$left}{$right}++ ?| %seen{$right}{$left}++;
            say '{' ~ $left ~ '} {' ~ $right ~ '}';
        }
    }
}

# vim: expandtab shiftwidth=4 ft=perl6

See Also

afrq-grondilu.pl

Counting Disease Carriers

aspc-grondilu.pl

Introduction to Alternative Splicing

cons-grondilu.pl

Consensus and Profile

conv-grondilu.pl

Comparing Spectra with the Spectral Convolution

cstr-grondilu.pl

Creating a Character Table from Genetic Strings

ctbl-grondilu.pl

Creating a Character Table

dbpr-grondilu.pl

Introduction to Protein Databases

dna-gerdr.pl

Counting DNA Nucleotides

dna-grondilu.pl

Counting DNA Nucleotides

eubt-grondilu.pl

Enumerating Unrooted Binary Trees

eval-grondilu.pl

Expected Number of Restriction Sites

fib-grondilu.pl

Rabbits and Recurrence Relations

fibd-grondilu.pl

Mortal Fibonacci Rabbits

gc-gerdr.pl

Computing GC Content

grph-grondilu.pl

Overlap Graphs

hamm-grondilu.pl

Counting Point Mutations

iev-grondilu.pl

Calculating Expected Offspring

indc-grondilu.pl

Independent Segregation of Chromosomes

iprb-grondilu.pl

Mendel's First Law

itwv-grondilu.pl

Finding Disjoint Motifs in a Gene

lcsq-grondilu.pl

Finding a Shared Spliced Motif

lia-grondilu.pl

Independent Alleles

lrep-grondilu-p5.pl

mmch-grondilu.pl

Maximum Matchings and RNA Secondary Structures

mprt-grondilu.pl

Finding a Protein Motif

mrna-grondilu.pl

Inferring mRNA from Protein

nwck-grondilu.pl

Distances in Trees

orf-grondilu.pl

Open Reading Frames

pmch-grondilu.pl

Perfect Matchings and RNA Secondary Structures

pper-grondilu.pl

Partial Permutations

prob-grondilu.pl

Introduction to Random Strings

README.md

revc-gerdr.pl

Complementing a Strand of DNA

rna-gerdr.pl

Transcribing DNA into RNA

rstr-grondilu.pl

Matching Random Motifs

sexl-grondilu.pl

Sex-Linked Inheritance

sgra-grondilu.pl

Using the Spectrum Graph to Infer Peptides

spec-grondilu.pl

Inferring Protein from Spectrum

sseq-grondilu.pl

Finding a Spliced Motif

subs-grondilu.pl

Finding a Motif in DNA

suff-grondilu.pl

Encoding Suffix Trees

tran-grondilu.pl

Transitions and Transversions

trie-grondilu.pl

Introduction to Pattern Matching

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.