This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Split Sequence In Fasta File In Python

I want to extract and split sequences from fasta file I write this code :

outfile=open('outf','w')
for line in open('input'):
      if line[0]==">":
         outfile.write('\n')
     else:
    outfile.write(line.strip())

   outfile.close()

 all_codons=[]
 for line in open('outf', 'r'):
     seq=line.strip()
     codons = [seq[i:i+3] for i in xrange(0, len(seq), 3) if len(seq[i:i+3])==3]
     all_codons.append(codons)

Then, from the splited sequences I want to take three sequences its lengths is 9( 9 bases) example:

    CGTAACAAG 
    AATCCGGAG 
    CCGCCTCGG

,I split the first sequence to 3 sub-sequences of three bases, so, from one sequences I obtain 3 sub-sequences, I do the same thing for the two other sequences.

Like this:

    CGT    AAC     AAG 
    AAT    CCG     GAG 
    CCG    CCT     CGG

Then , I apply this function :

def identical_segment(input_string): 
code={"a":0,"c":1,"g":2,"t":3} 
p=[code[i] for i in input_string] 
n=len(input_string) 
c=0
 for i, n in enumerate(range(n, 0, -1)):
     c +=p[i]*(4**(n-1)) 
   return c+1

example:

identical_segment('CGT')

I want to apply this function to each sub-sequences of th three sequences, then apply the same thing on all fasta file. So, the purpose is to obtain matrix, for example I take the first sub-sequence 'CGT' and apply the function identical_segment() , it returns 28, the same thing for the rest 8 sub-sequences. So I obtain a matrix(3,3):

28         2             3
4          23            35
23         4             27

Can some one help me?

split sequence python

It looks like you did what you wanted. What is the problem here? How can we help?

0 answers

No answers yet.

Log in to answer this question.