Thành viên:Phạm Thạch Thảo/Note: Cách đưa thêm annotation vào tree

Từ VLOS
Bước tới: chuyển hướng, tìm kiếm
  • Trước hết ta cài gói ape trong R (gói này tương đối tiện và có hỗ trợ nhiều tính năng khác) từ trang chủ của R bằng lệnh:
>install.packages("ape");

Sau đó chọn một redirect link gần với nơi ở của mình trong bảng hội thoại xuất hiện.

  • Việc tiếp theo ta tạo file annotation dạng txt gồm hai cột: cột thứ nhất chứa assession number, cột thứ hai chứa thông tin muốn đưa vào (trường hợp này là tên loài.) Mỗi xâu ký tự trong một cặp dấu ngoặc kép. Chẳng hạn:
"AF020886.1" "Gopherus_polyphemus"
"AF020887.1" "Pyxis_arachnoides"

File như vậy có thể edit từ file allname.txt trước đây khi ta edit name (xóa các thông tin dư thừa và thêm dấu ngoặc kép ngăn hai cột.) Ta chỉ cần một file lớn nhất, nên edit cho file với toàn bộ Rùa là đủ (không phải làm với họ ba ba riêng, vì họ này cũng có sẵn thông tin trong file của bộ Rùa.)

File annotation như vậy có thể thiết lập gián tiếp nhờ format GenBank hỗ trợ với ncbi và package ape. Sử dụng accession numbers của "all", ta có thể load thông tin tương ứng với chúng.

#To attache the annotation into trees.
rm(list = ls());
#library(Biostrings);
library(ape);
 
#Read all species file:
allNumber = read.table("16S.all.txt", colClass = "character", sep = "\t", row.names = NULL)[,1];
allNumber = intersect(allNumber,allNumber);
 
allSEQ = read.GenBank(allNumber);
nSEQ = length(allSEQ);
 
allNumber = attr(allSEQ,"names");
allSpecies = attr(allSEQ,"species");
Table = cbind(allNumber,allSpecies);
write.table(Table, file = "16S.annotation.txt", row.names = FALSE, col.names = FALSE);

Lưu ý, file annotation sẽ còn đóng góp vào việc thống kê khoảng cách trong và ngoài loài nên cần đảm bảo ghi tên thống nhất (sao cho máy tính hiểu được), tên nên lấy theo taxonomy, đặc biệt chú ý chữ viết thường, chữ viết hoa, các khoảng trắng nhất là ở đầu và cuối của xâu.

  • Tiếp đó ta thực hiện script sau:
rm(list = ls());
library(Biostrings);
library(ape);
allName = read.table("16S.annotation.txt", colClass = "character", row.names = 1); 
tree = read.tree("16S.Tri.0.ph");
 
tiplabel = tree$tip.label
n = length(tiplabel)
for (k in 1:n)
{
	label = tiplabel[k]; 
	label =strsplit(label, split = '|', fixed = TRUE)[[1]][1];
	label = paste(label,allName[label,1], sep = "|");
	tiplabel[k] = label;
};
tree$tip.label = tiplabel;
plot(tree, cex = 0.7);
write.tree(tree,file =  "16S.Tri.0.annot.ph");

Script này sẽ đọc thông tin annotation từ file "16S.annot.0.txt" (chỉ cần một file cho tất cả bộ rùa), đọc cây từ file "16S.Tri.0.ph", gắn thêm annotation mà ta đã chuẩn bị vào tip của tree. Cuối cùng cây mới được ghi vào file "16S.Tri.0.annot.ph." Đồng thời một cây mới được vẽ với annotation. Muốn lưu cây này vào file pdf ta dùng lệnh:

dev.copy2pdf(file = "new.tree.pdf");

Ví dụ file annotation cho 16S toàn bộ Rùa Tập tin:16S.annot.0.txt.zip, sẽ update sau khi được edit chuẩn hơn.

Kỹ thuật xử lý xâu ký tự và phân tích "words" (lưu trữ)[sửa]

Các đoạn scripts sau không còn quan trọng do cách download bằng GenBank quá tiện lợi, nhưng giữ lại làm "kỷ niệm" về thuật toán sau này có thể dùng đến:

  • Đoạn script sau có thể giúp ích việc loại bỏ các khoảng trắng đầu và cuối của xâu:
allName = read.table("16S.annot.0.txt", colClass = "character", row.names = 1); 
nName = nrow(allName);
for (kName in 1:nName)
{
	Name = allName[kName,1];
	Name = sub("^[[:space:]]*(.*?)[[:space:]]*$", "\\1", Name, perl=TRUE);
	allName[kName,1] = Name;
};
write.table(allName, file = "16S.annot.1.txt", header = FALSE);

Đoạn script đó đọc một file tên có thể có các khoảng trắng đầu và cuối, cho ra file đã loại bỏ các khoảng trắng đầu và cuối annotation.

  • Đoạn scripts sau sử dụng thuật toán phân tích một dãy ký tự thành words, lưu lại các words có trong từ điển, xóa các words tạp:
#Read all species file:
allSpecies = read.table("allspeciesname.txt", colClass = "character", sep = "\t", row.names = NULL)[,1];
allSpecies = as.vector(allSpecies);
nspecies = length(allSpecies);
 
#Create the dictionary:
allWords = NULL;
for (kspecies in 1:nspecies)
{
	Name = allSpecies[kspecies];
	Name = sub("^[[:space:]]*(.*?)[[:space:]]*$", "\\1", Name, perl=TRUE);
	Name = tolower(Name);
	Name = gsub("\\(","",Name);
	Name = gsub("\\)","",Name);
	allSpecies[kspecies] = Name;
	Words = strsplit(Name, split = " ")[[1]];
	allWords = c(allWords,Words);
};
allWords = intersect(allWords,allWords);
 
#Read the annotation:
allName = read.table("16S.annot.0.txt", colClass = "character", row.names = 1);
allID = rownames(allName);
allName = as.vector(allName[,1]); 
nName = length(allName);
 
#Remove white space:
for (kName in 1:nName)
{
	Name = allName[kName];
	Name = tolower(Name);
	#Delete the ",", ";"...
	Name = gsub(",", "", Name);
	Name = gsub(";", "", Name);
 
	Words = strsplit(Name, split = " ")[[1]];
	Words = intersect(Words, allWords);
	n = length(Words);
	if (n > 0)
	{
		Name = NULL;
		for (k in 1:n)
		{
			Name = paste(Name,Words[k]);
		};	
	} else
	{
		#Name = allName[kName];
		#print(paste("Check",kName, ":", Name));
	};
	Name = sub("^[[:space:]]*(.*?)[[:space:]]*$", "\\1", Name, perl=TRUE);
	if (!is.element(Name,allSpecies))
	{
		Name = tolower(allName[kName]);
		print(paste("Check",kName, ":", Name));
	};
 
	#Delete the white space:
	Name = sub("^[[:space:]]*(.*?)[[:space:]]*$", "\\1", Name, perl=TRUE);
 
	#Rewrite the name:
	allName[kName] = Name;
};
allName = data.frame(allName);
rownames(allName) = allID;
write.table(allName, file = "16S.annot.00.txt", col.names = FALSE);

<comments> </comments>

Liên kết đến đây