This is my first code here and also my first program of this size. I don't really know what is expected from a programmer who writes "good, readable" code. This is my first program which will be used in a real-world application. Also I'm extremely new to Python. So while reviewing please be kind enough to give constructive criticism about how this code or my code in general can be better with respect to both Python and programming in general. I'll try to explain the problem in the following paragraph in the best way possible. If any clarifications are needed about my code/logic/the problem, feel free to ask in the comments, I'll try my best to clear the doubts.
The problem -
- Consider two files.
- Each containing a list of strings.
- One list has strings of some combination of 'a','t','g', and 'c'
- One list has strings of some combination of 'A',U','G', and 'C'
- I have to convert the strings from the capitalized list as a's to t's, c's to g's, u's to a's and g's to c's [ a-t, c-g, g-c, u-a ]. And another special condition is that there can be at most of two instances where u's convert to g's and/or g's convert to t's [ u-g, g-t ]
- The conversions only need to be done for four regions of the strings, indices 2-7(6 characters), 2-8(7 characters), 1-7(7 characters) and 1-8(8 characters), provided the starting index is 1
- After generating all possible conversions, I have to check each of them against all the strings in the other list and find out the locations where they match.
If you are looking for an output of sorts, I won't be able to provide it yet, since the comparisons I need to do are about (38869 * 2588 * all possibble combinatons of each of the 2588) + time taken to generate all the permutations
. So my machine is extremely inadequate of doing something like that.
My Program -
## Date : 2017-08-10
## Author : dadyodevil
## Contact : [email protected]
##
## A python program to detect all indices of complimentary Micro-RNA(miRNA) target sites on Messenger-RNAs(mRNA)
##
## As an input, this program needs two lists -
## 1. A list of mRNAs where each entry is represented in a two line format:
## >hg19_refGene NM_032291 range=chr1:67208779-67210768...
## Sequence of mRNA
## 2. A list of miRNAs where each entry is represented in a two line format:
## >hsa-miR-576-3p MIMAT000...
## Sequence of miRNA
##
## Pre-requisites for the reader -
## 1. Understanding of programming concepts
## 2. A moderate understanding of the Python programming language version 2.7
## 3. Knowledge of terms regarding miRNA-mRNA target detection
import re
def extractSeed(miRNA):
## There are 4 seed regions with indices from 2-7, 2-8, 1-7 and 1-8
miRNAfor6mer.append(miRNA[1:7][::-1])
miRNAfor7mer.append(miRNA[1:8][::-1])
miRNAfor7a1.append(miRNA[:7][::-1])
miRNAfor8mer.append(miRNA[0:9][::-1])
def createCompliment(allCompliments, miRNA, wobbleCount, compliment):
## For the compliment, the convertions include a:t, u:a, g:c, c:g and for Wobble-Pairs, u:g and g:u
if wobbleCount == 2:
for letter in miRNA:
if letter == 'a':
compliment += 't'
elif letter == 'c':
compliment += 'g'
elif letter == 'g':
compliment += 'c'
else:
compliment += 'a'
allCompliments.append(compliment)
else:
for index, letter in enumerate(miRNA):
if letter == 'a':
compliment += 't'
elif letter == 'c':
compliment += 'g'
elif letter == 'g':
createCompliment(allCompliments, miRNA[index+1:], wobbleCount + 1, compliment + "t")
createCompliment(allCompliments, miRNA[index+1:], wobbleCount + 1, compliment + "c")
compliment += 'c'
elif letter == 'u':
createCompliment(allCompliments, miRNA[index+1:], wobbleCount + 1, compliment + "g")
createCompliment(allCompliments, miRNA[index+1:], wobbleCount + 1, compliment + "a")
compliment += 'a'
## Now that all possibilities are generated, the duplicates need to be removed
allCompliments = sorted(list(set(allCompliments)))
def checkForMatch(miRNACompliments, seedRegion, miRNAname):
## Each miRNA that is recived by this function will be compared against the whole list of mRNAs and the matching indices will be saved
## Since the mRNA sequences are in alternate lines the sequences will be extracted as such and the when matches are found, the name of the mRNA will be extracted from teh index just before the current one
for index in range(1, len(mRNA_List), 2):
for entry in miRNACompliments:
mRNA = mRNA_List[index]
matchesStart = [m.start() for m in re.finditer(entry, mRNA)]
if (len(matchesStart) > 0):
mRNAname = mRNA_List[index-1][14:mRNA_List[index-1].find(" ",15)]
matchesEnd = []
for index2 in range(0, len(matchesStart)):
matchesEnd.append(matchesStart[index2] + len(entry))
allindices = zip(matchesStart, matchesEnd)
complimentarySiteList.append([miRNAname, mRNAname, seedRegion, allindices])
def prepareForMatch(miRNA, miRNAname):
global miRNAfor6mer, miRNAfor7mer, miRNAfor7a1, miRNAfor8mer
miRNAfor6mer, miRNAfor7mer, miRNAfor7a1, miRNAfor8mer = [], [], [], []
## First the seed sites will be extracted and reversed
extractSeed(miRNA)
## Empty lists will be generated to store all the compliments
miRNAfor6mer.append([])
miRNAfor7mer.append([])
miRNAfor7a1.append([])
miRNAfor8mer.append([])
## Then the compliments will be generated from the seed regions along with atmost of two Wobble-Pairs
miRNAfor6mer.append(createCompliment(miRNAfor6mer[1], miRNAfor6mer[0], 0, ""))
miRNAfor7mer.append(createCompliment(miRNAfor7mer[1], miRNAfor7mer[0], 0, ""))
miRNAfor7a1.append(createCompliment(miRNAfor7a1[1], miRNAfor7a1[0], 0, ""))
miRNAfor8mer.append(createCompliment(miRNAfor8mer[1], miRNAfor8mer[0], 0, ""))
## After generating all possible compliments, they will be checked for matching sites
checkForMatch(miRNAfor6mer[1], "6mer", miRNAname)
checkForMatch(miRNAfor7mer[1], "7mer", miRNAname)
checkForMatch(miRNAfor7a1[1], "7A1", miRNAname)
checkForMatch(miRNAfor8mer[1], "8mer", miRNAname)
def Main():
global mRNA_List, miRNA_List, complimentarySiteList
miRNA_List = open('miRNA_list.txt').read().splitlines()
mRNA_List = open('mRNA_list.txt').read().splitlines()
complimentarySiteList = []
## Since the sequences are in every alteRNAte lines, the 'index' needs to be incremeted by 2 to access only the sequences
## The miRNA lengths are also checked whether they are atleast 8 neucleotides long, if they are not, they will not be checked
for index in range(1,len(miRNA_List),2):
miRNAname = miRNA_List[index-1][5:miRNA_List[index-1].find(' ')]
if (len(miRNA_List[index]) < 8):
print "%s at %d has insufficient length." %(miRNAname, index)
else:
prepareForMatch(miRNA_List[index].lower(), miRNAname)
for entry in complimentarySiteList:
print entry
if __name__ == '__main__':
Main()