I have been using Python for only a few days, so I am trying to learn about some best practices. An explanation of what this code is supposed to do is at the bottom of this post. It is an exercise to rewrite a script I made in VBA a while ago and I am doing it to learn Python.
Basically the strings of seemingly random letters belong in one long string, but the output from its original source comes in blocks like you see in the text file. I am trying to string the letter sequences together and keep them with their appropriate organism.
My end game is to move this information into Excel using Python, but I am not at that stage yet.
Here are the first two stanzas of the SampleSequences.txt
to be parsed:
gi|499323459|WP_011013951.1|C-glutam MEDAQPEQITATEELAGLLENPTNLEGKLADAEEEIILEGEDTQASLNWSVIVPALVIVL
gi|459384007|AGG66298.1|C-call meDAHSEQVTATEELAGLLKNPDXVGEQLADFEEEIVFESESETASLDWTVIIPALAVVL
gi|814963457|AKE41823.1|C-kuts MTDqsFSETTATEQLATMLAGREVIEEHAAQPEEEIIYEGEDEDAKIDFKVAIPAVAIVL
gi|334696457|AEG81254.1|C-ulcer meSSSAGATSATAQLAAMLSGEDKISAASNLPEEKITFEGEDSEARIDWKVTIPAFVIVL
gi|504084851|WP_014318845.1|C-dip mtTSSAPVVSATAELAAMIEGEGLIAEATAAPEKEITFEGEDDNAGVDWKVTAPAMVIVL
gi|828161585|WP_047252615.1|C-testu MTneQAEPSTATEQLASMMSDAELIGGHLDETSDAVDRHEEDDKPSFDWAVILPTAGVIV
gi|644967131|WP_025387886.1|C-casei mnDTYETNESATGQLASMLDTDFNPSAXXHPVEEEIELAADQKDAKIDWTITGIAGVLVA
gi|491466010|WP_005323773.1|C-pseudogen mvDEQLDAQSATGQLQDMIHSDGFHPEXXRYDDPEVEIAADRDNMPIDWSIVGIAGVLIA
gi|917407105|P_052013817.1|P-pseudodip mvREDLDELSATRQIAYMLEHEDDXXVAVSSVEDSVELASETEDSSIDWAVVAPAGLLVL
gi|516649031|WP_018018020.1|C-capito MAEStpGEQTATSELASLLHADRttLANYSGAETPEPLAAEDENAPVDWAIIIPLAGIIV
* :** :: :: . : .:: : ::
gi|499323459|WP_011013951.1|C-glutam ATVVWGIGFKDSFTNFASSALSAVVDNLGWAFILFGTVFVFFIVVIAASKFGTIRLGRID
gi|459384007|AGG66298.1|C-call ATVVWGIGFNESFSKVAGSALGFVVNNLGWAFVLFGTVFVFFAIAIAASKFGQIRLGRID
gi|814963457|AKE41823.1|C-kuts GVVIWGLLAGDSFSTFASTGLSWVVTNLGWAFILFSSVFLFFAIALAVSNFGTIKLGRTD
gi|334696457|AEG81254.1|C-ulcer AVVLWGIFGTSSFSTFASDALSFVVSDFGWAFIFFTTVFLVFAIVIAVSNFGTIKLGRND
gi|504084851|WP_014318845.1|C-dip AVVAWGILGTTSFASFAASSLGYIVDSFGWAFIFFTTVFLIFAIAIALSNFGTIKLGRND
gi|828161585|WP_047252615.1|C-testu AVVLWGLLSPDSFANFASSALSLVVNNLGWAFVLFGTVFVAFMIIIAASKFGQIKLGASD
gi|644967131|WP_025387886.1|C-casei AVVVWGLSAPDTFADVSAKALNFVVNNFGWAFILFTTVFVVFVIGIAASKFGSIKLGAIN
gi|491466010|WP_005323773.1|C-pseudogen AIVVWGLAAPDNFANFSSSALTWVVDNFGWAYILFGTVFVVFVVFIAASKFGSIKLGHIN
gi|917407105|P_052013817.1|P-pseudodip ATVIWGLAFTDSFTAFASTAFGWIIDNFGWAFMLFTTVFVVFVIAIAASKFGAIKLGSIN
gi|516649031|WP_018018020.1|C-capito ALVTWGLAGAGSFSSFADTSFSWVIDNLGWAFVLFGTVFVFFVLFIAFSSFGSIRLGGPD
. * **: .*: .: .: :: .:***:::* :**: * : :* *.** *:** :
My code:
main_seq_two = open("SampleSequences.txt","r")
org_dict = {}
myOffset = 0
with main_seq_two as m:
content = m.read().splitlines()
for item in content:
myStr = str(item) # Turn entire line into a string
myStrLast60 = str(item)[-60:] # Last 60 characters of the line string
myStrLen = len(myStr)
myNameStripped = myStr[:myStrLen - 60].strip()
if "gi|" in myNameStripped: # Checks to see if line is normal contents
if " " in myStrLast60: # To delete empty spaces in an AA string
myStrSymbols = myStrLast60 # To store myStrSymbols and delete extra space in myStrLast60
myStrLast60 = str(myStrLast60.replace(" ", '')) # Replace spaces with nothing
myOffset = len(myStrSymbols) - len(myStrLast60) # Stores text offset for last block
if myNameStripped not in org_dict.keys(): # Creates dictionary key if not exist yet
org_dict.update({myNameStripped : ''}) # Initializes dictionary key
temp_str = str(org_dict[myNameStripped]) # Temp string for currently built AA string
org_dict.update({myNameStripped : temp_str + myStrLast60}) # Updates organism key by adding most recent line of AA
if "gi|" not in myNameStripped and myStrLen > 1: # Checks to see if line is symbol contents
if "Symbols" not in org_dict.keys(): # Creates dictionary key if not exist yet
org_dict.update({"Symbols" : ''}) # Initializes dictionary key
if myOffset > 0: # Checks text offset for alignment, see line 15 - 18
myStrLast60 = myStrLast60[myOffset:60] # Snips myOffset chars from beginning of Symbols String
temp_symbols = str(org_dict["Symbols"]) # Stores currently built Symbols String
org_dict.update({"Symbols" : temp_symbols + myStrLast60}) # Updates symbols dictionary to finalize Symbols String
for key, value in org_dict.items():
print(key, value)
I am posting this to get some input from the community so I can learn other ways of doing things. I have some string parsing code here which gives me the output that I am looking for. I was wondering if anyone can give me some tips on what code is bad, or where / how this could be condensed, what I've done here which is bad coding practice, etc. Even just a single command which could cut down a line or two would be great.