0

Tengo un programe que me genera documentos txt y me gustaría seleccionar por lineas un conjunto de palabras usando Python.

Yo lo que necesito exactamente es:

  1. Seleccionar el nombre del gen, que el primero empieza en la linea 19 y el último acaba en la -9. Si miramos este txt sería el Ribosomal_S9 y el TIGR03953

  2. Seleccionar el número que aparece detras de LEN.

  3. Crear un nuevo txt y guardar el nombre del gen y su lontitud

Muestro el txt completo para que se pueda ver lo complicado (al menos para mi) de hacer lo que quiero.

[2020-03-14 10:51:33] INFO: GTDB-Tk v1.0.2
[2020-03-14 10:51:33] INFO: gtdbtk align --identify_dir /Users/monkiky/Desktop/GTDB/Output2/ --skip_trimming --out_dir /Users/monkiky/Desktop/GTDB/Output_align2/
[2020-03-14 10:51:33] INFO: Using GTDB-Tk reference data version r89: /Users/monkiky/Desktop/GTDB/gtdbtk/release89
[2020-03-14 10:51:33] INFO: Aligning markers in 2 genomes with 1 threads.
[2020-03-14 10:51:33] INFO: Processing 1 genomes identified as bacterial.
[2020-03-14 10:51:35] INFO: Read concatenated alignment for 23458 GTDB genomes.
[2020-03-14 10:51:38] INFO: Skipping custom filtering and selection of columns.
[2020-03-14 10:51:38] INFO: Creating concatenated alignment for 23459 GTDB and user genomes.
[2020-03-14 10:51:50] INFO: Creating concatenated alignment for 1 user genomes.
[2020-03-14 10:51:50] INFO: Done.
[2020-03-14 11:04:07] INFO: GTDB-Tk v1.0.2
[2020-03-14 11:04:07] INFO: gtdbtk align --identify_dir /Users/monkiky/Desktop/GTDB/Output2/ --custom_msa_filters --out_dir /Users/monkiky/Desktop/GTDB/Output_align2/
[2020-03-14 11:04:07] INFO: Using GTDB-Tk reference data version r89: /Users/monkiky/Desktop/GTDB/gtdbtk/release89
[2020-03-14 11:04:07] INFO: Aligning markers in 2 genomes with 1 threads.
[2020-03-14 11:04:07] INFO: Processing 1 genomes identified as bacterial.
[2020-03-14 11:04:09] INFO: Read concatenated alignment for 23458 GTDB genomes.
[2020-03-14 11:04:11] INFO: Performing custom filtering and selection of columns.
[2020-03-14 11:04:11] INFO: Reading marker info.
[2020-03-14 11:04:11] INFO: Length of MSA and length of marker genes both equal 41155 columns
[2020-03-14 11:04:11] INFO: Randomly sampling 42 columns passing filtering criteria from each marker gene.
[2020-03-14 11:04:12] INFO: Ribosomal_S9: Ribosomal protein S9/S16: S:0, E:121, LEN:121, COLS:89, PERC:73.6
[2020-03-14 11:04:12] INFO: Ribosomal_S8: Ribosomal protein S8: S:121, E:250, LEN:129, COLS:95, PERC:73.6
[2020-03-14 11:04:13] INFO: Ribosomal_L10: Ribosomal protein L10: S:250, E:350, LEN:100, COLS:80, PERC:80.0
[2020-03-14 11:04:14] INFO: GrpE: GrpE: S:350, E:516, LEN:166, COLS:118, PERC:71.1
[2020-03-14 11:04:14] INFO: DUF150: Uncharacterised BCR, YhbC family COG0779: S:516, E:657, LEN:141, COLS:104, PERC:73.8
[2020-03-14 11:04:15] INFO: PNPase: Polyribonucleotide nucleotidyltransferase, RNA binding domain: S:657, E:740, LEN:83, COLS:48, PERC:57.8
[2020-03-14 11:04:16] INFO: TIGR00006: TIGR00006: 16S rRNA (cytosine(1402)-N(4))-methyltransferase: S:740, E:1050, LEN:310, COLS:219, PERC:70.6
[2020-03-14 11:04:18] INFO: TIGR00019: prfA: peptide chain release factor 1: S:1050, E:1411, LEN:361, COLS:281, PERC:77.8
[2020-03-14 11:04:20] INFO: TIGR00020: prfB: peptide chain release factor 2: S:1411, E:1776, LEN:365, COLS:244, PERC:66.8
[2020-03-14 11:04:20] INFO: TIGR00029: S20: ribosomal protein bS20: S:1776, E:1863, LEN:87, COLS:74, PERC:85.1
[2020-03-14 11:04:21] INFO: TIGR00043: TIGR00043: rRNA maturation RNase YbeY: S:1863, E:1974, LEN:111, COLS:77, PERC:69.4
[2020-03-14 11:04:23] INFO: TIGR00054: TIGR00054: RIP metalloprotease RseP: S:1974, E:2395, LEN:421, COLS:278, PERC:66.0
[2020-03-14 11:04:24] INFO: TIGR00059: L17: ribosomal protein bL17: S:2395, E:2507, LEN:112, COLS:98, PERC:87.5
[2020-03-14 11:04:24] INFO: TIGR00061: L21: ribosomal protein bL21: S:2507, E:2608, LEN:101, COLS:85, PERC:84.2
[2020-03-14 11:04:25] INFO: TIGR00064: ftsY: signal recognition particle-docking protein FtsY: S:2608, E:2887, LEN:279, COLS:184, PERC:65.9
[2020-03-14 11:04:27] INFO: TIGR00065: ftsZ: cell division protein FtsZ: S:2887, E:3240, LEN:353, COLS:215, PERC:60.9
[2020-03-14 11:04:28] INFO: TIGR00082: rbfA: ribosome-binding factor A: S:3240, E:3355, LEN:115, COLS:84, PERC:73.0
[2020-03-14 11:04:29] INFO: TIGR00083: ribF: riboflavin biosynthesis protein RibF: S:3355, E:3645, LEN:290, COLS:193, PERC:66.6
[2020-03-14 11:04:30] INFO: TIGR00084: ruvA: Holliday junction DNA helicase RuvA: S:3645, E:3837, LEN:192, COLS:125, PERC:65.1
[2020-03-14 11:04:31] INFO: TIGR00086: smpB: SsrA-binding protein: S:3837, E:3981, LEN:144, COLS:112, PERC:77.8
[2020-03-14 11:04:32] INFO: TIGR00088: trmD: tRNA (guanine(37)-N(1))-methyltransferase: S:3981, E:4214, LEN:233, COLS:169, PERC:72.5
[2020-03-14 11:04:33] INFO: TIGR00090: rsfS_iojap_ybeB: ribosome silencing factor: S:4214, E:4313, LEN:99, COLS:74, PERC:74.7
[2020-03-14 11:04:35] INFO: TIGR00092: TIGR00092: GTP-binding protein YchF: S:4313, E:4681, LEN:368, COLS:284, PERC:77.2
[2020-03-14 11:04:36] INFO: TIGR00095: TIGR00095: 16S rRNA (guanine(966)-N(2))-methyltransferase RsmD: S:4681, E:4875, LEN:194, COLS:113, PERC:58.2
[2020-03-14 11:04:38] INFO: TIGR00115: tig: trigger factor: S:4875, E:5285, LEN:410, COLS:311, PERC:75.9
[2020-03-14 11:04:39] INFO: TIGR00116: tsf: translation elongation factor Ts: S:5285, E:5578, LEN:293, COLS:237, PERC:80.9
[2020-03-14 11:04:40] INFO: TIGR00138: rsmG_gidB: 16S rRNA (guanine(527)-N(7))-methyltransferase RsmG: S:5578, E:5761, LEN:183, COLS:122, PERC:66.7
[2020-03-14 11:04:41] INFO: TIGR00158: L9: ribosomal protein bL9: S:5761, E:5909, LEN:148, COLS:116, PERC:78.4
[2020-03-14 11:04:41] INFO: TIGR00166: S6: ribosomal protein bS6: S:5909, E:6004, LEN:95, COLS:77, PERC:81.1
[2020-03-14 11:04:42] INFO: TIGR00168: infC: translation initiation factor IF-3: S:6004, E:6169, LEN:165, COLS:131, PERC:79.4
[2020-03-14 11:04:43] INFO: TIGR00186: rRNA_methyl_3: RNA methyltransferase, TrmH family, group 3: S:6169, E:6409, LEN:240, COLS:178, PERC:74.2
[2020-03-14 11:04:46] INFO: TIGR00194: uvrC: excinuclease ABC subunit C: S:6409, E:6983, LEN:574, COLS:402, PERC:70.0
[2020-03-14 11:04:47] INFO: TIGR00250: RNAse_H_YqgF: putative transcription antitermination factor YqgF: S:6983, E:7113, LEN:130, COLS:91, PERC:70.0
[2020-03-14 11:04:49] INFO: TIGR00337: PyrG: CTP synthase: S:7113, E:7639, LEN:526, COLS:363, PERC:69.0
[2020-03-14 11:04:54] INFO: TIGR00344: alaS: alanine--tRNA ligase: S:7639, E:8486, LEN:847, COLS:602, PERC:71.1
[2020-03-14 11:04:56] INFO: TIGR00362: DnaA: chromosomal replication initiator protein DnaA: S:8486, E:8923, LEN:437, COLS:304, PERC:69.6
[2020-03-14 11:04:58] INFO: TIGR00382: clpX: ATP-dependent Clp protease, ATP-binding subunit ClpX: S:8923, E:9337, LEN:414, COLS:249, PERC:60.1
[2020-03-14 11:05:03] INFO: TIGR00392: ileS: isoleucine--tRNA ligase: S:9337, E:10198, LEN:861, COLS:636, PERC:73.9
[2020-03-14 11:05:07] INFO: TIGR00396: leuS_bact: leucine--tRNA ligase: S:10198, E:11041, LEN:843, COLS:637, PERC:75.6
[2020-03-14 11:05:10] INFO: TIGR00398: metG: methionine--tRNA ligase: S:11041, E:11571, LEN:530, COLS:396, PERC:74.7
[2020-03-14 11:05:12] INFO: TIGR00414: serS: serine--tRNA ligase: S:11571, E:11989, LEN:418, COLS:342, PERC:81.8
[2020-03-14 11:05:14] INFO: TIGR00416: sms: DNA repair protein RadA: S:11989, E:12443, LEN:454, COLS:306, PERC:67.4
[2020-03-14 11:05:16] INFO: TIGR00420: trmU: tRNA (5-methylaminomethyl-2-thiouridylate)-methyltransferase: S:12443, E:12794, LEN:351, COLS:241, PERC:68.7
[2020-03-14 11:05:17] INFO: TIGR00431: TruB: tRNA pseudouridine(55) synthase: S:12794, E:13004, LEN:210, COLS:151, PERC:71.9
[2020-03-14 11:05:20] INFO: TIGR00435: cysS: cysteine--tRNA ligase: S:13004, E:13470, LEN:466, COLS:333, PERC:71.5
[2020-03-14 11:05:21] INFO: TIGR00436: era: GTP-binding protein Era: S:13470, E:13740, LEN:270, COLS:197, PERC:73.0
[2020-03-14 11:05:23] INFO: TIGR00442: hisS: histidine--tRNA ligase: S:13740, E:14146, LEN:406, COLS:304, PERC:74.9
[2020-03-14 11:05:25] INFO: TIGR00445: mraY: phospho-N-acetylmuramoyl-pentapeptide-transferase: S:14146, E:14467, LEN:321, COLS:251, PERC:78.2
[2020-03-14 11:05:28] INFO: TIGR00456: argS: arginine--tRNA ligase: S:14467, E:15036, LEN:569, COLS:399, PERC:70.1
[2020-03-14 11:05:31] INFO: TIGR00459: aspS_bact: aspartate--tRNA ligase: S:15036, E:15622, LEN:586, COLS:437, PERC:74.6
[2020-03-14 11:05:32] INFO: TIGR00460: fmt: methionyl-tRNA formyltransferase: S:15622, E:15937, LEN:315, COLS:234, PERC:74.3
[2020-03-14 11:05:34] INFO: TIGR00468: pheS: phenylalanine--tRNA ligase, alpha subunit: S:15937, E:16261, LEN:324, COLS:207, PERC:63.9
[2020-03-14 11:05:38] INFO: TIGR00472: pheT_bact: phenylalanine--tRNA ligase, beta subunit: S:16261, E:17059, LEN:798, COLS:572, PERC:71.7
[2020-03-14 11:05:42] INFO: TIGR00487: IF-2: translation initiation factor IF-2: S:17059, E:17646, LEN:587, COLS:445, PERC:75.8
[2020-03-14 11:05:43] INFO: TIGR00496: frr: ribosome recycling factor: S:17646, E:17822, LEN:176, COLS:149, PERC:84.7
[2020-03-14 11:05:45] INFO: TIGR00539: hemN_rel: putative oxygen-independent coproporphyrinogen III oxidase: S:17822, E:18183, LEN:361, COLS:200, PERC:55.4
[2020-03-14 11:05:49] INFO: TIGR00580: mfd: transcription-repair coupling factor: S:18183, E:19106, LEN:923, COLS:600, PERC:65.0
[2020-03-14 11:05:54] INFO: TIGR00593: pola: DNA polymerase I: S:19106, E:19996, LEN:890, COLS:627, PERC:70.4
[2020-03-14 11:05:55] INFO: TIGR00615: recR: recombination protein RecR: S:19996, E:20192, LEN:196, COLS:141, PERC:71.9
[2020-03-14 11:05:59] INFO: TIGR00631: uvrb: excinuclease ABC subunit B: S:20192, E:20850, LEN:658, COLS:429, PERC:65.2
[2020-03-14 11:06:02] INFO: TIGR00634: recN: DNA repair protein RecN: S:20850, E:21413, LEN:563, COLS:375, PERC:66.6
[2020-03-14 11:06:03] INFO: TIGR00635: ruvB: Holliday junction DNA helicase RuvB: S:21413, E:21718, LEN:305, COLS:208, PERC:68.2
[2020-03-14 11:06:07] INFO: TIGR00643: recG: ATP-dependent DNA helicase RecG: S:21718, E:22347, LEN:629, COLS:415, PERC:66.0
[2020-03-14 11:06:09] INFO: TIGR00663: dnan: DNA polymerase III, beta subunit: S:22347, E:22714, LEN:367, COLS:258, PERC:70.3
[2020-03-14 11:06:11] INFO: TIGR00717: rpsA: ribosomal protein bS1: S:22714, E:23230, LEN:516, COLS:349, PERC:67.6
[2020-03-14 11:06:12] INFO: TIGR00755: ksgA: ribosomal RNA small subunit methyltransferase A: S:23230, E:23486, LEN:256, COLS:177, PERC:69.1
[2020-03-14 11:06:13] INFO: TIGR00810: secG: preprotein translocase, SecG subunit: S:23486, E:23559, LEN:73, COLS:60, PERC:82.2
[2020-03-14 11:06:13] INFO: TIGR00922: nusG: transcription termination/antitermination factor NusG: S:23559, E:23731, LEN:172, COLS:140, PERC:81.4
[2020-03-14 11:06:16] INFO: TIGR00928: purB: adenylosuccinate lyase: S:23731, E:24167, LEN:436, COLS:344, PERC:78.9
[2020-03-14 11:06:18] INFO: TIGR00959: ffh: signal recognition particle protein: S:24167, E:24595, LEN:428, COLS:327, PERC:76.4
[2020-03-14 11:06:22] INFO: TIGR00963: secA: preprotein translocase, SecA subunit: S:24595, E:25382, LEN:787, COLS:514, PERC:65.3
[2020-03-14 11:06:22] INFO: TIGR00964: secE_bact: preprotein translocase, SecE subunit: S:25382, E:25439, LEN:57, COLS:42, PERC:73.7
[2020-03-14 11:06:24] INFO: TIGR00967: 3a0501s007: preprotein translocase, SecY subunit: S:25439, E:25853, LEN:414, COLS:315, PERC:76.1
[2020-03-14 11:06:25] INFO: TIGR01009: rpsC_bact: ribosomal protein uS3: S:25853, E:26065, LEN:212, COLS:162, PERC:76.4
[2020-03-14 11:06:26] INFO: TIGR01011: rpsB_bact: ribosomal protein uS2: S:26065, E:26290, LEN:225, COLS:175, PERC:77.8
[2020-03-14 11:06:27] INFO: TIGR01017: rpsD_bact: ribosomal protein uS4: S:26290, E:26490, LEN:200, COLS:156, PERC:78.0
[2020-03-14 11:06:28] INFO: TIGR01021: rpsE_bact: ribosomal protein uS5: S:26490, E:26646, LEN:156, COLS:121, PERC:77.6
[2020-03-14 11:06:29] INFO: TIGR01029: rpsG_bact: ribosomal protein uS7: S:26646, E:26800, LEN:154, COLS:120, PERC:77.9
[2020-03-14 11:06:30] INFO: TIGR01032: rplT_bact: ribosomal protein bL20: S:26800, E:26914, LEN:114, COLS:93, PERC:81.6
[2020-03-14 11:06:32] INFO: TIGR01039: atpD: ATP synthase F1, beta subunit: S:26914, E:27376, LEN:462, COLS:263, PERC:56.9
[2020-03-14 11:06:32] INFO: TIGR01044: rplV_bact: ribosomal protein uL22: S:27376, E:27479, LEN:103, COLS:83, PERC:80.6
[2020-03-14 11:06:36] INFO: TIGR01059: gyrB: DNA gyrase, B subunit: S:27479, E:28118, LEN:639, COLS:436, PERC:68.2
[2020-03-14 11:06:40] INFO: TIGR01063: gyrA: DNA gyrase, A subunit: S:28118, E:28918, LEN:800, COLS:612, PERC:76.5
[2020-03-14 11:06:41] INFO: TIGR01066: rplM_bact: ribosomal protein uL13: S:28918, E:29059, LEN:141, COLS:109, PERC:77.3
[2020-03-14 11:06:41] INFO: TIGR01071: rplO_bact: ribosomal protein uL15: S:29059, E:29203, LEN:144, COLS:110, PERC:76.4
[2020-03-14 11:06:42] INFO: TIGR01079: rplX_bact: ribosomal protein uL24: S:29203, E:29307, LEN:104, COLS:93, PERC:89.4
[2020-03-14 11:06:44] INFO: TIGR01082: murC: UDP-N-acetylmuramate--L-alanine ligase: S:29307, E:29756, LEN:449, COLS:324, PERC:72.2
[2020-03-14 11:06:46] INFO: TIGR01087: murD: UDP-N-acetylmuramoylalanine--D-glutamate ligase: S:29756, E:30197, LEN:441, COLS:278, PERC:63.0
[2020-03-14 11:06:48] INFO: TIGR01128: holA: DNA polymerase III, delta subunit: S:30197, E:30511, LEN:314, COLS:194, PERC:61.8
[2020-03-14 11:06:49] INFO: TIGR01146: ATPsyn_F1gamma: ATP synthase F1, gamma subunit: S:30511, E:30797, LEN:286, COLS:217, PERC:75.9
[2020-03-14 11:06:50] INFO: TIGR01164: rplP_bact: ribosomal protein uL16: S:30797, E:30923, LEN:126, COLS:97, PERC:77.0
[2020-03-14 11:06:51] INFO: TIGR01169: rplA_bact: ribosomal protein uL1: S:30923, E:31150, LEN:227, COLS:180, PERC:79.3
[2020-03-14 11:06:52] INFO: TIGR01171: rplB_bact: ribosomal protein uL2: S:31150, E:31425, LEN:275, COLS:189, PERC:68.7
[2020-03-14 11:06:55] INFO: TIGR01302: IMP_dehydrog: inosine-5'-monophosphate dehydrogenase: S:31425, E:31875, LEN:450, COLS:344, PERC:76.4
[2020-03-14 11:06:57] INFO: TIGR01391: dnaG: DNA primase: S:31875, E:32289, LEN:414, COLS:277, PERC:66.9
[2020-03-14 11:07:00] INFO: TIGR01393: lepA: elongation factor 4: S:32289, E:32884, LEN:595, COLS:421, PERC:70.8
[2020-03-14 11:07:03] INFO: TIGR01394: TypA_BipA: GTP-binding protein TypA/BipA: S:32884, E:33478, LEN:594, COLS:438, PERC:73.7
[2020-03-14 11:07:04] INFO: TIGR01510: coaD_prev_kdtB: pantetheine-phosphate adenylyltransferase: S:33478, E:33633, LEN:155, COLS:123, PERC:79.4
[2020-03-14 11:07:04] INFO: TIGR01632: L11_bact: ribosomal protein uL11: S:33633, E:33773, LEN:140, COLS:104, PERC:74.3
[2020-03-14 11:07:05] INFO: TIGR01951: nusB: transcription antitermination factor NusB: S:33773, E:33904, LEN:131, COLS:83, PERC:63.4
[2020-03-14 11:07:07] INFO: TIGR01953: NusA: transcription termination factor NusA: S:33904, E:34244, LEN:340, COLS:282, PERC:82.9
[2020-03-14 11:07:08] INFO: TIGR02012: tigrfam_recA: protein RecA: S:34244, E:34565, LEN:321, COLS:213, PERC:66.4
[2020-03-14 11:07:15] INFO: TIGR02013: rpoB: DNA-directed RNA polymerase, beta subunit: S:34565, E:35803, LEN:1238, COLS:781, PERC:63.1
[2020-03-14 11:07:16] INFO: TIGR02027: rpoA: DNA-directed RNA polymerase, alpha subunit: S:35803, E:36101, LEN:298, COLS:227, PERC:76.2
[2020-03-14 11:07:18] INFO: TIGR02075: pyrH_bact: UMP kinase: S:36101, E:36334, LEN:233, COLS:179, PERC:76.8
[2020-03-14 11:07:19] INFO: TIGR02191: RNaseIII: ribonuclease III: S:36334, E:36553, LEN:219, COLS:149, PERC:68.0
[2020-03-14 11:07:20] INFO: TIGR02273: 16S_RimM: 16S rRNA processing protein RimM: S:36553, E:36719, LEN:166, COLS:121, PERC:72.9
[2020-03-14 11:07:23] INFO: TIGR02350: prok_dnaK: chaperone protein DnaK: S:36719, E:37315, LEN:596, COLS:423, PERC:71.0
[2020-03-14 11:07:29] INFO: TIGR02386: rpoC_TIGR: DNA-directed RNA polymerase, beta' subunit: S:37315, E:38462, LEN:1147, COLS:757, PERC:66.0
[2020-03-14 11:07:31] INFO: TIGR02397: dnaX_nterm: DNA polymerase III, subunit gamma and tau: S:38462, E:38817, LEN:355, COLS:243, PERC:68.5
[2020-03-14 11:07:31] INFO: TIGR02432: lysidine_TilS_N: tRNA(Ile)-lysidine synthetase: S:38817, E:39006, LEN:189, COLS:126, PERC:66.7
[2020-03-14 11:07:33] INFO: TIGR02729: Obg_CgtA: Obg family GTPase CgtA: S:39006, E:39335, LEN:329, COLS:233, PERC:70.8
[2020-03-14 11:07:34] INFO: TIGR03263: guanyl_kin: guanylate kinase: S:39335, E:39515, LEN:180, COLS:133, PERC:73.9
[2020-03-14 11:07:36] INFO: TIGR03594: GTPase_EngA: ribosome-associated GTPase EngA: S:39515, E:39947, LEN:432, COLS:325, PERC:75.2
[2020-03-14 11:07:37] INFO: TIGR03625: L3_bact: 50S ribosomal protein uL3: S:39947, E:40149, LEN:202, COLS:169, PERC:83.7
[2020-03-14 11:07:38] INFO: TIGR03632: uS11_bact: ribosomal protein uS11: S:40149, E:40266, LEN:117, COLS:81, PERC:69.2
[2020-03-14 11:07:39] INFO: TIGR03654: L6_bact: ribosomal protein uL6: S:40266, E:40441, LEN:175, COLS:131, PERC:74.9
[2020-03-14 11:07:41] INFO: TIGR03723: T6A_TsaD_YgjD: tRNA threonylcarbamoyl adenosine modification protein TsaD: S:40441, E:40755, LEN:314, COLS:219, PERC:69.7
[2020-03-14 11:07:42] INFO: TIGR03725: T6A_YeaZ: tRNA threonylcarbamoyl adenosine modification protein YeaZ: S:40755, E:40967, LEN:212, COLS:126, PERC:59.4
[2020-03-14 11:07:43] INFO: TIGR03953: rplD_bact: 50S ribosomal protein uL4: S:40967, E:41155, LEN:188, COLS:159, PERC:84.6
[2020-03-14 11:07:45] INFO: Identified 0 of 120 marker genes with <42 columns for sampling:
[2020-03-14 11:07:45] INFO: 
[2020-03-14 11:07:45] INFO: Marker genes had 72.6+/-6.7% of columns available for selection on average.
[2020-03-14 11:07:45] INFO: Final MSA contains 5040 columns.
[2020-03-14 11:08:51] INFO: Filtered MSA from 41155 to 5040 AAs.
[2020-03-14 11:08:51] INFO: Filtered 0 genomes with amino acids in <10.0% of columns in filtered MSA.
[2020-03-14 11:08:51] INFO: Creating concatenated alignment for 23459 GTDB and user genomes.
[2020-03-14 11:08:53] INFO: Creating concatenated alignment for 1 user genomes.
[2020-03-14 11:08:53] INFO: Done.

Y muestro ahora como debería ser el nuevo txt creado:

Ribosomal_S9 121
Ribosomal_S8 129
Ribosomal_L10 100
... 
TIGR03953: 188

No soy nuevo con Python y he hecho estas cosas antes pero en este caso en el que comienza y acaba en lineas particulares y en el que no puedo hacer split y seleccionar el split 4 y 9 por ejemplo porque como se puede ver, detras del nombre del gen viene la descripcion que hace que cada linea tenga un numero diferente de palabras haciendome imposible seleccionar LEN.

Y no puedo hacerlo manualmente porque el programa me genera este documento miles de veces y estoy haciendo un script en el cual en uno de sus pasos requiero obtener estos dos datos (nombre del gen y su LEN.

3
  • ¿siempre hay 20 lineas antes del primer gen y 9 después del último o esto es variable también?
    – FJSevilla
    Commented el 14 mar. 2020 a las 19:43
  • Esto no varía por suerte Commented el 14 mar. 2020 a las 19:44
  • Listo, respondí con una posible respuesta
    – user71085
    Commented el 14 mar. 2020 a las 20:53

3 respuestas 3

1

Si los archivos no son excesivamente grandes como para que cargarlos en memoria al completo sea un problema (en este ejemplo son unos 17 KiB simplemente), una forma bastante simple sería usar expresiones regulares de la siguiente forma:

import re


PATT = re.compile("^.+INFO: (?P<gen>.+?):.+LEN:(?P<len>[0-9]+),.+$",
                  flags=re.MULTILINE)

fichero_entrada = "ruta/a/entrada.txt" # <<<<<<< Cambiar 
fichero_salida = "ruta/a/salida.txt"   # <<<<<<< Cambiar 

with open(fichero_entrada) as file_in, open(fichero_salida, "w") as file_out:
    for match in re.finditer(PATT, file_in.read()):
        file_out.write(f'{match.group("gen")}: {match.group("len")}\n')

La expresión:

  • ^ -> Captura justo después de un carácter de nueva línea.
  • .+ -> Captura cualquier carácter (menos nueva linea) de una a todas las veces posible (ambicioso).
  • (?P<name>.+?) -> Captura de grupo con nombre (gen). Cualquier carácter menos nueva linea, al menos una vez, pero los menos posibles (perezoso).
  • (?P<len>[0-9]+) -> Captura de grupo con nombre (len). Cualquier digito (de 0 a 9) de una a todas las veces posibles (ambicioso).
  • $ -> Captura justo antes del carácter de nueva línea.

  • El resto del patrón son literales ("INFO", "LEN", ":" y ",")

0

Tras un largo labor, conseguí exactamente lo que pediste

Espero que te ayude y sirva

import re

# abrimos el archivo
lista = open("example001.txt").read().splitlines()

# primer array con los numeros
array = []
for line in lista:
    linea = line[28:]
    try:
        linea2 = re.sub(r'.*, L', '', linea)
        if "EN:" in linea2:
            linea3 = "L" + str(linea2)
            linea3 = linea3[:7].replace(",", "").replace("LEN:", "")
        array.append(linea3)
    except:
        pass

# segundo array con los nombres     
array2 = []
for line in lista:
    linea = line[28:]
    try:
        if "LEN:" in linea:
            linea = linea
            if "Ribosomal_S9" in linea:
                start = True
                linea = linea[:13]
                array2.append(linea)
            if start == True:
                linea = linea[:13]
                if "TIGR" in linea:
                    linea = linea[:9]
                if ":" in linea:
                    linea = linea.split(":", 1)[0]
                if ":" in linea:
                    linea = linea.replace(":", "")
                array2.append(linea)
        if "EN:" in linea2:
            linea3 = "L" + str(linea2)
            linea3 = linea3[:7].replace(",", "").replace("LEN:", "")
    except:
        pass

# se elimina el primer valor, al estar duplicado
array2.pop(0)

# guardamos en un array combinado
array3 = []
for item in range(len(array2)):
    # combinamos nombres y LEN
    array3.append(array2[item] + ": " + array[item])

# finalmente guardamos el archivo
with open("lugar.txt", "w") as archivo:
    for line in array3:
        archivo.write(line + "\n")


# aqui imprimimos cómo se vería en el archivo final
# se puede eliminar
for item in range(len(array2)):
    print(array2[item] + ": " + array[item])

Un ejemplo del archivo final, sería:

Ribosomal_S9: 121
Ribosomal_S8: 129
Ribosomal_L10: 100
GrpE: 166
DUF150: 141
PNPase: 83
TIGR00006: 310
TIGR00019: 361
TIGR00020: 365
TIGR00029: 87
TIGR00043: 111
TIGR00054: 421
TIGR00059: 112
TIGR00061: 101
TIGR00064: 279
TIGR00065: 353
TIGR00082: 115
TIGR00083: 290
TIGR00084: 192
TIGR00086: 144
TIGR00088: 233
TIGR00090: 99
TIGR00092: 368
TIGR00095: 194
TIGR00115: 410
TIGR00116: 293
TIGR00138: 183
TIGR00158: 148
TIGR00166: 95
TIGR00168: 165
TIGR00186: 240
TIGR00194: 574
TIGR00250: 130
TIGR00337: 526
TIGR00344: 847
TIGR00362: 437
TIGR00382: 414
TIGR00392: 861
TIGR00396: 843
TIGR00398: 530
TIGR00414: 418
TIGR00416: 454
TIGR00420: 351
TIGR00431: 210
TIGR00435: 466
TIGR00436: 270
TIGR00442: 406
TIGR00445: 321
TIGR00456: 569
TIGR00459: 586
TIGR00460: 315
TIGR00468: 324
TIGR00472: 798
TIGR00487: 587
TIGR00496: 176
TIGR00539: 361
TIGR00580: 923
TIGR00593: 890
TIGR00615: 196
TIGR00631: 658
TIGR00634: 563
TIGR00635: 305
TIGR00643: 629
TIGR00663: 367
TIGR00717: 516
TIGR00755: 256
TIGR00810: 73
TIGR00922: 172
TIGR00928: 436
TIGR00959: 428
TIGR00963: 787
TIGR00964: 57
TIGR00967: 414
TIGR01009: 212
TIGR01011: 225
TIGR01017: 200
TIGR01021: 156
TIGR01029: 154
TIGR01032: 114
TIGR01039: 462
TIGR01044: 103
TIGR01059: 639
TIGR01063: 800
TIGR01066: 141
TIGR01071: 144
TIGR01079: 104
TIGR01082: 449
TIGR01087: 441
TIGR01128: 314
TIGR01146: 286
TIGR01164: 126
TIGR01169: 227
TIGR01171: 275
TIGR01302: 450
TIGR01391: 414
TIGR01393: 595
TIGR01394: 594
TIGR01510: 155
TIGR01632: 140
TIGR01951: 131
TIGR01953: 340
TIGR02012: 321
TIGR02013: 123
TIGR02027: 298
TIGR02075: 233
TIGR02191: 219
TIGR02273: 166
TIGR02350: 596
TIGR02386: 114
TIGR02397: 355
TIGR02432: 189
TIGR02729: 329
TIGR03263: 180
TIGR03594: 432
TIGR03625: 202
TIGR03632: 117
TIGR03654: 175
TIGR03723: 314
TIGR03725: 212
TIGR03953: 188

Si algo está incorrecto, avísame :)

2
  • 1
    Lo pruebo y te digo. Voy a poner en una respuesta a mi propia pregunta como lo he hecho yo hasta el momento. Cuando me vuelva a poner, comparo y hago un comentario comparando el tuyo y el mio. Muchas gracias de antemano. Commented el 14 mar. 2020 a las 21:00
  • @ManuelDominguezBecerra si lo has solucionado ya por tu mano, sería recomendable cerrar el thread o publicar la respuesta lo antes posible, asi ahorras el tiempo de los que quieran ayudarte :) Pero bueno yo te ayudé ya
    – user71085
    Commented el 14 mar. 2020 a las 21:08
0
# Guarda la longitud del gen
f = open("/Users/Desktop/GTDB/output_align2/gtdbtk.log", "r")
LEN= []
import re
for line in f:
    secuence = re.search('LEN:(\d+)', line)
    if secuence:
        LEN.append(secuence.group(1))

# Esto guarda su nombre
f = open("/Users/monkiky/Desktop/GTDB/output_align2/gtdbtk.log", "r")
Names =[]
for i, line in enumerate(f):
    if i > 19:
        Names.append(line.split(" ")[3])

# Borrar las 9 lineas porque no son proteinas
Names = Names[:-9]

#Crea un dataframe haciendo uso de las las listas ya creadas

bac120 = {'Names':Names,'LEN':LEN}


import pandas as pd
df = pd.DataFrame(bac120)

print(df)

           Names  LEN
0     Ribosomal_S9:  121
1     Ribosomal_S8:  129
2    Ribosomal_L10:  100
3             GrpE:  166
4           DUF150:  141
..              ...  ...
115      TIGR03632:  117
116      TIGR03654:  175
117      TIGR03723:  314
118      TIGR03725:  212
119      TIGR03953:  188
2
  • Dado que estamos en SO en español te recomiendo que los comntarios que explican tu código vayan en español
    – user128299
    Commented el 14 mar. 2020 a las 21:05
  • Vale, lo cambio luego. Es que me duele hacerlo en espanol porque recuerdo que tutores mios espanoles me decian LOS COMENTARIOS SIEMPRE EN INGLES y ya me duele cuando no lo hago. Eran muy pesadoss con eso. Commented el 14 mar. 2020 a las 21:07

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.