使用两个'字母'的不同长度的序列组合

时间:2012-08-14 00:49:25

标签: objective-c algorithm permutation combinations bioinformatics

我试图在给定某个字母表的情况下生成长度为k的序列的所有可能组合(这是为生物信息学项目生成查询序列)。

序列的形式如下:

第一个字符和最后一个字符可以是A C G U中的任何一个(称为Y)和其间的k - 2个字符,可以是A C G U中的任何一个或? (称之为X)。

e.g。如果k = 3,那么模式的形式为YXY,如果k = 5则为YXXXY。

如果已知k,则生成所有可能的序列很容易,因为您可以使用k嵌套for循环。但是如果事先不知道k,那么这种实现就不合适了。

可能序列的总数可以用4 ^ 2 * 5 ^(k-2)表示。当k = 3时,这只得到80个组合,但是可以扩展到k = 9,你有1,250,000!

非常感谢任何提示,想法或建议。

我需要使用生成的每个序列,因此它们需要存储在数组中,或者在创建/生成时传递给另一个函数,它并不重要,尽管我不希望存储所有他们。

非常感谢。

N.B。我在objective-c中写作,但任何c风格的代码,或者伪造的代码或只是简单的英语描述的算法都会有所帮助。

更新:

以下是我根据Analog File的精彩回答编写的objc代码。目前它只是每行输出一个序列到stdout,但我会修改它以产生一个字符串数组。

非常感谢所有贡献者。

    NSArray *yAlphabet = [NSArray arrayWithObjects:@"A", @"C", @"G", @"U", nil];
    NSArray *xAlphabet = [NSArray arrayWithObjects:@"A", @"C", @"G", @"U", @"?", nil];
    int i, v;
    int count = 0;
    int numberOfCases = 16 * pow(5 , (k - 2));
    for (int n = 0; n < (numberOfCases); n++) {
        i = n;
        v = i % 4;
        i = i / 4;
        count++;
        printf("\n%s", [[yAlphabet objectAtIndex:v] cStringUsingEncoding:NSUTF8StringEncoding]);
        for (int m = 1; m < (k - 1); m++) {
            v = i % 5;
            i = i / 5;
            printf("%s", [[xAlphabet objectAtIndex:v] cStringUsingEncoding:NSUTF8StringEncoding]);
        }
        printf("%s", [[yAlphabet objectAtIndex:i] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    printf("\n");
    NSLog(@"No. Sequences: %i", count);

更新2:

这是代码,将生成的序列输出到字符串数组。注意,k是所需序列的长度,并作为参数在别处给出。我已经测试了这个到k = 9(1,250,000个序列)。另请注意,我的代码使用ARC,因此没有显示内存释放。

    NSArray *yAlphabet = [NSArray arrayWithObjects:@"A", @"C", @"G", @"U", nil];
    NSArray *xAlphabet = [NSArray arrayWithObjects:@"A", @"C", @"G", @"U", @"?", nil];
    NSMutableArray *sequences = [[NSMutableArray alloc] init];
    int i, v;
    int count = 0;
    int numberOfCases = 16 * pow(5 , (k - 2));
    for (int n = 0; n < (numberOfCases); n++) {
        i = n;
        v = i % 4;
        i = i / 4;
        count++;
        NSMutableString *seq = [[NSMutableString alloc] initWithString:[yAlphabet objectAtIndex:v]];
        for (int m = 1; m < (k - 1); m++) {
            v = i % 5;
            i = i / 5;
            [seq appendString:[xAlphabet objectAtIndex:v]];
        }
        [seq appendString:[yAlphabet objectAtIndex:i]];
        [sequences addObject:seq];
    }
    NSLog(@"No. Sequences looped: %i", count);

    //print the array to confirm
    int count1 = 0;
    for (NSMutableString *str in sequences) {
        fprintf(stderr, "%s\n", [str cStringUsingEncoding:NSUTF8StringEncoding]);
        count1++;
    }
    NSLog(@"No. Sequences printed: %i", count1);
    NSLog(@"Counts match? : %@", (count == count1 ? @"YES" : @"NO"));

3 个答案:

答案 0 :(得分:1)

你知道你会得到多少案件。 这是伪代码(k是序列长度)

 for n = 0 to num_of_cases - 1

    i = n

    v = i % length_of_alphabeth_Y
    i = i / length_of_alphabeth_Y
    output vth char in alphabeth Y

    for m = 1 to k-1
       v = i % length_of_alphabeth_X
       i = i / length_of_alphabeth_X
       output vth char in alphabeth X

    output ith char in alphabeth Y
    output end of sequence

外部循环的每次迭代都会生成一个case.I写了output但是很容易将数据“存储”在一个动态分配的结构中(n个索引在roes中,第一种情况是第0列然后是m个索引在列和最后一种情况下是列k-1。如果你这样做,“序列结束”不需要输出,因为它包含在n)的增量中。

请注意我们如何有效地“计算”基数length_of_alphabeth,除了我们根据数字使用不同的基数。 Modulo为您提供最低有效数字,整数除法将其除去并将下一个数字移至最不重要的位置。

如果你能想象n只是一个,在没有特定的基础上,逻辑就相当简单了。一旦你理解了,你就可以从头开始写这个。

答案 1 :(得分:0)

执行此操作的基本形式如下(Java-is pseudocode)

char[] output = new char[k];
pubilc void go(cur_k){
   if(cur_k>k) // do something - copy the array and store it, etc.
   for( char letter : alphabet ){
       output[cur_k]=char_letter;
       go(cur_k+1);
   }
}

答案 2 :(得分:0)

听起来k会作为参数传入,所以像这样(在Python-ish伪代码中)应该可以工作

Y_alphabet = ['A','C','G','U']
X_alphabet = ['A','C','G','U','?']

outputs = []

for i in range(k):
    if i == 0 or i == k-1:
        current_alphabet = Y_alphabet
    else:
        current_alphabet = X_alphabet

    last_outputs = outputs
    outputs = []

    for next_character in current_alphabet:
        # This just replaces outputs with a new list that consists
        # of all the possible sequences of length i appended with
        # the current character
        outputs += [seq + next_character for seq in last_outputs]