IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Chercher un mot, et extraire la réponse dans un fichier


Sujet :

Python

  1. #1
    Membre à l'essai Avatar de Shyma
    Femme Profil pro
    Étudiant
    Inscrit en
    Novembre 2015
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 100
    Points : 19
    Points
    19
    Par défaut Chercher un mot, et extraire la réponse dans un fichier
    Bonjour,

    J'ai un fichier tab, je voudrais chercher des mots clés, et extraire la réponse dans un fichier, mon fichier tab est mal organisé et il est plein d 'espace.

    Voilà le fichier en question :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Entry	Entry name	Status	Protein names	Gene names	Organism	Length	Protein existence	Absorption	Enzyme regulation	pH dependence	DNA binding	Site	Nucleotide binding	Fragment	Gene encoded by	Alternative products (isoforms)	Mass spectrometry	Polymorphism	RNA editing	Sequence caution	Mass	Sequence	Proteomes	Pathway	Active site	Binding site	Catalytic activity	EC number	Cofactor	Function [CC]	Kinetics	Redox potential	Temperature dependence	Calcium binding	Metal binding
    P04637	P53_HUMAN	reviewed	Cellular tumor antigen p53 (Antigen NY-CO-13) (Phosphoprotein p53) (Tumor suppressor p53)	TP53 P53	Homo sapiens (Human)	393	Evidence at protein level				DNA_BIND 102 292	SITE 120 120 Interaction with DNA.				ALTERNATIVE PRODUCTS:  Event=Alternative promoter usage, Alternative splicing; Named isoforms=9;  Name=1; Synonyms=p53, p53alpha; IsoId=P04637-1; Sequence=Displayed; Name=2; Synonyms=I9RET, p53beta; IsoId=P04637-2; Sequence=VSP_006535, VSP_006536; Note=Expressed in quiescent lymphocytes. Seems to be non-functional. May be produced at very low levels due to a premature stop codon in the mRNA, leading to nonsense-mediated mRNA decay.; Name=3; Synonyms=p53gamma; IsoId=P04637-3; Sequence=VSP_040560, VSP_040561; Note=Expressed in quiescent lymphocytes. Seems to be non-functional. May be produced at very low levels due to a premature stop codon in the mRNA, leading to nonsense-mediated mRNA decay.; Name=4; Synonyms=Del40-p53, Del40-p53alpha, p47; IsoId=P04637-4; Sequence=VSP_040832; Name=5; Synonyms=Del40-p53beta; IsoId=P04637-5; Sequence=VSP_040832, VSP_006535, VSP_006536; Name=6; Synonyms=Del40-p53gamma; IsoId=P04637-6; Sequence=VSP_040832, VSP_040560, VSP_040561; Name=7; Synonyms=Del133-p53, Del133-p53alpha; IsoId=P04637-7; Sequence=VSP_040833; Note=Produced by alternative promoter usage.; Name=8; Synonyms=Del133-p53beta; IsoId=P04637-8; Sequence=VSP_040833, VSP_006535, VSP_006536; Note=Produced by alternative promoter usage and alternative splicing.; Name=9; Synonyms=Del133-p53gamma; IsoId=P04637-9; Sequence=VSP_040833, VSP_040560, VSP_040561; Note=Produced by alternative promoter usage and alternative splicing.; 					43,653	MEEPQSDPSVEPPLSQETFSDLWKLLPENNVLSPLPSQAMDDLMLSPDDIEQWFTEDPGPDEAPRMPEAAPPVAPAPAAPTPAAPAPAPSWPLSSSVPSQKTYQGSYGFRLGFLHSGTAKSVTCTYSPALNKMFCQLAKTCPVQLWVDSTPPPGTRVRAMAIYKQSQHMTEVVRRCPHHERCSDSDGLAPPQHLIRVEGNLRVEYLDDRNTFRHSVVVPYEPPEVGSDCTTIHYNYMCNSSCMGGMNRRPILTIITLEDSSGNLLGRNSFEVRVCACPGRDRRTEEENLRKKGEPHHELPPGSTKRALPNNTSSSPQPKKKPLDGEYFTLQIRGRERFEMFRELNEALELKDAQAGKEPGGSRAHSSHLKSKKGQSTSRHKKLMFKTEGPDSD	UP000005640: Chromosome 17						COFACTOR: Name=Zn(2+); Xref=ChEBI:CHEBI:29105; ;  Note=Binds 1 zinc ion per subunit.;	FUNCTION: Acts as a tumor suppressor in many tumor types; induces growth arrest or apoptosis depending on the physiological circumstances and cell type. Involved in cell cycle regulation as a trans-activator that acts to negatively regulate cell division by controlling a set of genes required for this process. One of the activated genes is an inhibitor of cyclin-dependent kinases. Apoptosis induction seems to be mediated either by stimulation of BAX and FAS antigen expression, or by repression of Bcl-2 expression. In cooperation with mitochondrial PPIF is involved in activating oxidative stress-induced necrosis; the function is largely independent of transcription. Induces the transcription of long intergenic non-coding RNA p21 (lincRNA-p21) and lincRNA-Mkln1. LincRNA-p21 participates in TP53-dependent transcriptional repression leading to apoptosis and seem to have to effect on cell-cycle regulation. Implicated in Notch signaling cross-over. Prevents CDK7 kinase activity when associated to CAK complex in response to DNA damage, thus stopping cell cycle progression. Isoform 2 enhances the transactivation activity of isoform 1 from some but not all TP53-inducible promoters. Isoform 4 suppresses transactivation activity and impairs growth suppression mediated by isoform 1. Isoform 7 inhibits isoform 1-mediated apoptosis. Regulates the circadian clock by repressing CLOCK-ARNTL/BMAL1-mediated transcriptional activation of PER2 (PubMed:24051492). {ECO:0000269|PubMed:11025664, ECO:0000269|PubMed:12810724, ECO:0000269|PubMed:15186775, ECO:0000269|PubMed:15340061, ECO:0000269|PubMed:17317671, ECO:0000269|PubMed:17349958, ECO:0000269|PubMed:19556538, ECO:0000269|PubMed:20673990, ECO:0000269|PubMed:20959462, ECO:0000269|PubMed:22726440, ECO:0000269|PubMed:24051492, ECO:0000269|PubMed:9840937}.					METAL 176 176 Zinc.; METAL 179 179 Zinc.; METAL 238 238 Zinc.; METAL 242 242 Zinc.
    Et voilà mon bout de code que je viens de commencer , mais il m'affiche tout le fichier !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    chaine = "Cellular"
    f = open("uniprot-id%3AP04637.tab", "r")
    for line in f:
    	if chaine in line:
    		print line
     
    f.close()

  2. #2
    Expert éminent Avatar de CosmoKnacki
    Homme Profil pro
    Justicier interdimensionnel
    Inscrit en
    Mars 2009
    Messages
    2 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Justicier interdimensionnel

    Informations forums :
    Inscription : Mars 2009
    Messages : 2 858
    Points : 6 556
    Points
    6 556
    Par défaut
    mon fichier tab est mal organisé et il est plein d 'espace
    Les deux lignes d'exemples que tu donnes sont formatées correctement (il y a bien 36 champs séparés par une tabulation pour chaque ligne) et le fait que les champs aient des espaces ne doit pas poser de problème particulier.

    il m'affiche tout le fichier
    Ah bon? Est-ce qu'il affiche la première ligne avec les entêtes (ligne qui ne contient pas le mot "Cellular")? As-tu essayé avec un mot plus rare?
    Brachygobius xanthozonus
    Ctenobrycon Gymnocorymbus

  3. #3
    Membre à l'essai Avatar de Shyma
    Femme Profil pro
    Étudiant
    Inscrit en
    Novembre 2015
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 100
    Points : 19
    Points
    19
    Par défaut
    En fait mon fichier ne se présente pas sous cette forme là ; Je veux dire en 2 lignes.

    Oui j'ai essayé avec d'autres mots, mais il m'affiche toujours tout le fichier !

    Là par exemple avec le mot "FUNCTION"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    chaine = "FUNCTION"
    f = open("uniprot-id%3AP04637.tab", "r")
    for line in f:
    	if chaine in line:
    		print line
     
    f.close()
    voilà ce qu'il m'affiche :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    P04637	P53_HUMAN	reviewed	Cellular tumor antigen p53 (Antigen NY-CO-13) (Phosphoprotein p53) (Tumor suppressor p53)	TP53 P53Homo sapiens (Human)	393	Evidence at protein level				DNA_BIND 102 292	SITE 120 120 Interaction with DNA.				ALTERNATIVE PRODUCTS:  Event=Alternative promoter usage, Alternative splicing; Named isoforms=9;  Name=1; Synonyms=p53, p53alpha; IsoId=P04637-1; Sequence=Displayed; Name=2; Synonyms=I9RET, p53beta; IsoId=P04637-2; Sequence=VSP_006535, VSP_006536; Note=Expressed in quiescent lymphocytes. Seems to be non-functional. May be produced at very low levels due to a premature stop codon in the mRNA, leading to nonsense-mediated mRNA decay.; Name=3; Synonyms=p53gamma; IsoId=P04637-3; Sequence=VSP_040560, VSP_040561; Note=Expressed in quiescent lymphocytes. Seems to be non-functional. May be produced at very low levels due to a premature stop codon in the mRNA, leading to nonsense-mediated mRNA decay.; Name=4; Synonyms=Del40-p53, Del40-p53alpha, p47; IsoId=P04637-4; Sequence=VSP_040832; Name=5; Synonyms=Del40-p53beta; IsoId=P04637-5; Sequence=VSP_040832, VSP_006535, VSP_006536; Name=6; Synonyms=Del40-p53gamma; IsoId=P04637-6; Sequence=VSP_040832, VSP_040560, VSP_040561; Name=7; Synonyms=Del133-p53, Del133-p53alpha; IsoId=P04637-7; Sequence=VSP_040833; Note=Produced by alternative promoter usage.; Name=8; Synonyms=Del133-p53beta; IsoId=P04637-8; Sequence=VSP_040833, VSP_006535, VSP_006536; Note=Produced by alternative promoter usage and alternative splicing.; Name=9; Synonyms=Del133-p53gamma; IsoId=P04637-9; Sequence=VSP_040833, VSP_040560, VSP_040561; Note=Produced by alternative promoter usage and alternative splicing.; 					43,653	MEEPQSDPSVEPPLSQETFSDLWKLLPENNVLSPLPSQAMDDLMLSPDDIEQWFTEDPGPDEAPRMPEAAPPVAPAPAAPTPAAPAPAPSWPLSSSVPSQKTYQGSYGFRLGFLHSGTAKSVTCTYSPALNKMFCQLAKTCPVQLWVDSTPPPGTRVRAMAIYKQSQHMTEVVRRCPHHERCSDSDGLAPPQHLIRVEGNLRVEYLDDRNTFRHSVVVPYEPPEVGSDCTTIHYNYMCNSSCMGGMNRRPILTIITLEDSSGNLLGRNSFEVRVCACPGRDRRTEEENLRKKGEPHHELPPGSTKRALPNNTSSSPQPKKKPLDGEYFTLQIRGRERFEMFRELNEALELKDAQAGKEPGGSRAHSSHLKSKKGQSTSRHKKLMFKTEGPDSD	UP000005640: Chromosome 17						COFACTOR: Name=Zn(2+); Xref=ChEBI:CHEBI:29105; ;  Note=Binds 1 zinc ion per subunit.;	FUNCTION: Acts as a tumor suppressor in many tumor types; induces growth arrest or apoptosis depending on the physiological circumstances and cell type. Involved in cell cycle regulation as a trans-activator that acts to negatively regulate cell division by controlling a set of genes required for this process. One of the activated genes is an inhibitor of cyclin-dependent kinases. Apoptosis induction seems to be mediated either by stimulation of BAX and FAS antigen expression, or by repression of Bcl-2 expression. In cooperation with mitochondrial PPIF is involved in activating oxidative stress-induced necrosis; the function is largely independent of transcription. Induces the transcription of long intergenic non-coding RNA p21 (lincRNA-p21) and lincRNA-Mkln1. LincRNA-p21 participates in TP53-dependent transcriptional repression leading to apoptosis and seem to have to effect on cell-cycle regulation. Implicated in Notch signaling cross-over. Prevents CDK7 kinase activity when associated to CAK complex in response to DNA damage, thus stopping cell cycle progression. Isoform 2 enhances the transactivation activity of isoform 1 from some but not all TP53-inducible promoters. Isoform 4 suppresses transactivation activity and impairs growth suppression mediated by isoform 1. Isoform 7 inhibits isoform 1-mediated apoptosis. Regulates the circadian clock by repressing CLOCK-ARNTL/BMAL1-mediated transcriptional activation of PER2 (PubMed:24051492). {ECO:0000269|PubMed:11025664, ECO:0000269|PubMed:12810724, ECO:0000269|PubMed:15186775, ECO:0000269|PubMed:15340061, ECO:0000269|PubMed:17317671, ECO:0000269|PubMed:17349958, ECO:0000269|PubMed:19556538, ECO:0000269|PubMed:20673990, ECO:0000269|PubMed:20959462, ECO:0000269|PubMed:22726440, ECO:0000269|PubMed:24051492, ECO:0000269|PubMed:9840937}.	METAL 176 176 Zinc.; METAL 179 179 Zinc.; METAL 238 238 Zinc.; METAL 242 242 Zinc.

  4. #4
    Expert éminent Avatar de CosmoKnacki
    Homme Profil pro
    Justicier interdimensionnel
    Inscrit en
    Mars 2009
    Messages
    2 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Justicier interdimensionnel

    Informations forums :
    Inscription : Mars 2009
    Messages : 2 858
    Points : 6 556
    Points
    6 556
    Par défaut
    J'imagine qu'il y a plus de deux lignes à ton fichier.

    Quand tu lui demandes de rechercher "FUNCTION" Il affiche une ligne complète qui contient "FUNCTION", ce n'est pas ce que tu attends? (parce que c'est exactement ce que ton code demande). Quel est la réponse que tu attends?

    Est-ce que ton fichier est trouvable quelque part, as-tu un lien vers ce fichier?
    Brachygobius xanthozonus
    Ctenobrycon Gymnocorymbus

  5. #5
    Membre à l'essai Avatar de Shyma
    Femme Profil pro
    Étudiant
    Inscrit en
    Novembre 2015
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 100
    Points : 19
    Points
    19
    Par défaut
    Il n'affiche pas une ligne, pas la ligne qui contient le mot, mais il m'affiche tout le fichier en entier, je voudrais juste qu'il m'affiche ce que ce mot indique ! Genre :

    FUNCTION : FUNCTION: Acts as a tumor suppressor in many tumor types;

  6. #6
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 281
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 281
    Points : 36 768
    Points
    36 768
    Par défaut
    Salut,

    Citation Envoyé par Shyma Voir le message
    Il n'affiche pas une ligne, pas la ligne qui contient le mot, mais il m'affiche tout le fichier en entier, je voudrais juste qu'il m'affiche ce que ce mot indique ! Genre :

    FUNCTION : FUNCTION: Acts as a tumor suppressor in many tumor types;
    Votre programme affiche ce que vous lui avez demandé d'afficher...

    Il faudrait commencer par rendre compte que votre fichier représente un tableau à 2 dimensions: lignes et colonnes.
    Par exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    >>> data = '''A\tB\tC\tD
    ... 1\t2\t3\t4
    ... 1\t2\t3\t4
    ... '''
    >>> data
    'A\tB\tC\tD\n1\t2\t3\t4\n1\t2\t3\t4\n'
    sont des données ayant la même structure: les lignes sont matérialisées par '\n' et les colonnes par '\t'.
    Et si on veut parcourir cette structure là, il suffit de découper cette chaine de caractères aux bons endroits:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    >>> data.splitlines()
    ['A\tB\tC\tD', '1\t2\t3\t4', '1\t2\t3\t4']
    Et si on découpe chaque ligne en colonnes, çà donne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    >>> for line in data.splitlines():
    ...     print(line.split('\t'))
    ...
    ['A', 'B', 'C', 'D']
    ['1', '2', '3', '4']
    ['1', '2', '3', '4']
    i.e. des "cases" qui auront un numéro de ligne x colonne.

    Cela compris, compliquons les choses:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    >>> data = 'A\tB\tC\tD\n1\t2\t3\t4:1\n1\t2\t3\t4:2\n'
    >>> print(data)
    A       B       C       D
    1       2       3       4:1
    1       2       3       4:2
    Afficher les cases qui contiennent '4' est différent d'afficher les lignes qui contiennent '4':

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    >>> for line in data.splitlines():
    ...     if '4' in line:
    ...        print(line)
    ...
    1       2       3       4:1
    1       2       3       4:2
    >>>
    puisque c'est un tableau 2D, accéder aux cases, c'est 2 boucles/2 indices:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    >>> for line in data.splitlines():
    ...     items = line.split('\t')
    ...     for e in items:
    ...         if '4' in e:
    ...            print(e)
    ...
    4:1
    4:2
    >>>
    Sauf à savoir que ce que vous cherchez est dans la 4ème colonne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    >>> for line in data.splitlines():
    ...     items = line.split('\t')
    ...     if '4' in items[3]:
    ...         print(items[3])
    ...
    4:1
    4:2
    Dans ce cas, plus besoin de la 2ème boucle (on connaît la colonne intéressante) mais il faut quand même découper les lignes en colonnes pour accéder aux "cases". Et pour le reste si vous ne comprenez pas, pour apprendre à utiliser chaînes de caractères et listes de listes il y a de bons tutos...

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

Discussions similaires

  1. Extraire des données dans un fichier
    Par kali38 dans le forum Général Python
    Réponses: 4
    Dernier message: 31/01/2011, 13h51
  2. Réponses: 5
    Dernier message: 05/12/2008, 15h23
  3. Extraire un float dans un fichier .txt
    Par gregterraz dans le forum C
    Réponses: 4
    Dernier message: 20/05/2008, 09h30
  4. Extraire des données dans un fichier .csv
    Par orj30754 dans le forum C
    Réponses: 7
    Dernier message: 03/11/2006, 14h48
  5. rechercher et extraire deux chaines dans un fichier
    Par piotr dans le forum Langage
    Réponses: 16
    Dernier message: 13/07/2006, 15h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo