Debian Codegraph @

scripts/clusters_and_fasta_to_fasta.py_DEPRECATED

raw
1 import sys
2 # coding: utf-8
3
4 # In[ ]:
5
6
7
8
9 # In[29]:
10
11 def store_clusters(cluster_file):
12 clusters=open(cluster_file,"r")
13 read_id_to_cluster_id={}
14 cluster_id_to_cluster_size={}
15 cluster_id=-1
16 for cluster in clusters:
17 # a line is "70166 70345 70409 70222 70406 70167 70223 69786 70407 69787 70408 70611 70610 70344 "
18 cluster_id+=1
19 cluster_id_to_cluster_size[cluster_id]=len(cluster.rstrip().split())
20 for read_id in cluster.rstrip().split():
21 read_id_to_cluster_id[int(read_id.split('-')[0])]=cluster_id # A line can be formated as 70166 70345-info_about_similarity
22 clusters.close()
23 return read_id_to_cluster_id, cluster_id_to_cluster_size
24
25 def assign_cluster_id_to_sequence_and_print(fasta_file, read_id_to_cluster_id, cluster_id_to_cluster_size):
26 fasta=open(fasta_file,"r")
27 read_id=-1
28 while True:
29 read_id+=1
30 comment=fasta.readline().rstrip()
31 #>SNP_higher_path_9996|P_1:30_C/T|high|nb_pol_1|left_unitig_length_8|right_unitig_length_5|C1_55|C2_0|C3_0|C4_0|C5_0|Q1_0|Q2_0|Q3_0|Q4_0|Q5_0|G1_0/0:7,170,1104|G2_1/1:1084,167,7|G3_1/1:1064,164,7|G4_1/1:984,152,6|G5_1/1:1264,194,7|rank_1
32 if not comment: break
33 sequence=fasta.readline().rstrip()
34 #gcggaatgAATTAGTGGTATGTCAAGAGGGACTGCTATCAACACTTACGTAGTGCACATATTTCTTTGCatcgc
35 SNP_id=comment.split("|")[0][1:] #SNP_higher_path_9996
36 if read_id not in read_id_to_cluster_id:
37 print("Warning, read id "+str(read_id)+" not in clusters",file=sys.stderr)
38 print(comment,file=sys.stderr)
39 print(sequence,file=sys.stderr)
40 SNP_id=">cluster_-1_size_1_"+SNP_id
41 else:
42 cluster_id=read_id_to_cluster_id[read_id]
43 SNP_id=">cluster_"+str(cluster_id)+"_size_"+str(cluster_id_to_cluster_size[cluster_id])+"_"+SNP_id
44 comment_new=SNP_id
45 for stuff in comment.split("|")[1:]:
46 comment_new+="|"+stuff
47 print (comment_new)
48 print (sequence)
49
50
51 fasta_file=sys.argv[1]
52 cluster_file=sys.argv[2]
53 read_id_to_cluster_id,cluster_id_to_cluster_size = store_clusters(cluster_file)
54 assign_cluster_id_to_sequence_and_print(fasta_file,read_id_to_cluster_id,cluster_id_to_cluster_size)
55