Skip to content

Conversation

@sourcery-ai
Copy link

@sourcery-ai sourcery-ai bot commented Dec 5, 2023

Branch master refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from zagorGit December 5, 2023 11:17
Comment on lines -12 to +33
f0 = open(pathTo2+"/Genotype_fasta-headers-rename.log", "a+") # change appropriately how do you want to call log table
fname=glob.glob(pathTo1+"/assemblerNameGenotype*.fasta") # change appropriately pattern in your file naming
for i in range (len(fname)):
vname=fname[i].split("/")
vname=vname[len(vname)-1]
idname=re.sub('\.fasta$', '', vname)
f4 = open(fname[i], "r")
f5 = open(pathTo2+"/"+str(idname) + "_renamed.fasta", "w")
line4=f4.readline()
count=1
while line4:
line4=str.rstrip(line4)
if '>' in line4:
line44=">"+str(idname)+"_"+str(count)
f5.write("%s\n" % (line44))
f0.write("%s\t" % (line4))
f0.write("%s\n" % (line44))
count=count+1
else:
f5.write("%s\n" % (line4))
line4 = f4.readline()
f4.close()
f5.close()
f0.close()
with open(f"{pathTo2}/Genotype_fasta-headers-rename.log", "a+") as f0:
fname = glob.glob(f"{pathTo1}/assemblerNameGenotype*.fasta")
for item in fname:
vname = item.split("/")
vname=vname[len(vname)-1]
idname=re.sub('\.fasta$', '', vname)
with open(item, "r") as f4:
f5 = open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w")
line4=f4.readline()
count=1
while line4:
line4=str.rstrip(line4)
if '>' in line4:
line44 = f">{str(idname)}_{str(count)}"
f5.write("%s\n" % (line44))
f0.write("%s\t" % (line4))
f0.write("%s\n" % (line44))
count=count+1
else:
f5.write("%s\n" % (line4))
line4 = f4.readline()
f5.close()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 12-35 refactored with the following changes:

This removes the following comments ( why? ):

# change appropriately how do you want to call log table
# change appropriately pattern in your file naming

Comment on lines -14 to +15
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
def prYellow(skk): print("\033[93m {}\033[00m" .format(skk))
def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk))
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prRed refactored with the following changes:

Comment on lines -15 to +17
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
def prYellow(skk): print("\033[93m {}\033[00m" .format(skk))
def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk))
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prGreen refactored with the following changes:

Comment on lines -16 to +19
def prYellow(skk): print("\033[93m {}\033[00m" .format(skk))
def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk))
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
def prYellow(skk):
print(f"\033[93m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prYellow refactored with the following changes:

Comment on lines -17 to +21
def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk))
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
def prYellow(skk):
print(f"\033[93m {skk}\033[00m")
def prLightPurple(skk):
print(f"\033[94m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prLightPurple refactored with the following changes:

Comment on lines -18 to +23
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
def prYellow(skk):
print(f"\033[93m {skk}\033[00m")
def prLightPurple(skk):
print(f"\033[94m {skk}\033[00m")
def prPurple(skk):
print(f"\033[95m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prPurple refactored with the following changes:

Comment on lines -19 to +25
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
def prYellow(skk):
print(f"\033[93m {skk}\033[00m")
def prLightPurple(skk):
print(f"\033[94m {skk}\033[00m")
def prPurple(skk):
print(f"\033[95m {skk}\033[00m")
def prCyan(skk):
print(f"\033[96m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prCyan refactored with the following changes:

Comment on lines -20 to +27
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
def prYellow(skk):
print(f"\033[93m {skk}\033[00m")
def prLightPurple(skk):
print(f"\033[94m {skk}\033[00m")
def prPurple(skk):
print(f"\033[95m {skk}\033[00m")
def prCyan(skk):
print(f"\033[96m {skk}\033[00m")
def prLightGray(skk):
print(f"\033[97m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prLightGray refactored with the following changes:

Comment on lines -21 to +29
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
def prYellow(skk):
print(f"\033[93m {skk}\033[00m")
def prLightPurple(skk):
print(f"\033[94m {skk}\033[00m")
def prPurple(skk):
print(f"\033[95m {skk}\033[00m")
def prCyan(skk):
print(f"\033[96m {skk}\033[00m")
def prLightGray(skk):
print(f"\033[97m {skk}\033[00m")
def prBlack(skk):
print(f"\033[98m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prBlack refactored with the following changes:

Comment on lines -22 to +31
def prBlue(skk): print("\033[94m {}\033[00m" .format(skk))
def prRed(skk):
print(f"\033[91m {skk}\033[00m")
def prGreen(skk):
print(f"\033[92m {skk}\033[00m")
def prYellow(skk):
print(f"\033[93m {skk}\033[00m")
def prLightPurple(skk):
print(f"\033[94m {skk}\033[00m")
def prPurple(skk):
print(f"\033[95m {skk}\033[00m")
def prCyan(skk):
print(f"\033[96m {skk}\033[00m")
def prLightGray(skk):
print(f"\033[97m {skk}\033[00m")
def prBlack(skk):
print(f"\033[98m {skk}\033[00m")
def prBlue(skk):
print(f"\033[94m {skk}\033[00m")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prBlue refactored with the following changes:

Copy link
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Type: Refactoring

Summary of PR: This PR introduces refactoring changes to two Python scripts, aiming to improve code readability and maintainability. It applies context management for file operations and updates string formatting to use f-strings.

General PR suggestions

  • Ensure that all file operations are performed using context managers (with statements) to guarantee that files are properly closed after their operations are complete, especially in the case of exceptions.
  • Precompile regular expressions that are used within loops to enhance performance, as the pattern does not change during iterations.
  • When using f-strings, avoid unnecessary calls to str() for variables, as f-strings automatically handle the conversion to strings.
  • Consider using built-in path manipulation functions like os.path.basename for extracting file names from paths to make the code more robust and platform-independent.
  • Review the consistency of error handling and resource management across the codebase to ensure that the refactoring maintains or improves the current standards.

Your trial expires on December 19, 2023. Please email [email protected] to continue using Sourcery ✨

vname=vname[len(vname)-1]
idname=re.sub('\.fasta$', '', vname)
with open(item, "r") as f4:
f5 = open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (llm): The file opened as 'f5' should be managed using a 'with' statement to ensure it is properly closed even if an error occurs.

Suggested change
f5 = open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w")
+ with open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w") as f5:

for item in fname:
vname = item.split("/")
vname=vname[len(vname)-1]
idname=re.sub('\.fasta$', '', vname)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): Consider precompiling the regular expression outside of the loop for better performance, as it does not change per iteration.

Suggested change
idname=re.sub('\.fasta$', '', vname)
Outside the loop: pattern = re.compile('\\.fasta$')

while line4:
line4=str.rstrip(line4)
if '>' in line4:
line44 = f">{str(idname)}_{str(count)}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (llm): The 'str' function is redundant when using formatted string literals (f-strings).

Suggested change
line44 = f">{str(idname)}_{str(count)}"
+ line44 = f">{idname}_{count}"

fname = glob.glob(f"{pathTo1}/assemblerNameGenotype*.fasta")
for item in fname:
vname = item.split("/")
vname=vname[len(vname)-1]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): Using 'os.path.basename' would be a more robust method for extracting the file name from a path.

Suggested change
vname=vname[len(vname)-1]
+ vname = os.path.basename(item)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant