Code Duplication    Length = 106-106 lines in 2 locations

rna_tools/rna_cif2pdb.py 1 location

@@ 34-139 (lines=106) @@
31
    return parser, version
32
33
34
if __name__ == '__main__':
35
    parser, version = get_parser()
36
    args = parser.parse_args()
37
38
    if list != type(args.file):
39
        args.file = [args.file]
40
41
    for cif_file in args.file:
42
        from Bio.PDB import MMCIFParser, PDBIO
43
        parser = MMCIFParser()
44
        structure = parser.get_structure("structure_id", cif_file)
45
        pdb_file = cif_file.replace('.cif', '_fCIF.pdb')
46
47
        try:
48
            # Save to PDB format
49
            io = PDBIO()
50
            io.set_structure(structure)
51
            io.save(pdb_file)
52
53
            print(f'saved: {pdb_file}')
54
            # open a file add remarks
55
            new_file = ''
56
            with open(pdb_file, 'r') as f:
57
                if not args.no_hr:
58
                    new_file += add_header(version) + '\n'
59
                new_file += f.read()
60
61
            with open(pdb_file, 'w') as f:
62
                f.write(new_file)
63
64
        except:
65
            print('Warning: some of the chains in this mmCIF file has chain names with more char than 1, e.g. AB, and the PDB format needs single-letter code, e.g. A.')
66
            def has_high_rna_content(chain, threshold=0.8):
67
                # RNA nucleotides: A, C, G, U, and X (you can modify as needed)
68
                rna_nucleotides = ['A', 'C', 'G', 'U', 'X']
69
                total_residues = 0
70
                rna_residues = 0
71
72
                # Count the total number of residues and RNA-like residues
73
                for residue in chain:
74
                    total_residues += 1
75
                    if residue.get_resname().strip() in rna_nucleotides:
76
                        rna_residues += 1
77
78
                # Calculate the proportion of RNA residues
79
                if total_residues == 0:
80
                    return False  # Avoid division by zero if chain has no residues
81
82
                rna_percentage = rna_residues / total_residues
83
84
                # Check if the percentage of RNA residues is greater than or equal to the threshold (80% by default)
85
                return rna_percentage >= threshold
86
87
            from Bio.PDB.MMCIFParser import MMCIFParser
88
            from Bio.PDB import MMCIFParser, Structure, Model, Chain
89
90
            # Initialize the parser
91
            parser = MMCIFParser()
92
93
            # Parse the structure
94
            structure = parser.get_structure("structure", cif_file)
95
96
            # Create a list of single-letter chain identifiers
97
            import string
98
            letters = list(string.ascii_uppercase)
99
100
            for model in structure:
101
                for chain in model:
102
                    if has_high_rna_content(chain):
103
                        # New structure
104
                        new_structure = Structure.Structure("new_structure")
105
                        new_model = Model.Model(0)  # Create a new model
106
                        new_structure.add(new_model)  # Add the new model to the new structure
107
108
                        chain_id_new = letters.pop(0)
109
                        chain_id = chain.get_id()
110
111
                        atom_count = 0
112
                        for residue in chain:
113
                              for atom in residue:
114
                                   atom_count += 1
115
116
                        remarks = []
117
                        remarks.append(f'REMARK rna chain {chain.id} -> {chain_id_new}')
118
119
                        pdb_file = cif_file.replace('.cif', f'_{chain_id}_n{chain_id_new}_fCIF.pdb')
120
                        print(f'rna chain {chain.id} -> {chain_id_new} {pdb_file} # of atoms: {atom_count}')
121
122
                        chain.id = chain_id_new
123
                        new_model.add(chain)
124
125
                        io = PDBIO()
126
                        io.set_structure(new_structure)
127
128
                        io.save(pdb_file)
129
                        # open a file add remarks
130
                        new_file = ''
131
                        with open(pdb_file, 'r') as f:
132
                            if not args.no_hr:
133
                                new_file += add_header(version) + '\n'
134
                            if remarks:
135
                                new_file += '\n'.join(remarks) + '\n'
136
                            new_file += f.read()
137
138
                        with open(pdb_file, 'w') as f:
139
                            f.write(new_file)
140

rna_tools/rna_pdb_tools.py 1 location

@@ 1310-1415 (lines=106) @@
1307
            
1308
    from rna_tools.rna_tools_config import PYMOL_PATH
1309
    sys.path.insert(0, PYMOL_PATH)
1310
    if args.cif2pdb:
1311
        # quick fix - make a list on the spot
1312
        if list != type(args.file):
1313
            args.file = [args.file]
1314
        ##################################
1315
        for cif_file in args.file:
1316
            from Bio.PDB import MMCIFParser, PDBIO
1317
            parser = MMCIFParser()
1318
            structure = parser.get_structure("structure_id", cif_file)
1319
            pdb_file = cif_file.replace('.cif', '_fCIF.pdb')
1320
1321
1322
            try:
1323
                # Save to PDB format
1324
                io = PDBIO()
1325
                io.set_structure(structure)
1326
                io.save(pdb_file)
1327
1328
                print(f'saved: {pdb_file}')
1329
                # open a file add remarks
1330
                new_file = ''
1331
                with open(pdb_file, 'r') as f:
1332
                    if not args.no_hr:
1333
                        new_file += add_header(version) + '\n'
1334
                    new_file += f.read()
1335
1336
                with open(pdb_file, 'w') as f:
1337
                    f.write(new_file)
1338
1339
            except:
1340
                print('Warning: some of the chains in this mmCIF file has chain names with more char than 1, e.g. AB, and the PDB format needs single-letter code, e.g. A.')
1341
1342
                def has_high_rna_content(chain, threshold=0.8):
1343
                    # RNA nucleotides: A, C, G, U, and X (you can modify as needed)
1344
                    rna_nucleotides = ['A', 'C', 'G', 'U', 'X']
1345
                    total_residues = 0
1346
                    rna_residues = 0
1347
1348
                    # Count the total number of residues and RNA-like residues
1349
                    for residue in chain:
1350
                        total_residues += 1
1351
                        if residue.get_resname().strip() in rna_nucleotides:
1352
                            rna_residues += 1
1353
1354
                    # Calculate the proportion of RNA residues
1355
                    if total_residues == 0:
1356
                        return False  # Avoid division by zero if chain has no residues
1357
1358
                    rna_percentage = rna_residues / total_residues
1359
1360
                    # Check if the percentage of RNA residues is greater than or equal to the threshold (80% by default)
1361
                    return rna_percentage >= threshold
1362
1363
                from Bio.PDB.MMCIFParser import MMCIFParser
1364
                from Bio.PDB import MMCIFParser, Structure, Model, Chain
1365
                
1366
                # Initialize the parser
1367
                parser = MMCIFParser()
1368
1369
                # Parse the structure
1370
                structure = parser.get_structure("structure", cif_file)
1371
1372
                # Create a list of single-letter chain identifiers
1373
                import string
1374
                letters = list(string.ascii_uppercase)
1375
1376
                for model in structure:
1377
                    for chain in model:
1378
                        if has_high_rna_content(chain):
1379
                            # New structure
1380
                            new_structure = Structure.Structure("new_structure")
1381
                            new_model = Model.Model(0)  # Create a new model
1382
                            new_structure.add(new_model)  # Add the new model to the new structure
1383
1384
                            chain_id_new = letters.pop(0)
1385
                            chain_id = chain.get_id()
1386
1387
                            atom_count = 0
1388
                            for residue in chain:
1389
                                  for atom in residue:
1390
                                       atom_count += 1
1391
1392
                            remarks = []
1393
                            remarks.append(f'REMARK rna chain {chain.id} -> {chain_id_new}')
1394
1395
                            pdb_file = cif_file.replace('.cif', f'_{chain_id}_n{chain_id_new}_fCIF.pdb')
1396
                            print(f'rna chain {chain.id} -> {chain_id_new} {pdb_file} # of atoms: {atom_count}')
1397
1398
                            chain.id = chain_id_new
1399
                            new_model.add(chain)
1400
1401
                            io = PDBIO()
1402
                            io.set_structure(new_structure)
1403
                            
1404
                            io.save(pdb_file)
1405
                            # open a file add remarks
1406
                            new_file = ''
1407
                            with open(pdb_file, 'r') as f:
1408
                                if not args.no_hr:
1409
                                    new_file += add_header(version) + '\n'
1410
                                if remarks:
1411
                                    new_file += '\n'.join(remarks) + '\n'
1412
                                new_file += f.read()
1413
1414
                            with open(pdb_file, 'w') as f:
1415
                                f.write(new_file)
1416
1417
    if args.pdb2cif:
1418
        try: