Repository navigation
Expand file tree
/
Copy pathpos.py
More file actions
180 lines (160 loc) · 8.46 KB
/
Copy pathpos.py
File metadata and controls
180 lines (160 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Libraries
import argparse
import logging
from pathlib import Path
from time import asctime
from Bio import SeqIO
from tqdm import tqdm
from collections import Counter
import pandas as pd
import pysam
#Arguments
parser = argparse.ArgumentParser(description='Process a fasta file for stats about the genome and C nucleotide. Add a GFF file for further analysis and to get the reference file you need to use in data.py . You can add the nucleotide you want (this script is cytosine oriented) and the annotations you want.')
parser.add_argument('-f', '--fasta', type=Path, help='load fasta file')
parser.add_argument('-g', '--gff', type=Path, help='load gff file')
parser.add_argument('-n', '--nucleotide', default="C", type=str, help='give a nucleotide to get stats for')
parser.add_argument('-a', '--annotation', default="gene", type=str, help='give the annotations from the gff files you want to study')
parser.add_argument("-d", "--debug", dest="loglevel", help="For debugging only.", action="store_const", const=logging.DEBUG, default=logging.INFO)
args = parser.parse_args()
# Data processing functions
def getGenomeStats(fasta, outfile, nucleotide):
""" Function that will return a txt file with basic information of your fasta : size of the chromosomes, size of the genome, nucleotides rates."""
# docu = http://biopython.org/DIST/docs/tutorial/Tutorial.html#sec53 chap 5
logging.info('Starting getGenomeStats')
total=0
totalMb=0
totalN=0
with open(outfile, mode = "w") as out:
for seq_ref in SeqIO.parse(fasta,"fasta"):
print(seq_ref.id, file=out)
logging.debug(repr(seq_ref.seq))
print(len(seq_ref)/10**6,"Mb.", file=out) #size of the chromosome
total+=len(seq_ref)
totalMb+=len(seq_ref)/10**6 #size of the genome
compteN=0
for nucl in tqdm((seq_ref)):
if nucl == nucleotide:
compteN+=1 #count the nucleotide
totalN+=compteN
print ("Le total est de",totalMb,"Mb.", file = out)
print('Il y a ',totalN/total*100,'% de',nucleotide, file = out)
logging.info('getGenomeStats finished')
def getNucleotidePosition (fasta,outfile, nucleotide):
"""Function that will write the positions of a given nucleotide from a fasta file"""
logging.info('Starting getNucleotidePosition')
with open(outfile, mode = "w") as out:
for seq_ref in SeqIO.parse(fasta,"fasta"):
for index, nucl in tqdm(enumerate(seq_ref, start = 1)):
if nucl == nucleotide:
print(seq_ref.id, index, file = out, sep="\t")
logging.info('getNucleotidePosition finished')
def GetCPos(sequence):
""""Function that will get the C positions"""
logging.info('Starting getNucleotidePosition')
positions=[]
for index, nucl in tqdm(enumerate(sequence, start = 1)):
if nucl == 'C':
positions.append(index)
logging.debug(positions)
logging.info('getGenomeStats finished')
return positions
def GetCContext(sequence):
"""Function that will get the C contexts"""
logging.info('Starting getCContext')
contextes=[]
for index, nucl in tqdm(enumerate(sequence, start = 1)):
if index+1 < len(sequence) :
if nucl =='C':
contexte = sequence[index:index+2]
if contexte[0]=='G':
contexte= 'CG'
elif contexte[1]=='G':
contexte= 'CHG'
else : contexte= 'CHH'
contextes.append(contexte)
else: pass
logging.info('getCContext finished')
return contextes
def dicoCompoGene(fasta):
logging.info('Starting dicoCompoGene')
genome = {}
# genome[key] = valeur
for seq_ref in SeqIO.parse(fasta,"fasta"):
genome[seq_ref.id] = seq_ref.seq
logging.info('dicoCompoGene finished')
return genome
def getGff(gff, outfile, annotation) :
"""Function that will parse the gff file to return a clean file."""
logging.info('Starting getGff')
with open (outfile, mode = "w") as out:
with open(gff) as gff:
for f in pysam.tabix_iterator(gff, parser = pysam.asGTF()):
# Retrieve gene feature information
if f.feature in annotation:
try:
# https://pysam.readthedocs.io/en/latest/api.html
attr_dict = dict(i.split("=") for i in f.attributes.split(";"))
name = attr_dict["Name"]
start = f.start
end = f.end
chromosome = f.contig
strand=f.strand
except ValueError:
raise Exception('Error. Cannot split the attributes in gff. Is the annotation file in GFF3 format?')
print(f.feature, name, chromosome,strand, start, end, sep="\t", file= out)
logging.info('getGff finished')
return f.feature, name, chromosome,strand, start, end
def getGeneComposition(fasta, gff, out, out2):
"""Function that will give two outfiles : first the reference file of the cytosines of the genome to use in data.py.
Second outfile gives informations on the genome, it adds the number of ATGC and the C rate """
logging.info('Starting getGeneComposition')
genome=dicoCompoGene(fasta)
with open(gff, mode = "r") as gff:
with open(out, 'w') as outfile:
listetotal=[]
nbrenucleotides={}
print('Chr','Pos_Locale', 'Pos_Globale','ID','Taille','Contexte',sep='\t', file=outfile) #header
for line in gff.readlines():
if line.startswith('#'):
continue
logging.debug(line)
type, id, chr, strand, start, end = line.strip().split("\t")
genesequence=genome[chr][int(start):int(end)]
genesequencecont=genome[chr][int(start):int(end)+2]
nbrenucleotides=Counter(genesequence) #fait un dico de la sequence
list_C_relative = GetCPos(genesequence) #obtient la position des C relatifs aux elements
list_contexte = GetCContext(genesequencecont)
list_C_genomic = [int(index) + int(start) for index in list_C_relative]
assert len(list_C_genomic) == len(list_C_relative), "Problem in list position" #permet de s'assurer de qqch, si faux message en ""
taille=int(end)-int(start)+1
for index_listC, index_list_G, contexte in zip(list_C_relative, list_C_genomic, list_contexte): #zip permet de lire plusieurs listes de mm taille en mm temps
print(chr, index_listC, index_list_G, id, taille, contexte, sep='\t', file=outfile)
liste=[type, id, chr, strand, start, end,nbrenucleotides['A'], nbrenucleotides['G'], nbrenucleotides['T'],nbrenucleotides['C'], taille, nbrenucleotides['C']/taille*100 ]
listetotal.append(liste)
tab=pd.DataFrame(listetotal, columns=['Annotation','ID','Chromosome', 'Brin','Start','End','Nbre_A', 'Nbre_G','Nbre_T','Nbre_C','Taille','Pourcentage_de_C'])
logging.debug(tab)
tab.sort_values(by=['Pourcentage_de_C'], ascending=False, inplace=True)
tab.to_csv(out2, sep='\t', index=False)
logging.info('getGeneComposition finished')
#Script/pipeline functions
def getAnalyseRef(fasta=args.fasta, nucleotide=args.nucleotide, annotation=args.annotation, gff=args.gff) :
getGenomeStats(fasta, 'Stats.txt',nucleotide)
getNucleotidePosition(fasta, nucleotide+".txt",nucleotide)
if not gff:
logging.warning('Please load a gff file if you want a complete analysis.')
else :
getGff(gff, "GffTraite.txt", annotation)
getGeneComposition(fasta,"GffTraite.txt" ,"RefPosC.txt", "Infos.txt")
def check_args(args):
if args.fasta and not args.fasta.exists():
logging.warning("Fasta input does not exist. Please give a valid file.")
exit()
if args.gff and not args.gff.exists():
logging.warning("Gff input does not exist. Please give a valid file.")
exit()
if __name__ == '__main__':
check_args(args)
logging.basicConfig(filename='myapppos.log', filemode='w', format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S', level=args.loglevel)
logging.info('Started')
getAnalyseRef(fasta=args.fasta, nucleotide=args.nucleotide, annotation=args.annotation, gff=args.gff)
logging.info('Finished')