0
 pat = "TATAG"
 DSIGMA = 4 //i used 4 character 'ACGT'
 m = len(pat)
 string = "ACGT"
 shift = [[0] * DSIGMA]*m
 for l in range(m):
   U[l] = 1
    for s in range(DSIGMA):
       shift[l][string[s]] = 1'''
Output

TypeError: list indices must be integers or slices, not str


I build this code in c++ and run perfectly, but when i convert into python it doesn't work. How to fix my code? Thanks!
Chris Charley
  • 6,403
  • 2
  • 24
  • 26

2 Answers2

0

You are writing python now :) so do not comment like // or /**/ and Use # or ''' ''' for commenting.

Be careful about Spaces/Tabs :)

Where you defined U parameter? You should define this somewhere; you can find and read about similar error here.

Why you passed string[s] as index? if you need to access via string/key, you can use something like a DICT. You can read about this here.

Remove ``` and also output in the last line.

I do not know what the result should be, but changing the code in this way does not give an error:

pat = "TATAG"
DSIGMA = 4 # i used 4 character 'ACGT' ****
m = len(pat)
string = "ACGT"
shift = [[0] * DSIGMA]*m
U=[0,0,0,0,0] # DEFINE SOMETHING ****
for l in range(m):
    U[l] = 1
    for s in range(DSIGMA):
        # Change this to something else ****
        # string[s] is not ok as an index ****
        # shift[l][string[s]] = 1
        shift[l]=1
Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36
0

I'm sorry, this is my first time using stack overflow.

I using string[s] as index because that string would be useful in my next code. There is my c++ program.

 #include <iostream>
 #include <stdlib.h>
 #include <string.h>
 #define DSIGMA 4
 #define SIGMA 256
 #include <fstream>
 using namespace std;
 void MAS_Search(char *P,char *T){
    int shift[100][SIGMA],U[100],l,s,k,safe[100],i;
    int mas,avr_shift,scan[100],max_pos,mas_shift[100][100];
    int w;
    int count=0;
    char dna[DSIGMA]={ 'A','C','G','T' };
    int freq[100];
    freq['A'] = 293;    freq['C'] = 207;
    freq['G'] = 207;    freq['T'] = 293;
    int n=strlen(T);
    int m=strlen(P);
    /*Preproses*/
    for (l = 0; l<m; l++) {
        U[l] = 1;   // Deklarasi U = {0,1,...,m-1}.
        for (s = 0; s<DSIGMA; s++) {
        shift[l][dna[s]] = 1;
        }
    }
    for (k = 1; k <= m; k++) {
        safe[k] = 0;
    }
    /* Preproses */
    for (i = 0; i <m; i++) {
        for (l = 0; l <m; l++) {
            if (U[l] == 1) {
                for (s = 0; s <DSIGMA; s++) {
                    for (k = shift[l][dna[s]]; k <= m; k++) {
                        if (safe[k] == 0 &&  dna[s] == P[l - k]) {
                            shift[l][dna[s]] = k;
                            break;
                        }
                    }
                }
            }
        }
        //Max
        mas=0;
        for (l = 0; l <m; l++) {
            if (U[l] == 1) {
                avr_shift = 0;
                for (s = 0; s <DSIGMA; s++) {
                    avr_shift = avr_shift + shift[l][dna[s]] * freq[dna[s]];
                }
                if ((mas < avr_shift) || (mas == avr_shift && freq[P[max_pos]] > freq[P[l]])) {
                    mas = avr_shift;
                    max_pos = l;
                    cout<<max_pos;
                }
            }
        }
        scan[i]=max_pos;
        U[max_pos]=0;
        for (k=1;k<=max_pos-1;k++){
            if (P[max_pos]!=P[max_pos-k]){
            safe[k]=1;
            }
        }
    }
    
    //Found pattern and text
    w = 0;
    while (w<=n-m){
    i=0;
    while (i<=m && T[w+scan[i]] == P[scan[i]]){
        i=i+1;
    }
    if (i>m){
        cout<<"Pattern found at: "<<w+1<<endl;
        w=w+shift[scan[m]][T[w+scan[m]]];
    }
    else{
        w=w+shift[scan[i]][T[w+scan[i]]];
    }
    }
}
//Input
main(){
    ifstream teks;
    string Data;
    int i=0;
    char T[40000];
    teks.open("Data.txt");
    while (!teks.eof()){
        teks.get(T[i]);
        i++;
    }
    char P[] = "TATA";
    MAS_Search(P, T);
    return 0;
    teks.close();
    cin.get();
}