-
Notifications
You must be signed in to change notification settings - Fork 8
/
cut_pfas_in_contigs.py
executable file
·113 lines (59 loc) · 2.55 KB
/
cut_pfas_in_contigs.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#-------------------------------------
#
# SCRIPT PYTHON CUT_PFAS_IN_CONTIGS
# Take a PFAS file : split it contig by contig
# 2 output format are possible
# Yan Holtz, yan1166@hotmail.com
#-------------------------------------
import os
import sys
import re
try:
import argparse
except ImportError:
print"oops, the import /argparse/ didn't work"
parser = argparse.ArgumentParser(description= '\n\nTake a PFAS file : split it contig by contig\n\n')
parser.add_argument('-pfas', required=True, help=' input PFAS file')
parser.add_argument('-format', required=True, help=' output format : "nabholz" or "initial". With Nabholz, sequence names are renamed, permitting to use the diversity indexes calculation programmes of Benoit Nabholz')
args = parser.parse_args()
fic_pfas=args.pfas
format=args.format
#------------------------------------------------------------------------------------------------------
print "\n\n--------\n\nBienvenu ! Merci de me faire taffer, je commencais à m'ennuyer !\n\n------------\n\n"
#Initiation
nb_contigs_traites=0
my_dico=dict()
print "\n\n--------\n\nRemplissage\n\n------------\n\n"
for line in open(fic_pfas):
line=line.strip()
#Si la ligne est un nom de séquence
if line.startswith('>'):
#De quel contig s'agit il ?
contig=line.split("|ind")[0]
contig=contig.replace(">","")
#Pour le blé tendre qui a des noms de contig a ralonge, n les raccourci
if contig.startswith("Traes"):
contig=contig.split("|")[0]
#numéro de l'allèle ?
allele=line.split("Allele_")[1]
#On change la ligne pour le format Nabholz:
if format =="nabholz":
line=re.sub(r">.*ind",r">EPOR",line)
line=re.sub(r"\|Allele_",r".",line)
#Et maintenant je rempli le dico
if contig in my_dico:
my_dico[contig]+=line+"\n"
else:
my_dico[contig]=line+"\n"
#------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------
#Impression du dico.
for contig in my_dico:
nb_contigs_traites += 1
fichier_de_sortie=contig+".fasta"
tmp=open(fichier_de_sortie , "w")
tmp.write(my_dico[contig])
print "\n\n--------\n\nTravail terminé, "+str(nb_contigs_traites)+" Contigs ont été extraits !\n\n--------"
#------------------------------------------------------------------------------------------------------