Python學(xué)習(xí)的第五天,主要學(xué)習(xí)數(shù)據(jù)的過濾淑际。
1. 轉(zhuǎn)錄組差異分析數(shù)據(jù)的過濾
Cuffcompare軟件進(jìn)行了轉(zhuǎn)錄組表達(dá)量的比較和統(tǒng)計(jì),至少存在于3個(gè)生物學(xué)重復(fù)中的兩個(gè)樣本的轉(zhuǎn)錄本才被留下扇住,否則被過濾掉春缕。
轉(zhuǎn)錄組差異分析
我們將處理流程圖中的transcripts.tracking文件。
tracking = open('transcripts.tracking', 'r')
out_file = open('transcripts-filtered.tracking', 'w')
for track in tracking:
# split tab-separated columns
columns = track.strip().split('\t') #以tab符為分隔符
wildtype = columns[4:7].count('-') #野生型是第4-6列艘蹋,統(tǒng)計(jì)"-"數(shù)量
treatment = columns[7:10].count('-') #野生型是第7-9列锄贼,統(tǒng)計(jì)"-"數(shù)量
if wildtype < 2 or treatment < 2: #如果"-"數(shù)量 小于2,則將該行寫入輸入文件中
out_file.write(track)
tracking.close()
out_file.close()
####使用for/if語句處理這個(gè)問題女阀,代碼更容易理解宅荤,但代碼比較冗長。
output_file = open('transcripts-filtered2.tracking', 'w')
for track in open('transcripts.tracking'):
columns = track.strip().split('\t')
length = len(columns)
wt = 0
t = 0
if columns[4] != '-':
wt += 1
if columns[5] != '-':
wt += 1
if columns[6] != '-':
wt += 1
if length >= 8:
if columns[7] != '-':
t += 1
if length >= 9:
if columns[8] != '-':
t += 1
if length >= 10:
if columns[9] != '-':
t += 1
if wt > 1 or t > 1:
output_file.write(track)
output_file.close()
2. 比較兩個(gè)數(shù)據(jù)列表
data_a = [1, 2, 3, 4, 5, 6]
data_b = [1, 5, 7, 8, 9]
a_and_b = []
for num in data_a:
if num in data_b:
a_and_b.append(num) #既在data_a中也存在于data_b中的數(shù)字寫入a_and_b文件中
print(a_and_b)
[1, 5]
######兩個(gè)集合數(shù)據(jù)的交集
data_a = set([1, 2, 3, 4, 5, 6])
data_b = set([1, 5, 7, 8, 9])
a_and_b = data_a.intersection(data_b) #直接使用intersection()函數(shù)獲得兩個(gè)數(shù)據(jù)列表的交集浸策。
print(a_and_b)
{1, 5}
######兩組數(shù)據(jù)列表的的差異
data_a = [1, 2, 3, 4, 5, 6]
data_b = [1, 5, 7, 8, 9]
a_not_b = []
b_not_a = []
for num in data_a:
if num not in data_b:
a_not_b.append(num)
for num in data_b:
if num not in data_a:
b_not_a.append(num)
print(a_not_b)
[2, 3, 4, 6]
print(b_not_a)
[7, 8, 9]
#####兩個(gè)集合數(shù)據(jù)的差異
data_a = set([1, 2, 3, 4, 5, 6])
data_b = set([1, 5, 7, 8, 9])
a_not_b = data_a.difference(data_b)
b_not_a = data_b.difference(data_a)
print(a_not_b)
{2, 3, 4, 6}
print(b_not_a)
{8, 9, 7}
####兩個(gè)數(shù)據(jù)的并集
a_or_b = data_a.union(data_b)
print(a_or_b)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
####兩個(gè)數(shù)據(jù)的的補(bǔ)集
a_xor_b = data_a.symmetric_difference(data_b)
print(a_xor_b)
{2, 3, 4, 6, 7, 8, 9}
3. 移除列表冯键,字典以及文件中的元素
#####移除列表中的數(shù)字
data = [1,2,3,6,2,3,5,7]
data.pop(0) #使用pop()函數(shù)移除指定位置的數(shù)字
Out[49]: 1
data
Out[50]: [2, 3, 6, 2, 3, 5, 7]
#####使用del()函數(shù)刪除列表中指定位置的數(shù)字
data = [1,2,3,6,2,3,5,7]
del(data[0])
data
Out[53]: [2, 3, 6, 2, 3, 5, 7]
#####使用remove()函數(shù)刪除列表中指定的數(shù)字
data = [1, 2, 3, 6, 2, 3, 5, 7]
data.remove(3)
data
Out[59]: [1, 2, 6, 2, 3, 5, 7] #結(jié)果顯示remove()函數(shù)只移除了第一3,第二3依然存在于列表中
####移除列表中所有的數(shù)字3
data = [1, 2, 3, 6, 2, 3, 5, 7]
data = [x for x in data if x != 3] #使用for/if語句將列表中的數(shù)字3都移除
data
Out[62]: [1, 2, 6, 2, 5, 7]
#使用slicing切片移除移除數(shù)字3
data = [1, 2, 3, 6, 2, 3, 5, 7]
data2 = data[:2] + data[3:]
data2
Out[65]: [1, 2, 6, 2, 3, 5, 7]
#####移除字典中的元素
#使用pop()函數(shù)
d = {'a':1, 'b':2, 'c':3} #移除的時(shí)候是指定字典中的key而不是value庸汗。
d.pop('a')
Out[70]: 1
d
Out[71]: {'b': 2, 'c': 3}
#使用del函數(shù)
d = {'a':1, 'b':2, 'c':3}
del d['a']
d
Out[74]: {'b': 2, 'c': 3}
#####移除文件中的指定行
in_file = open('text.txt')
out_file = open('new.txt', 'w')
index = 0
indices_to_remove = [1,2,5,6] #需要移除的行
for line in in_file:
index = index + 1
if index not in indices_to_remove: #如果index不在indices_to_remove中惫确,則將剩下的寫入輸入文件中
out_file.write(line)
in_file.close()
out_file.close()
###除去文件中的重復(fù)行
input_file = open('UniprotID.txt')
output_file = open('UniprotID-unique.txt','w')
unique = set(input_file) #set()函數(shù)是構(gòu)建一個(gè)含有無序但唯一元素的集合。
for line in input_file:
unique.add(line)
for line in unique:
output_file.write(line)
4. 處理數(shù)據(jù)集合
reduce()函數(shù)將triple_set中的第1和第2個(gè)元素傳遞給set.intersection函數(shù)進(jìn)行數(shù)據(jù)集合求交集的操作,得到的結(jié)果與第3個(gè)元素用set.intersection函數(shù)繼續(xù)進(jìn)行操作改化,這樣歷遍triple_set中的元素得到最后的結(jié)果昧诱。
###查找3個(gè)集合數(shù)據(jù)的交集
from functools import reduce
a = set((1, 2, 3, 4, 5))
b = set((2, 4, 6, 7, 1))
c = set((1, 4, 5, 9))
treiple_set = [a,b,c]
common = reduce(set.intersection, triple_set)
print(common)
{1, 4}
5. 比較或更新數(shù)據(jù)庫集合的變化
假設(shè)你有兩個(gè)Uniprot數(shù)據(jù)集合,你想知道哪一個(gè)數(shù)據(jù)集合是新的所袁,兩個(gè)數(shù)據(jù)集合有什么變化差異盏档。
old_db = set()
for line in open("list_old.txt"): #讀取舊的數(shù)據(jù)集合
accession = line.strip()
old_db.add(accession)
new_db = set()
for line in open("list_new.txt"): #讀取新的數(shù)據(jù)集集合
accession = line.strip()
new_db.add(accession)
new_entries = new_db.difference(old_db) #相較于舊數(shù)據(jù)庫,新數(shù)據(jù)庫的更新的內(nèi)容
print("new entries", list(new_entries))
new entries ['s', 'm', 'o', 'n', 'q', 'r', 'p']
old_entries = old_db.difference(new_db)
print("deprecated entries", list(old_entries)) #相較于新數(shù)據(jù)庫燥爷,舊數(shù)據(jù)庫的過時(shí)的數(shù)據(jù)內(nèi)容
deprecated entries ['c']
unique_entries = new_db.symmetric_difference(old_db) #新舊數(shù)據(jù)庫各種特有的數(shù)據(jù)內(nèi)容
print("unique entries", list(unique_entries))
unique entries ['s', 'r', 'm', 'o', 'c', 'n', 'q', 'p']