2012-04-09 19 views
1

我是python的新手,需要一些幫助。創建一個與輸入字符串大小相同的事件向量

我有一個字符串這樣的ACAACGG

我現在想創建3個向量,其中的元素是特定字母的計數。

例如,對於「A」,這將產生(1123333) 爲「C」,這將產生(0111222) 等

我不知道如何把計數結果成一個字符串或一個向量。 我相信這類似於計算字符串中字符的出現,但我不確定如何讓它通過字符串並將計數值放在每個點上。

作爲參考,我試圖實現Burrows-Wheeler轉換並將其用於字符串搜索。但是,我不確定如何爲角色創建出現向量。

def bwt(s): 
    s = s + '$' 
    return ''.join([x[-1] for x in 
    sorted([s[i:] + s[:i] for i in range(len(s))])]) 

這給了我的轉換,我試圖創建它的發生向量。最終,我想用它來搜索DNA字符串中的重複序列。

任何幫助將不勝感激。

回答

2

我不確定你想要什麼類型的矢量,但是這裏有一個函數,返回listint s。

In [1]: def countervector(s, char): 
    ....:  c = 0 
    ....:  v = [] 
    ....:  for x in s: 
    ....:   if x == char: 
    ....:    c += 1 
    ....:   v.append(c) 
    ....:  return v 
    ....: 

In [2]: countervector('ACAACGG', 'A') 
Out[2]: [1, 1, 2, 3, 3, 3, 3] 

In [3]: countervector('ACAACGG', 'C') 
Out[3]: [0, 1, 1, 1, 2, 2, 2] 

此外,這裏有一個更短的方式做到這一點,但它可能會是低效的長字符串:

def countervector(s, char): 
    return [s[:i+1].count(char) for i, _ in enumerate(s)] 

我希望它能幫助。

+0

感謝您的幫助。我能夠按照我想要的方式繼續下去。我不太熟悉.append命令,這正是我所需要的。此外,空對象的分配也有所幫助。完成後我會發布我的腳本。 – doggysaywhat 2012-04-09 10:57:12

+0

看看你可以用列表[這裏]做什麼(http://docs.python.org/tutorial/datastructures.html#more-on-lists)。 – 2012-04-09 11:00:14

0

這裏承諾的是我寫的腳本。作爲參考,我試圖用Burrows-Wheeler變換在DNA串中進行重複匹配。基本上這個想法是採取一段長度爲M的DNA鏈,並在該串中找到所有的重複序列。所以,舉個例子,如果我有奇怪的acaacg並且搜索了大小爲2的所有重複的子字符串,我會得到1的計數和0,3的起始位置。然後你可以鍵入字符串[0:2]和字符串[3:5]來驗證它們確實匹配,並且它們的結果是「ac」。

如果有人有興趣瞭解的挖洞輪車,就可以了維基百科搜索產生非常有用的結果。這是斯坦福大學的另一個來源,也很好地解釋了這一點。 http://www.stanford.edu/class/cs262/notes/lecture5.pdf

現在,我還沒有解決一些問題。首先,我使用n^2空間來創建BW變換。另外,我創建了一個後綴數組,對其進行排序,然後用數字替換,以創建可能佔用一定空間的數字。然而,最後我只是真的存儲了occ矩陣,最後一列和單詞本身。

儘管內存問題的字符串大於4^7(得到這個工作的字符串大小爲40,000,但沒有更大的...),我會稱這是一個成功,看到像以前的星期一,我唯一的新的在python中如何做是爲了讓它打印我的名字和你好世界。

# generate random string of DNA 
def get_string(length): 
    string="" 
    for i in range(length): 
     string += random.choice("ATGC") 
    return string 

# Make the BW transform from the generated string 
def make_bwt(word): 
    word = word + '$' 
    return ''.join([x[-1] for x in 
     sorted([word[i:] + word[:i] for i in range(len(word))])]) 
# Make the occurrence matrix from the transform   
def make_occ(bwt): 
    letters=set(bwt) 
    occ={} 
    for letter in letters: 
     c=0 
     occ[letter]=[] 
     for i in range(len(bwt)): 
      if bwt[i]==letter: 
      c+=1 
      occ[letter].append(c) 
    return occ 

# Get the initial starting locations for the Pos(x) values 
def get_starts(word): 
    list={} 
    word=word+"$" 
    for letter in set(word): 
     list[letter]=len([i for i in word if i < letter]) 
    return list 

# Single range finder for the BWT. This produces a first and last position for one read. 
def get_range(read,occ,pos): 
    read=read[::-1] 
    firstletter=read[0] 
    newread=read[1:len(read)] 
    readL=len(read) 
    F0=pos[firstletter] 
    L0=pos[firstletter]+occ[firstletter][-1]-1 

    F1=F0 
    L1=L0 
    for letter in newread: 
     F1=pos[letter]+occ[letter][F1-1] 
     L1=pos[letter]+occ[letter][L1] -1 
    return F1,L1 

# Iterate the single read finder over the entire string to search for duplicates 
def get_range_large(readlength,occ,pos,bwt): 
    output=[] 
    for i in range(0,len(bwt)-readlength): 
     output.append(get_range(word[i:(i+readlength)],occ,pos)) 
    return output 

# Create suffix array to use later 
def get_suf_array(word): 
    suffix_names=[word[i:] for i in range(len(word))] 
    suffix_position=range(0,len(word))     
    output=zip(suffix_names,suffix_position) 
    output.sort() 
    output2=[] 
    for i in range(len(output)):       
     output2.append(output[i][1]) 
    return output2 

# Remove single hits that were a result of using the substrings to scan the large string 
def keep_dupes(bwtrange): 
    mylist=[] 
    for i in range(0,len(bwtrange)): 
     if bwtrange[i][1]!=bwtrange[i][0]: 
      mylist.append(tuple(bwtrange[i])) 
    newset=set(mylist) 
    newlist=list(newset) 
    newlist.sort() 
    return newlist 

# Count the duplicate entries 
def count_dupes(hits): 
    c=0 
    for i in range(0,len(hits)): 
     sum=hits[i][1]-hits[i][0] 
     if sum > 0: 
      c=c+sum 
     else: 
      c 
    return c 

# Get the coordinates from BWT and use the suffix array to map them back to their original indices 
def get_coord(hits): 
    mylist=[] 
    for element in hits: 
     mylist.append(sa[element[0]-1:element[1]]) 
    return mylist 

# Use the coordinates to get the actual strings that are duplicated 
def get_dupstrings(coord,readlength): 
    output=[] 
    for element in coord: 
     temp=[] 
     for i in range(0,len(element)):   
      string=word[element[i]:(element[i]+readlength)] 
      temp.append(string) 
     output.append(temp) 
    return output 

# Merge the strings and the coordinates together for one big list. 
def together(dupstrings,coord): 
    output=[] 
    for i in range(0,len(coord)): 
     merge=dupstrings[i]+coord[i] 
     output.append(merge) 
    return output 

Now run the commands as follows 
import random # This is needed to generate a random string 
readlength=12 # pick read length 
word=get_string(4**7) # make random word 
bwt=make_bwt(word) # make bwt transform from word 
occ=make_occ(bwt) # make occurrence matrix 
pos=get_starts(word) # gets start positions of sorted first row  
bwtrange=get_range_large(readlength,occ,pos,bwt) # Runs the get_range function over all substrings in a string.  
sa=get_suf_array(word) # This function builds a suffix array and numbers it. 
hits=keep_dupes(bwtrange) # Pulls out the number of entries in the bwt results that have more than one hit. 
dupes=count_dupes(hits) # counts hits 
coord=get_coord(hits) # This part attempts to pull out the coordinates of the hits. 
dupstrings=get_dupstrings(coord,readlength) # pulls out all the duplicated strings 
strings_coord=together(dupstrings,coord) # puts coordinates and strings in one file for ease of viewing. 
print dupes 
print strings_coord 
相關問題